From aa55d0bb0ab19aefd33cbd05025c4b4b18e25116 Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Tue, 2 Apr 2024 08:55:42 -0700 Subject: [PATCH 1/4] Fixed creation of temporary dir in NativeUtils --- .../java/com/scurrilous/circe/utils/NativeUtils.java | 12 +++++------- .../common/util/affinity/impl/NativeUtils.java | 11 +++++------ 2 files changed, 10 insertions(+), 13 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 2932c650004..988264c9521 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.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 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]; 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 2dbec828d34..354ea31ef8a 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 @@ -27,6 +27,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; import lombok.experimental.UtilityClass; /** @@ -51,12 +53,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", ""); - 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]; From a38ee5679bffb7de9779b9e72201c0bdbe61def8 Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Tue, 2 Apr 2024 10:03:05 -0700 Subject: [PATCH 2/4] Removed unused import --- .../apache/bookkeeper/common/util/affinity/impl/NativeUtils.java | 1 - 1 file changed, 1 deletion(-) 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 354ea31ef8a..85e9983a6dd 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,7 +24,6 @@ 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; From c416e7bb799b6da71a4f64ecc09cd750da2c4a96 Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Tue, 2 Apr 2024 10:48:12 -0700 Subject: [PATCH 3/4] Fixed check conditions --- .../common/util/affinity/impl/NativeUtils.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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 85e9983a6dd..b5e02c05fd4 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 @@ -20,6 +20,7 @@ */ package org.apache.bookkeeper.common.util.affinity.impl; +import com.google.common.base.Preconditions; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.File; import java.io.FileNotFoundException; @@ -28,6 +29,7 @@ import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Objects; import lombok.experimental.UtilityClass; /** @@ -47,10 +49,13 @@ 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 /"); + Preconditions.checkArgument(path.startsWith("/"), "absolute path must start with /"); String[] parts = path.split("/"); - String filename = (parts.length > 0) ? parts[parts.length - 1] : null; + Preconditions.checkArgument(parts.length > 0, "absolute path must contain file name"); + + String filename = parts[parts.length - 1]; + Preconditions.checkArgument(path.startsWith("/"), "absolute path must start with /"); Path dir = Files.createTempDirectory("native"); dir.toFile().deleteOnExit(); From 2a90795bd75e6fa99ae0c08278d9bca0a3637e0f Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Tue, 2 Apr 2024 12:03:19 -0700 Subject: [PATCH 4/4] Fixed checkstyle --- .../common/util/affinity/impl/NativeUtils.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) 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 b5e02c05fd4..fde69f90114 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 @@ -20,7 +20,6 @@ */ package org.apache.bookkeeper.common.util.affinity.impl; -import com.google.common.base.Preconditions; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.File; import java.io.FileNotFoundException; @@ -29,7 +28,7 @@ import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Objects; +import lombok.NonNull; import lombok.experimental.UtilityClass; /** @@ -49,13 +48,13 @@ 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 { - Preconditions.checkArgument(path.startsWith("/"), "absolute path must start with /"); + checkArgument(path.startsWith("/"), "absolute path must start with /"); String[] parts = path.split("/"); - Preconditions.checkArgument(parts.length > 0, "absolute path must contain file name"); + checkArgument(parts.length > 0, "absolute path must contain file name"); String filename = parts[parts.length - 1]; - Preconditions.checkArgument(path.startsWith("/"), "absolute path must start with /"); + checkArgument(path.startsWith("/"), "absolute path must start with /"); Path dir = Files.createTempDirectory("native"); dir.toFile().deleteOnExit(); @@ -82,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)); + } + } }