-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.lua
More file actions
187 lines (164 loc) · 4.98 KB
/
fs.lua
File metadata and controls
187 lines (164 loc) · 4.98 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
local expect = require "cc.expect"
local syscall = require "_syscall"
local fs = {}
function fs.list(path)
expect(1, path, "string")
return syscall.list(path)
end
function fs.exists(path)
expect(1, path, "string")
return syscall.stat(path) ~= nil
end
function fs.isDir(path)
expect(1, path, "string")
local stat = syscall.stat(path)
return stat ~= nil and stat.type == "directory"
end
function fs.isReadOnly(path)
expect(1, path, "string")
local stat = syscall.stat(path)
while stat == nil do
path = fs.getDir(path)
stat = syscall.stat(path)
end
local user = syscall.getuser()
if stat.permissions[user] then return not stat.permissions[user].write
else return not stat.worldPermissions.write end
end
function fs.getName(path)
expect(1, path, "string")
return syscall.combine(path):match "[^/]+$"
end
function fs.getDrive(path)
expect(1, path, "string")
local stat = syscall.stat(path)
return stat and (stat.mountpoint == "/" and "hdd" or stat.mountpoint:gsub("^/", ""))
end
function fs.getSize(path)
expect(1, path, "string")
local stat = syscall.stat(path)
return stat and stat.size
end
function fs.getFreeSpace(path)
expect(1, path, "string")
local stat = syscall.stat(path)
while stat == nil do
path = fs.getDir(path)
stat = syscall.stat(path)
end
return stat and stat.freeSpace
end
function fs.makeDir(path)
expect(1, path, "string")
return syscall.mkdir(path)
end
function fs.move(from, to)
expect(1, from, "string")
expect(2, to, "string")
local fromstat = assert(syscall.stat(from), "No such file or directory")
local path = to
local tostat = syscall.stat(path)
if tostat then error("File exists", 2) end
while tostat == nil do
path = fs.getDir(path)
tostat = syscall.stat(path)
end
if fromstat.mountpoint == tostat.mountpoint then
return syscall.rename(from, to)
end
fs.copy(from, to)
syscall.remove(from)
end
function fs.copy(from, to)
expect(1, from, "string")
expect(2, to, "string")
local stat = syscall.stat(from)
if stat.type == "directory" then
local list = syscall.list(from)
syscall.mkdir(to)
for _, v in ipairs(list) do fs.copy(syscall.combine(from, v), syscall.combine(to, v)) end
else
local fromfile, err = syscall.open(from, "rb")
if not fromfile then error(err, 2) end
local tofile, err = syscall.open(to, "wb")
if not tofile then fromfile.close() error(err, 2) end
repeat
local buf = fromfile.read(512)
if buf then tofile.write(buf) end
until not buf
tofile.close()
fromfile.close()
end
end
function fs.delete(path)
expect(1, path, "string")
return syscall.remove(path)
end
function fs.combine(...)
return syscall.combine(...)
end
function fs.open(path, mode)
expect(1, path, "string")
expect(2, mode, "string")
return syscall.open(path, mode)
end
local function aux_find(options, pathc, i)
if i > #pathc then return {} end
local pathc_regex = pathc[i]:gsub("[%^%$%(%)%%%.%+%-]", "%%%1"):gsub("%*", ".*"):gsub("%?", "."):gsub("%[!", "[^")
local nextOptions = {}
for _, opt in ipairs(options) do
local ok, possible_paths = pcall(syscall.list, opt)
if ok then
for _, path in ipairs(possible_paths) do
if path:match(pathc_regex) then
nextOptions[#nextOptions+1] = syscall.combine(opt, path)
end
end
end
end
if i + 1 > #pathc then return nextOptions end
return aux_find(nextOptions, pathc, i + 1)
end
function fs.find(wildcard)
expect(1, wildcard, "string")
local parts = {}
for p in wildcard:gmatch("[^/]+") do parts[#parts+1] = p end
local retval = aux_find({wildcard:sub(1, 1) == "/" and "/" or "."}, parts, 1)
table.sort(retval)
return retval
end
function fs.getDir(path)
expect(1, path, "string")
local p = syscall.combine(path):match "^(.*)/[^/]*$" or ""
if p == "" then
if path:sub(1, 1) == "/" then return "/"
else return "." end
else return p end
end
function fs.complete(partial, path, files, slashes)
-- TODO
end
function fs.attributes(path)
expect(1, path, "string")
local stat = syscall.stat(path)
if not stat then return nil end
stat.isDir = stat.type == "directory"
local user = syscall.getuser()
if stat.permissions[user] then stat.isReadOnly = not stat.permissions[user].write
else stat.isReadOnly = not stat.worldPermissions.write end
end
function fs.getCapacity(path)
expect(1, path, "string")
local stat = syscall.stat(path)
while stat == nil do
path = fs.getDir(path)
stat = syscall.stat(path)
end
return stat and stat.capacity
end
function fs.isDriveRoot(path)
expect(1, path, "string")
local stat = syscall.stat(path)
return stat ~= nil and syscall.combine(path) == stat.mountpoint
end
return fs