[FEATURE] Исправление прав на ОС с поддержкой POSIX.

This commit is contained in:
Zaxar163 2019-05-03 10:57:28 +03:00
parent 889748712a
commit 66e8f29c19

View file

@ -4,27 +4,50 @@
import java.lang.instrument.Instrumentation; import java.lang.instrument.Instrumentation;
import java.nio.file.*; import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarFile; import java.util.jar.JarFile;
public final class StarterAgent { public final class StarterAgent {
private static final class StarterVisitor extends SimpleFileVisitor<Path> { private static final class StarterVisitor extends SimpleFileVisitor<Path> {
private final Instrumentation inst; private static final Set<PosixFilePermission> DPERMS;
static {
Set<PosixFilePermission> perms = new HashSet<>(Arrays.asList(PosixFilePermission.values()));
perms.remove(PosixFilePermission.OTHERS_WRITE);
perms.remove(PosixFilePermission.GROUP_WRITE);
DPERMS = Collections.unmodifiableSet(perms);
}
private StarterVisitor(Instrumentation inst) { private final Path filef;
this.inst = inst; private final boolean fixLib;
private StarterVisitor() {
this.filef = StarterAgent.libraries.resolve(".libraries_chmoded");
this.fixLib = !Files.exists(filef) && !Boolean.getBoolean("launcher.noLibrariesPosixPermsFix");
if (fixLib) {
try {
Files.deleteIfExists(filef);
Files.createFile(filef);
} catch (Throwable t) { }
}
} }
@Override @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (fixLib && Files.getFileAttributeView(file, PosixFileAttributeView.class) != null) Files.setPosixFilePermissions(file, DPERMS);
if (file.toFile().getName().endsWith(".jar")) if (file.toFile().getName().endsWith(".jar"))
inst.appendToSystemClassLoaderSearch(new JarFile(file.toFile())); inst.appendToSystemClassLoaderSearch(new JarFile(file.toFile()));
return super.visitFile(file, attrs); return super.visitFile(file, attrs);
} }
} }
public static Instrumentation inst; public static Instrumentation inst = null;
public static Path libraries = null;
private static boolean isStarted = false; private static boolean isStarted = false;
public static boolean isAgentStarted() { public static boolean isAgentStarted() {
@ -33,9 +56,10 @@ public static boolean isAgentStarted() {
public static void premain(String agentArgument, Instrumentation inst) { public static void premain(String agentArgument, Instrumentation inst) {
StarterAgent.inst = inst; StarterAgent.inst = inst;
libraries = Paths.get("libraries");
isStarted = true; isStarted = true;
try { try {
Files.walkFileTree(Paths.get("libraries"), Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new StarterVisitor(inst)); Files.walkFileTree(libraries, Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new StarterVisitor());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(System.err); e.printStackTrace(System.err);
} }