Skip to content

Commit

Permalink
Merge pull request #22 from slaunay/use-java7-chmod-with-unix-chmod-f…
Browse files Browse the repository at this point in the history
…allback

Use Files.setPosixFilePermissions for chmod when possible
  • Loading branch information
chirino committed Jun 20, 2016
2 parents 2952e02 + 61ac652 commit 40f9f23
Showing 1 changed file with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
package org.fusesource.hawtjni.runtime;

import java.io.*;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import java.util.Set;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -287,7 +289,7 @@ private File extract(ArrayList<String> errors, URL source, String prefix, String
while ((read = is.read(buffer)) != -1) {
os.write(buffer, 0, read);
}
chmod("755", target);
chmod755(target);
}
target.deleteOnExit();
return target;
Expand All @@ -313,12 +315,39 @@ static private void close(Closeable file) {
}
}

private void chmod(String permision, File path) {
private void chmod755(File file) {
if (getPlatform().startsWith("windows"))
return;
return;
// Use Files.setPosixFilePermissions if we are running Java 7+ to avoid forking the JVM for executing chmod
boolean chmodSuccessful = false;
try {
Runtime.getRuntime().exec(new String[] { "chmod", permision, path.getCanonicalPath() }).waitFor();
} catch (Throwable e) {
ClassLoader classLoader = getClass().getClassLoader();
// Check if the PosixFilePermissions exists in the JVM, if not this will throw a ClassNotFoundException
Class<?> posixFilePermissionsClass = classLoader.loadClass("java.nio.file.attribute.PosixFilePermissions");
// Set <PosixFilePermission> permissionSet = PosixFilePermissions.fromString("rwxr-xr-x")
Method fromStringMethod = posixFilePermissionsClass.getMethod("fromString", String.class);
Object permissionSet = fromStringMethod.invoke(null, new Object[] {"rwxr-xr-x"});
// Path path = file.toPath()
Object path = file.getClass().getMethod("toPath").invoke(file);
// Files.setPosixFilePermissions(path, permissionSet)
Class<?> pathClass = classLoader.loadClass("java.nio.file.Path");
Class<?> filesClass = classLoader.loadClass("java.nio.file.Files");
Method setPosixFilePermissionsMethod = filesClass.getMethod("setPosixFilePermissions", pathClass, Set.class);
setPosixFilePermissionsMethod.invoke(null, new Object[] {path, permissionSet});
chmodSuccessful = true;
} catch (ClassNotFoundException ignored) {
// Ignored as we are probably running in a JVM < 7
} catch (Exception e) {
// NoSuchMethodException | InvocationTargetException | IllegalAccessException
System.err.println("Unable to use Files.setPosixFilePermissions: " + e.getMessage() +
", falling back to Runtime.exec");
}
if (!chmodSuccessful) {
// Fallback to starting a new process
try {
Runtime.getRuntime().exec(new String[]{"chmod", "755", file.getCanonicalPath()}).waitFor();
} catch (Throwable e) {
}
}
}

Expand Down

0 comments on commit 40f9f23

Please sign in to comment.