mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-31 05:33:00 +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:
		| @@ -223,16 +223,21 @@ end | |||||||
| --- Returns true if a path is mounted to the parent filesystem. | --- Returns true if a path is mounted to the parent filesystem. | ||||||
| -- | -- | ||||||
| -- The root filesystem "/" is considered a mount, along with disk folders and | -- 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 | -- the rom folder. | ||||||
| -- make other mount types by correctly assigning their return value for getDrive. |  | ||||||
| -- | -- | ||||||
| -- @tparam string path The path to check. | -- @tparam string path The path to check. | ||||||
| -- @treturn boolean If the path is mounted, rather than a normal file/folder. | -- @treturn boolean If the path is mounted, rather than a normal file/folder. | ||||||
| -- @throws If the path does not exist. | -- @throws If the path does not exist. | ||||||
| -- @see getDrive | -- @see getDrive | ||||||
| -- @since 1.87.0 | -- @since 1.87.0 | ||||||
| function fs.isDriveRoot(sPath) | function fs.isDriveRoot(path) | ||||||
|     expect(1, sPath, "string") |     expect(1, path, "string") | ||||||
|  |  | ||||||
|  |     local parent = fs.getDir(path) | ||||||
|  |  | ||||||
|     -- Force the root directory to be a mount. |     -- 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 | end | ||||||
|   | |||||||
| @@ -104,7 +104,7 @@ while running do | |||||||
|             end |             end | ||||||
|         else |         else | ||||||
|             printError(results[2]) |             printError(results[2]) | ||||||
|             require "cc.internal.exception".report(results[2], results[3], chunk_map) |             exception.report(results[2], results[3], chunk_map) | ||||||
|         end |         end | ||||||
|     else |     else | ||||||
|         local parser = require "cc.internal.syntax" |         local parser = require "cc.internal.syntax" | ||||||
|   | |||||||
| @@ -83,6 +83,10 @@ describe("The fs library", function() | |||||||
|             expect(fs.isDriveRoot("/rom/startup.lua")):eq(false) |             expect(fs.isDriveRoot("/rom/startup.lua")):eq(false) | ||||||
|             expect(fs.isDriveRoot("/rom/programs/delete.lua")):eq(false) |             expect(fs.isDriveRoot("/rom/programs/delete.lua")):eq(false) | ||||||
|         end) |         end) | ||||||
|  |  | ||||||
|  |         it("returns false for missing files", function() | ||||||
|  |             expect(fs.isDriveRoot("does_not_exist")):eq(false) | ||||||
|  |         end) | ||||||
|     end) |     end) | ||||||
|  |  | ||||||
|     describe("fs.list", function() |     describe("fs.list", function() | ||||||
| @@ -555,6 +559,22 @@ describe("The fs library", function() | |||||||
|         end) |         end) | ||||||
|     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() |     describe("fs.attributes", function() | ||||||
|         it("errors on non-existent files", function() |         it("errors on non-existent files", function() | ||||||
|             expect.error(fs.attributes, "xuxu_nao_existe"):eq("/xuxu_nao_existe: No such file") |             expect.error(fs.attributes, "xuxu_nao_existe"):eq("/xuxu_nao_existe: No such file") | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jonathan Coates
					Jonathan Coates