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
|
#!/usr/bin/env bun
import { FFIType, linkSymbols, ptr, read, toArrayBuffer } from "bun:ffi";
import { unlinkSync, writeFileSync } from "node:fs";
import { unlink as fsUnlink, lstat, rmdir } from "node:fs/promises";
import { isAbsolute, basename as pathBasename, dirname as pathDirname } from "node:path";
import { changeDirectory, cstrPath, invalidOptionMessage, libc, libcErrno, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, parseOptions, pathDisplayName, pathLikeJoin, rawCommandArgs, shellEscapeLsName } from "../shared/common.js";
import { InvocationError, UsageError, stderr, stdout } from "../shared/diagnostics.js";
import { confirmRemoval, errnoMessage, isRecursiveRootTarget, isWriteProtected, lstatSyncNoThrow, preserveRootMessage, processCwdOrNull, rawArgLooksLikeOption, rmLibcError } from "../shared/filesystem.js";
import { defineCommand, runAsMain } from "../shared/command.js";
export const RM_LONG_OPTIONS = ["force", "interactive", "recursive", "dir", "verbose", "preserve-root", "no-preserve-root", "-presume-input-tty", "one-file-system", "help", "version"];
export function rmMetaOption(args) {
const longFlagOptions = new Set(["force", "recursive", "dir", "verbose", "no-preserve-root", "-presume-input-tty", "one-file-system"]);
for (const arg of args) {
if (arg === "--") return null;
rejectAbbreviatedRmNoPreserveRoot(arg);
let normalized = arg;
if (arg.startsWith("--")) normalized = normalizeLongOptionByPrefix(arg, RM_LONG_OPTIONS);
const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined];
if (name === "help" || name === "version") {
if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true);
return normalized;
}
if (longFlagOptions.has(name)) {
if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true);
continue;
}
if (name === "interactive") {
if (inlineValue !== undefined) validateRmInteractiveValue(inlineValue);
continue;
}
if (name === "preserve-root") {
if (inlineValue !== undefined) validateRmPreserveRootValue(inlineValue);
continue;
}
if (/^-[firdvIR]+$/.test(arg)) continue;
if (arg.startsWith("-")) throw rmInvalidOptionUsageError(arg, args);
continue;
}
return null;
}
export let rmDirectoryApi;
export function rmInterposedDirectoryApi() {
if (!process.env.LD_PRELOAD) {
return {
opendir: libc.symbols.opendir,
readdir: libc.symbols.readdir,
closedir: libc.symbols.closedir,
};
}
if (rmDirectoryApi) return rmDirectoryApi;
const definitions = {
opendir: { args: [FFIType.cstring], returns: FFIType.ptr },
readdir: { args: [FFIType.ptr], returns: FFIType.ptr },
closedir: { args: [FFIType.ptr], returns: FFIType.i32 },
};
const symbols = {};
for (const [name, definition] of Object.entries(definitions)) {
const address = libc.symbols.dlsym(0, ptr(Buffer.from(`${name}\0`)));
symbols[name] = { ptr: address, ...definition };
}
rmDirectoryApi = linkSymbols(symbols).symbols;
return rmDirectoryApi;
}
export function rmReadDirectoryEntries(path) {
return [...rmIterateDirectoryEntries(path)];
}
export function rmIterateDirectoryEntries(path) {
const api = rmInterposedDirectoryApi();
const directory = api.opendir(cstrPath(path));
if (!directory) throw rmLibcError(libcErrno());
return (function* () {
let yielded = 0;
try {
while (true) {
const errnoPointer = libc.symbols.__errno_location();
if (errnoPointer) libc.symbols.memset(errnoPointer, 0, 4);
const address = api.readdir(directory);
if (!address) {
const errno = libcErrno();
if (errno) {
const error = rmLibcError(errno);
error.rmPartialTraversal = yielded > 0;
throw error;
}
break;
}
// Linux struct dirent places d_name immediately after d_type at byte
// 19. Copy it before the next call reuses libc's dirent storage.
const recordLength = read.u16(address, 16);
const maximumNameLength = Math.max(0, recordLength - 19);
let nameLength = 0;
while (nameLength < maximumNameLength && read.u8(address + 19, nameLength) !== 0) nameLength++;
const name = Buffer.from(new Uint8Array(toArrayBuffer(address + 19, 0, nameLength)));
if (name.equals(Buffer.from(".")) || name.equals(Buffer.from(".."))) continue;
yielded++;
yield name;
}
} finally {
api.closedir(directory);
}
})();
}
export function rmPrimeDirectoryEntries(entries) {
const first = entries.next();
return (function* () {
if (!first.done) yield first.value;
yield* entries;
})();
}
export async function rmCmd(args) {
for (const arg of args) {
if (arg === "--") break;
rejectAbbreviatedRmNoPreserveRoot(arg);
}
args = normalizeRmArgs(args);
let parsed;
try {
parsed = parseOptions(args, { short: { f: false, i: false, I: false, r: false, R: false, d: false, v: false }, long: { force: false, interactive: "optional-value", recursive: false, dir: false, verbose: false, "preserve-root": "optional-value", "no-preserve-root": false, "-presume-input-tty": false, "one-file-system": false, help: false, version: false } });
} catch (error) {
const hint = rmDashFileHint(args);
if (hint && error instanceof UsageError && error.showHelp) throw new UsageError(`${error.message}\n${hint}`, true);
throw error;
}
const { opts, operands } = parsed;
validateRmInteractive(opts);
validateRmPreserveRoot(opts);
if (!operands.length) {
if (opts.f || opts.force) return 0;
throw new UsageError("missing operand", true);
}
const preserveRoot = !opts["no-preserve-root"];
const options = {
force: opts.f || opts.force,
recursive: opts.r || opts.R || opts.recursive,
dir: opts.d || opts.dir,
verbose: opts.v || opts.verbose,
preserveRoot,
preserveRootAll: preserveRoot && opts["preserve-root"] === "all",
oneFileSystem: opts["one-file-system"],
interactive: rmInteractiveMode(args, opts),
presumeInputTty: opts["-presume-input-tty"],
};
if (options.interactive === "once" && (operands.length > 3 || options.recursive)) {
const suffix = options.recursive ? " recursively" : "";
if (!confirmRemoval(`rm: remove ${operands.length} argument${operands.length === 1 ? "" : "s"}${suffix}? `)) return 0;
}
const rawOperands = rawRmOperands(operands);
let failed = false;
for (let i = 0; i < operands.length; i++) {
const target = rawOperands?.[i] ?? operands[i];
if (options.recursive && options.preserveRoot && await isRecursiveRootTarget(target)) {
stderr(preserveRootMessage("rm", operands[i]));
failed = true;
continue;
}
if (options.recursive && !options.preserveRoot && process.env.BNU_RM_TEST_INTERCEPT_FILE && await isRecursiveRootTarget(target)) {
rmTestInterceptRemoval();
}
const targetOptions = { ...options };
if (options.recursive && (options.oneFileSystem || options.preserveRootAll)) {
targetOptions.rootDev = await rmRootDevice(target);
if (targetOptions.preserveRootAll && targetOptions.rootDev != null && await rmIsDifferentDeviceFromParent(target, targetOptions.rootDev)) {
stderr(rmDifferentDeviceMessage(rmDisplayPath(operands[i])));
stderr("rm: and --preserve-root=all is in effect\n");
failed = true;
continue;
}
}
if (!await removePath(target, targetOptions, rmDisplayPath(operands[i]), true)) failed = true;
}
return failed ? 1 : 0;
}
export function rejectAbbreviatedRmNoPreserveRoot(arg) {
if (!arg.startsWith("--") || arg.includes("=")) return;
const name = arg.slice(2);
if (name !== "no-preserve-root" && "no-preserve-root".startsWith(name)) {
throw new InvocationError("you may not abbreviate the --no-preserve-root option", 1, false);
}
}
export function normalizeRmArgs(args) {
const out = [];
let end = false;
for (const arg of args) {
if (end || arg === "--") {
out.push(arg);
if (arg === "--") end = true;
continue;
}
out.push(arg.startsWith("--") ? normalizeLongOptionByPrefix(arg, RM_LONG_OPTIONS) : arg);
}
return out;
}
export function rawRmOperands(operands) {
if (!operands.some((operand) => operand.includes("\uFFFD"))) return null;
const raw = rawCommandArgs("rm");
if (!raw) return null;
const parsed = [];
for (let i = 0; i < raw.length; i++) {
const arg = raw[i];
if (arg.equals(Buffer.from("--"))) {
parsed.push(...raw.slice(i + 1));
break;
}
if (!rawArgLooksLikeOption(arg)) {
parsed.push(arg);
continue;
}
const text = arg.toString();
if (text === "--interactive" || text === "-I") continue;
if (text === "--") continue;
if (text.startsWith("--interactive=")) continue;
}
return parsed.length === operands.length ? parsed : null;
}
export function validateRmInteractive(opts) {
const mode = opts.interactive === true ? "always" : opts.interactive;
if (mode == null) return;
validateRmInteractiveValue(mode);
}
export function validateRmInteractiveValue(mode) {
if (!["always", "yes", "once", "no", "never", "none"].includes(mode)) {
const kind = mode === "" ? "ambiguous" : "invalid";
throw new UsageError(`${kind} argument ${localeQuotedEscapedDiagnostic(mode)} for ${localeQuotedDiagnostic("--interactive")}\nValid arguments are:\n - ${["never", "no", "none"].map(localeQuotedDiagnostic).join(", ")}\n - ${localeQuotedDiagnostic("once")}\n - ${["always", "yes"].map(localeQuotedDiagnostic).join(", ")}`, true);
}
}
export function validateRmPreserveRoot(opts) {
const value = opts["preserve-root"];
if (value == null || value === true) return;
validateRmPreserveRootValue(value);
}
export function validateRmPreserveRootValue(value) {
if (value !== "all") throw new UsageError(`unrecognized --preserve-root argument: '${value}'`);
}
export function rmInteractiveMode(args, opts) {
let mode = "never";
for (const arg of args) {
if (arg === "--") break;
if (arg === "-f" || arg === "--force") mode = "never";
else if (arg === "-i") mode = "always";
else if (arg === "-I") mode = "once";
else if (arg === "--interactive") mode = "always";
else if (arg.startsWith("--interactive=")) mode = arg.slice("--interactive=".length);
else if (/^-[^-]/.test(arg)) {
for (const ch of arg.slice(1)) {
if (ch === "f") mode = "never";
else if (ch === "i") mode = "always";
else if (ch === "I") mode = "once";
}
}
}
if ((opts.f || opts.force) && mode === "never") return "never";
return mode;
}
export async function removePath(path, options, display = path, isRoot = false) {
if (options.recursive && isDotOrDotDotPath(path)) {
stderr(`rm: refusing to remove '.' or '..' directory: skipping ${rmQuotedName(path)}\n`);
return false;
}
let s;
try {
s = await lstat(path);
} catch (error) {
if (options.force && (error?.code === "ENOENT" || error?.code === "ENOTDIR")) return true;
stderr(rmErrorLine(display, error));
return false;
}
if (s.isDirectory()) {
if (!options.recursive && !options.dir) {
stderr(`rm: cannot remove ${rmQuotedName(display)}: Is a directory\n`);
return false;
}
if (options.recursive && options.oneFileSystem && !isRoot && options.rootDev != null && s.dev !== options.rootDev) {
stderr(rmDifferentDeviceMessage(display));
return false;
}
if (options.recursive) {
if (isRoot && options.interactive === "always" && !confirmRemoval(`rm: descend into directory ${rmQuotedName(display)}? `)) return true;
let entries;
try {
entries = options.interactive === "always"
? rmReadDirectoryEntries(path)
: rmPrimeDirectoryEntries(rmIterateDirectoryEntries(path));
} catch (error) {
if (options.force && (error?.code === "EACCES" || error?.code === "EPERM")) {
try {
await rmdir(path);
if (options.verbose) stdout(`removed directory ${rmQuotedName(display)}\n`);
return true;
} catch {}
}
if (error.rmPartialTraversal) stderr(`rm: traversal failed: ${pathDisplayName(display)}: ${errnoMessage(error)}\n`);
else stderr(rmErrorLine(path, error));
return false;
}
if (!isRoot && entries.length > 0 && options.interactive === "always" && !confirmRemoval(`rm: descend into directory ${rmQuotedName(display)}? `)) return true;
let ok = true;
try {
const chdirName = rmRelativeTraversalName(path, isRoot);
if (chdirName != null) {
const restoreCwd = isRoot ? processCwdOrNull() : null;
try {
changeDirectory(chdirName);
} catch (error) {
stderr(rmErrorLine(display, error));
return false;
}
try {
for (const entry of entries) {
const childDisplay = `${display.replace(/\/+$/, "")}/${pathDisplayName(entry)}`;
if (rmFastUnlinkEntry(entry, options)) continue;
if (!await removePath(entry, options, childDisplay, false)) ok = false;
}
} finally {
changeDirectory(restoreCwd ?? "..");
}
} else {
for (const entry of entries) {
const child = pathLikeJoin(path, entry);
const childDisplay = `${display.replace(/\/+$/, "")}/${pathDisplayName(entry)}`;
if (rmFastUnlinkEntry(child, options)) continue;
if (!await removePath(child, options, childDisplay, false)) ok = false;
}
}
} catch (error) {
if (error.rmPartialTraversal) stderr(`rm: traversal failed: ${pathDisplayName(display)}: ${errnoMessage(error)}\n`);
else stderr(rmErrorLine(path, error));
return false;
}
if (!ok) return false;
}
if (shouldPromptForRemoval(options, s) && !confirmRemoval(`rm: ${rmDirectoryPromptAction(s)} ${rmQuotedName(display)}? `)) return true;
try {
rmTestInterceptRemoval();
await rmdir(path);
if (options.verbose) stdout(`removed directory ${rmQuotedName(display)}\n`);
return true;
} catch (error) {
stderr(rmErrorLine(display, error, error?.code !== "ENOTEMPTY"));
return false;
}
}
if (shouldPromptForRemoval(options, s) && !confirmRemoval(`rm: remove ${rmPromptDescription(s)} ${rmQuotedName(display)}? `)) return true;
try {
rmTestInterceptRemoval();
await fsUnlink(path);
if (options.verbose) stdout(`removed ${rmQuotedName(display)}\n`);
return true;
} catch (error) {
stderr(rmErrorLine(display, error));
return false;
}
}
export function rmFastUnlinkEntry(path, options) {
rmTestInterceptRemoval();
if (process.env.LD_PRELOAD || options.verbose || options.interactive === "always" || options.presumeInputTty) return false;
try {
unlinkSync(path);
return true;
} catch {
// Directories, mount points, immutable files, and permission failures
// require the full metadata-aware recursive/diagnostic path below.
return false;
}
}
export function rmTestInterceptRemoval() {
const evidence = process.env.BNU_RM_TEST_INTERCEPT_FILE;
if (!evidence) return;
writeFileSync(evidence, "removal attempted\n");
process.exit(0);
}
export async function rmRootDevice(path) {
try {
return (await lstat(path)).dev;
} catch {
return null;
}
}
export async function rmIsDifferentDeviceFromParent(path, rootDev) {
const parent = pathDirname(String(path).replace(/\/+$/, "") || ".");
try {
return (await lstat(parent)).dev !== rootDev;
} catch {
return false;
}
}
export function rmDifferentDeviceMessage(path) {
return `rm: skipping ${rmQuotedName(path)}, since it's on a different device\n`;
}
export function rmFileTypeDescription(s) {
if (s.isSymbolicLink()) return "symbolic link";
if (s.isFIFO()) return "fifo";
if (s.isDirectory()) return "directory";
return s.size === 0 ? "regular empty file" : "regular file";
}
export function rmPromptDescription(s) {
const type = rmFileTypeDescription(s);
return s.isSymbolicLink() || (s.mode & 0o200) ? type : `write-protected ${type}`;
}
export function shouldPromptForRemoval(options, s) {
if (options.interactive === "always") return true;
if (options.presumeInputTty && isWriteProtected(s)) return true;
return options.interactive === "once" && isWriteProtected(s);
}
export function rmDirectoryPromptAction(s) {
if (!(s.mode & 0o500)) return "attempt removal of inaccessible directory";
return `remove ${rmPromptDescription(s)}`;
}
export function rmDisplayPath(path) {
return String(path).replace(/\/+$/, "/");
}
export function rmRelativeTraversalName(path, isRoot = false) {
const text = pathDisplayName(path);
if (isRoot && isAbsolute(text)) return null;
if (text.includes("\uFFFD") || /[\uDC80-\uDCFF]/.test(text) || text === "" || text === "." || text === "..") return null;
return text;
}
export function isDotOrDotDotPath(path) {
const stripped = String(path).replace(/\/+$/, "");
const base = pathBasename(stripped);
return base === "." || base === "..";
}
export function rmErrorLine(path, error, directory = false) {
const action = directory ? "cannot remove directory" : "cannot remove";
return `rm: ${action} ${rmQuotedName(path)}: ${errnoMessage(error)}\n`;
}
export function rmQuotedName(path) {
return shellEscapeLsName(pathDisplayName(path), true);
}
export function rmInvalidOptionUsageError(arg, args) {
const message = invalidOptionMessage(rmInvalidOptionArg(arg));
const hint = rmDashFileHint(args);
return new UsageError(hint ? `${message}\n${hint}` : message, true);
}
export function rmInvalidOptionArg(arg) {
if (arg.startsWith("--")) return arg;
for (const ch of arg.slice(1)) {
if (!"firdvIR".includes(ch)) return `-${ch}`;
}
return arg;
}
export function rmDashFileHint(args) {
for (const arg of args) {
if (!arg.startsWith("-") || arg === "-" || arg === "--") continue;
if (!lstatSyncNoThrow(arg)) continue;
return `Try 'rm ./${rmShellEscapeCommandPath(arg)}' to remove the file ${rmShellQuote(arg)}.`;
}
return "";
}
export function rmShellEscapeCommandPath(path) {
return /^[A-Za-z0-9_./=-]+$/.test(path) ? path : rmShellQuote(path);
}
export function rmShellQuote(value) {
const text = String(value);
if (!/[^A-Za-z0-9_./=-]/.test(text)) return `'${text}'`;
return `'${text.replace(/'/g, "'\\''").replace(/\n/g, () => "'$'\\n''")}'`;
}
const singleCall = defineCommand("rm", rmCmd, rmMetaOption);
export default singleCall;
if (import.meta.main) await runAsMain(singleCall);
|