Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lazily creating a tempdir in NativeLoader #1326

Merged
merged 1 commit into from
Jan 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@
* */
public class NativeLoader implements Serializable {

private String resourcesPath;
private final String resourcesPath;
private Boolean extractionDone = false;
private File tempDir;
private File tempDir = null;

public NativeLoader(String topLevelResourcesPath) throws IOException {
private File getOrCreateTempDir() throws IOException {
if (tempDir == null) {
tempDir = Files.createTempDirectory("mml-natives").toFile();
tempDir.deleteOnExit();
}

return tempDir;
}

public NativeLoader(String topLevelResourcesPath) {
this.resourcesPath = getResourcesPath(topLevelResourcesPath);
tempDir = Files.createTempDirectory("mml-natives").toFile();
tempDir.deleteOnExit();
}

/**
Expand All @@ -56,7 +63,7 @@ public void loadLibraryByName(String libName) {
libName = System.mapLibraryName(libName);
extractNativeLibraries(libName);
// Try to load library from extracted native resources
System.load(tempDir.getAbsolutePath() + File.separator + libName);
System.load(getOrCreateTempDir().getAbsolutePath() + File.separator + libName);
}
catch (Exception ee) {
throw new UnsatisfiedLinkError(String.format(
Expand Down Expand Up @@ -106,7 +113,7 @@ private static String getResourcesPath(String topLevelResourcesPath) {

private void extractResourceFromPath(String libName, String prefix) throws IOException {

File temp = new File(tempDir.getPath() + File.separator + libName);
File temp = new File(getOrCreateTempDir().getPath() + File.separator + libName);
temp.createNewFile();
temp.deleteOnExit();

Expand Down