Skip to content

Commit

Permalink
Fix native library cleanup
Browse files Browse the repository at this point in the history
Ref sbt/sbt#6931

Problem
-------
JNI version of the ipc-socket extracts native library
into a temp directory, and after it's done tries to delete it.
Because it uses /tmp by default, this effectively attemps to
remove empty directories it.

Solution
--------
1. Namespace it to use `.sbt` under temp.
2. Check that the file has the libsbtipcsocket prefix.
  • Loading branch information
eed3si9n committed Jun 26, 2022
1 parent 7b97350 commit 6889572
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
1 change: 0 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ buildWin32 / skip := {
isWin || Try(s"which ${(buildWin32 / nativeCompiler).value}".!!).fold(_ => true, _.isEmpty)
}
Test / fork := true
Test / javaOptions += s"-Dsbt.ipcsocket.tmpdir=${(Compile / target).value}/jni"
Test / fullClasspath :=
(Test / fullClasspath).dependsOn(buildDarwin, buildLinux, buildWin32).value
clangfmt / fileInputs += baseDirectory.value.toGlob / "jni" / "*.c"
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.5.3
sbt.version=1.6.2
27 changes: 17 additions & 10 deletions src/main/java/org/scalasbt/ipcsocket/NativeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,25 @@ class NativeLoader {
private static final String pid =
isWindows ? "" : ManagementFactory.getRuntimeMXBean().getName().replaceAll("@.*", "");

private static String tmpDirLocation() {
return System.getProperty("sbt.ipcsocket.tmpdir", System.getProperty("java.io.tmpdir"));
private static Path tmpDir() {
String prop = System.getProperty("sbt.ipcsocket.tmpdir");
String tmp = System.getProperty("java.io.tmpdir");
if (prop != null) {
return Paths.get(prop);
} else {
return Paths.get(tmp).resolve(".sbt").resolve("ipcsocket");
}
}

private static final String tempFilePrefix = "libsbtipcsocket";

static void load() throws UnsatisfiedLinkError {
if (!loaded.get()) {
final String os = System.getProperty("os.name", "").toLowerCase();
final boolean isMac = os.startsWith("mac");
final boolean isLinux = os.startsWith("linux");
final boolean isWindows = os.startsWith("windows");
final boolean is64bit = System.getProperty("sun.arch.data.model", "64").equals("64");
String tmpDir = tmpDirLocation();
if (is64bit && (isMac || isLinux || isWindows)) {
final String extension = "." + (isMac ? "dylib" : isWindows ? "dll" : "so");
final String libName = (isWindows ? "" : "lib") + "sbtipcsocket" + extension;
Expand All @@ -55,11 +62,8 @@ static void load() throws UnsatisfiedLinkError {
final URL url = NativeLoader.class.getClassLoader().getResource(resource);
if (url == null) throw new UnsatisfiedLinkError(resource + " not found on classpath");
try {
final Path base =
tmpDir == null
? Files.createTempDirectory("sbtipcsocket")
: Files.createDirectories(Paths.get(tmpDir));
final Path output = Files.createTempFile(base, "libsbtipcsocket", extension);
final Path base = Files.createDirectories(tmpDir());
final Path output = Files.createTempFile(base, tempFilePrefix, extension);
try (final InputStream in = url.openStream();
final FileChannel channel = FileChannel.open(output, StandardOpenOption.WRITE)) {
int total = 0;
Expand Down Expand Up @@ -117,7 +121,7 @@ private static class CleanupRunnable implements Runnable {
public void run() {
try {
Files.walkFileTree(
Paths.get(tmpDirLocation()),
tmpDir(),
new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(
Expand All @@ -128,7 +132,10 @@ public FileVisitResult preVisitDirectory(
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs)
throws IOException {
if (isWindows) {
if (!file.getFileName().toString().startsWith(tempFilePrefix)) {
// do nothing
return FileVisitResult.CONTINUE;
} else if (isWindows) {
try {
Files.deleteIfExists(file);
} catch (final IOException e) {
Expand Down

0 comments on commit 6889572

Please sign in to comment.