1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-07 07:50:27 +00:00

Fix fs.isDriveRoot for missing files

fs.getDrive returns nil for missing files, rather than the mount of the
parent file. This is a bit confusing — other mount-related functions
(e.g. getFreeSpace) find the nearest mount — but I think it's too late
to change this. Instead, we check if the file exists first.
This commit is contained in:
Jonathan Coates 2024-12-31 11:12:58 +00:00
parent 7e2f490626
commit 2ba6d5815b
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
3 changed files with 31 additions and 6 deletions

View File

@ -223,16 +223,21 @@ end
--- Returns true if a path is mounted to the parent filesystem.
--
-- The root filesystem "/" is considered a mount, along with disk folders and
-- the rom folder. Other programs (such as network shares) can exstend this to
-- make other mount types by correctly assigning their return value for getDrive.
-- the rom folder.
--
-- @tparam string path The path to check.
-- @treturn boolean If the path is mounted, rather than a normal file/folder.
-- @throws If the path does not exist.
-- @see getDrive
-- @since 1.87.0
function fs.isDriveRoot(sPath)
expect(1, sPath, "string")
function fs.isDriveRoot(path)
expect(1, path, "string")
local parent = fs.getDir(path)
-- Force the root directory to be a mount.
return fs.getDir(sPath) == ".." or fs.getDrive(sPath) ~= fs.getDrive(fs.getDir(sPath))
if parent == ".." then return true end
local drive = fs.getDrive(path)
return drive ~= nil and drive ~= fs.getDrive(parent)
end

View File

@ -104,7 +104,7 @@ while running do
end
else
printError(results[2])
require "cc.internal.exception".report(results[2], results[3], chunk_map)
exception.report(results[2], results[3], chunk_map)
end
else
local parser = require "cc.internal.syntax"

View File

@ -83,6 +83,10 @@ describe("The fs library", function()
expect(fs.isDriveRoot("/rom/startup.lua")):eq(false)
expect(fs.isDriveRoot("/rom/programs/delete.lua")):eq(false)
end)
it("returns false for missing files", function()
expect(fs.isDriveRoot("does_not_exist")):eq(false)
end)
end)
describe("fs.list", function()
@ -555,6 +559,22 @@ describe("The fs library", function()
end)
end)
describe("fs.getDrive", function()
it("returns the drive for the mount roots", function()
expect(fs.getDrive("")):eq("hdd")
expect(fs.getDrive("rom")):eq("rom")
end)
it("returns the drive for subdirectories", function()
expect(fs.getDrive("rom/startup.lua")):eq("rom")
end)
it("returns nothing for missing files", function()
-- Peculiar, but we return no values, rather than nil!
expect(table.pack(fs.getDrive("no_such_file"))):same { n = 0 }
end)
end)
describe("fs.attributes", function()
it("errors on non-existent files", function()
expect.error(fs.attributes, "xuxu_nao_existe"):eq("/xuxu_nao_existe: No such file")