This repository has been archived by the owner on Jul 18, 2020. It is now read-only.
/
fs.lua
281 lines (245 loc) · 9.05 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
-- fake lil filesystem
orb.fs = {
-- This gives us a raw filesystem that's just a table with permissions data
empty = function()
return {_user = "root", _group = "all", proc = {
_user = "root", _group = "all"
}}
end,
mkdir = function(f, path, env)
local dir, base = orb.fs.dirname(orb.fs.normalize(path, env and env.CWD))
local parent = f[dir]
if(not parent) then orb.fs.mkdir(f, dir, env) end
if(parent[base]) then return parent[base] end
parent[base] = {
_user = parent._user,
_group = parent._group,
}
return parent[base]
end,
-- Actually returns both the dirname and the basename.
-- for instance, "/path/to/file" returns "/path/to" and "file"
dirname = function(path)
local t = orb.utils.split(path, "/")
local basename = t[#t]
table.remove(t, #t)
return "/" .. table.concat(t, "/"), basename
end,
-- read/write/append here are wrappers that help you work with
-- function files, which is how pipes and other special devices are
-- implemented. When dealing with regular files, you can just grab
-- them straight out of the filesystem as strings to read or drop strings
-- into the directory to write.
read = function(f, path)
local contents = f[path]
if(type(contents) == "string") then
return contents
elseif(type(contents) == "function") then
return contents()
else
error("Tried to read " .. type(contents) .. " at " .. path)
end
end,
write = function(f, path, content)
local dir, base = orb.fs.dirname(path)
local target = f[path]
if(not target) then
f[dir][base] = content
elseif(type(target) == "string") then
f[dir][base] = f[dir][base] .. content
elseif(type(target) == "function") then
target(content)
else
error("Tried to append to " .. type(target) .. " at " .. path)
end
end,
add_user = function(f, user)
local home = "/home/" .. user
orb.fs.mkdir(f, home)
orb.fs.add_to_group(f, user, user)
orb.fs.add_to_group(f, user, "all")
f[home]._user = user
f[home]._group = user
orb.fs.mkdir(f, "/proc/" .. user)
f.proc[user]._user = user
f.proc[user]._group = user
end,
add_to_group = function(f, user, group)
assert(type(user) == "string" and type(group) == "string")
local group_dir = f[orb.fs.normalize("/etc/groups/" .. group)]
if(not group_dir) then
group_dir = orb.fs.mkdir(f, "/etc/groups/" .. group)
group_dir._user = user
end
group_dir._group = group
group_dir[user] = user
end,
-- This is for copying stuff from the host OS into the virtualized OS.
copy_to_fs = function(f, fs_path, real_path)
local dir, base = orb.fs.dirname(fs_path)
local path = orb.mod_dir .. "/resources/" .. real_path
local file = io.open(path, "r")
dir = dir:gsub("^/", "")
f[dir][base] = file:read("*all")
file:close()
end,
load_bin = function(f)
for real_path, fs_path in pairs({ls = "/bin/ls",
mkdir = "/bin/mkdir",
cat = "/bin/cat",
env = "/bin/env",
mv = "/bin/mv",
cp = "/bin/cp",
rm = "/bin/rm",
echo = "/bin/echo",
smash = "/bin/smash",
chmod = "/bin/chmod",
chgrp = "/bin/chgrp",
chown = "/bin/chown",
ps = "/bin/ps",
grep = "/bin/grep",
reload = "/bin/reload",
mkfifo = "/bin/mkfifo",
digi = "/bin/digi",
}) do
orb.fs.copy_to_fs(f, fs_path, real_path)
end
end,
-- Load up an empty filesystem.
seed = function(f, users)
for _,d in pairs({"/etc", "/home", "/tmp", "/bin", "/digi"}) do
orb.fs.mkdir(f, d)
f[d]._group = "all"
end
orb.fs.mkdir(f, "/etc/groups")
f["/tmp"]._group_write = "true"
f["/digi"]._group_write = "true"
for _,user in pairs(users) do
orb.fs.add_user(f, user)
end
orb.fs.load_bin(f)
return f
end,
normalize = function(path, cwd)
if(path == ".") then return cwd end
if(not path:match("^/")) then path = cwd .. "/" .. path end
local final = {}
for _,segment in pairs(orb.utils.split(path, "/")) do
if(segment == "..") then
table.remove(final, #final)
else
final[#final + 1] = segment
end
end
return "/" .. table.concat(final, "/")
end,
dir_meta = function(dir)
return dir._user, dir._group, dir._group_write
end,
readable = function(f, dir, user)
if(user == "root") then return true end
local owner, group = orb.fs.dir_meta(dir)
return owner == user or orb.shell.in_group(f, user, group)
end,
writeable = function(f, dir, user)
if(user == "root") then return true end
local owner, group, group_write = orb.fs.dir_meta(dir)
return owner == user or
(group_write and orb.shell.in_group(f, user, group))
end,
reloaders = (orb.fs and orb.fs.reloaders) or {},
-- Reload all of orb's own code, and reset the /bin directory.
reloader = function(f)
return function()
dofile(orb.mod_dir .. "/init.lua")
orb.fs.load_bin(f)
end
end,
strip_special = function(f)
for k,v in orb.utils.mtpairs(f) do
if(type(v) == "string") then
elseif(type(v) == "table") then
orb.fs.strip_special(v)
else
f[k] = nil
end
end
end,
-- Proxying a raw filesystem has two purposes: one is to enforce filesystem
-- permissions rules (this is done using a metatable) and one is to allow
-- access using full filenames. For instance, these are equivalent:
--
-- f.home.technomancy.bin["myls"]
-- f["/home/technomancy/bin/myls"]
--
-- Raw filesystems require the first style, but the latter works with
-- proxied filesystems.
--
-- Be aware that f["/home"] will return another proxied subfilesystem
-- that looks like a filesystem but is actually just sliced off at
-- a subdirectory. This is a bit of a problem since calculating permissions
-- requires access to the "/etc/groups" directory, which is why this
-- function takes a raw_root argument as well.
proxy = function(raw, user, raw_root)
local descend = function(f, path, user)
local target = f
for _,d in pairs(orb.utils.split(path, "/")) do
if(d == "") then break end
-- readable here needs a fully-rooted fs to read groups
assert(type(target) == "string" or
orb.fs.readable(raw_root, target, user),
("Not readable: " .. path .. " d: " .. d))
target = target[d]
end
return target
end
local unreadable = function(_k, v)
return {_user = v._user, _group = v._group}
end
local f = {}
local mt = {
__index = function(_f, path)
local target = descend(raw, path, user)
if(type(target) == "table") then
return orb.fs.proxy(target, user, raw_root)
else
return target
end
end,
__newindex = function(f, path, content)
local segments = orb.utils.split(path, "/")
local base = table.remove(segments, #segments)
local target = descend(raw, "/"..table.concat(segments,"/"), user)
assert(orb.fs.writeable(raw_root, target, user),
"Not writeable: " .. path)
target[base] = content
end,
-- Unfortunately Lua 5.1 has no way to specify an iterator from the
-- metatable, so this only works with orb.utils.mtpairs. =(
__iterator = function(_f)
assert(orb.fs.readable(raw_root, raw, user), "Not readable")
local f = {}
for k,v in pairs(raw) do
if(type(v) == "string") then
f[k] = v
elseif(type(v) == "table" and
orb.fs.readable(raw_root, v, user)) then
f[k] = orb.fs.proxy(v, user, raw_root)
elseif(type(v) == "table") then
f[k] = unreadable(k, v)
else
print("Unknown file type: " .. k)
print(v)
end
end
return next,f,nil
end,
}
setmetatable(f, mt)
-- Only need this for fs roots.
if(raw == raw_root) then
orb.fs.reloaders[f] = orb.fs.reloader(raw_root)
end
return f
end,
}