diff --git a/projects/core/src/main/java/dan200/computercraft/core/apis/FSAPI.java b/projects/core/src/main/java/dan200/computercraft/core/apis/FSAPI.java index 794d8b7c4..09c37ba9c 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/apis/FSAPI.java +++ b/projects/core/src/main/java/dan200/computercraft/core/apis/FSAPI.java @@ -101,7 +101,7 @@ public class FSAPI implements ILuaAPI { * } */ @LuaFunction - public final String[] list(String path) throws LuaException { + public final List list(String path) throws LuaException { try (var ignored = environment.time(Metrics.FS_OPS)) { return getFileSystem().list(path); } catch (FileSystemException e) { diff --git a/projects/core/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java b/projects/core/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java index 75a4ce1f8..509ee63f8 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java +++ b/projects/core/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java @@ -149,7 +149,7 @@ public class FileSystem { return getMount(sanitizePath(path)).getAttributes(sanitizePath(path)); } - public synchronized String[] list(String path) throws FileSystemException { + public synchronized List list(String path) throws FileSystemException { path = sanitizePath(path); var mount = getMount(path); @@ -165,10 +165,8 @@ public class FileSystem { } // Return list - var array = new String[list.size()]; - list.toArray(array); - Arrays.sort(array); - return array; + list.sort(Comparator.naturalOrder()); + return list; } public synchronized boolean exists(String path) throws FileSystemException { diff --git a/projects/core/src/test/resources/test-rom/spec/apis/fs_spec.lua b/projects/core/src/test/resources/test-rom/spec/apis/fs_spec.lua index e6af8d55c..084511173 100644 --- a/projects/core/src/test/resources/test-rom/spec/apis/fs_spec.lua +++ b/projects/core/src/test/resources/test-rom/spec/apis/fs_spec.lua @@ -158,6 +158,14 @@ describe("The fs library", function() expect(fs.combine("", "a")):eq("a") expect(fs.combine("a", "", "b", "c")):eq("a/b/c") end) + + it("preserves pattern characters", function() + expect(fs.combine("foo*?")):eq("foo*?") + end) + + it("sanitises paths", function() + expect(fs.combine("foo\":<>|")):eq("foo") + end) end) describe("fs.getName", function() @@ -175,6 +183,14 @@ describe("The fs library", function() it("returns '..' for parent directories", function() expect(fs.getName("..")):eq("..") end) + + it("preserves pattern characters", function() + expect(fs.getName("foo*?")):eq("foo*?") + end) + + it("sanitises paths", function() + expect(fs.getName("foo\":<>|")):eq("foo") + end) end) describe("fs.getDir", function() @@ -193,6 +209,14 @@ describe("The fs library", function() expect(fs.getDir("..")):eq("../..") expect(fs.getDir("../..")):eq("../../..") end) + + it("preserves pattern characters", function() + expect(fs.getDir("foo*?/x")):eq("foo*?") + end) + + it("sanitises paths", function() + expect(fs.getDir("foo\":<>|/x")):eq("foo") + end) end) describe("fs.getSize", function()