forked from streamich/memfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.ts
558 lines (450 loc) · 11.5 KB
/
node.ts
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
import process from './process';
import { bufferAllocUnsafe, bufferFrom } from './internal/buffer';
import { constants, S } from './constants';
import { Volume } from './volume';
import { EventEmitter } from 'events';
import Stats from './Stats';
const { S_IFMT, S_IFDIR, S_IFREG, S_IFLNK, O_APPEND } = constants;
const getuid = (): number => process.getuid?.() ?? 0;
const getgid = (): number => process.getgid?.() ?? 0;
export const SEP = '/';
/**
* Node in a file system (like i-node, v-node).
*/
export class Node extends EventEmitter {
// i-node number.
ino: number;
// User ID and group ID.
private _uid: number = getuid();
private _gid: number = getgid();
private _atime = new Date();
private _mtime = new Date();
private _ctime = new Date();
// data: string = '';
buf: Buffer;
private _perm = 0o666; // Permissions `chmod`, `fchmod`
mode = S_IFREG; // S_IFDIR, S_IFREG, etc.. (file by default?)
// Number of hard links pointing at this Node.
private _nlink = 1;
// Steps to another node, if this node is a symlink.
symlink: string[];
constructor(ino: number, perm: number = 0o666) {
super();
this._perm = perm;
this.mode |= perm;
this.ino = ino;
}
public set ctime(ctime: Date) {
this._ctime = ctime;
}
public get ctime(): Date {
return this._ctime;
}
public set uid(uid: number) {
this._uid = uid;
this.ctime = new Date();
}
public get uid(): number {
return this._uid;
}
public set gid(gid: number) {
this._gid = gid;
this.ctime = new Date();
}
public get gid(): number {
return this._gid;
}
public set atime(atime: Date) {
this._atime = atime;
this.ctime = new Date();
}
public get atime(): Date {
return this._atime;
}
public set mtime(mtime: Date) {
this._mtime = mtime;
this.ctime = new Date();
}
public get mtime(): Date {
return this._mtime;
}
public set perm(perm: number) {
this._perm = perm;
this.ctime = new Date();
}
public get perm(): number {
return this._perm;
}
public set nlink(nlink: number) {
this._nlink = nlink;
this.ctime = new Date();
}
public get nlink(): number {
return this._nlink;
}
getString(encoding = 'utf8'): string {
this.atime = new Date();
return this.getBuffer().toString(encoding);
}
setString(str: string) {
// this.setBuffer(bufferFrom(str, 'utf8'));
this.buf = bufferFrom(str, 'utf8');
this.touch();
}
getBuffer(): Buffer {
this.atime = new Date();
if (!this.buf) this.setBuffer(bufferAllocUnsafe(0));
return bufferFrom(this.buf); // Return a copy.
}
setBuffer(buf: Buffer) {
this.buf = bufferFrom(buf); // Creates a copy of data.
this.touch();
}
getSize(): number {
return this.buf ? this.buf.length : 0;
}
setModeProperty(property: number) {
this.mode = (this.mode & ~S_IFMT) | property;
}
setIsFile() {
this.setModeProperty(S_IFREG);
}
setIsDirectory() {
this.setModeProperty(S_IFDIR);
}
setIsSymlink() {
this.setModeProperty(S_IFLNK);
}
isFile() {
return (this.mode & S_IFMT) === S_IFREG;
}
isDirectory() {
return (this.mode & S_IFMT) === S_IFDIR;
}
isSymlink() {
// return !!this.symlink;
return (this.mode & S_IFMT) === S_IFLNK;
}
makeSymlink(steps: string[]) {
this.symlink = steps;
this.setIsSymlink();
}
write(buf: Buffer, off: number = 0, len: number = buf.length, pos: number = 0): number {
if (!this.buf) this.buf = bufferAllocUnsafe(0);
if (pos + len > this.buf.length) {
const newBuf = bufferAllocUnsafe(pos + len);
this.buf.copy(newBuf, 0, 0, this.buf.length);
this.buf = newBuf;
}
buf.copy(this.buf, pos, off, off + len);
this.touch();
return len;
}
// Returns the number of bytes read.
read(
buf: Buffer | ArrayBufferView | DataView,
off: number = 0,
len: number = buf.byteLength,
pos: number = 0,
): number {
this.atime = new Date();
if (!this.buf) this.buf = bufferAllocUnsafe(0);
let actualLen = len;
if (actualLen > buf.byteLength) {
actualLen = buf.byteLength;
}
if (actualLen + pos > this.buf.length) {
actualLen = this.buf.length - pos;
}
const buf2 = buf instanceof Buffer ? buf : Buffer.from(buf.buffer);
this.buf.copy(buf2, off, pos, pos + actualLen);
return actualLen;
}
truncate(len: number = 0) {
if (!len) this.buf = bufferAllocUnsafe(0);
else {
if (!this.buf) this.buf = bufferAllocUnsafe(0);
if (len <= this.buf.length) {
this.buf = this.buf.slice(0, len);
} else {
const buf = bufferAllocUnsafe(len);
this.buf.copy(buf);
buf.fill(0, this.buf.length);
this.buf = buf;
}
}
this.touch();
}
chmod(perm: number) {
this.perm = perm;
this.mode = (this.mode & ~0o777) | perm;
this.touch();
}
chown(uid: number, gid: number) {
this.uid = uid;
this.gid = gid;
this.touch();
}
touch() {
this.mtime = new Date();
this.emit('change', this);
}
canRead(uid: number = getuid(), gid: number = getgid()): boolean {
if (this.perm & S.IROTH) {
return true;
}
if (gid === this.gid) {
if (this.perm & S.IRGRP) {
return true;
}
}
if (uid === this.uid) {
if (this.perm & S.IRUSR) {
return true;
}
}
return false;
}
canWrite(uid: number = getuid(), gid: number = getgid()): boolean {
if (this.perm & S.IWOTH) {
return true;
}
if (gid === this.gid) {
if (this.perm & S.IWGRP) {
return true;
}
}
if (uid === this.uid) {
if (this.perm & S.IWUSR) {
return true;
}
}
return false;
}
del() {
this.emit('delete', this);
}
toJSON() {
return {
ino: this.ino,
uid: this.uid,
gid: this.gid,
atime: this.atime.getTime(),
mtime: this.mtime.getTime(),
ctime: this.ctime.getTime(),
perm: this.perm,
mode: this.mode,
nlink: this.nlink,
symlink: this.symlink,
data: this.getString(),
};
}
}
/**
* Represents a hard link that points to an i-node `node`.
*/
export class Link extends EventEmitter {
vol: Volume;
parent: Link;
children = new Map<string, Link | undefined>();
// Path to this node as Array: ['usr', 'bin', 'node'].
private _steps: string[] = [];
// "i-node" of this hard link.
node: Node;
// "i-node" number of the node.
ino: number = 0;
// Number of children.
length: number = 0;
name: string;
get steps() {
return this._steps;
}
// Recursively sync children steps, e.g. in case of dir rename
set steps(val) {
this._steps = val;
for (const [child, link] of this.children.entries()) {
if (child === '.' || child === '..') {
continue;
}
link?.syncSteps();
}
}
constructor(vol: Volume, parent: Link, name: string) {
super();
this.vol = vol;
this.parent = parent;
this.name = name;
this.syncSteps();
}
setNode(node: Node) {
this.node = node;
this.ino = node.ino;
}
getNode(): Node {
return this.node;
}
createChild(name: string, node: Node = this.vol.createNode()): Link {
const link = new Link(this.vol, this, name);
link.setNode(node);
if (node.isDirectory()) {
link.children.set('.', link);
link.getNode().nlink++;
}
this.setChild(name, link);
return link;
}
setChild(name: string, link: Link = new Link(this.vol, this, name)): Link {
this.children.set(name, link);
link.parent = this;
this.length++;
const node = link.getNode();
if (node.isDirectory()) {
link.children.set('..', this);
this.getNode().nlink++;
}
this.getNode().mtime = new Date();
this.emit('child:add', link, this);
return link;
}
deleteChild(link: Link) {
const node = link.getNode();
if (node.isDirectory()) {
link.children.delete('..');
this.getNode().nlink--;
}
this.children.delete(link.getName());
this.length--;
this.getNode().mtime = new Date();
this.emit('child:delete', link, this);
}
getChild(name: string): Link | undefined {
this.getNode().mtime = new Date();
return this.children.get(name);
}
getPath(): string {
return this.steps.join(SEP);
}
getName(): string {
return this.steps[this.steps.length - 1];
}
// del() {
// const parent = this.parent;
// if(parent) {
// parent.deleteChild(link);
// }
// this.parent = null;
// this.vol = null;
// }
/**
* Walk the tree path and return the `Link` at that location, if any.
* @param steps {string[]} Desired location.
* @param stop {number} Max steps to go into.
* @param i {number} Current step in the `steps` array.
*
* @return {Link|null}
*/
walk(steps: string[], stop: number = steps.length, i: number = 0): Link | null {
if (i >= steps.length) return this;
if (i >= stop) return this;
const step = steps[i];
const link = this.getChild(step);
if (!link) return null;
return link.walk(steps, stop, i + 1);
}
toJSON() {
return {
steps: this.steps,
ino: this.ino,
children: Array.from(this.children.keys()),
};
}
syncSteps() {
this.steps = this.parent ? this.parent.steps.concat([this.name]) : [this.name];
}
}
/**
* Represents an open file (file descriptor) that points to a `Link` (Hard-link) and a `Node`.
*/
export class File {
fd: number;
/**
* Hard link that this file opened.
* @type {any}
*/
link: Link;
/**
* Reference to a `Node`.
* @type {Node}
*/
node: Node;
/**
* A cursor/offset position in a file, where data will be written on write.
* User can "seek" this position.
*/
position: number;
// Flags used when opening the file.
flags: number;
/**
* Open a Link-Node pair. `node` is provided separately as that might be a different node
* rather the one `link` points to, because it might be a symlink.
* @param link
* @param node
* @param flags
* @param fd
*/
constructor(link: Link, node: Node, flags: number, fd: number) {
this.link = link;
this.node = node;
this.flags = flags;
this.fd = fd;
this.position = 0;
if (this.flags & O_APPEND) this.position = this.getSize();
}
getString(encoding = 'utf8'): string {
return this.node.getString();
}
setString(str: string) {
this.node.setString(str);
}
getBuffer(): Buffer {
return this.node.getBuffer();
}
setBuffer(buf: Buffer) {
this.node.setBuffer(buf);
}
getSize(): number {
return this.node.getSize();
}
truncate(len?: number) {
this.node.truncate(len);
}
seekTo(position: number) {
this.position = position;
}
stats(): Stats<number> {
return Stats.build(this.node) as Stats<number>;
}
write(buf: Buffer, offset: number = 0, length: number = buf.length, position?: number | null): number {
if (typeof position !== 'number') position = this.position;
const bytes = this.node.write(buf, offset, length, position);
this.position = position + bytes;
return bytes;
}
read(
buf: Buffer | ArrayBufferView | DataView,
offset: number = 0,
length: number = buf.byteLength,
position?: number,
): number {
if (typeof position !== 'number') position = this.position;
const bytes = this.node.read(buf, offset, length, position);
this.position = position + bytes;
return bytes;
}
chmod(perm: number) {
this.node.chmod(perm);
}
chown(uid: number, gid: number) {
this.node.chown(uid, gid);
}
}