forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreal.js
More file actions
571 lines (497 loc) · 16.4 KB
/
Copy pathreal.js
File metadata and controls
571 lines (497 loc) · 16.4 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
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
'use strict';
const {
ArrayPrototypePush,
Promise,
StringPrototypeStartsWith,
} = primordials;
const { Buffer } = require('buffer');
const fs = require('fs');
const path = require('path');
const { VirtualProvider } = require('internal/vfs/provider');
const { VirtualFileHandle } = require('internal/vfs/file_handle');
const { getValidatedPath } = require('internal/fs/utils');
const { setOwnProperty } = require('internal/util');
const {
ERR_METHOD_NOT_IMPLEMENTED,
} = require('internal/errors').codes;
const {
createEACCES,
createEBADF,
createENOENT,
} = require('internal/vfs/errors');
const kReadFileUnknownBufferLength = 8192;
/**
* A file handle that wraps a real file descriptor.
*/
// TODO(mcollina): reuse FileHandle from internal/fs/promises for the async
// methods instead of manually wrapping fs.read/write/fstat/ftruncate/close in
// Promises. Blocked on a way to wrap an existing numeric fd in a FileHandle so
// sync-opened handles can still share one underlying handle for async ops.
class RealFileHandle extends VirtualFileHandle {
#fd;
#realPath;
#checkClosed(syscall) {
if (this.closed) {
throw createEBADF(syscall);
}
}
#readFileResult(buffer, bytesRead, options) {
buffer = buffer.subarray(0, bytesRead);
const encoding = typeof options === 'string' ? options : options?.encoding;
if (encoding && encoding !== 'buffer') {
buffer = buffer.toString(encoding);
}
return buffer;
}
#readFileUnknownSizeResult(buffers, totalRead, options) {
return this.#readFileResult(
Buffer.concat(buffers, totalRead), totalRead, options);
}
/**
* @param {string} path The VFS path
* @param {string} flags The open flags
* @param {number} mode The file mode
* @param {number} fd The real file descriptor
* @param {string} realPath The real filesystem path
*/
constructor(path, flags, mode, fd, realPath) {
super(path, flags, mode);
this.#fd = fd;
this.#realPath = realPath;
}
readSync(buffer, offset, length, position) {
this.#checkClosed('read');
return fs.readSync(this.#fd, buffer, offset, length, position);
}
async read(buffer, offset, length, position) {
this.#checkClosed('read');
return new Promise((resolve, reject) => {
fs.read(this.#fd, buffer, offset, length, position, (err, bytesRead) => {
if (err) reject(err);
else resolve({ __proto__: null, bytesRead, buffer });
});
});
}
writeSync(buffer, offset, length, position) {
this.#checkClosed('write');
return fs.writeSync(this.#fd, buffer, offset, length, position);
}
async write(buffer, offset, length, position) {
this.#checkClosed('write');
return new Promise((resolve, reject) => {
fs.write(this.#fd, buffer, offset, length, position, (err, bytesWritten) => {
if (err) reject(err);
else resolve({ __proto__: null, bytesWritten, buffer });
});
});
}
readFileSync(options) {
this.#checkClosed('read');
const size = fs.fstatSync(this.#fd).size;
if (size === 0) {
const buffers = [];
let totalRead = 0;
while (true) {
const buffer = Buffer.allocUnsafe(kReadFileUnknownBufferLength);
const read = fs.readSync(
this.#fd, buffer, 0, buffer.byteLength, totalRead);
if (read === 0) break;
ArrayPrototypePush(buffers, buffer.subarray(0, read));
totalRead += read;
}
return this.#readFileUnknownSizeResult(buffers, totalRead, options);
}
const buffer = Buffer.allocUnsafe(size);
let bytesRead = 0;
while (bytesRead < buffer.byteLength) {
const read = fs.readSync(
this.#fd,
buffer,
bytesRead,
buffer.byteLength - bytesRead,
bytesRead,
);
if (read === 0) break;
bytesRead += read;
}
return this.#readFileResult(buffer, bytesRead, options);
}
async readFile(options) {
this.#checkClosed('read');
const size = (await this.stat()).size;
if (size === 0) {
const buffers = [];
let totalRead = 0;
while (true) {
const buffer = Buffer.allocUnsafe(kReadFileUnknownBufferLength);
const { bytesRead: read } = await this.read(
buffer,
0,
buffer.byteLength,
totalRead,
);
if (read === 0) break;
ArrayPrototypePush(buffers, buffer.subarray(0, read));
totalRead += read;
}
return this.#readFileUnknownSizeResult(buffers, totalRead, options);
}
const buffer = Buffer.allocUnsafe(size);
let bytesRead = 0;
while (bytesRead < buffer.byteLength) {
const { bytesRead: read } = await this.read(
buffer,
bytesRead,
buffer.byteLength - bytesRead,
bytesRead,
);
if (read === 0) break;
bytesRead += read;
}
return this.#readFileResult(buffer, bytesRead, options);
}
writeFileSync(data, options) {
this.#checkClosed('write');
fs.writeFileSync(this.#realPath, data, options);
}
async writeFile(data, options) {
this.#checkClosed('write');
return fs.promises.writeFile(this.#realPath, data, options);
}
statSync(options) {
this.#checkClosed('fstat');
return fs.fstatSync(this.#fd, options);
}
async stat(options) {
this.#checkClosed('fstat');
return new Promise((resolve, reject) => {
fs.fstat(this.#fd, options, (err, stats) => {
if (err) reject(err);
else resolve(stats);
});
});
}
truncateSync(len = 0) {
this.#checkClosed('ftruncate');
fs.ftruncateSync(this.#fd, len);
}
async truncate(len = 0) {
this.#checkClosed('ftruncate');
return new Promise((resolve, reject) => {
fs.ftruncate(this.#fd, len, (err) => {
if (err) reject(err);
else resolve();
});
});
}
closeSync() {
if (!this.closed) {
fs.closeSync(this.#fd);
super.closeSync();
}
}
async close() {
if (!this.closed) {
return new Promise((resolve, reject) => {
fs.close(this.#fd, (err) => {
if (err) reject(err);
else {
super.closeSync();
resolve();
}
});
});
}
}
}
/**
* A provider that wraps a real filesystem directory.
* Allows mounting a real directory at a different VFS path.
*/
class RealFSProvider extends VirtualProvider {
#rootPath;
/**
* @param {string} rootPath The real filesystem path to use as root
*/
constructor(rootPath) {
super();
// Resolve to absolute path and normalize
this.#rootPath = path.resolve(getValidatedPath(rootPath, 'rootPath'));
setOwnProperty(this, 'readonly', false);
setOwnProperty(this, 'supportsSymlinks', true);
}
/**
* Gets the root path of this provider.
* @returns {string}
*/
get rootPath() {
return this.#rootPath;
}
/**
* Resolves a VFS path to a real filesystem path.
* Ensures the path doesn't escape the root directory.
* @param {string} vfsPath The VFS path (relative to provider root)
* @returns {string} The real filesystem path
* @private
*/
#resolvePath(vfsPath, followSymlinks = true) {
// Normalize the VFS path (remove leading slash, handle . and ..)
let normalized = vfsPath;
if (normalized.startsWith('/')) {
normalized = normalized.slice(1);
}
// Join with root and resolve
const realPath = path.resolve(this.#rootPath, normalized);
// Security check: ensure the resolved path is within rootPath
const rootWithSep = this.#rootPath.endsWith(path.sep) ?
this.#rootPath :
this.#rootPath + path.sep;
if (realPath !== this.#rootPath && !StringPrototypeStartsWith(realPath, rootWithSep)) {
throw createENOENT('open', vfsPath);
}
// Resolve symlinks to prevent escape via symbolic links
if (followSymlinks) {
try {
const resolved = fs.realpathSync(realPath);
if (resolved !== this.#rootPath &&
!StringPrototypeStartsWith(resolved, rootWithSep)) {
throw createENOENT('open', vfsPath);
}
return resolved;
} catch (err) {
if (err?.code !== 'ENOENT') throw err;
// Path doesn't exist yet - verify deepest existing ancestor
this.#verifyAncestorInRoot(realPath, rootWithSep, vfsPath);
return realPath;
}
}
// For lstat/readlink (no final symlink follow), check parent only
this.#verifyAncestorInRoot(realPath, rootWithSep, vfsPath);
return realPath;
}
/**
* Verifies that the deepest existing ancestor of a path is within rootPath.
* @param {string} realPath The real filesystem path
* @param {string} rootWithSep The rootPath with trailing separator
* @param {string} vfsPath The original VFS path (for error messages)
*/
#verifyAncestorInRoot(realPath, rootWithSep, vfsPath) {
let current = path.dirname(realPath);
while (current.length >= this.#rootPath.length) {
try {
const resolved = fs.realpathSync(current);
if (resolved !== this.#rootPath &&
!StringPrototypeStartsWith(resolved, rootWithSep)) {
throw createENOENT('open', vfsPath);
}
return;
} catch (err) {
if (err?.code !== 'ENOENT') throw err;
current = path.dirname(current);
}
}
}
openSync(vfsPath, flags, mode) {
const realPath = this.#resolvePath(vfsPath);
const fd = fs.openSync(realPath, flags, mode);
return new RealFileHandle(vfsPath, flags, mode ?? 0o644, fd, realPath);
}
async open(vfsPath, flags, mode) {
const realPath = this.#resolvePath(vfsPath);
return new Promise((resolve, reject) => {
fs.open(realPath, flags, mode, (err, fd) => {
if (err) reject(err);
else resolve(new RealFileHandle(vfsPath, flags, mode ?? 0o644, fd, realPath));
});
});
}
statSync(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.statSync(realPath, options);
}
async stat(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.promises.stat(realPath, options);
}
lstatSync(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath, false);
return fs.lstatSync(realPath, options);
}
async lstat(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath, false);
return fs.promises.lstat(realPath, options);
}
readdirSync(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.readdirSync(realPath, options);
}
async readdir(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.promises.readdir(realPath, options);
}
mkdirSync(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.mkdirSync(realPath, options);
}
async mkdir(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.promises.mkdir(realPath, options);
}
rmdirSync(vfsPath) {
const realPath = this.#resolvePath(vfsPath);
fs.rmdirSync(realPath);
}
async rmdir(vfsPath) {
const realPath = this.#resolvePath(vfsPath);
return fs.promises.rmdir(realPath);
}
unlinkSync(vfsPath) {
const realPath = this.#resolvePath(vfsPath);
fs.unlinkSync(realPath);
}
async unlink(vfsPath) {
const realPath = this.#resolvePath(vfsPath);
return fs.promises.unlink(realPath);
}
renameSync(oldVfsPath, newVfsPath) {
const oldRealPath = this.#resolvePath(oldVfsPath);
const newRealPath = this.#resolvePath(newVfsPath);
fs.renameSync(oldRealPath, newRealPath);
}
async rename(oldVfsPath, newVfsPath) {
const oldRealPath = this.#resolvePath(oldVfsPath);
const newRealPath = this.#resolvePath(newVfsPath);
return fs.promises.rename(oldRealPath, newRealPath);
}
readlinkSync(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath, false);
const target = fs.readlinkSync(realPath, options);
// Translate absolute targets within rootPath to VFS-relative
if (path.isAbsolute(target)) {
const rootWithSep = this.#rootPath + path.sep;
if (target === this.#rootPath) {
return '/';
}
if (StringPrototypeStartsWith(target, rootWithSep)) {
return '/' + target.slice(rootWithSep.length).replace(/\\/g, '/');
}
}
return target;
}
async readlink(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath, false);
const target = await fs.promises.readlink(realPath, options);
// Translate absolute targets within rootPath to VFS-relative
if (path.isAbsolute(target)) {
const rootWithSep = this.#rootPath + path.sep;
if (target === this.#rootPath) {
return '/';
}
if (StringPrototypeStartsWith(target, rootWithSep)) {
return '/' + target.slice(rootWithSep.length).replace(/\\/g, '/');
}
}
return target;
}
symlinkSync(target, vfsPath, type) {
// Validate target resolves within rootPath
if (path.isAbsolute(target)) {
throw createEACCES('symlink', vfsPath);
}
const realPath = this.#resolvePath(vfsPath);
const resolvedTarget = path.resolve(path.dirname(realPath), target);
const rootWithSep = this.#rootPath.endsWith(path.sep) ?
this.#rootPath : this.#rootPath + path.sep;
if (resolvedTarget !== this.#rootPath &&
!StringPrototypeStartsWith(resolvedTarget, rootWithSep)) {
throw createEACCES('symlink', vfsPath);
}
fs.symlinkSync(target, realPath, type);
}
async symlink(target, vfsPath, type) {
// Validate target resolves within rootPath
if (path.isAbsolute(target)) {
throw createEACCES('symlink', vfsPath);
}
const realPath = this.#resolvePath(vfsPath);
const resolvedTarget = path.resolve(path.dirname(realPath), target);
const rootWithSep = this.#rootPath.endsWith(path.sep) ?
this.#rootPath : this.#rootPath + path.sep;
if (resolvedTarget !== this.#rootPath &&
!StringPrototypeStartsWith(resolvedTarget, rootWithSep)) {
throw createEACCES('symlink', vfsPath);
}
return fs.promises.symlink(target, realPath, type);
}
// path.relative handles case-insensitivity on Windows, which matters here
// because fs.realpathSync (a JS impl) preserves case but fs.promises.realpath
// (native) canonicalizes the drive letter and other components.
#resolvedToVfsPath(resolved, vfsPath, syscall) {
const rel = path.relative(this.#rootPath, resolved);
if (rel === '') return '/';
if (rel === '..' ||
StringPrototypeStartsWith(rel, '..' + path.sep) ||
path.isAbsolute(rel)) {
throw createEACCES(syscall, vfsPath);
}
return '/' + rel.replace(/\\/g, '/');
}
realpathSync(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
const resolved = fs.realpathSync(realPath, options);
return this.#resolvedToVfsPath(resolved, vfsPath, 'realpath');
}
async realpath(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
const resolved = await fs.promises.realpath(realPath, options);
return this.#resolvedToVfsPath(resolved, vfsPath, 'realpath');
}
accessSync(vfsPath, mode) {
const realPath = this.#resolvePath(vfsPath);
fs.accessSync(realPath, mode);
}
async access(vfsPath, mode) {
const realPath = this.#resolvePath(vfsPath);
return fs.promises.access(realPath, mode);
}
lchmodSync(vfsPath, mode) {
if (fs.lchmodSync === undefined) {
throw new ERR_METHOD_NOT_IMPLEMENTED('lchmodSync');
}
const realPath = this.#resolvePath(vfsPath, false);
fs.lchmodSync(realPath, mode);
}
copyFileSync(srcVfsPath, destVfsPath, mode) {
const srcRealPath = this.#resolvePath(srcVfsPath);
const destRealPath = this.#resolvePath(destVfsPath);
fs.copyFileSync(srcRealPath, destRealPath, mode);
}
async copyFile(srcVfsPath, destVfsPath, mode) {
const srcRealPath = this.#resolvePath(srcVfsPath);
const destRealPath = this.#resolvePath(destVfsPath);
return fs.promises.copyFile(srcRealPath, destRealPath, mode);
}
get supportsWatch() {
return true;
}
watch(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.watch(realPath, options);
}
watchAsync(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.promises.watch(realPath, options);
}
watchFile(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.watchFile(realPath, options, () => {});
}
unwatchFile(vfsPath, listener) {
const realPath = this.#resolvePath(vfsPath);
fs.unwatchFile(realPath, listener);
}
}
module.exports = {
RealFSProvider,
RealFileHandle,
};