From 66e8f29c19d9c6074bb37d796bece460144cebd1 Mon Sep 17 00:00:00 2001 From: Zaxar163 Date: Fri, 3 May 2019 10:57:28 +0300 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=98=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D0=9E=D0=A1=20=D1=81=20=D0=BF=D0=BE=D0=B4?= =?UTF-8?q?=D0=B4=D0=B5=D1=80=D0=B6=D0=BA=D0=BE=D0=B9=20POSIX.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/gravit/launchserver/StarterAgent.java | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) 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); }