mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-01 02:46:41 +00:00
Properly close all files when dealing with archives (#1274)
* Properly close all files when dealing with archives * Move file utils to SafeFiles class * Licenses (cherry picked from commit a600266d41151eec4f2239cf90e202bb99fa3a8b)
This commit is contained in:
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.util.function;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* I/O function type.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface IOFunction<T, R> {
|
||||
|
||||
static <T, R> Function<T, R> unchecked(IOFunction<T, R> function) {
|
||||
return param -> {
|
||||
try {
|
||||
return function.apply(param);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
R apply(T param) throws IOException;
|
||||
|
||||
}
|
@ -20,6 +20,7 @@
|
||||
package com.sk89q.worldedit.util.function;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
|
||||
/**
|
||||
* I/O runnable type.
|
||||
@ -27,6 +28,16 @@ import java.io.IOException;
|
||||
@FunctionalInterface
|
||||
public interface IORunnable {
|
||||
|
||||
static Runnable unchecked(IORunnable runnable) {
|
||||
return () -> {
|
||||
try {
|
||||
runnable.run();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void run() throws IOException;
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.util.io.file;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* Represents an archive opened as a directory. This must be closed after work on the Path is
|
||||
* done.
|
||||
*/
|
||||
public interface ArchiveDir extends Closeable {
|
||||
|
||||
Path getPath();
|
||||
|
||||
}
|
@ -35,6 +35,6 @@ public interface ArchiveNioSupport {
|
||||
* @param archive the archive to open
|
||||
* @return the path for the root of the archive, if available
|
||||
*/
|
||||
Optional<Path> tryOpenAsDir(Path archive) throws IOException;
|
||||
Optional<ArchiveDir> tryOpenAsDir(Path archive) throws IOException;
|
||||
|
||||
}
|
||||
|
@ -45,9 +45,9 @@ public class ArchiveNioSupports {
|
||||
.build();
|
||||
}
|
||||
|
||||
public static Optional<Path> tryOpenAsDir(Path archive) throws IOException {
|
||||
public static Optional<ArchiveDir> tryOpenAsDir(Path archive) throws IOException {
|
||||
for (ArchiveNioSupport support : SUPPORTS) {
|
||||
Optional<Path> fs = support.tryOpenAsDir(archive);
|
||||
Optional<ArchiveDir> fs = support.tryOpenAsDir(archive);
|
||||
if (fs.isPresent()) {
|
||||
return fs;
|
||||
}
|
||||
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.util.io.file;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class SafeFiles {
|
||||
|
||||
/**
|
||||
* A version of {@link Files#list(Path)} that won't leak resources.
|
||||
*
|
||||
* <p>
|
||||
* Instead, it immediately consumes the entire listing into a {@link List} and
|
||||
* calls {@link List#stream()}.
|
||||
* </p>
|
||||
*
|
||||
* @param dir the directory to list
|
||||
* @return an I/O-resource-free stream of the files in the directory
|
||||
* @throws IOException if an I/O error occurs
|
||||
*/
|
||||
public static Stream<Path> noLeakFileList(Path dir) throws IOException {
|
||||
try (Stream<Path> stream = Files.list(dir)) {
|
||||
return stream.collect(Collectors.toList()).stream();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link Path#getFileName()} includes a slash sometimes for some reason.
|
||||
* This will get rid of it.
|
||||
*
|
||||
* @param path the path to get the file name for
|
||||
* @return the file name of the given path
|
||||
*/
|
||||
public static String canonicalFileName(Path path) {
|
||||
return dropSlash(path.getFileName().toString());
|
||||
}
|
||||
|
||||
private static String dropSlash(String name) {
|
||||
if (name.isEmpty() || name.codePointBefore(name.length()) != '/') {
|
||||
return name;
|
||||
}
|
||||
return name.substring(0, name.length() - 1);
|
||||
}
|
||||
|
||||
private SafeFiles() {
|
||||
}
|
||||
}
|
@ -22,6 +22,7 @@ package com.sk89q.worldedit.util.io.file;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import net.java.truevfs.access.TArchiveDetector;
|
||||
import net.java.truevfs.access.TFileSystem;
|
||||
import net.java.truevfs.access.TPath;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -45,15 +46,28 @@ public final class TrueVfsArchiveNioSupport implements ArchiveNioSupport {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Path> tryOpenAsDir(Path archive) throws IOException {
|
||||
public Optional<ArchiveDir> tryOpenAsDir(Path archive) throws IOException {
|
||||
String fileName = archive.getFileName().toString();
|
||||
int dot = fileName.indexOf('.');
|
||||
if (dot < 0 || dot >= fileName.length() || !ALLOWED_EXTENSIONS.contains(fileName.substring(dot + 1))) {
|
||||
if (dot < 0 || dot >= fileName.length() || !ALLOWED_EXTENSIONS
|
||||
.contains(fileName.substring(dot + 1))) {
|
||||
return Optional.empty();
|
||||
}
|
||||
TPath root = new TPath(archive).getFileSystem().getPath("/");
|
||||
return Optional.of(ArchiveNioSupports.skipRootSameName(
|
||||
TFileSystem fileSystem = new TPath(archive).getFileSystem();
|
||||
TPath root = fileSystem.getPath("/");
|
||||
Path realRoot = ArchiveNioSupports.skipRootSameName(
|
||||
root, fileName.substring(0, dot)
|
||||
));
|
||||
);
|
||||
return Optional.of(new ArchiveDir() {
|
||||
@Override
|
||||
public Path getPath() {
|
||||
return realRoot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
fileSystem.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -37,17 +37,28 @@ public final class ZipArchiveNioSupport implements ArchiveNioSupport {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Path> tryOpenAsDir(Path archive) throws IOException {
|
||||
public Optional<ArchiveDir> tryOpenAsDir(Path archive) throws IOException {
|
||||
if (!archive.getFileName().toString().endsWith(".zip")) {
|
||||
return Optional.empty();
|
||||
}
|
||||
FileSystem zipFs = FileSystems.newFileSystem(
|
||||
archive, getClass().getClassLoader()
|
||||
);
|
||||
return Optional.of(ArchiveNioSupports.skipRootSameName(
|
||||
Path root = ArchiveNioSupports.skipRootSameName(
|
||||
zipFs.getPath("/"), archive.getFileName().toString()
|
||||
.replaceFirst("\\.zip$", "")
|
||||
));
|
||||
);
|
||||
return Optional.of(new ArchiveDir() {
|
||||
@Override
|
||||
public Path getPath() {
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
zipFs.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user