-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlienVFS
executable file
·409 lines (364 loc) · 12.6 KB
/
AlienVFS
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/lua
-- AlienVFS: a read-only virtual file system for programming language package managers
-- Written by Lucas C. Villa Real <lucasvr@gobolinux.org>
-- Released under the GNU GPL version 2
local inspect = require "inspect"
local inotify = require "inotify"
local flu = require "flu"
local posix = require "posix"
local stat = require "posix.sys.stat"
local lanes = require "lanes".configure({track_lanes=true})
local pip = require "gobo.alienvfs.pip"
local cpan = require "gobo.alienvfs.cpan"
local lua = require "gobo.alienvfs.luarocks"
local rubygems = require "gobo.alienvfs.rubygems"
local config = require "gobo.alienvfs.config"
-- Basic operations on paths
local Path = {}
Path.__index = Path
function Path:split(path, istart, iend)
local parts = {}
for name in string.gmatch(path, "[^/]+") do
table.insert(parts, name)
end
if istart and iend then
return {table.unpack(parts, istart, iend)}
end
return parts
end
function Path:dentry(rootfs, path)
return rootfs:lookup(path)
end
-- Basic operations on dentries
local Dentry = {}
Dentry.__index = Dentry
function Dentry:create(parent, name, module_dir)
if parent ~= nil and parent.children[name] ~= nil then
return parent.children[name]
else
local dentry = {}
setmetatable(dentry, Dentry)
dentry.module_dir = module_dir
dentry.name = name
dentry.parent = parent
dentry.lower_file = nil
dentry.virtual = false
dentry.children = {}
if parent ~= nil then
parent.children[name] = dentry
end
return dentry
end
end
function Dentry:remove()
if self.parent ~= nil then
self.parent.children[self.name] = nil
end
end
function Dentry:childCount()
local count = 0
for _ in pairs(self.children) do
count = count + 1
end
return count
end
function Dentry:realpath()
if self.lower_file ~= nil then
return self.lower_file
end
local dpath = {}
local dentry = self
while dentry ~= nil do
if dentry.module_dir == nil then break end
table.insert(dpath, 1, dentry.name)
dentry = dentry.parent
end
table.insert(dpath, 1, self.module_dir)
return table.concat(dpath, "/")
end
function Dentry:lookup(path)
local p = Path
local dentry = self
if type(path) == "string" then
for _,name in ipairs(p:split(path)) do
dentry = dentry.children[name]
if dentry == nil then break end
end
elseif type(path) == "table" then
for _,name in ipairs(path) do
dentry = dentry.children[name]
if dentry == nil then break end
end
end
return dentry
end
function Dentry:populate(filelist, module_dir)
local p = Path
for _,pathinfo in pairs(filelist) do
local last = nil
local virtualdir = pathinfo[2] ~= nil
for i,name in ipairs(p:split(pathinfo[1])) do
if i == 1 then
last = Dentry:create(self, name, module_dir)
last.virtual = virtualdir
else
local parent = self:lookup(p:split(pathinfo[1], 1, i-1))
last = Dentry:create(parent, name, module_dir)
last.virtual = virtualdir
end
end
if last ~= nil and pathinfo[2] ~= nil then
last.lower_file = pathinfo[2]
last.virtual = false
end
end
end
function Dentry:getAttributes()
local path = self:realpath()
if path:len() == 0 or self.virtual == true then
path = "/"
end
local attrs = stat.lstat(path)
if attrs == nil then
error(flu.errno.ENOENT)
end
return {
dev = attrs.st_dev,
ino = attrs.st_ino,
mode = self:makeMode(attrs),
nlink = attrs.st_nlink,
uid = attrs.st_uid,
gid = attrs.st_gid,
rdev = attrs.st_rdev,
access = attrs.st_atime,
modification = attrs.st_mtime,
change = attrs.st_ctime,
size = attrs.st_size,
blocks = attrs.st_blocks,
blksize = attrs.st_blksize,
}
end
function Dentry:makeMode(attrs)
local set = {}
if stat.S_ISBLK(attrs.st_mode) ~= 0 then set["blk"] = true end
if stat.S_ISCHR(attrs.st_mode) ~= 0 then set["chr"] = true end
if stat.S_ISFIFO(attrs.st_mode) ~= 0 then set["fifo"] = true end
if stat.S_ISREG(attrs.st_mode) ~= 0 then set["reg"] = true end
if stat.S_ISDIR(attrs.st_mode) ~= 0 then set["dir"] = true end
if stat.S_ISLNK(attrs.st_mode) ~= 0 then set["lnk"] = true end
if stat.S_ISSOCK(attrs.st_mode) ~= 0 then set["sock"] = true end
if stat.S_IRUSR & attrs.st_mode ~= 0 then set["rusr"] = true end
if stat.S_IWUSR & attrs.st_mode ~= 0 then set["wusr"] = true end
if stat.S_IXUSR & attrs.st_mode ~= 0 then set["xusr"] = true end
if stat.S_IRGRP & attrs.st_mode ~= 0 then set["rgrp"] = true end
if stat.S_IWGRP & attrs.st_mode ~= 0 then set["wgrp"] = true end
if stat.S_IXGRP & attrs.st_mode ~= 0 then set["xgrp"] = true end
if stat.S_IROTH & attrs.st_mode ~= 0 then set["roth"] = true end
if stat.S_IWOTH & attrs.st_mode ~= 0 then set["woth"] = true end
if stat.S_IXOTH & attrs.st_mode ~= 0 then set["xoth"] = true end
if stat.S_ISUID & attrs.st_mode ~= 0 then set["suid"] = true end
if stat.S_ISGID & attrs.st_mode ~= 0 then set["sgid"] = true end
return set
end
-- Filesystem notification mechanism
local inotify_monitor = lanes.gen("*", function(ch, prefix, dirs)
local lanes = require "lanes"
local posix = require "posix"
local inotify = require "inotify"
-- XXX catches changes to programs, but not to individual versions
local handle = inotify.init()
for _,path in pairs(dirs) do
local wd = handle:addwatch(path,
inotify.IN_CREATE,
inotify.IN_DELETE,
inotify.IN_DELETE_SELF,
inotify.IN_CLOSE_WRITE,
inotify.IN_MOVED_FROM,
inotify.IN_MOVE)
print("Added watch to " .. path)
end
while true do
local events = handle:read()
for _, ev in ipairs(events) do
ch:send("inotify_event", {ev, prefix, dirs[ev.wd]})
end
end
end)
local namespace_update = nil
local function bit_set(event, bit)
return event.mask & bit == bit
end
local function check_filesystem_watches(ch)
while true do
local key, value = ch:receive(0, "inotify_event", 1)
if not key then
break
end
local event_type, event_idx = {'CREATE', 'DELETE', nil}, 3
local event, prefix, watchdir = value[1], value[2], value[3]
if bit_set(event, inotify.IN_CREATE) or
bit_set(event, inotify.IN_MOVED_TO) then
print(" * " .. prefix .. ": CREATED/MOVED_IN/MODIFIED " .. event.name)
event_idx = 1
elseif bit_set(event, inotify.IN_DELETE) or
bit_set(event, inotify.IN_DELETE_SELF) or
bit_set(event, inotify.IN_MOVED_FROM) then
if event.name == nil then
-- event.name will be nil on IN_DELETE_SELF
event.name = watchdir
end
print(" * " .. prefix .. ": DELETED/MOVED_OUT " .. event.name)
event_idx = 2
end
if event_type[event_idx] ~= nil then
if prefix == "PIP:" or prefix == "PIP3:" then
namespace_update(pip, prefix, watchdir, event.name, event_type[event_idx])
elseif prefix == "LuaRocks:" then
namespace_update(lua, prefix, watchdir, event.name, event_type[event_idx])
elseif prefix == "CPAN:" then
namespace_update(cpan, prefix, watchdir, event.name, event_type[event_idx])
elseif prefix == "RubyGems:" then
namespace_update(rubygems, prefix, watchdir, event.name, event_type[event_idx])
end
end
end
end
-- Virtual file system methods
local vfs = {}
vfs.ch = lanes.linda()
vfs.descriptors = {}
vfs.rootfs = Dentry:create(nil, "/")
function vfs.getattr(path, stat)
if path == "/" then
return vfs.rootfs:getAttributes()
else
check_filesystem_watches(vfs.ch)
local dentry = Path:dentry(vfs.rootfs, path)
if dentry ~= nil then
return dentry:getAttributes()
end
end
error(flu.errno.ENOENT)
end
function vfs.readdir(path, filler, fi)
check_filesystem_watches(vfs.ch)
local dentry = Path:dentry(vfs.rootfs, path)
if dentry ~= nil then
filler(".")
filler("..")
for name,dentry in pairs(dentry.children) do
filler(name)
end
end
end
function vfs.open(path, fi)
-- operates on the real file system
check_filesystem_watches(vfs.ch)
local dentry = Path:dentry(vfs.rootfs, path)
local flags = posix.O_RDONLY
if fi.flags.wronly then
flags = posix.O_WRONLY
elseif fi.flags.rdwr then
flags = posix.O_RDWR
end
fi.fh = posix.open(dentry:realpath(), flags)
if fi.fh == nil then
error(flu.errno.ENOENT)
end
vfs.descriptors[math.floor(fi.fh)] = dentry
end
function vfs.read(path, size, offset, fi)
posix.lseek(math.floor(fi.fh), math.floor(offset), posix.SEEK_SET)
return posix.read(math.floor(fi.fh), size)
end
function vfs.release(path, fi)
vfs.descriptors[fi.fh] = nil
posix.close(math.floor(fi.fh))
fi.fh = nil
end
-- Main operation
local function namespace_add_program(prefix, program)
local pname = Dentry:create(vfs.rootfs, prefix .. program.name)
local pversion = Dentry:create(pname, program.version)
if program.filelist ~= nil then
pversion:populate(program.filelist, program.module_dir)
end
end
local function namespace_create(backend, prefix, add_watches, module_dirs)
local watchlist = {}
if module_dirs == nil then
module_dirs = backend:moduleDirs()
end
for _,module_dir in pairs(module_dirs) do
local attrs = stat.lstat(module_dir)
if attrs ~= nil then
table.insert(watchlist, module_dir)
local modules = backend:parse(module_dir)
if modules ~= nil then
for _,program in pairs(modules) do
if program.name ~= nil then
namespace_add_program(prefix, program)
end
end
end
end
end
if add_watches then
inotify_monitor(vfs.ch, prefix, watchlist)
end
end
--
-- Update namespace upon a DELETE or CREATE event
-- @backend: pip, lua, cpan, rubygems
-- @prefix: "PIP:", "PIP3:", "LuaRocks:", "CPAN:", "RubyGems:"
-- @watchdir: /System/Aliens/PIP, /System/Aliens/LuaRocks, ...
-- @path: foo, bar, anotherdirname, ...
-- @event_type: 'CREATE', 'DELETE'
--
namespace_update = function(backend, prefix, watchdir, path, event_type)
if event_type == "DELETE" then
local vfsname = backend:map(path, event_type)
if vfsname == nil then
return
end
local fullname = "/" .. prefix .. vfsname
local dentry = Path:dentry(vfs.rootfs, fullname)
if dentry ~= nil then
local parent = dentry.parent
dentry:remove()
if parent ~= nil and parent:childCount() == 0 then
parent:remove()
end
end
elseif event_type == "CREATE" then
if not backend:valid(path) then
return
end
local vfsname = backend:map(path, event_type)
if vfsname ~= nil then
local fullname = "/" .. prefix .. vfsname
local dentry = Path:dentry(vfs.rootfs, fullname)
if dentry == nil then
local program = backend:populate(watchdir, path)
if program ~= nil then
namespace_add_program(prefix, program)
end
end
end
end
end
-- Namespace creation
namespace_create(pip, "PIP:", true, config.pip_directories())
namespace_create(pip, "PIP3:", true, config.pip3_directories())
namespace_create(lua, "LuaRocks:", true, config.luarocks_directories())
namespace_create(rubygems, "RubyGems:", false, nil)
namespace_create(cpan, "CPAN:", false, config.cpan_directories())
-- Inotify watches to catch removal of CPAN modules and installation of new ones
local packlist = {}
for key in pairs(cpan.packlists) do table.insert(packlist, key) end
inotify_monitor(vfs.ch, "CPAN:", packlist)
inotify_monitor(vfs.ch, "CPAN:", config:cpan_inotify_directories())
-- Run FLU main loop
local argv = {"AlienVFS", "-oallow_other", "-oauto_unmount", select(1, ...)}
flu.main(argv, vfs)
-- vim: ts=4 sts=4 sw=4 expandtab