Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Commit

Permalink
Fixed creation of temporary dir in NativeUtils (apache#4262)
Browse files Browse the repository at this point in the history
### Motivation

Creating the temp directory for unpacking the native library is failing for the affinity library.

### Changes

Use `Files.createTempDirectory()` instead.
  • Loading branch information
merlimat committed Apr 3, 2024
1 parent 67a5819 commit a23b1a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
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));
}
}
}

0 comments on commit a23b1a0

Please sign in to comment.