forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinode.ts
More file actions
119 lines (105 loc) · 3.13 KB
/
inode.ts
File metadata and controls
119 lines (105 loc) · 3.13 KB
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
import {default as Stats, FileType} from '../core/node_fs_stats';
/**
* Generic inode definition that can easily be serialized.
*/
export default class Inode {
/**
* Converts the buffer into an Inode.
*/
public static fromBuffer(buffer: Buffer): Inode {
if (buffer === undefined) {
throw new Error("NO");
}
return new Inode(buffer.toString('ascii', 30),
buffer.readUInt32LE(0),
buffer.readUInt16LE(4),
buffer.readDoubleLE(6),
buffer.readDoubleLE(14),
buffer.readDoubleLE(22)
);
}
constructor(public id: string,
public size: number,
public mode: number,
public atime: number,
public mtime: number,
public ctime: number) { }
/**
* Handy function that converts the Inode to a Node Stats object.
*/
public toStats(): Stats {
return new Stats(
(this.mode & 0xF000) === FileType.DIRECTORY ? FileType.DIRECTORY : FileType.FILE,
this.size, this.mode, this.atime, this.mtime, this.ctime);
}
/**
* Get the size of this Inode, in bytes.
*/
public getSize(): number {
// ASSUMPTION: ID is ASCII (1 byte per char).
return 30 + this.id.length;
}
/**
* Writes the inode into the start of the buffer.
*/
public toBuffer(buff: Buffer = Buffer.alloc(this.getSize())): Buffer {
buff.writeUInt32LE(this.size, 0);
buff.writeUInt16LE(this.mode, 4);
buff.writeDoubleLE(this.atime, 6);
buff.writeDoubleLE(this.mtime, 14);
buff.writeDoubleLE(this.ctime, 22);
buff.write(this.id, 30, this.id.length, 'ascii');
return buff;
}
/**
* Updates the Inode using information from the stats object. Used by file
* systems at sync time, e.g.:
* - Program opens file and gets a File object.
* - Program mutates file. File object is responsible for maintaining
* metadata changes locally -- typically in a Stats object.
* - Program closes file. File object's metadata changes are synced with the
* file system.
* @return True if any changes have occurred.
*/
public update(stats: Stats): boolean {
let hasChanged = false;
if (this.size !== stats.size) {
this.size = stats.size;
hasChanged = true;
}
if (this.mode !== stats.mode) {
this.mode = stats.mode;
hasChanged = true;
}
const atimeMs = stats.atime.getTime();
if (this.atime !== atimeMs) {
this.atime = atimeMs;
hasChanged = true;
}
const mtimeMs = stats.mtime.getTime();
if (this.mtime !== mtimeMs) {
this.mtime = mtimeMs;
hasChanged = true;
}
const ctimeMs = stats.ctime.getTime();
if (this.ctime !== ctimeMs) {
this.ctime = ctimeMs;
hasChanged = true;
}
return hasChanged;
}
// XXX: Copied from Stats. Should reconcile these two into something more
// compact.
/**
* @return [Boolean] True if this item is a file.
*/
public isFile(): boolean {
return (this.mode & 0xF000) === FileType.FILE;
}
/**
* @return [Boolean] True if this item is a directory.
*/
public isDirectory(): boolean {
return (this.mode & 0xF000) === FileType.DIRECTORY;
}
}