* jbigi, cpuid:

- Extract files from jar to temp dir, load from that dir, then
        copy to the base dir if we have permissions (and failing silently
        if we don't), so we have optimized libs and no complaints
        when we have a read-only base dir.
This commit is contained in:
zzz
2009-06-14 01:49:27 +00:00
parent e5ec72b09b
commit 112ddc7156
3 changed files with 37 additions and 11 deletions

View File

@ -10,6 +10,7 @@ import java.io.InputStream;
import java.net.URL;
import net.i2p.I2PAppContext;
import net.i2p.util.FileUtil;
/**
* @author Iakin
@ -484,8 +485,11 @@ public class CPUID {
*
* <p>This is a pretty ugly hack, using the general technique illustrated by the
* onion FEC libraries. It works by pulling the resource, writing out the
* byte stream to a temporary file, loading the native library from that file,
* then deleting the file.</p>
* byte stream to a temporary file, loading the native library from that file.
* We then attempt to copy the file from the temporary dir to the base install dir,
* so we don't have to do this next time - but we don't complain if it fails,
* so we transparently support read-only base dirs.
* </p>
*
* @return true if it was loaded successfully, else false
*
@ -503,9 +507,10 @@ public class CPUID {
File outFile = null;
FileOutputStream fos = null;
String filename = libPrefix + "jcpuid" + libSuffix;
try {
InputStream libStream = resource.openStream();
outFile = new File(I2PAppContext.getGlobalContext().getBaseDir(), libPrefix + "jcpuid" + libSuffix);
outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename);
fos = new FileOutputStream(outFile);
// wtf this was 4096*1024 which is really excessive for a roughly 4KB file
byte buf[] = new byte[4096];
@ -517,7 +522,6 @@ public class CPUID {
fos.close();
fos = null;
System.load(outFile.getAbsolutePath());//System.load requires an absolute path to the lib
return true;
} catch (UnsatisfiedLinkError ule) {
if (_doLog) {
System.err.println("ERROR: The resource " + resourceName
@ -536,6 +540,10 @@ public class CPUID {
try { fos.close(); } catch (IOException ioe) {}
}
}
// copy to install dir, ignore failure
File newFile = new File(I2PAppContext.getGlobalContext().getBaseDir(), filename);
FileUtil.copy(outFile.getAbsolutePath(), newFile.getAbsolutePath(), false, true);
return true;
}
private static final String getResourceName()

View File

@ -201,9 +201,18 @@ public class FileUtil {
}
}
/** return true if it was copied successfully */
/**
* @return true if it was copied successfully
*/
public static boolean copy(String source, String dest, boolean overwriteExisting) {
return copy(source, dest, overwriteExisting, false);
}
/**
* @param quiet don't log fails to wrapper log if true
* @return true if it was copied successfully
*/
public static boolean copy(String source, String dest, boolean overwriteExisting, boolean quiet) {
File src = new File(source);
File dst = new File(dest);
@ -226,6 +235,7 @@ public class FileUtil {
out.close();
return true;
} catch (IOException ioe) {
if (!quiet)
ioe.printStackTrace();
return false;
}

View File

@ -24,6 +24,7 @@ import freenet.support.CPUInformation.IntelCPUInfo;
import freenet.support.CPUInformation.UnknownCPUException;
import net.i2p.I2PAppContext;
import net.i2p.util.FileUtil;
import net.i2p.util.Log;
/**
@ -516,8 +517,11 @@ public class NativeBigInteger extends BigInteger {
*
* <p>This is a pretty ugly hack, using the general technique illustrated by the
* onion FEC libraries. It works by pulling the resource, writing out the
* byte stream to a temporary file, loading the native library from that file,
* then deleting the file.</p>
* byte stream to a temporary file, loading the native library from that file.
* We then attempt to copy the file from the temporary dir to the base install dir,
* so we don't have to do this next time - but we don't complain if it fails,
* so we transparently support read-only base dirs.
* </p>
*
* @return true if it was loaded successfully, else false
*
@ -538,9 +542,10 @@ public class NativeBigInteger extends BigInteger {
File outFile = null;
FileOutputStream fos = null;
String filename = _libPrefix + "jbigi" + _libSuffix;
try {
InputStream libStream = resource.openStream();
outFile = new File(I2PAppContext.getGlobalContext().getBaseDir(), _libPrefix + "jbigi" + _libSuffix);
outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename);
fos = new FileOutputStream(outFile);
// wtf this was 4096*1024 which is really excessive for a roughly 50KB file
byte buf[] = new byte[4096];
@ -552,7 +557,6 @@ public class NativeBigInteger extends BigInteger {
fos.close();
fos = null;
System.load(outFile.getAbsolutePath()); //System.load requires an absolute path to the lib
return true;
} catch (UnsatisfiedLinkError ule) {
if (_doLog) {
System.err.println("ERROR: The resource " + resourceName
@ -571,6 +575,10 @@ public class NativeBigInteger extends BigInteger {
try { fos.close(); } catch (IOException ioe) {}
}
}
// copy to install dir, ignore failure
File newFile = new File(I2PAppContext.getGlobalContext().getBaseDir(), filename);
FileUtil.copy(outFile.getAbsolutePath(), newFile.getAbsolutePath(), false, true);
return true;
}
private static final String getResourceName(boolean optimized) {