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

Fixed creation of temporary dir in NativeUtils #4262

Merged
merged 4 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
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 @@ -26,6 +26,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;

/**
Expand All @@ -50,13 +52,9 @@ public static void loadLibraryFromJar(String path) throws Exception {
String[] parts = path.split("/");
String filename = (parts.length > 0) ? parts[parts.length - 1] : null;

File dir = File.createTempFile("native", "");
dir.delete();
if (!(dir.mkdir())) {
throw new IOException("Failed to create temp directory " + dir.getAbsolutePath());
}
dir.deleteOnExit();
File temp = new File(dir, filename);
Path dir = Files.createTempDirectory("native");
dir.toFile().deleteOnExit();
File temp = new File(dir.toString(), filename);
temp.deleteOnExit();

byte[] buffer = new byte[1024];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import lombok.NonNull;
import lombok.experimental.UtilityClass;

/**
Expand All @@ -46,17 +48,17 @@ public class NativeUtils {
value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
justification = "work around for java 9: https://github.com/spotbugs/spotbugs/issues/493")
public static void loadLibraryFromJar(String path) throws Exception {
com.google.common.base.Preconditions.checkArgument(path.startsWith("/"), "absolute path must start with /");
checkArgument(path.startsWith("/"), "absolute path must start with /");

String[] parts = path.split("/");
String filename = (parts.length > 0) ? parts[parts.length - 1] : null;
checkArgument(parts.length > 0, "absolute path must contain file name");

File dir = File.createTempFile("native", "");
if (!(dir.mkdir())) {
throw new IOException("Failed to create temp directory " + dir.getAbsolutePath());
}
dir.deleteOnExit();
File temp = new File(dir, filename);
String filename = parts[parts.length - 1];
checkArgument(path.startsWith("/"), "absolute path must start with /");

Path dir = Files.createTempDirectory("native");
dir.toFile().deleteOnExit();
File temp = new File(dir.toString(), filename);
temp.deleteOnExit();

byte[] buffer = new byte[1024];
Expand All @@ -79,4 +81,10 @@ public static void loadLibraryFromJar(String path) throws Exception {

System.load(temp.getAbsolutePath());
}

private static void checkArgument(boolean expression, @NonNull Object errorMessage) {
if (!expression) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
}
}
}
Loading