-
Notifications
You must be signed in to change notification settings - Fork 9
/
9p.js
451 lines (438 loc) · 11.2 KB
/
9p.js
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
"use strict";
const Enoent = 'no such file or directory';
const QTDIR = 0x80;
function Qid(path, vers, type) {
this.path = path;
this.vers = vers;
this.type = type;
}
function Stat() {
this.type = 0;
this.dev = 0;
this.qid = new Qid(0,0,0);
this.mode = 0o664;
this.atime = Date.now()/1000|0;
this.mtime = Date.now()/1000|0;
this.length = 0;
this.name = '';
this.uid = 'drawterm';
this.gid = 'drawterm';
this.muid = 'drawterm';
}
function Fid(n, old) {
this.n = n;
this.opened = false;
this.diroffset = 0;
this.direntries = [];
if(old)
this.file = old.file;
}
var allocpath = 0;
function File(name, type, parent) {
this.name = name;
this.qid = new Qid(allocpath++, 0, type);
this.sub = {};
this.parent = parent;
if(parent !== null)
parent.sub[name] = this;
}
File.prototype.walk = function(name){
if(name == '..')
return this.parent;
if(name in this.sub)
return this.sub[name];
return new Error(Enoent);
};
File.prototype.open = function(fid, mode){
}
File.prototype.read = function(fid, count, offset){
return new Error('no reads');
}
File.prototype.dirread = function() {
var l = [], x;
for(x in this.sub)
l.push(this.sub[x].stat());
return l;
}
File.prototype.write = function(fid, data, mode){
return new Error('no writes');
}
File.prototype.stat = function(fid){
let s = new Stat();
s.name = this.name;
s.qid = this.qid;
if((s.qid.type & QTDIR) != 0)
s.mode |= 0o111;
return s;
};
File.prototype.wstat = function(fid, stat){
return new Error('no wstat');
};
File.prototype.clunk = function(fid){
};
File.prototype.remove = function(fid){
return new Error('no remove');
};
const root = new File('/', QTDIR, null);
root.parent = root;
const dev = new File('dev', QTDIR, root);
function NineP(chan){
const Eduptag = new Error('duplicate tag');
const Enoauth = new Error('authentication not required');
const Enofid = new Error('no such fid');
const $Tversion = 100;
const $Rversion = 101;
const $Tauth = 102;
const $Rauth = 103;
const $Tattach = 104;
const $Rattach = 105;
const $Terror = 106; /* illegal */
const $Rerror = 107;
const $Tflush = 108;
const $Rflush = 109;
const $Twalk = 110;
const $Rwalk = 111;
const $Topen = 112;
const $Ropen = 113;
const $Tcreate = 114;
const $Rcreate = 115;
const $Tread = 116;
const $Rread = 117;
const $Twrite = 118;
const $Rwrite = 119;
const $Tclunk = 120;
const $Rclunk = 121;
const $Tremove = 122;
const $Rremove = 123;
const $Tstat = 124;
const $Rstat = 125;
const $Twstat = 126;
const $Rwstat = 127;
const NOTAG = 65535;
const NOFID = -1;
const minMsize = 256;
const maxMsize = 1048576;
var msize;
function botch() {
throw new Error("9P botch");
}
const string = VariableString(u16);
const qid = Struct(['type', u8, 'vers', u32, 'path', u64]);
const dirstat = Length(u16, Struct([
'type', u16,
'dev', u32,
'qid', qid,
'mode', u32,
'atime', u32,
'mtime', u32,
'length', u64,
'name', string,
'uid', string,
'gid', string,
'muid', string
]));
const Tversion = Struct(['msize', u32, 'version', string]);
const Rversion = Tversion;
const Tauth = Struct(['afid', u32, 'uname', u32, 'aname', u32]);
const Rauth = Struct(['aqid', qid]);
const Tattach = Struct(['fid', u32, 'afid', u32, 'uname', string,'aname', string]);
const Rattach = Struct(['qid', qid]);
const Rerror = Struct(['ename', string]);
const Twalk = Struct(['fid', u32, 'newfid', u32, 'wname', NArray(u16, string)]);
const Rwalk = Struct(['wqid', NArray(u16, qid)]);
const Tclunk = Struct(['fid', u32]);
const Rclunk = Struct([]);
const Topen = Struct(['fid', u32, 'mode', u8]);
const Ropen = Struct(['qid', qid, 'iounit', u32]);
const Tcreate = Struct(['fid', u32, 'name', string, 'perm', u32, 'mode', u8]);
const Rcreate = Struct(['qid', qid, 'iounit', u32]);
const Twrite = Struct(['fid', u32, 'offset', u64, 'data', OpaqueVector(u32)]);
const Rwrite = Struct(['count', u32]);
const Tread = Struct(['fid', u32, 'offset', u64, 'count', u32]);
const Rread = Struct(['data', OpaqueVector(u32)]);
const Tstat = Struct(['fid', u32]);
const Rstat = Struct(['stat', Length(u16, dirstat)]);
const Tremove = Tclunk;
const Rremove = Rclunk;
const Twstat = Struct(['fid', u32, 'stat', Length(u16, dirstat)]);
const Rwstat = Rclunk;
const Tflush = Struct(['oldtag', u16]);
const Rflush = Rclunk;
const Msg9P = Length(u32, Struct([
'type', u8,
'tag', u16,
null, Select(o => o.type, {
100: Tversion,
101: Rversion,
102: Tauth,
103: Rauth,
104: Tattach,
105: Rattach,
107: Rerror,
108: Tflush,
109: Rflush,
110: Twalk,
111: Rwalk,
112: Topen,
113: Ropen,
114: Tcreate,
115: Rcreate,
116: Tread,
117: Rread,
118: Twrite,
119: Rwrite,
120: Tclunk,
121: Rclunk,
122: Tremove,
123: Rremove,
124: Tstat,
125: Rstat,
126: Twstat,
127: Rwstat
})
]), 4);
function recvMsg() {
function msglen(b) {
if(b.length < 4) return -1;
return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24;
}
return chan.read(msglen)
.then(b => unpack(Msg9P, b));
}
function sendMsg(m) {
//console.log('-> ', m);
var b = pack(Msg9P, m);
return chan.write(b);
}
function version() {
return recvMsg().then(m => {
if(m.type != $Tversion || m.tag != NOTAG)
botch();
if(m.msize < minMsize)
botch();
msize = Math.min(m.msize, maxMsize);
if(m.version != '9P2000' && !m.version.startswith('9P2000.'))
botch();
return sendMsg({
type: $Rversion,
tag: NOTAG,
msize: msize,
version: '9P2000'});
});
}
var tags = {};
var fids = {};
function Req(msg) {
this.tag = msg.tag;
this.ifcall = msg;
this.ofcall = {
type: msg.type | 1,
tag: msg.tag
};
this.responded = false;
this.flush = [];
}
Req.prototype.respond = function(ename) {
if(this.responded)
throw new Error("9P: request answered twice");
this.responded = true;
delete tags[this.tag];
let out;
if(ename === undefined || ename === null)
out = sendMsg(this.ofcall);
else
out = sendMsg({type: $Rerror, tag: this.tag, ename: ename.message});
for(let i = 0; i < this.flush.length; i++)
out = out.then(this.flush[i]);
return out;
}
function newReq(m) {
if(m.tag in tags){
console.log('9P: duplicate tag ' + m.tag);
return null;
}
var req = new Req(m);
tags[m.tag] = req;
return req;
}
function attach(req) {
if(req.ifcall.afid != -1)
return req.respond(Enoauth);
if(req.ifcall.fid in fids)
return req.respond(Edupfid);
req.fid = new Fid(req.ifcall.fid);
fids[req.ifcall.fid] = req.fid;
req.fid.file = root;
req.ofcall.qid = req.fid.file.qid;
req.respond();
}
function walk(req) {
if(req.ifcall.fid !== req.ifcall.newfid && req.ifcall.newfid in fids)
return req.respond(Edupfid);
if(req.fid.opened)
return req.respond('cannot clone open fid');
if((req.fid.file.qid.type & QTDIR) == 0 && req.ifcall.wname.length > 0)
return req.respond('cannot walk file');
let nfid = new Fid(req.ifcall.newfid, req.fid);
var p = Promise.resolve(false);
req.ofcall.wqid = [];
for(var i = 0; i < req.ifcall.wname.length; i++){
let name = req.ifcall.wname[i];
p = p.then(abort => {
if(abort) return true;
return Promise.resolve(nfid.file.walk(name)).then(e => {
if(e instanceof Error){
if(i == 0)
return req.respond(e).then(true);
return req.respond().then(true);
}
nfid.file = e;
req.ofcall.wqid.push(nfid.file.qid);
return false;
});
});
}
p.then(abort => {if(!abort){
fids[req.ifcall.newfid] = nfid;
return req.respond();
}});
return p;
}
function clunk(req) {
return Promise.resolve(req.fid.file.clunk(req.fid)).then(() => {
delete fids[req.fid.n];
req.respond();
});
}
function remove(req) {
return Promise.resolve(req.fid.file.remove(req.fid)).then(e => {
delete fids[req.fid.n];
req.respond(e);
});
}
function open(req) {
return Promise.resolve(req.fid.file.open(req.fid, req.ifcall.mode))
.then(e => {
if(e !== null && e !== undefined)
return req.respond(e);
req.fid.opened = true;
req.fid.mode = req.ifcall.mode;
req.ofcall.qid = req.fid.file.qid;
req.ofcall.iounit = 0;
return req.respond();
});
}
function write(req) {
return Promise.resolve(req.fid.file.write(req.fid, req.ifcall.data, req.ifcall.offset))
.then(e => {
if(e instanceof Error)
return req.respond(e);
if(typeof(e) == 'number'){
req.ofcall.count = e;
return req.respond();
}
if(e === undefined){
req.ofcall.count = req.ifcall.data.length;
return req.respond();
}
throw new Error('write should return undefined, error or number');
});
}
function read(req) {
if((req.fid.file.qid.type & QTDIR) != 0){
if(req.ifcall.offset !== 0 && req.ifcall.offset !== req.fid.diroffset)
return req.respond('no seek on directory');
return Promise.resolve(req.ifcall.offset == 0 ? req.fid.file.dirread() : req.fid.direntries)
.then(d => {
let b = new VBuffer();
let i, lp;
for(i = 0; b.p < req.ifcall.count && i < d.length; i++){
lp = b.p;
dirstat.put(b, d[i]);
}
if(b.p > req.ifcall.count){
i--;
b.p = lp;
}
req.ofcall.data = b.data();
d.splice(0, i);
req.fid.direntries = d;
return req.respond();
});
}else{
return Promise.resolve(req.fid.file.read(req.fid, req.ifcall.count, req.ifcall.offset))
.then(e => {
if(e instanceof Error)
return req.respond(e);
if(typeof(e) == 'string'){
req.ofcall.data = new TextEncoder('utf-8').encode(e);
return req.respond();
}
if(e instanceof Uint8Array){
req.ofcall.data = e;
return req.respond();
}
throw new Error('read should return error, string or Uint8Array');
});
}
}
function stat(req) {
return Promise.resolve(req.fid.file.stat(req.fid))
.then(e => {
if(e instanceof Error)
return req.respond(e);
if(e instanceof Stat){
req.ofcall.stat = e;
return req.respond();
}
throw new Error('stat should return error or Stat');
});
}
function wstat(req) {
return Promise.resolve(req.fid.file.wstat(req.fid, req.ifcall.stat))
.then(e => {
if(e instanceof Error)
return req.respond(e);
if(e === undefined)
return req.respond();
throw new Error('stat should return error or undefined');
});
}
function flush(req) {
let oldreq = tags[req.ifcall.oldtag];
if(oldreq !== undefined && oldreq.responded == 0)
oldreq.flush.push(() => req.respond());
else
return req.respond();
}
function srv() {
return recvMsg().then(m => {
//console.log('<- ', m);
let req = newReq(m);
if(req === null)
return sendMsg({type: $Rerror, tag: m.tag, ename: Eduptag});
if('fid' in m && m.type != $Tattach){
req.fid = fids[m.fid];
if(req.fid === undefined)
return req.respond(Enofid);
}
switch(m.type){
case $Tattach: attach(req); break;
case $Tauth: req.respond(Enoauth); break;
case $Twalk: walk(req); break;
case $Tclunk: clunk(req); break;
case $Tremove: remove(req); break;
case $Topen: open(req); break;
case $Tcreate: return req.respond('no create');
case $Twrite: write(req); break;
case $Tread: read(req); break;
case $Tstat: stat(req); break;
case $Twstat: wstat(req); break;
case $Tflush: flush(req); break;
default: botch();
}
}).then(srv);
}
return version()
.then(srv);
}