Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Files.setPosixFilePermissions for chmod when possible #22

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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