1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-17 10:50:01 +00:00

Trim spaces from filesystem paths

I kinda hate this, but not sure what else to do. It might be worth
rewriting sanitizePath in the future to loop through the string once,
but I think this is good enough for now.
This commit is contained in:
Jonathan Coates 2022-11-25 20:12:10 +00:00
parent ee2670d53b
commit b8fce1eecc
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
2 changed files with 23 additions and 2 deletions

View File

@ -429,7 +429,9 @@ public static String sanitizePath(String path, boolean allowWildcards) {
// Collapse the string into its component parts, removing ..'s
var outputParts = new ArrayDeque<String>();
for (var part : Splitter.on('/').split(path)) {
for (var fullPart : Splitter.on('/').split(path)) {
var part = fullPart.strip();
if (part.isEmpty() || part.equals(".") || threeDotsPattern.matcher(part).matches()) {
// . is redundant
// ... and more are treated as .
@ -450,7 +452,7 @@ public static String sanitizePath(String path, boolean allowWildcards) {
}
} else if (part.length() >= 255) {
// If part length > 255 and it is the last part
outputParts.addLast(part.substring(0, 255));
outputParts.addLast(part.substring(0, 255).strip());
} else {
// Anything else we add to the stack
outputParts.addLast(part);

View File

@ -12,6 +12,8 @@
import dan200.computercraft.core.apis.ObjectWrapper;
import dan200.computercraft.core.apis.handles.EncodedWritableHandle;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.io.File;
import java.io.IOException;
@ -74,4 +76,21 @@ public void testUnmountCloses() throws FileSystemException {
var err = assertThrows(LuaException.class, () -> wrapper.call("write", "Tiny line"));
assertEquals("attempt to use a closed file", err.getMessage());
}
@ParameterizedTest(name = "{0}")
@MethodSource("sanitiseCases")
public void testSanitize(String input, String output) {
assertEquals(output, FileSystem.sanitizePath(input, false));
}
public static String[][] sanitiseCases() {
return new String[][]{
new String[]{ "a//b", "a/b" },
new String[]{ "a/./b", "a/b" },
new String[]{ "a/../b", "b" },
new String[]{ "a/.../b", "a/b" },
new String[]{ " a ", "a" },
new String[]{ "a b c", "a b c" },
};
}
}