Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a system property to make UnionFS optional #66

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/cpw/mods/jarhandling/SecureJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static Provider fromPath(final Path path, final BiPredicate<String, Strin
try {
var entries = Files.readAllLines(path).stream()
.map(String::trim)
.filter(l->l.length() > 0 && !l.startsWith("#")) // We support comments :)
.filter(l-> !l.isEmpty() && !l.startsWith("#")) // We support comments :)
.filter(p-> pkgFilter == null || pkgFilter.test(p.replace('.','/'), ""))
.toList();
return new Provider(sname, entries);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/cpw/mods/jarhandling/impl/Jar.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import cpw.mods.jarhandling.JarMetadata;
import cpw.mods.jarhandling.SecureJar;
import cpw.mods.niofs.union.UnionFileSystem;
import cpw.mods.util.LambdaExceptionUtils;
import org.jetbrains.annotations.Nullable;

import java.io.InputStream;
import java.lang.module.ModuleDescriptor;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -24,7 +24,7 @@ public class Jar implements SecureJar {
private final JarContentsImpl contents;
private final Manifest manifest;
private final JarSigningData signingData;
private final UnionFileSystem filesystem;
private final FileSystem filesystem;

private final JarModuleDataProvider moduleDataProvider;

Expand Down Expand Up @@ -66,7 +66,7 @@ public ModuleDataProvider moduleDataProvider() {

@Override
public Path getPrimaryPath() {
return filesystem.getPrimaryPath();
return contents.getPrimaryPath();
}

public Optional<URI> findFile(final String name) {
Expand Down
35 changes: 26 additions & 9 deletions src/main/java/cpw/mods/jarhandling/impl/JarContentsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import cpw.mods.jarhandling.JarContents;
import cpw.mods.jarhandling.SecureJar;
import cpw.mods.niofs.union.UnionFileSystem;
import cpw.mods.niofs.union.UnionFileSystemProvider;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -29,14 +30,19 @@
import java.util.jar.Manifest;

public class JarContentsImpl implements JarContents {
private static final boolean USE_UNION_FS_ONLY_IF_NEEDED = Boolean.getBoolean("securejarhandler.useUnionFsOnlyIfNeeded");
private static final UnionFileSystemProvider UFSP = (UnionFileSystemProvider) FileSystemProvider.installedProviders()
.stream()
.filter(fsp->fsp.getScheme().equals("union"))
.findFirst()
.orElseThrow(()->new IllegalStateException("Couldn't find UnionFileSystemProvider"));
private static final Set<String> NAUGHTY_SERVICE_FILES = Set.of("org.codehaus.groovy.runtime.ExtensionModule");

final UnionFileSystem filesystem;
final FileSystem filesystem;
final Path rootPath;
final Path primaryPath;
@Nullable
final BiPredicate<String, String> pathFilter;
// Code signing data
final JarSigningData signingData = new JarSigningData();
// Manifest of the jar
Expand All @@ -53,7 +59,18 @@ public JarContentsImpl(Path[] paths, Supplier<Manifest> defaultManifest, @Nullab
var validPaths = Arrays.stream(paths).filter(Files::exists).toArray(Path[]::new);
if (validPaths.length == 0)
throw new UncheckedIOException(new IOException("Invalid paths argument, contained no existing paths: " + Arrays.toString(paths)));
this.filesystem = UFSP.newFileSystem(pathFilter, validPaths);
if (USE_UNION_FS_ONLY_IF_NEEDED && pathFilter == null && validPaths.length == 1 && !Files.isDirectory(validPaths[0])) {
try {
this.filesystem = FileSystems.newFileSystem(validPaths[0]);
} catch (IOException e) {
throw new UncheckedIOException("Failed to open " + validPaths[0], e);
}
} else {
this.filesystem = UFSP.newFileSystem(pathFilter, validPaths);
}
this.pathFilter = pathFilter;
this.primaryPath = validPaths[0];
this.rootPath = this.filesystem.getRootDirectories().iterator().next();
// Find the manifest, and read its signing data
this.manifest = readManifestAndSigningData(defaultManifest, validPaths);
// Read multi-release jar information
Expand Down Expand Up @@ -115,7 +132,7 @@ private Map<Path, Integer> readMultiReleaseInfo() {
return Map.of();
}

var vers = filesystem.getRoot().resolve("META-INF/versions");
var vers = rootPath.resolve("META-INF/versions");
if (!Files.isDirectory(vers)) return Map.of();

try (var walk = Files.walk(vers)) {
Expand All @@ -139,7 +156,7 @@ private Map<Path, Integer> readMultiReleaseInfo() {

@Override
public Path getPrimaryPath() {
return filesystem.getPrimaryPath();
return primaryPath;
}

@Override
Expand All @@ -148,7 +165,7 @@ public Optional<URI> findFile(String name) {
if (this.nameOverrides.containsKey(rel)) {
rel = this.filesystem.getPath("META-INF", "versions", this.nameOverrides.get(rel).toString()).resolve(rel);
}
return Optional.of(this.filesystem.getRoot().resolve(rel)).filter(Files::exists).map(Path::toUri);
return Optional.of(this.rootPath.resolve(rel)).filter(Files::exists).map(Path::toUri);
}

@Override
Expand All @@ -164,7 +181,7 @@ public Set<String> getPackagesExcluding(String... excludedRootPackages) {

Set<String> packages = new HashSet<>();
try {
Files.walkFileTree(this.filesystem.getRoot(), new SimpleFileVisitor<>() {
Files.walkFileTree(this.rootPath, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (file.getFileName().toString().endsWith(".class") && attrs.isRegularFile()) {
Expand Down Expand Up @@ -201,12 +218,12 @@ public Set<String> getPackages() {
@Override
public List<SecureJar.Provider> getMetaInfServices() {
if (this.providers == null) {
final var services = this.filesystem.getRoot().resolve("META-INF/services/");
final var services = this.rootPath.resolve("META-INF/services/");
if (Files.exists(services)) {
try (var walk = Files.walk(services, 1)) {
this.providers = walk.filter(path->!Files.isDirectory(path))
.filter(path -> !NAUGHTY_SERVICE_FILES.contains(path.getFileName().toString()))
.map((Path path1) -> SecureJar.Provider.fromPath(path1, filesystem.getFilesystemFilter()))
.map((Path path1) -> SecureJar.Provider.fromPath(path1, pathFilter))
.toList();
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down
Loading