-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno.lua
644 lines (619 loc) · 14.5 KB
/
no.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
local co = require "coroutine"
local unpack, pack = string.unpack, string.pack
local min, abs = math.min, math.abs
local dump = (function()
local ok, mod = pcall(require, "dump")
return ok and mod or print
end)()
local no = {}
no.Tversion, no.Rversion = 100, 101
no.Tauth, no.Rauth = 102, 103
no.Tattach, no.Rattach = 104, 105
no.Terror, no.Rerror = 106, 107
no.Tflush, no.Rflush = 108, 109
no.Twalk, no.Rwalk = 110, 111
no.Topen, no.Ropen = 112, 113
no.Tcreate, no.Rcreate = 114, 115
no.Tread, no.Rread = 116, 117
no.Twrite, no.Rwrite = 118, 119
no.Tclunk, no.Rclunk = 120, 121
no.Tremove, no.Rremove = 122, 123
no.Tstat, no.Rstat = 124, 125
no.Twstat, no.Rwstat = 126, 127
no.NOTAG, no.NOFID = 0xFFFF, 0xFFFFFFFF
-- Bits of Qid.type
no.QTDIR = 0x80
no.QTAPPEND = 0x40
no.QTEXCL = 0x20
no.QTMOUNT = 0x10
no.QTAUTH = 0x08
no.QTTMP = 0x04
no.QTFILE = 0x00
-- Bits of Dir.mode
no.DMDIR = 0x80000000
no.DMAPPEND = 0x40000000
no.DMEXCL = 0x20000000
no.DMMOUNT = 0x10000000
no.DMAUTH = 0x08000000
no.DMTMP = 0x04000000
no.DMREAD = 0x4
no.DMWRITE = 0x2
no.DMEXEC = 0x1
-- Open mode
no.OREAD = 0x0
no.OWRITE = 0x1
no.ORDWR = 0x2
no.OEXEC = 0x3
no.OTRUNC = 0x10
no.OCEXEC = 0x20
no.ORCLOSE = 0x40
no.EXCL = 0x1000
no.Ebotch = "9P protocol botch"
local tohuman = {
[no.Tversion] = "Tversion", [no.Rversion] = "Rversion",
[no.Tauth] = "Tauth", [no.Rauth] = "Rauth",
[no.Terror] = "Terror", [no.Rerror] = "Rerror",
[no.Tflush] = "Tflush", [no.Rflush] = "Rflush",
[no.Tattach] = "Tattach", [no.Rattach] = "Rattach",
[no.Twalk] = "Twalk", [no.Rwalk] = "Rwalk",
[no.Topen] = "Topen", [no.Ropen] = "Ropen",
[no.Tcreate] = "Tcreate", [no.Rcreate] = "Rcreate",
[no.Tread] = "Tread", [no.Rread] = "Rread",
[no.Twrite] = "Twrite", [no.Rwrite] = "Rwrite",
[no.Tclunk] = "Tclunk", [no.Rclunk] = "Rclunk",
[no.Tremove] = "Tremove", [no.Rremove] = "Rremove",
[no.Tstat] = "Tstat", [no.Rstat] = "Rstat",
[no.Twstat] = "Twstat", [no.Rwstat] = "Rwstat",
}
local fmt = string.format
local qidfmt = function(qid)
return fmt("(path=%d type=%d vers=%d)", qid.path, qid.type, qid.vers)
end
local tracefn = {
[no.Tattach] = function(T)
return fmt("afid=%d uname=%s aname=%s", T.afid, T.uname, T.aname)
end;
[no.Rattach] = function(R)
return fmt("qid=%s", qidfmt(R.qid))
end;
[no.Twalk] = function(T)
return fmt("nwname=%d wname=(%s)", T.nwname, table.concat(T.wname, " "))
end;
[no.Rwalk] = function(R)
local buf = fmt("nwqid=%d ", #R.wqid)
for i = 1, #R.wqid do
buf = buf .. fmt("%s ", qidfmt(R.wqid[i]))
end
return buf
end;
[no.Rerror] = function(R)
return R.ename
end;
}
local function trace(TR)
local buf = fmt("%s %s\n",
tohuman[TR.type],
tracefn[TR.type] and tracefn[TR.type](TR) or "")
io.stderr:write(buf)
end
local decode = {
[no.Tversion] = function(T, buf, p)
T.msize, T.version, p = unpack("< I4 s2", buf, p)
T.msize = abs(T.msize)
return p
end;
[no.Tauth] = function(T, buf, p)
T.afid, T.uname, T.aname, p = unpack("< I4 s2 s2", buf, p)
return p
end;
[no.Tflush] = function(T, buf, p)
T.oldtag, p = unpack("< I2", buf, p)
return p
end;
[no.Tattach] = function(T, buf, p)
T.fid, T.afid, T.uname, T.aname, p = unpack("< I4 I4 s2 s2", buf, p)
return p
end;
[no.Twalk] = function(T, buf, p)
T.fid, T.newfid, T.nwname, p = unpack("< I4 I4 I2", buf, p)
T.wname = {}
for _ = 1, T.nwname do
local name
name, p = unpack("< s2", buf, p)
table.insert(T.wname, name)
end
return p
end;
[no.Topen] = function(T, buf, p)
T.fid, T.mode, p = unpack("< I4 I1", buf, p)
return p
end;
[no.Tcreate] = function(T, buf, p)
T.fid, T.name, T.perm, T.mode, p = unpack("< I4 s2 I4 I1", buf, p)
return p
end;
[no.Tread] = function(T, buf, p)
T.fid, T.offset, T.count, p = unpack("< I4 I8 I4", buf, p)
T.count = abs(T.count)
return p
end;
[no.Twrite] = function(T, buf, p)
T.fid, T.offset, T.count, p = unpack("< I4 I8 I4", buf, p)
T.count = abs(T.count)
T.data, p = unpack("c" .. T.count, buf, p)
return p
end;
[no.Tclunk] = function(T, buf, p)
T.fid, p = unpack("< I4", buf, p)
return p
end;
[no.Tremove] = function(T, buf, p)
T.fid, p = unpack("< I4", buf, p)
return p
end;
[no.Tstat] = function(T, buf, p)
T.fid, p = unpack("< I4", buf, p)
return p
end;
[no.Twstat] = function(T, buf, p)
T.fid, T.stat, p = unpack("< I4 s2", buf, p)
return p
end
}
function no.decode(read)
local T = {}
local buf = read(4)
T.size = unpack("< I4", buf)
buf = read(T.size - 4); assert(#buf == T.size - 4)
local p
T.type, T.tag, p = unpack("< I1 I2", buf)
p = decode[T.type](T, buf, p)
if p < #buf then
error("error decoding message")
end
return T
end
local encodeqid = function(qid)
return pack("< I1 I4 I8", qid.type, qid.vers, qid.path)
end
local encodestat = function(stat)
local buf = pack("< I2 I4 c13 I4 I4 I4 I8 s2 s2 s2 s2",
0, 0, encodeqid(stat.qid),
stat.mode, stat.atime, stat.mtime, stat.length,
stat.name, stat.uid, stat.gid, stat.muid
)
return pack("< I2", #buf) .. buf
end
local encode = {
[no.Rversion] = function(R)
return pack("< I4 s2", R.msize, R.version) end;
[no.Rerror] = function(R)
return pack("< s2", R.ename) end;
[no.Rflush] = function(R)
return pack("< I2", R.oldtag) end;
[no.Rauth] = function(R)
return encodeqid(R.qid) end;
[no.Rattach] = function(R)
return encodeqid(R.qid) end;
[no.Rwalk] = function(R)
local t = {}
t[1] = pack("< I2", #R.wqid)
for i = 1, #R.wqid do
table.insert(t, encodeqid(R.wqid[i]))
end
return table.concat(t)
end;
[no.Ropen] = function(R)
return pack("< c13 I4", encodeqid(R.qid), R.iounit) end;
[no.Rcreate] = function(R)
return pack("< c13 I4", encodeqid(R.qid), R.iounit) end;
[no.Rread] = function(R)
return pack("< I4", R.count or #R.data) .. R.data end;
[no.Rwrite] = function(R)
return pack("< I4", R.count) end;
[no.Rclunk] = function() end;
[no.Rremove] = function() end;
[no.Rwstat] = function() end;
[no.Rstat] = function(R)
local buf = encodestat(R.stat, R.stat.qid)
return pack("< I2", #buf) .. buf
end;
}
function no.encode(R)
if not encode[R.type] then
return nil, "unknown request type"
end
local buf = encode[R.type](R) or ""
return pack("< I4 I1 I2", 4+1+2 + #buf, R.type, R.tag) .. buf
end
local function respond(T, S, R)
R = R or {}
R.tag = T.tag
if R.error then
R.type = no.Rerror
R.ename = R.error
else
R.type = T.type + 1
end
local buf, err = no.encode(R)
if not buf then
error(err)
end
S.writer(buf)
S.tags[T.tag] = nil
end
local function listdir(T, S)
local stats = S.list(T)
if #stats == 0 then
return ""
end
local data = {}
local left = S.iounit
for i = 1, #stats do
local buf = encodestat(stats[i])
left = left - #buf
if left < 0 then
co.yield(table.concat(data))
data = {}
left = S.iounit - #buf
end
table.insert(data, buf)
end
co.yield(table.concat(data))
return "done"
end
local dispatch = {
[no.Tversion] = function(T, S)
S.msize = min(T.msize, S.msize)
local R = {}
R.msize = S.msize
if T.version:match("^9P") then
R.version = "9P2000"
else
R.version = "unknown"
end
T:respond(R)
end;
[no.Tflush] = function(T, S)
local oldT = S.tags[T.oldtag]
if not oldT or not S.flush then
-- Already responded or no delayed responses
T:respond()
else
-- User must ensure the delayed response is canceled
S.flush(T, oldT)
end
end;
[no.Tauth] = function(T)
T:error("authentication not required")
end;
[no.Tattach] = function(T, S)
if S.fids[T.fid] then
T:error("duplicate fid")
return
end
T.state = {}
T.state.user = T.uname
T.state.tree = T.aname
T.respond = function(_, qid)
respond(T, S, {qid = qid})
T.state.qid = qid
S.fids[T.fid] = T.state
end
S.attach(T)
end;
[no.Twalk] = function(T, S)
if not S.fids[T.fid] then
T:error("unknown fid"); return
end
if T.state.mode then
T:error("cannot clone open fid"); return
end
if T.nwname > 0 and not (T.state.qid.type & no.QTDIR > 0) then
T:error("walk in a non-directory"); return
end
T.move = T.fid == T.newfid
T.clone = not T.move and T.nwname == 0
if T.move then
T.newstate = T.state
else
if S.fids[T.newfid] then
T:error("duplicate fid"); return
end
T.newstate = {
user = T.state.user,
tree = T.state.tree,
qid = T.state.qid,
}
end
T.respond = function(_, wqid)
wqid = wqid or {}
local newstate = T.newstate
if #wqid < T.nwname then
if #wqid == 0 and T.nwqid ~= 0 then
T:error("not found")
return
end
-- Otherwise it was a partial walk, which
-- does not draw error and does not set up
-- a new fid either.
else
-- Successful walk
if #wqid == 0 then
newstate.qid = T.state.qid
else
newstate.qid = wqid[#wqid]
end
if not T.move then
S.fids[T.newfid] = newstate
end
end
respond(T, S, {wqid = wqid})
end
S.walk(T)
end;
[no.Tclunk] = function(T, S)
if S.clunk
then S.clunk(T)
else T:respond()
end
S.fids[T.fid] = nil
end;
[no.Tremove] = function(T, S)
if S.remove
then S.remove(T)
else T:error("remove not implemented")
end
S.fids[T.fid] = nil
end;
[no.Tstat] = function(T, S)
if not S.stat
then T:error("stat not implemented"); return
end
T.respond = function(_, statlike)
respond(T, S, {stat = statlike})
end
S.stat(T)
end;
[no.Twstat] = function(T, S)
if not S.wstat
then T:error("wstat not implemented"); return
end
S.wstat(T)
end;
[no.Topen] = function(T, S)
local st = T.state
local isdir = st.qid.type & no.QTDIR > 0
if st.mode then
T:error(no.Ebotch); return
elseif isdir then
if T.mode & no.OTRUNC > 0 then
T.mode = T.mode | no.OWRITE
end
if T.mode&3 ~= no.OREAD then
T:error("permission denied"); return
end
end
T.respond = function(_, qid, iounit)
respond(T, S, {
qid = qid and qid or T.state.qid,
iounit = iounit and iounit or S.iounit
})
T.state.mode = T.mode
end
if isdir and S.list then
T.state.listfn = co.create(listdir)
T:respond(st.qid)
elseif S.open then
S.open(T)
else
T:respond()
end
end;
[no.Tcreate] = function(T, S)
if not S.create
then T:error("create not implemented"); return
end
local isdir = T.state.qid.type & no.QTDIR > 0
if T.state.mode then
T:error(no.Ebotch); return
elseif not isdir then
T:error("create in a non-directory"); return
end
T.respond = function(_, qid, iounit)
respond(T, S, {
qid = qid,
iounit = iounit and iounit or S.iounit
})
T.state.mode = T.mode
end
S.create(T)
end;
[no.Tread] = function(T, S)
if not (S.read or S.list) then
T:error("read prohibited"); return
end
local m = T.state.mode and T.state.mode & 3
if not m
or not (m == no.OREAD or m == no.ORDWR or m == no.OEXEC)
then
T:error(no.Ebotch); return
end
T.count = min(T.count, S.iounit)
T.respond = function(_, data)
respond(T, S, {data = data and data or ""})
end
if T.state.listfn then
local ok, data = co.resume(T.state.listfn, T, S)
if not ok then
T:error(data)
T.state.listfn = nil
elseif data == "done" then
T:respond()
T.state.listfn = nil
else
T:respond(data)
end
else
S.read(T)
end
end;
[no.Twrite] = function(T, S)
if not S.write then
T:error("write prohibited"); return
end
local m = T.state.mode and T.state.mode & 3
if not m
or not (m == no.OWRITE or m == no.ORDWR)
or T.state.qid.type & no.QTDIR > 0
then
T:error(no.Ebotch); return
end
T.count = min(T.count, S.iounit)
T.respond = function(_, count)
respond(T, S, {count = count and count or 0})
end
S.write(T)
end;
}
function no.server(S)
assert(type(S) == "table")
assert(S.reader and S.writer, "server i/o functions missing")
assert(S.attach and S.walk, "missing required handlers")
S.fids = {}
S.tags = {}
S.msize = (S.msize and S.msize or 8192) + 24
S.iounit = S.msize - 24
return function()
local T = no.decode(S.reader)
-- Generic response. Most transactions define
-- custom versions to handle dependent response
-- parameters, to properly implement the protocol,
-- to maintain session state, and similar.
T.respond = function(self, R)
respond(self, S, R)
end
T.error = function(self, message)
respond(self, S, {error = message})
end
--
if T.tag == no.NOTAG then
if T.type ~= no.Tversion then
T:error(no.Ebotch); return false
end
else
if S.tags[T.tag] then
T:error("duplicate tag"); return false
end
S.tags[T.tag] = T
end
--
T.state = S.fids[T.fid]
if S.trace then trace(T, S) end
dispatch[T.type](T, S)
if S.trace and T.response then trace(T.response, S) end
end
end
-- Tree helper
local function qidgen()
local path = -1
return function()
path = path + 1
return path
end
end
local qidnext = qidgen()
function no.qid(type, path, vers)
return {
type = type and type or no.QTFILE,
path = path and type or qidnext(),
vers = vers and vers or 0
}
end
local function extend(t, ...)
assert(type(t) == "table")
for i = 1, select("#", ...) do
local et = select(i, ...); assert(type(et) == "table")
for k, v in pairs(et) do
if not t[k] then t[k] = v end
end
end
return t
end
local function lexwalk(dir, name)
local tree = dir.tree
local path = dir.path
local try
if name == ".." then
if path == "/" then return dir end
path = path:gsub("/[^/]+/?$", "")
name = ""
end
try = (path:match("/$") and path or (path .. "/")) .. name
if tree[try] then
return tree[try]
end
try = try .. "/"
if tree[try] then
return tree[try]
end
return false
end
local protofile = {
atime = os.time(),
mtime = os.time(),
uid = "glenda",
gid = "glenda",
muid = "glenda",
length = 0,
is = function(self, what)
if what == "file" then
return self.qid.type & no.QTDIR == 0
elseif what == "dir" then
return self.qid.type & no.QTDIR > 0
end
end;
walk = function(self, wname)
local f = self
local wqid = {}
for _, name in ipairs(wname) do
f = lexwalk(f, name)
if not f then
return nil, wqid
end
table.insert(wqid, f.qid)
end
return f, wqid
end;
kids = function(self)
local t = {}
for k, v in pairs(self.tree) do
if k:match('^' ..self.path.. '[^/]+/?$') then
table.insert(t, v)
end
end
return t
end;
}
local treemt = {
__newindex = function(t, k, v)
local f = extend(v, protofile)
f.tree = t
f.path = k
if k:match("/$") then
f.name = f.name == "/" and "/" or k:gsub("^.*/([^/]+)/$", "%1")
f.qid = f.qid or no.qid(no.QTDIR)
f.mode = f.mode or tonumber("755", 8) | no.DMDIR
else
f.name = k:gsub("^.*/(.+)$", "%1")
f.qid = f.qid or no.qid()
f.mode = f.mode or tonumber("644", 8)
end
rawset(t, k, f)
end;
}
function no.tree()
local tree = {}
return setmetatable(tree, treemt)
end
return no