1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-10 09:20:28 +00:00

Make FileSystem.toLocal private

Use a custom to-local function in the various ArchiveMounts, which don't
faff around with sanitising paths.
This commit is contained in:
Jonathan Coates 2024-06-26 19:48:18 +01:00
parent 0895200681
commit 052e7a7ae5
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
4 changed files with 26 additions and 8 deletions

View File

@ -7,7 +7,6 @@ package dan200.computercraft.shared.computer.core;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import dan200.computercraft.api.filesystem.FileOperationException; import dan200.computercraft.api.filesystem.FileOperationException;
import dan200.computercraft.core.filesystem.ArchiveMount; import dan200.computercraft.core.filesystem.ArchiveMount;
import dan200.computercraft.core.filesystem.FileSystem;
import net.minecraft.ResourceLocationException; import net.minecraft.ResourceLocationException;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
@ -65,9 +64,10 @@ public final class ResourceMount extends ArchiveMount<ResourceMount.FileEntry> {
existingNamespace = file.getNamespace(); existingNamespace = file.getNamespace();
if (!file.getNamespace().equals(namespace)) continue; if (!file.getNamespace().equals(namespace)) continue;
if (!FileSystem.contains(subPath, file.getPath())) continue; // Some packs seem to include the parent?
var localPath = FileSystem.toLocal(file.getPath(), subPath); var localPath = getLocalPath(file.getPath(), subPath);
if (localPath == null) continue;
try { try {
getOrCreateChild(newRoot, localPath, this::createEntry); getOrCreateChild(newRoot, localPath, this::createEntry);
} catch (ResourceLocationException e) { } catch (ResourceLocationException e) {

View File

@ -8,6 +8,7 @@ import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import dan200.computercraft.core.apis.handles.ArrayByteChannel; import dan200.computercraft.core.apis.handles.ArrayByteChannel;
import javax.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
import java.nio.channels.SeekableByteChannel; import java.nio.channels.SeekableByteChannel;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -82,6 +83,24 @@ public abstract class ArchiveMount<T extends ArchiveMount.FileEntry<T>> extends
*/ */
protected abstract byte[] getFileContents(String path, T file) throws IOException; protected abstract byte[] getFileContents(String path, T file) throws IOException;
/**
* Convert an absolute path to one relative to {@code root}. If this path is not a child of {@code root}, return
* {@code null}.
*
* @param path The full path.
* @param root The root directory to be relative to.
* @return The relativised path, or {@code null}.
*/
protected static @Nullable String getLocalPath(String path, String root) {
// Some packs seem to include files not under the root, so drop them immediately.
if (!path.startsWith(root)) return null;
if (path.length() == root.length()) return "";
if (path.charAt(root.length()) != '/') return null;
return path.substring(root.length() + 1);
}
protected static class FileEntry<T extends FileEntry<T>> extends AbstractInMemoryMount.FileEntry<T> { protected static class FileEntry<T extends FileEntry<T>> extends AbstractInMemoryMount.FileEntry<T> {
long size = -1; long size = -1;
} }

View File

@ -404,7 +404,7 @@ public class FileSystem {
return String.join("/", outputParts); return String.join("/", outputParts);
} }
public static boolean contains(String pathA, String pathB) { private static boolean contains(String pathA, String pathB) {
pathA = sanitizePath(pathA).toLowerCase(Locale.ROOT); pathA = sanitizePath(pathA).toLowerCase(Locale.ROOT);
pathB = sanitizePath(pathB).toLowerCase(Locale.ROOT); pathB = sanitizePath(pathB).toLowerCase(Locale.ROOT);
@ -421,7 +421,7 @@ public class FileSystem {
} }
} }
public static String toLocal(String path, String location) { static String toLocal(String path, String location) {
path = sanitizePath(path); path = sanitizePath(path);
location = sanitizePath(location); location = sanitizePath(location);

View File

@ -49,10 +49,9 @@ public final class JarMount extends ArchiveMount<JarMount.FileEntry> implements
while (zipEntries.hasMoreElements()) { while (zipEntries.hasMoreElements()) {
var entry = zipEntries.nextElement(); var entry = zipEntries.nextElement();
var entryPath = entry.getName(); var localPath = getLocalPath(entry.getName(), subPath);
if (!entryPath.startsWith(subPath)) continue; if (localPath == null) continue;
var localPath = FileSystem.toLocal(entryPath, subPath);
getOrCreateChild(root, localPath, x -> new FileEntry()).setup(entry); getOrCreateChild(root, localPath, x -> new FileEntry()).setup(entry);
} }
} }