This is an automated email from the ASF dual-hosted git repository. mmerli pushed a commit to branch branch-4.16 in repository https://gitbox.apache.org/repos/asf/bookkeeper.git
The following commit(s) were added to refs/heads/branch-4.16 by this push: new a23b1a0294 Fixed creation of temporary dir in NativeUtils (#4262) a23b1a0294 is described below commit a23b1a02949f580f4e584dbff9131b26bd0712ff Author: Matteo Merli <mme...@apache.org> AuthorDate: Tue Apr 2 18:56:10 2024 -0700 Fixed creation of temporary dir in NativeUtils (#4262) ### Motivation Creating the temp directory for unpacking the native library is failing for the affinity library. ### Changes Use `Files.createTempDirectory()` instead. --- .../com/scurrilous/circe/utils/NativeUtils.java | 12 +++++----- .../common/util/affinity/impl/NativeUtils.java | 26 ++++++++++++++-------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/circe-checksum/src/main/java/com/scurrilous/circe/utils/NativeUtils.java b/circe-checksum/src/main/java/com/scurrilous/circe/utils/NativeUtils.java index 2932c65000..988264c952 100644 --- a/circe-checksum/src/main/java/com/scurrilous/circe/utils/NativeUtils.java +++ b/circe-checksum/src/main/java/com/scurrilous/circe/utils/NativeUtils.java @@ -26,6 +26,8 @@ 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 java.util.Locale; /** @@ -50,13 +52,9 @@ public class NativeUtils { 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]; diff --git a/cpu-affinity/src/main/java/org/apache/bookkeeper/common/util/affinity/impl/NativeUtils.java b/cpu-affinity/src/main/java/org/apache/bookkeeper/common/util/affinity/impl/NativeUtils.java index 2dbec828d3..fde69f9011 100644 --- a/cpu-affinity/src/main/java/org/apache/bookkeeper/common/util/affinity/impl/NativeUtils.java +++ b/cpu-affinity/src/main/java/org/apache/bookkeeper/common/util/affinity/impl/NativeUtils.java @@ -24,9 +24,11 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 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; /** @@ -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]; @@ -79,4 +81,10 @@ public class NativeUtils { System.load(temp.getAbsolutePath()); } + + private static void checkArgument(boolean expression, @NonNull Object errorMessage) { + if (!expression) { + throw new IllegalArgumentException(String.valueOf(errorMessage)); + } + } }