This repository has been archived by the owner on Oct 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.lua
73 lines (72 loc) · 1.88 KB
/
fs.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
return function(guest)
local fs = {}
function sanitizePath(path)
path = string.normalizePath("ccsf/"..path)
assert(path == "ccsf" or string.find(path, "^ccsf/"), "Invalid Path")
if file.exists(path..".lua") then
path = path..".lua"
elseif file.exists(path..".ccsf.dat") then
path = path..".ccsf.dat"
end
return path
end
function fs.combine(basePath, localPath)
return string.normalizePath(basePath.."/"..localPath)
end
function fs.exists(path)
return file.exists(sanitizePath(path))
end
function fs.getName(path)
path = string.normalizePath(path)
return path == "" and "root" or string.match(path, "[^/]+$") or path
end
local function isDir(path)
local dirname = string.match(path, "[^/]+$") or path
path = string.normalizePath("ccsf/"..path.."/..")
if path == "ccsf" then
return true
end
if not string.find(path, "^ccsf/") then
return false
end
local files, dirs = file.find(path.."/"..dirname)
return dirs[1] == dirname
end
fs.isDir = isDir
function fs.list(path)
local files, dirs = file.find(sanitizePath(path).."/*")
for k, v in pairs(files) do
v = string.gsub(v, "%.lua$", "")
v = string.gsub(v, "%.ccsf%.dat$", "")
files[k] = v
end
for k, v in pairs(dirs) do
table.insert(files, v)
end
return files
end
function fs.open(path, mode)
if isDir(path) then
return
end
path = sanitizePath(path)
local binary
mode, binary = string.match(mode, "^([rwa])(b?)$")
binary = binary == "b"
assert(mode, "Unsupported mode")
local handle = file.open(path, mode)
if not handle then
return
end
-- I'm not going to use a metatable because ComputerCraft itself doesn't. I wish I was joking.
local output = {}
function output.close()
handle:close()
end
function output.readAll()
return string.gsub(assert(handle:read(handle:size()), "failed to read file"), "\n$", "")
end
return output
end
guest.fs = fs
end