* 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 java.net.URL;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
import net.i2p.util.FileUtil;
/** /**
* @author Iakin * @author Iakin
@ -484,8 +485,11 @@ public class CPUID {
* *
* <p>This is a pretty ugly hack, using the general technique illustrated by the * <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 * 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, * byte stream to a temporary file, loading the native library from that file.
* then deleting the file.</p> * 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 * @return true if it was loaded successfully, else false
* *
@ -503,9 +507,10 @@ public class CPUID {
File outFile = null; File outFile = null;
FileOutputStream fos = null; FileOutputStream fos = null;
String filename = libPrefix + "jcpuid" + libSuffix;
try { try {
InputStream libStream = resource.openStream(); InputStream libStream = resource.openStream();
outFile = new File(I2PAppContext.getGlobalContext().getBaseDir(), libPrefix + "jcpuid" + libSuffix); outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename);
fos = new FileOutputStream(outFile); fos = new FileOutputStream(outFile);
// wtf this was 4096*1024 which is really excessive for a roughly 4KB file // wtf this was 4096*1024 which is really excessive for a roughly 4KB file
byte buf[] = new byte[4096]; byte buf[] = new byte[4096];
@ -517,7 +522,6 @@ public class CPUID {
fos.close(); fos.close();
fos = null; fos = null;
System.load(outFile.getAbsolutePath());//System.load requires an absolute path to the lib System.load(outFile.getAbsolutePath());//System.load requires an absolute path to the lib
return true;
} catch (UnsatisfiedLinkError ule) { } catch (UnsatisfiedLinkError ule) {
if (_doLog) { if (_doLog) {
System.err.println("ERROR: The resource " + resourceName System.err.println("ERROR: The resource " + resourceName
@ -536,6 +540,10 @@ public class CPUID {
try { fos.close(); } catch (IOException ioe) {} 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() 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) { 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 src = new File(source);
File dst = new File(dest); File dst = new File(dest);
@ -226,7 +235,8 @@ public class FileUtil {
out.close(); out.close();
return true; return true;
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(); if (!quiet)
ioe.printStackTrace();
return false; return false;
} }
} }

View File

@ -24,6 +24,7 @@ import freenet.support.CPUInformation.IntelCPUInfo;
import freenet.support.CPUInformation.UnknownCPUException; import freenet.support.CPUInformation.UnknownCPUException;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
import net.i2p.util.FileUtil;
import net.i2p.util.Log; 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 * <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 * 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, * byte stream to a temporary file, loading the native library from that file.
* then deleting the file.</p> * 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 * @return true if it was loaded successfully, else false
* *
@ -538,9 +542,10 @@ public class NativeBigInteger extends BigInteger {
File outFile = null; File outFile = null;
FileOutputStream fos = null; FileOutputStream fos = null;
String filename = _libPrefix + "jbigi" + _libSuffix;
try { try {
InputStream libStream = resource.openStream(); InputStream libStream = resource.openStream();
outFile = new File(I2PAppContext.getGlobalContext().getBaseDir(), _libPrefix + "jbigi" + _libSuffix); outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename);
fos = new FileOutputStream(outFile); fos = new FileOutputStream(outFile);
// wtf this was 4096*1024 which is really excessive for a roughly 50KB file // wtf this was 4096*1024 which is really excessive for a roughly 50KB file
byte buf[] = new byte[4096]; byte buf[] = new byte[4096];
@ -552,7 +557,6 @@ public class NativeBigInteger extends BigInteger {
fos.close(); fos.close();
fos = null; fos = null;
System.load(outFile.getAbsolutePath()); //System.load requires an absolute path to the lib System.load(outFile.getAbsolutePath()); //System.load requires an absolute path to the lib
return true;
} catch (UnsatisfiedLinkError ule) { } catch (UnsatisfiedLinkError ule) {
if (_doLog) { if (_doLog) {
System.err.println("ERROR: The resource " + resourceName System.err.println("ERROR: The resource " + resourceName
@ -571,6 +575,10 @@ public class NativeBigInteger extends BigInteger {
try { fos.close(); } catch (IOException ioe) {} 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) { private static final String getResourceName(boolean optimized) {