diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/StarterAgent.java b/LaunchServer/src/main/java/ru/gravit/launchserver/StarterAgent.java index ed8b85c5..dc8b7457 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/StarterAgent.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/StarterAgent.java @@ -4,27 +4,50 @@ import java.lang.instrument.Instrumentation; import java.nio.file.*; 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.HashSet; +import java.util.Set; import java.util.jar.JarFile; public final class StarterAgent { private static final class StarterVisitor extends SimpleFileVisitor { - private final Instrumentation inst; + private static final Set DPERMS; + static { + Set 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) { - this.inst = inst; + private final Path filef; + 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 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")) inst.appendToSystemClassLoaderSearch(new JarFile(file.toFile())); return super.visitFile(file, attrs); } } - public static Instrumentation inst; + public static Instrumentation inst = null; + public static Path libraries = null; private static boolean isStarted = false; public static boolean isAgentStarted() { @@ -33,9 +56,10 @@ public static boolean isAgentStarted() { public static void premain(String agentArgument, Instrumentation inst) { StarterAgent.inst = inst; + libraries = Paths.get("libraries"); isStarted = true; 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) { e.printStackTrace(System.err); }