2018-09-17 10:07:32 +03:00
|
|
|
package ru.gravit.launchserver;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.instrument.Instrumentation;
|
2019-01-15 06:35:39 +03:00
|
|
|
import java.nio.file.*;
|
2018-09-17 10:07:32 +03:00
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
2019-05-03 10:57:28 +03:00
|
|
|
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;
|
2019-05-03 10:57:28 +03:00
|
|
|
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
|
|
|
|
2019-05-03 10:57:28 +03:00
|
|
|
private final Path filef;
|
2019-05-15 14:11:22 +03:00
|
|
|
private final boolean fixLib;
|
2019-05-03 10:57:28 +03:00
|
|
|
|
2019-05-15 14:11:22 +03:00
|
|
|
private StarterVisitor() {
|
2019-05-03 10:57:28 +03:00
|
|
|
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) {
|
|
|
|
}
|
2019-05-03 10:57:28 +03:00
|
|
|
}
|
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
|
|
|
|
2019-05-03 10:57:28 +03:00
|
|
|
public static Instrumentation inst = null;
|
|
|
|
public static Path libraries = null;
|
2018-12-20 18:36:02 +03:00
|
|
|
private static boolean isStarted = false;
|
2018-12-20 18:45:01 +03:00
|
|
|
|
|
|
|
public static boolean isAgentStarted() {
|
2018-12-20 18:36:02 +03:00
|
|
|
return isStarted;
|
|
|
|
}
|
2018-09-22 17:33:00 +03:00
|
|
|
|
|
|
|
public static void premain(String agentArgument, Instrumentation inst) {
|
2018-12-20 18:36:02 +03:00
|
|
|
StarterAgent.inst = inst;
|
2019-05-03 10:57:28 +03:00
|
|
|
libraries = Paths.get("libraries");
|
2018-12-20 18:36:02 +03:00
|
|
|
isStarted = true;
|
2018-09-22 17:33:00 +03:00
|
|
|
try {
|
2019-05-03 10:57:28 +03:00
|
|
|
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
|
|
|
}
|