1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | if (typeof b !== "function" && b !== null)
|
11 | throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
12 | extendStatics(d, b);
|
13 | function __() { this.constructor = d; }
|
14 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
15 | };
|
16 | })();
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.File = exports.Link = exports.Node = exports.SEP = void 0;
|
19 | var process_1 = require("./process");
|
20 | var buffer_1 = require("./internal/buffer");
|
21 | var constants_1 = require("./constants");
|
22 | var events_1 = require("events");
|
23 | var Stats_1 = require("./Stats");
|
24 | var S_IFMT = constants_1.constants.S_IFMT, S_IFDIR = constants_1.constants.S_IFDIR, S_IFREG = constants_1.constants.S_IFREG, S_IFLNK = constants_1.constants.S_IFLNK, O_APPEND = constants_1.constants.O_APPEND;
|
25 | exports.SEP = '/';
|
26 |
|
27 |
|
28 |
|
29 | var Node = (function (_super) {
|
30 | __extends(Node, _super);
|
31 | function Node(ino, perm) {
|
32 | if (perm === void 0) { perm = 438; }
|
33 | var _this = _super.call(this) || this;
|
34 |
|
35 | _this.uid = process_1.default.getuid();
|
36 | _this.gid = process_1.default.getgid();
|
37 | _this.atime = new Date();
|
38 | _this.mtime = new Date();
|
39 | _this.ctime = new Date();
|
40 | _this.perm = 438;
|
41 | _this.mode = S_IFREG;
|
42 |
|
43 | _this.nlink = 1;
|
44 | _this.perm = perm;
|
45 | _this.mode |= perm;
|
46 | _this.ino = ino;
|
47 | return _this;
|
48 | }
|
49 | Node.prototype.getString = function (encoding) {
|
50 | if (encoding === void 0) { encoding = 'utf8'; }
|
51 | return this.getBuffer().toString(encoding);
|
52 | };
|
53 | Node.prototype.setString = function (str) {
|
54 |
|
55 | this.buf = (0, buffer_1.bufferFrom)(str, 'utf8');
|
56 | this.touch();
|
57 | };
|
58 | Node.prototype.getBuffer = function () {
|
59 | if (!this.buf)
|
60 | this.setBuffer((0, buffer_1.bufferAllocUnsafe)(0));
|
61 | return (0, buffer_1.bufferFrom)(this.buf);
|
62 | };
|
63 | Node.prototype.setBuffer = function (buf) {
|
64 | this.buf = (0, buffer_1.bufferFrom)(buf);
|
65 | this.touch();
|
66 | };
|
67 | Node.prototype.getSize = function () {
|
68 | return this.buf ? this.buf.length : 0;
|
69 | };
|
70 | Node.prototype.setModeProperty = function (property) {
|
71 | this.mode = (this.mode & ~S_IFMT) | property;
|
72 | };
|
73 | Node.prototype.setIsFile = function () {
|
74 | this.setModeProperty(S_IFREG);
|
75 | };
|
76 | Node.prototype.setIsDirectory = function () {
|
77 | this.setModeProperty(S_IFDIR);
|
78 | };
|
79 | Node.prototype.setIsSymlink = function () {
|
80 | this.setModeProperty(S_IFLNK);
|
81 | };
|
82 | Node.prototype.isFile = function () {
|
83 | return (this.mode & S_IFMT) === S_IFREG;
|
84 | };
|
85 | Node.prototype.isDirectory = function () {
|
86 | return (this.mode & S_IFMT) === S_IFDIR;
|
87 | };
|
88 | Node.prototype.isSymlink = function () {
|
89 |
|
90 | return (this.mode & S_IFMT) === S_IFLNK;
|
91 | };
|
92 | Node.prototype.makeSymlink = function (steps) {
|
93 | this.symlink = steps;
|
94 | this.setIsSymlink();
|
95 | };
|
96 | Node.prototype.write = function (buf, off, len, pos) {
|
97 | if (off === void 0) { off = 0; }
|
98 | if (len === void 0) { len = buf.length; }
|
99 | if (pos === void 0) { pos = 0; }
|
100 | if (!this.buf)
|
101 | this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
|
102 | if (pos + len > this.buf.length) {
|
103 | var newBuf = (0, buffer_1.bufferAllocUnsafe)(pos + len);
|
104 | this.buf.copy(newBuf, 0, 0, this.buf.length);
|
105 | this.buf = newBuf;
|
106 | }
|
107 | buf.copy(this.buf, pos, off, off + len);
|
108 | this.touch();
|
109 | return len;
|
110 | };
|
111 |
|
112 | Node.prototype.read = function (buf, off, len, pos) {
|
113 | if (off === void 0) { off = 0; }
|
114 | if (len === void 0) { len = buf.byteLength; }
|
115 | if (pos === void 0) { pos = 0; }
|
116 | if (!this.buf)
|
117 | this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
|
118 | var actualLen = len;
|
119 | if (actualLen > buf.byteLength) {
|
120 | actualLen = buf.byteLength;
|
121 | }
|
122 | if (actualLen + pos > this.buf.length) {
|
123 | actualLen = this.buf.length - pos;
|
124 | }
|
125 | this.buf.copy(buf, off, pos, pos + actualLen);
|
126 | return actualLen;
|
127 | };
|
128 | Node.prototype.truncate = function (len) {
|
129 | if (len === void 0) { len = 0; }
|
130 | if (!len)
|
131 | this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
|
132 | else {
|
133 | if (!this.buf)
|
134 | this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
|
135 | if (len <= this.buf.length) {
|
136 | this.buf = this.buf.slice(0, len);
|
137 | }
|
138 | else {
|
139 | var buf = (0, buffer_1.bufferAllocUnsafe)(0);
|
140 | this.buf.copy(buf);
|
141 | buf.fill(0, len);
|
142 | }
|
143 | }
|
144 | this.touch();
|
145 | };
|
146 | Node.prototype.chmod = function (perm) {
|
147 | this.perm = perm;
|
148 | this.mode = (this.mode & ~511) | perm;
|
149 | this.touch();
|
150 | };
|
151 | Node.prototype.chown = function (uid, gid) {
|
152 | this.uid = uid;
|
153 | this.gid = gid;
|
154 | this.touch();
|
155 | };
|
156 | Node.prototype.touch = function () {
|
157 | this.mtime = new Date();
|
158 | this.emit('change', this);
|
159 | };
|
160 | Node.prototype.canRead = function (uid, gid) {
|
161 | if (uid === void 0) { uid = process_1.default.getuid(); }
|
162 | if (gid === void 0) { gid = process_1.default.getgid(); }
|
163 | if (this.perm & 4 ) {
|
164 | return true;
|
165 | }
|
166 | if (gid === this.gid) {
|
167 | if (this.perm & 32 ) {
|
168 | return true;
|
169 | }
|
170 | }
|
171 | if (uid === this.uid) {
|
172 | if (this.perm & 256 ) {
|
173 | return true;
|
174 | }
|
175 | }
|
176 | return false;
|
177 | };
|
178 | Node.prototype.canWrite = function (uid, gid) {
|
179 | if (uid === void 0) { uid = process_1.default.getuid(); }
|
180 | if (gid === void 0) { gid = process_1.default.getgid(); }
|
181 | if (this.perm & 2 ) {
|
182 | return true;
|
183 | }
|
184 | if (gid === this.gid) {
|
185 | if (this.perm & 16 ) {
|
186 | return true;
|
187 | }
|
188 | }
|
189 | if (uid === this.uid) {
|
190 | if (this.perm & 128 ) {
|
191 | return true;
|
192 | }
|
193 | }
|
194 | return false;
|
195 | };
|
196 | Node.prototype.del = function () {
|
197 | this.emit('delete', this);
|
198 | };
|
199 | Node.prototype.toJSON = function () {
|
200 | return {
|
201 | ino: this.ino,
|
202 | uid: this.uid,
|
203 | gid: this.gid,
|
204 | atime: this.atime.getTime(),
|
205 | mtime: this.mtime.getTime(),
|
206 | ctime: this.ctime.getTime(),
|
207 | perm: this.perm,
|
208 | mode: this.mode,
|
209 | nlink: this.nlink,
|
210 | symlink: this.symlink,
|
211 | data: this.getString(),
|
212 | };
|
213 | };
|
214 | return Node;
|
215 | }(events_1.EventEmitter));
|
216 | exports.Node = Node;
|
217 |
|
218 |
|
219 |
|
220 | var Link = (function (_super) {
|
221 | __extends(Link, _super);
|
222 | function Link(vol, parent, name) {
|
223 | var _this = _super.call(this) || this;
|
224 | _this.children = {};
|
225 |
|
226 | _this.steps = [];
|
227 |
|
228 | _this.ino = 0;
|
229 |
|
230 | _this.length = 0;
|
231 | _this.vol = vol;
|
232 | _this.parent = parent;
|
233 | _this.steps = parent ? parent.steps.concat([name]) : [name];
|
234 | return _this;
|
235 | }
|
236 | Link.prototype.setNode = function (node) {
|
237 | this.node = node;
|
238 | this.ino = node.ino;
|
239 | };
|
240 | Link.prototype.getNode = function () {
|
241 | return this.node;
|
242 | };
|
243 | Link.prototype.createChild = function (name, node) {
|
244 | if (node === void 0) { node = this.vol.createNode(); }
|
245 | var link = new Link(this.vol, this, name);
|
246 | link.setNode(node);
|
247 | if (node.isDirectory()) {
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | }
|
253 | this.setChild(name, link);
|
254 | return link;
|
255 | };
|
256 | Link.prototype.setChild = function (name, link) {
|
257 | if (link === void 0) { link = new Link(this.vol, this, name); }
|
258 | this.children[name] = link;
|
259 | link.parent = this;
|
260 | this.length++;
|
261 | this.emit('child:add', link, this);
|
262 | return link;
|
263 | };
|
264 | Link.prototype.deleteChild = function (link) {
|
265 | delete this.children[link.getName()];
|
266 | this.length--;
|
267 | this.emit('child:delete', link, this);
|
268 | };
|
269 | Link.prototype.getChild = function (name) {
|
270 | if (Object.hasOwnProperty.call(this.children, name)) {
|
271 | return this.children[name];
|
272 | }
|
273 | };
|
274 | Link.prototype.getPath = function () {
|
275 | return this.steps.join(exports.SEP);
|
276 | };
|
277 | Link.prototype.getName = function () {
|
278 | return this.steps[this.steps.length - 1];
|
279 | };
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | |
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 | Link.prototype.walk = function (steps, stop, i) {
|
297 | if (stop === void 0) { stop = steps.length; }
|
298 | if (i === void 0) { i = 0; }
|
299 | if (i >= steps.length)
|
300 | return this;
|
301 | if (i >= stop)
|
302 | return this;
|
303 | var step = steps[i];
|
304 | var link = this.getChild(step);
|
305 | if (!link)
|
306 | return null;
|
307 | return link.walk(steps, stop, i + 1);
|
308 | };
|
309 | Link.prototype.toJSON = function () {
|
310 | return {
|
311 | steps: this.steps,
|
312 | ino: this.ino,
|
313 | children: Object.keys(this.children),
|
314 | };
|
315 | };
|
316 | return Link;
|
317 | }(events_1.EventEmitter));
|
318 | exports.Link = Link;
|
319 |
|
320 |
|
321 |
|
322 | var File = (function () {
|
323 | |
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 | function File(link, node, flags, fd) {
|
332 | |
333 |
|
334 |
|
335 |
|
336 | this.position = 0;
|
337 | this.link = link;
|
338 | this.node = node;
|
339 | this.flags = flags;
|
340 | this.fd = fd;
|
341 | }
|
342 | File.prototype.getString = function (encoding) {
|
343 | if (encoding === void 0) { encoding = 'utf8'; }
|
344 | return this.node.getString();
|
345 | };
|
346 | File.prototype.setString = function (str) {
|
347 | this.node.setString(str);
|
348 | };
|
349 | File.prototype.getBuffer = function () {
|
350 | return this.node.getBuffer();
|
351 | };
|
352 | File.prototype.setBuffer = function (buf) {
|
353 | this.node.setBuffer(buf);
|
354 | };
|
355 | File.prototype.getSize = function () {
|
356 | return this.node.getSize();
|
357 | };
|
358 | File.prototype.truncate = function (len) {
|
359 | this.node.truncate(len);
|
360 | };
|
361 | File.prototype.seekTo = function (position) {
|
362 | this.position = position;
|
363 | };
|
364 | File.prototype.stats = function () {
|
365 | return Stats_1.default.build(this.node);
|
366 | };
|
367 | File.prototype.write = function (buf, offset, length, position) {
|
368 | if (offset === void 0) { offset = 0; }
|
369 | if (length === void 0) { length = buf.length; }
|
370 | if (typeof position !== 'number')
|
371 | position = this.position;
|
372 | if (this.flags & O_APPEND)
|
373 | position = this.getSize();
|
374 | var bytes = this.node.write(buf, offset, length, position);
|
375 | this.position = position + bytes;
|
376 | return bytes;
|
377 | };
|
378 | File.prototype.read = function (buf, offset, length, position) {
|
379 | if (offset === void 0) { offset = 0; }
|
380 | if (length === void 0) { length = buf.byteLength; }
|
381 | if (typeof position !== 'number')
|
382 | position = this.position;
|
383 | var bytes = this.node.read(buf, offset, length, position);
|
384 | this.position = position + bytes;
|
385 | return bytes;
|
386 | };
|
387 | File.prototype.chmod = function (perm) {
|
388 | this.node.chmod(perm);
|
389 | };
|
390 | File.prototype.chown = function (uid, gid) {
|
391 | this.node.chown(uid, gid);
|
392 | };
|
393 | return File;
|
394 | }());
|
395 | exports.File = File;
|