brsh 2.5.0 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/POSIX-CHECKLIST.md +14 -15
- package/Readme.md +13 -3
- package/bin/brsh-shell.js +0 -0
- package/bin/exportFileSystem.js +42 -47
- package/dist/shell.es.js +2524 -2248
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +11 -9
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.es.js +2540 -2264
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +13 -11
- package/dist/terminal.min.js.map +1 -1
- package/lib/BaseFileSystem.js +1 -1
- package/lib/FileSystem.js +171 -27
- package/lib/NodeFileSystem.js +1 -1
- package/lib/local/index.js +3 -0
- package/lib/local/ln.js +101 -0
- package/lib/local/stat.js +94 -0
- package/lib/local/touch.js +59 -0
- package/package.json +1 -1
- package/test/binary-filesystem.test.js +174 -0
- package/test/exportFileSystem.test.js +33 -24
- package/test/node-filesystem.test.js +169 -0
package/lib/BaseFileSystem.js
CHANGED
package/lib/FileSystem.js
CHANGED
|
@@ -2,9 +2,79 @@
|
|
|
2
2
|
|
|
3
3
|
const BaseFileSystem = require('./BaseFileSystem');
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Represents a binary file stored as raw bytes in the virtual filesystem.
|
|
7
|
+
* Extends Uint8Array so binary-aware commands can access bytes directly,
|
|
8
|
+
* while text-based commands (cat, grep, etc.) get the original data URI
|
|
9
|
+
* via toString() — avoiding the 33% base64 memory overhead at runtime.
|
|
10
|
+
*/
|
|
11
|
+
class BinaryFile extends Uint8Array {
|
|
12
|
+
constructor(buffer, mimeType) {
|
|
13
|
+
super(buffer);
|
|
14
|
+
Object.defineProperty(this, 'mimeType', { value: mimeType, enumerable: false, configurable: false, writable: false });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
toString() {
|
|
18
|
+
if (typeof Buffer !== 'undefined') {
|
|
19
|
+
return `data:${this.mimeType};base64,${Buffer.from(this).toString('base64')}`;
|
|
20
|
+
}
|
|
21
|
+
// Browser path — chunked to avoid stack overflow on large files
|
|
22
|
+
let binary = '';
|
|
23
|
+
const chunk = 8192;
|
|
24
|
+
for (let i = 0; i < this.length; i += chunk) {
|
|
25
|
+
binary += String.fromCharCode(...this.subarray(i, i + chunk));
|
|
26
|
+
}
|
|
27
|
+
return `data:${this.mimeType};base64,${btoa(binary)}`;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Represents a symbolic link stored in the virtual filesystem.
|
|
33
|
+
* Acts as an opaque marker: `_resolveByAbsolutePath` follows these
|
|
34
|
+
* transparently during file access, while lstat-like calls return the
|
|
35
|
+
* SymlinkRef itself so callers can detect and report symlinks.
|
|
36
|
+
*/
|
|
37
|
+
class SymlinkRef {
|
|
38
|
+
constructor(target) {
|
|
39
|
+
this.target = target;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Recursively converts `data:<mime>;base64,<...>` strings in a filesystem
|
|
45
|
+
* node to BinaryFile instances (Uint8Array subclass), saving the ~33%
|
|
46
|
+
* memory overhead of storing base64-encoded binary data as UTF-16 strings.
|
|
47
|
+
* Mutates the node in-place.
|
|
48
|
+
*/
|
|
49
|
+
function decodeBinaryFiles(node) {
|
|
50
|
+
for (const key of Object.keys(node)) {
|
|
51
|
+
const value = node[key];
|
|
52
|
+
if (value !== null && typeof value === 'object' && !(value instanceof Uint8Array) && !(value instanceof SymlinkRef)) {
|
|
53
|
+
decodeBinaryFiles(value);
|
|
54
|
+
} else if (typeof value === 'string' && value.startsWith('data:')) {
|
|
55
|
+
const semicolon = value.indexOf(';');
|
|
56
|
+
const comma = value.indexOf(',', semicolon);
|
|
57
|
+
if (semicolon !== -1 && comma !== -1 && value.slice(semicolon + 1, comma) === 'base64') {
|
|
58
|
+
const mimeType = value.slice(5, semicolon);
|
|
59
|
+
const b64 = value.slice(comma + 1);
|
|
60
|
+
let bytes;
|
|
61
|
+
if (typeof Buffer !== 'undefined') {
|
|
62
|
+
bytes = new Uint8Array(Buffer.from(b64, 'base64'));
|
|
63
|
+
} else {
|
|
64
|
+
const binary = atob(b64);
|
|
65
|
+
bytes = new Uint8Array(binary.length);
|
|
66
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
67
|
+
}
|
|
68
|
+
node[key] = new BinaryFile(bytes.buffer, mimeType);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
5
74
|
class FileSystem extends BaseFileSystem {
|
|
6
75
|
constructor(cwd = '/', filesystem = {}, permissions = {}, onChange = null) {
|
|
7
76
|
super();
|
|
77
|
+
decodeBinaryFiles(filesystem);
|
|
8
78
|
this.fileSystem = filesystem;
|
|
9
79
|
this.permissions = Object.assign({}, permissions);
|
|
10
80
|
this.cwd = cwd;
|
|
@@ -13,9 +83,9 @@ class FileSystem extends BaseFileSystem {
|
|
|
13
83
|
}
|
|
14
84
|
|
|
15
85
|
// Returns file content if the path exists AND is readable; null otherwise.
|
|
16
|
-
// Use exists() or stat() for permission-free
|
|
86
|
+
// Follows symlinks transparently. Use exists() or stat() for permission-free checks.
|
|
17
87
|
getFileByPath(path) {
|
|
18
|
-
const file = this.
|
|
88
|
+
const file = this._resolveByAbsolutePath(this._pathToAbsolute(path), true);
|
|
19
89
|
if (file === null || file === undefined) return null;
|
|
20
90
|
if (!this.isReadable(path)) return null;
|
|
21
91
|
return file;
|
|
@@ -26,21 +96,38 @@ class FileSystem extends BaseFileSystem {
|
|
|
26
96
|
return this._getFileByAbsolutePath(this._pathToAbsolute(path));
|
|
27
97
|
}
|
|
28
98
|
|
|
29
|
-
// Returns true if the path exists
|
|
99
|
+
// Returns true if the path exists (and for symlinks, the target resolves too).
|
|
30
100
|
exists(path) {
|
|
31
|
-
const file = this.
|
|
101
|
+
const file = this._resolveByAbsolutePath(this._pathToAbsolute(path), true);
|
|
32
102
|
return file !== null && file !== undefined;
|
|
33
103
|
}
|
|
34
104
|
|
|
35
|
-
|
|
105
|
+
isSymlink(path) {
|
|
106
|
+
const file = this._resolveByAbsolutePath(this._pathToAbsolute(path), false);
|
|
107
|
+
return file instanceof SymlinkRef;
|
|
108
|
+
}
|
|
109
|
+
readSymlink(path) {
|
|
110
|
+
const file = this._resolveByAbsolutePath(this._pathToAbsolute(path), false);
|
|
111
|
+
return file instanceof SymlinkRef ? file.target : null;
|
|
112
|
+
}
|
|
113
|
+
createSymlink(target, linkPath) {
|
|
114
|
+
this._setFileRaw(linkPath, new SymlinkRef(target));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Returns type metadata without following the final symlink (like lstat).
|
|
36
118
|
// Returns null if the path does not exist.
|
|
37
119
|
stat(path) {
|
|
38
|
-
const file = this.
|
|
120
|
+
const file = this._resolveByAbsolutePath(this._pathToAbsolute(path), false);
|
|
39
121
|
if (file === null || file === undefined) return null;
|
|
122
|
+
if (file instanceof SymlinkRef) {
|
|
123
|
+
return { isDirectory: false, isFile: false, isCommand: false, isBinary: false, isSymlink: true };
|
|
124
|
+
}
|
|
40
125
|
return {
|
|
41
126
|
isDirectory: file.constructor === Object,
|
|
42
|
-
isFile: typeof file === 'string',
|
|
43
|
-
isCommand: typeof file === 'function'
|
|
127
|
+
isFile: typeof file === 'string' || file instanceof Uint8Array,
|
|
128
|
+
isCommand: typeof file === 'function',
|
|
129
|
+
isBinary: file instanceof Uint8Array,
|
|
130
|
+
isSymlink: false,
|
|
44
131
|
};
|
|
45
132
|
}
|
|
46
133
|
|
|
@@ -97,22 +184,65 @@ class FileSystem extends BaseFileSystem {
|
|
|
97
184
|
_setFileByAbsolutePath(absolute, content, createDir = true) {
|
|
98
185
|
const parts = absolute.split('/');
|
|
99
186
|
parts.shift();
|
|
100
|
-
|
|
101
187
|
const filename = parts.pop();
|
|
102
188
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
189
|
+
let dir = this.fileSystem;
|
|
190
|
+
const pathSoFar = [];
|
|
191
|
+
for (const handle of parts) {
|
|
192
|
+
pathSoFar.push(handle);
|
|
193
|
+
if (!Object.prototype.hasOwnProperty.call(dir, handle)) {
|
|
194
|
+
if (!createDir) throw new Error(`${handle}: No such file or directory`);
|
|
195
|
+
dir[handle] = {};
|
|
196
|
+
}
|
|
197
|
+
let next = dir[handle];
|
|
198
|
+
// Follow symlinks to directories in intermediate path components
|
|
199
|
+
if (next instanceof SymlinkRef) {
|
|
200
|
+
const symlinkDir = '/' + pathSoFar.slice(0, -1).join('/');
|
|
201
|
+
const target = next.target.startsWith('/')
|
|
202
|
+
? this._traverseComplexAbsolutePath(next.target)
|
|
203
|
+
: this._traverseComplexAbsolutePath(symlinkDir + '/' + next.target);
|
|
204
|
+
next = this._resolveByAbsolutePath(target, true);
|
|
205
|
+
if (next == null || typeof next !== 'object' || next instanceof SymlinkRef) {
|
|
206
|
+
throw new Error(`${handle}: Not a directory`);
|
|
107
207
|
}
|
|
108
|
-
pointer[handle] = {};
|
|
109
208
|
}
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
|
|
209
|
+
dir = next;
|
|
210
|
+
}
|
|
113
211
|
dir[filename] = content;
|
|
114
212
|
}
|
|
115
213
|
|
|
214
|
+
// Resolves a path, optionally following the final symlink component.
|
|
215
|
+
// followLast=true → like stat() — follows everything (for reading through symlinks)
|
|
216
|
+
// followLast=false → like lstat() — stops at a final SymlinkRef (for isSymlink/readSymlink/stat)
|
|
217
|
+
// Intermediate symlinks in directory components are always followed.
|
|
218
|
+
// Returns null on a missing path or a symlink loop (depth > 10).
|
|
219
|
+
_resolveByAbsolutePath(absolute, followLast = true, depth = 0) {
|
|
220
|
+
if (depth > 10) return null; // loop protection
|
|
221
|
+
const parts = absolute.split('/');
|
|
222
|
+
parts.shift();
|
|
223
|
+
if (parts.length > 0 && parts[parts.length - 1].length < 1) parts.pop();
|
|
224
|
+
|
|
225
|
+
let pointer = this.fileSystem;
|
|
226
|
+
const pathSoFar = [];
|
|
227
|
+
for (let i = 0; i < parts.length; i++) {
|
|
228
|
+
const handle = parts[i];
|
|
229
|
+
const isLast = i === parts.length - 1;
|
|
230
|
+
if (pointer == null || typeof pointer !== 'object' || pointer instanceof SymlinkRef) return null;
|
|
231
|
+
if (!Object.prototype.hasOwnProperty.call(pointer, handle)) return null;
|
|
232
|
+
pointer = pointer[handle];
|
|
233
|
+
pathSoFar.push(handle);
|
|
234
|
+
if (pointer instanceof SymlinkRef && (!isLast || followLast)) {
|
|
235
|
+
const symlinkDir = '/' + pathSoFar.slice(0, -1).join('/');
|
|
236
|
+
const target = pointer.target.startsWith('/')
|
|
237
|
+
? this._traverseComplexAbsolutePath(pointer.target)
|
|
238
|
+
: this._traverseComplexAbsolutePath(symlinkDir + '/' + pointer.target);
|
|
239
|
+
pointer = this._resolveByAbsolutePath(target, true, depth + 1);
|
|
240
|
+
if (pointer == null) return null;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return pointer;
|
|
244
|
+
}
|
|
245
|
+
|
|
116
246
|
_getFileByAbsolutePath(absolute) {
|
|
117
247
|
const parts = absolute.split('/');
|
|
118
248
|
parts.shift();
|
|
@@ -122,10 +252,9 @@ class FileSystem extends BaseFileSystem {
|
|
|
122
252
|
}
|
|
123
253
|
|
|
124
254
|
return parts.reduce((pointer, handle) => {
|
|
125
|
-
if(pointer
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
return null;
|
|
255
|
+
if (pointer == null || typeof pointer !== 'object') return null;
|
|
256
|
+
if (!Object.prototype.hasOwnProperty.call(pointer, handle)) return null;
|
|
257
|
+
return pointer[handle];
|
|
129
258
|
}, this.fileSystem);
|
|
130
259
|
}
|
|
131
260
|
|
|
@@ -158,10 +287,22 @@ class FileSystem extends BaseFileSystem {
|
|
|
158
287
|
const filename = parts.pop();
|
|
159
288
|
if (!filename) return;
|
|
160
289
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
290
|
+
let parent = this.fileSystem;
|
|
291
|
+
const pathSoFar = [];
|
|
292
|
+
for (const handle of parts) {
|
|
293
|
+
pathSoFar.push(handle);
|
|
294
|
+
if (!Object.prototype.hasOwnProperty.call(parent, handle)) return;
|
|
295
|
+
let next = parent[handle];
|
|
296
|
+
if (next instanceof SymlinkRef) {
|
|
297
|
+
const symlinkDir = '/' + pathSoFar.slice(0, -1).join('/');
|
|
298
|
+
const target = next.target.startsWith('/')
|
|
299
|
+
? this._traverseComplexAbsolutePath(next.target)
|
|
300
|
+
: this._traverseComplexAbsolutePath(symlinkDir + '/' + next.target);
|
|
301
|
+
next = this._resolveByAbsolutePath(target, true);
|
|
302
|
+
if (next == null || typeof next !== 'object' || next instanceof SymlinkRef) return;
|
|
303
|
+
}
|
|
304
|
+
parent = next;
|
|
305
|
+
}
|
|
165
306
|
|
|
166
307
|
if (parent && filename in parent) {
|
|
167
308
|
delete parent[filename];
|
|
@@ -173,7 +314,7 @@ class FileSystem extends BaseFileSystem {
|
|
|
173
314
|
if (this.permissions[absolute] !== undefined) {
|
|
174
315
|
return this.permissions[absolute];
|
|
175
316
|
}
|
|
176
|
-
const file = this.
|
|
317
|
+
const file = this._resolveByAbsolutePath(absolute, true);
|
|
177
318
|
if (file === null || file === undefined) return null;
|
|
178
319
|
if (file.constructor === Object) return 0o755;
|
|
179
320
|
if (typeof file === 'function') return 0o555;
|
|
@@ -188,9 +329,10 @@ class FileSystem extends BaseFileSystem {
|
|
|
188
329
|
|
|
189
330
|
getSize(path) {
|
|
190
331
|
const absolute = this._pathToAbsolute(path);
|
|
191
|
-
const file = this.
|
|
332
|
+
const file = this._resolveByAbsolutePath(absolute, true);
|
|
192
333
|
if (file === null || file === undefined) return 0;
|
|
193
334
|
if (file.constructor === Object) return JSON.stringify(file).length;
|
|
335
|
+
if (file instanceof Uint8Array) return file.length;
|
|
194
336
|
return file.toString().length;
|
|
195
337
|
}
|
|
196
338
|
|
|
@@ -250,3 +392,5 @@ class FileSystem extends BaseFileSystem {
|
|
|
250
392
|
|
|
251
393
|
|
|
252
394
|
module.exports = FileSystem;
|
|
395
|
+
module.exports.BinaryFile = BinaryFile;
|
|
396
|
+
module.exports.SymlinkRef = SymlinkRef;
|
package/lib/NodeFileSystem.js
CHANGED
|
@@ -238,7 +238,7 @@ class NodeFileSystem extends BaseFileSystem {
|
|
|
238
238
|
const abs = this.absolutePath(path);
|
|
239
239
|
if (this._virtualFiles[abs] !== undefined) {
|
|
240
240
|
const v = this._virtualFiles[abs];
|
|
241
|
-
return { isDirectory: v !== null && v !== undefined && v.constructor === Object, isFile: typeof v === 'string', isCommand: typeof v === 'function' };
|
|
241
|
+
return { isDirectory: v !== null && v !== undefined && v.constructor === Object, isFile: typeof v === 'string' || v instanceof Uint8Array, isCommand: typeof v === 'function', isBinary: v instanceof Uint8Array };
|
|
242
242
|
}
|
|
243
243
|
try {
|
|
244
244
|
const s = nodeFs.statSync(abs);
|
package/lib/local/index.js
CHANGED
|
@@ -6,10 +6,13 @@ module.exports = [
|
|
|
6
6
|
require('./cd'),
|
|
7
7
|
require('./chmod'),
|
|
8
8
|
require('./cp'),
|
|
9
|
+
require('./ln'),
|
|
9
10
|
require('./mkdir'),
|
|
10
11
|
require('./mv'),
|
|
11
12
|
require('./pwd'),
|
|
12
13
|
require('./rm'),
|
|
14
|
+
require('./stat'),
|
|
15
|
+
require('./touch'),
|
|
13
16
|
require('./which'),
|
|
14
17
|
require('./source'),
|
|
15
18
|
require('./clear'),
|
package/lib/local/ln.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LocalCommand = require('./LocalCommand');
|
|
4
|
+
const nodeFs = (() => { try { return require('fs'); } catch { return null; } })();
|
|
5
|
+
|
|
6
|
+
class Ln extends LocalCommand {
|
|
7
|
+
static get name() { return 'ln'; }
|
|
8
|
+
get requiresFilesystem() { return true; }
|
|
9
|
+
|
|
10
|
+
run() {
|
|
11
|
+
let symbolic = false;
|
|
12
|
+
let force = false;
|
|
13
|
+
const paths = [];
|
|
14
|
+
|
|
15
|
+
const args = [...this.arguments];
|
|
16
|
+
let i = 0;
|
|
17
|
+
while (i < args.length) {
|
|
18
|
+
const arg = args[i];
|
|
19
|
+
if (arg === '--') { i++; break; }
|
|
20
|
+
if (arg.startsWith('-') && arg.length > 1) {
|
|
21
|
+
for (const ch of arg.slice(1)) {
|
|
22
|
+
switch (ch) {
|
|
23
|
+
case 's': symbolic = true; break;
|
|
24
|
+
case 'f': force = true; break;
|
|
25
|
+
case 'n': break; // --no-dereference, no-op
|
|
26
|
+
case 'v': break; // verbose, no-op
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
} else {
|
|
30
|
+
paths.push(arg);
|
|
31
|
+
}
|
|
32
|
+
i++;
|
|
33
|
+
}
|
|
34
|
+
while (i < args.length) paths.push(args[i++]);
|
|
35
|
+
|
|
36
|
+
if (paths.length < 2) {
|
|
37
|
+
this.stdErr = paths.length === 0
|
|
38
|
+
? 'ln: missing file operand'
|
|
39
|
+
: `ln: missing destination file operand after '${paths[0]}'`;
|
|
40
|
+
this.exitCode = 1;
|
|
41
|
+
return Promise.resolve();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const dest = paths[paths.length - 1];
|
|
45
|
+
const sources = paths.slice(0, -1);
|
|
46
|
+
const destStat = this.fs.stat(dest);
|
|
47
|
+
const destIsDir = destStat !== null && destStat.isDirectory;
|
|
48
|
+
|
|
49
|
+
for (const src of sources) {
|
|
50
|
+
const linkPath = destIsDir
|
|
51
|
+
? dest.replace(/\/$/, '') + '/' + src.split('/').pop()
|
|
52
|
+
: dest;
|
|
53
|
+
|
|
54
|
+
// Remove existing target if -f
|
|
55
|
+
if (force && this.fs.exists(linkPath)) {
|
|
56
|
+
const ls = this.fs.stat(linkPath);
|
|
57
|
+
if (!ls?.isDirectory) {
|
|
58
|
+
try { this.fs.deleteFileByPath(linkPath); } catch(e) { /* ignore */ }
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (symbolic) {
|
|
63
|
+
if (!this.fs.createSymlink) {
|
|
64
|
+
this.stdErr = `ln: cannot create symlink '${linkPath}': Operation not supported`;
|
|
65
|
+
this.exitCode = 1;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
this.fs.createSymlink(src, linkPath);
|
|
70
|
+
} catch(e) {
|
|
71
|
+
this.stdErr = `ln: cannot create symlink '${linkPath}': ${e.message}`;
|
|
72
|
+
this.exitCode = 1;
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
// Hard link — only supported on real FS
|
|
76
|
+
if (!this.fs.isRealFilesystem || !nodeFs) {
|
|
77
|
+
this.stdErr = `ln: cannot create hard link '${linkPath}': Operation not supported`;
|
|
78
|
+
this.exitCode = 1;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (!this.fs.exists(src)) {
|
|
82
|
+
this.stdErr = `ln: failed to create hard link '${linkPath}' => '${src}': No such file or directory`;
|
|
83
|
+
this.exitCode = 1;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
nodeFs.linkSync(
|
|
88
|
+
this.fs.absolutePath(src),
|
|
89
|
+
this.fs.absolutePath(linkPath)
|
|
90
|
+
);
|
|
91
|
+
} catch(e) {
|
|
92
|
+
this.stdErr = `ln: failed to create hard link '${linkPath}' => '${src}': ${e.message}`;
|
|
93
|
+
this.exitCode = 1;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return Promise.resolve();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
module.exports = Ln;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LocalCommand = require('./LocalCommand');
|
|
4
|
+
const nodeFs = (() => { try { return require('fs'); } catch { return null; } })();
|
|
5
|
+
|
|
6
|
+
class Stat extends LocalCommand {
|
|
7
|
+
static get name() { return 'stat'; }
|
|
8
|
+
get requiresFilesystem() { return true; }
|
|
9
|
+
|
|
10
|
+
run() {
|
|
11
|
+
const paths = [];
|
|
12
|
+
for (const arg of this.arguments) {
|
|
13
|
+
if (arg === '--' ) break;
|
|
14
|
+
if (!arg.startsWith('-')) paths.push(arg);
|
|
15
|
+
// -L (follow symlinks) is already our default; other flags ignored
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (paths.length === 0) {
|
|
19
|
+
this.stdErr = 'stat: missing operand';
|
|
20
|
+
this.exitCode = 1;
|
|
21
|
+
return Promise.resolve();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
for (const path of paths) {
|
|
25
|
+
const isLink = this.fs.isSymlink ? this.fs.isSymlink(path) : false;
|
|
26
|
+
if (!this.fs.exists(path) && !isLink) {
|
|
27
|
+
this.stdErr = `stat: cannot stat '${path}': No such file or directory`;
|
|
28
|
+
this.exitCode = 1;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
this.stdOut = this._format(path, isLink);
|
|
32
|
+
}
|
|
33
|
+
return Promise.resolve();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
_format(path, isLink) {
|
|
37
|
+
const abs = this.fs.absolutePath(path);
|
|
38
|
+
|
|
39
|
+
// Real filesystem — use lstat for accurate info
|
|
40
|
+
if (this.fs.isRealFilesystem && nodeFs) {
|
|
41
|
+
try {
|
|
42
|
+
const st = nodeFs.lstatSync(abs);
|
|
43
|
+
const typChar = st.isSymbolicLink() ? 'l'
|
|
44
|
+
: st.isDirectory() ? 'd'
|
|
45
|
+
: '-';
|
|
46
|
+
const typeName = st.isSymbolicLink() ? 'symbolic link'
|
|
47
|
+
: st.isDirectory() ? 'directory'
|
|
48
|
+
: 'regular file';
|
|
49
|
+
const modeOctal = (st.mode & 0o777).toString(8).padStart(4, '0');
|
|
50
|
+
const modeStr = typChar + this._rwxBits(st.mode & 0o777);
|
|
51
|
+
const linkTarget = st.isSymbolicLink() ? ` -> ${nodeFs.readlinkSync(abs)}` : '';
|
|
52
|
+
return [
|
|
53
|
+
` File: ${abs}${linkTarget}`,
|
|
54
|
+
` Size: ${st.size}\t\tBlocks: ${st.blocks}\t\tIO Block: ${st.blksize}\t${typeName}`,
|
|
55
|
+
`Device: ${st.dev}\t\tInode: ${st.ino}\t\tLinks: ${st.nlink}`,
|
|
56
|
+
`Access: (${modeOctal}/${modeStr})\tUid: ${st.uid}\tGid: ${st.gid}`,
|
|
57
|
+
`Access: ${st.atime.toISOString()}`,
|
|
58
|
+
`Modify: ${st.mtime.toISOString()}`,
|
|
59
|
+
`Change: ${st.ctime.toISOString()}`,
|
|
60
|
+
].join('\n');
|
|
61
|
+
} catch(e) {
|
|
62
|
+
return `stat: cannot stat '${path}': ${e.message}`;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Virtual filesystem — derive info from what we have
|
|
67
|
+
const s = this.fs.stat(path);
|
|
68
|
+
const mode = this.fs.getMode(path) ?? 0o644;
|
|
69
|
+
const size = this.fs.getSize(path);
|
|
70
|
+
const typChar = isLink ? 'l' : s?.isDirectory ? 'd' : '-';
|
|
71
|
+
const typeName = isLink ? 'symbolic link'
|
|
72
|
+
: s?.isDirectory ? 'directory'
|
|
73
|
+
: s?.isCommand ? 'regular file'
|
|
74
|
+
: 'regular file';
|
|
75
|
+
const modeOctal = mode.toString(8).padStart(4, '0');
|
|
76
|
+
const modeStr = typChar + this._rwxBits(mode);
|
|
77
|
+
const linkTarget = isLink && this.fs.readSymlink ? ` -> ${this.fs.readSymlink(path)}` : '';
|
|
78
|
+
return [
|
|
79
|
+
` File: ${abs}${linkTarget}`,
|
|
80
|
+
` Size: ${size}\t\tType: ${typeName}`,
|
|
81
|
+
`Access: (${modeOctal}/${modeStr})`,
|
|
82
|
+
].join('\n');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
_rwxBits(mode) {
|
|
86
|
+
return [
|
|
87
|
+
(mode & 0o400) ? 'r' : '-', (mode & 0o200) ? 'w' : '-', (mode & 0o100) ? 'x' : '-',
|
|
88
|
+
(mode & 0o040) ? 'r' : '-', (mode & 0o020) ? 'w' : '-', (mode & 0o010) ? 'x' : '-',
|
|
89
|
+
(mode & 0o004) ? 'r' : '-', (mode & 0o002) ? 'w' : '-', (mode & 0o001) ? 'x' : '-',
|
|
90
|
+
].join('');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = Stat;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LocalCommand = require('./LocalCommand');
|
|
4
|
+
const nodeFs = (() => { try { return require('fs'); } catch { return null; } })();
|
|
5
|
+
|
|
6
|
+
class Touch extends LocalCommand {
|
|
7
|
+
static get name() { return 'touch'; }
|
|
8
|
+
get requiresFilesystem() { return true; }
|
|
9
|
+
|
|
10
|
+
run() {
|
|
11
|
+
let noCreate = false;
|
|
12
|
+
const paths = [];
|
|
13
|
+
|
|
14
|
+
for (const arg of this.arguments) {
|
|
15
|
+
if (arg === '--') break;
|
|
16
|
+
if (arg.startsWith('-') && arg.length > 1) {
|
|
17
|
+
for (const ch of arg.slice(1)) {
|
|
18
|
+
if (ch === 'c') noCreate = true;
|
|
19
|
+
// -a, -m, -t, -d: timestamp flags — update timestamps but
|
|
20
|
+
// ignore the specific value (no mtime tracking in virtual FS)
|
|
21
|
+
}
|
|
22
|
+
} else {
|
|
23
|
+
paths.push(arg);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (paths.length === 0) {
|
|
28
|
+
this.stdErr = 'touch: missing file operand';
|
|
29
|
+
this.exitCode = 1;
|
|
30
|
+
return Promise.resolve();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const now = new Date();
|
|
34
|
+
for (const path of paths) {
|
|
35
|
+
if (this.fs.exists(path)) {
|
|
36
|
+
// File exists — update timestamps on real FS; no-op on virtual FS
|
|
37
|
+
if (nodeFs && this.fs.isRealFilesystem) {
|
|
38
|
+
try {
|
|
39
|
+
nodeFs.utimesSync(this.fs.absolutePath(path), now, now);
|
|
40
|
+
} catch(e) {
|
|
41
|
+
this.stdErr = `touch: cannot touch '${path}': ${e.message}`;
|
|
42
|
+
this.exitCode = 1;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
if (noCreate) continue;
|
|
47
|
+
try {
|
|
48
|
+
this.fs.setFileByPath(path, '', true);
|
|
49
|
+
} catch(e) {
|
|
50
|
+
this.stdErr = `touch: cannot touch '${path}': ${e.message}`;
|
|
51
|
+
this.exitCode = 1;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return Promise.resolve();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = Touch;
|