Launcher/LaunchServer/src/main/java/pro/gravit/launchserver/StarterAgent.java

76 lines
2.7 KiB
Java
Raw Normal View History

package pro.gravit.launchserver;
2018-09-17 10:07:32 +03:00
import java.io.IOException;
import java.lang.instrument.Instrumentation;
2019-06-03 10:58:10 +03:00
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
2018-09-17 10:07:32 +03:00
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Arrays;
2018-09-17 10:07:32 +03:00
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
2018-09-17 10:07:32 +03:00
import java.util.jar.JarFile;
2019-01-09 11:29:54 +03:00
public final class StarterAgent {
2018-09-17 10:07:32 +03:00
2019-01-09 11:29:54 +03:00
private static final class StarterVisitor extends SimpleFileVisitor<Path> {
2019-05-15 14:11:22 +03:00
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);
}
2018-09-17 10:07:32 +03:00
private final Path filef;
2019-05-15 14:11:22 +03:00
private final boolean fixLib;
2019-05-15 14:11:22 +03:00
private StarterVisitor() {
this.filef = StarterAgent.libraries.resolve(".libraries_chmoded");
this.fixLib = !Files.exists(filef) && !Boolean.getBoolean("launcher.noLibrariesPosixPermsFix");
if (fixLib) {
2019-05-15 14:11:22 +03:00
try {
Files.deleteIfExists(filef);
Files.createFile(filef);
} catch (Throwable ignored) {
}
}
2018-09-22 17:33:00 +03:00
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
2019-05-15 14:11:22 +03:00
if (fixLib && Files.getFileAttributeView(file, PosixFileAttributeView.class) != null)
Files.setPosixFilePermissions(file, DPERMS);
2018-09-22 17:33:00 +03:00
if (file.toFile().getName().endsWith(".jar"))
inst.appendToSystemClassLoaderSearch(new JarFile(file.toFile()));
return super.visitFile(file, attrs);
}
}
2018-12-20 18:45:01 +03:00
public static Instrumentation inst = null;
public static Path libraries = null;
private static boolean isStarted = false;
2018-12-20 18:45:01 +03:00
public static boolean isAgentStarted() {
return isStarted;
}
2018-09-22 17:33:00 +03:00
public static void premain(String agentArgument, Instrumentation inst) {
StarterAgent.inst = inst;
libraries = Paths.get("libraries");
isStarted = true;
2018-09-22 17:33:00 +03:00
try {
Files.walkFileTree(libraries, Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new StarterVisitor());
2018-09-22 17:33:00 +03:00
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
2018-09-17 10:07:32 +03:00
}