Files
bare-operating-system/packages/bare-os-seeder/kernel/bin/rm
T
2026-04-03 04:01:24 -04:00

66 lines
1.6 KiB
Plaintext

/** Shared helpers for drive-resident /bin scripts (prepended before each command). */
function bareStdin(ctx) {
return typeof ctx.shellStdin === 'string' ? ctx.shellStdin : ''
}
/**
* rm — remove files or directories.
* Flags: -r -R --recursive, -f --force, -- ; bundled e.g. -rf
*/
async function run(ctx, argv) {
const vfs = ctx.vfs
let recursive = false
let force = false
const files = []
let dash = false
for (let i = 1; i < argv.length; i++) {
const a = argv[i]
if (dash) {
files.push(a)
continue
}
if (a === '--') {
dash = true
continue
}
if (a === '--recursive' || a === '-r' || a === '-R') {
recursive = true
continue
}
if (a === '--force' || a === '-f') {
force = true
continue
}
if (a.startsWith('-') && a.length > 1) {
for (let j = 1; j < a.length; j++) {
const c = a[j]
if (c === 'r' || c === 'R') recursive = true
else if (c === 'f') force = true
}
continue
}
files.push(a)
}
if (!files.length) {
ctx.console.error('rm: missing operand')
ctx.exitCode = 1
return
}
const doRm =
vfs && typeof vfs.rm === 'function'
? (p) => vfs.rm(p, { recursive, force })
: async (p) => {
if (recursive) throw new Error('recursive rm not supported')
return vfs.unlink(p)
}
for (const f of files) {
try {
await doRm(f)
} catch (e) {
if (force) continue
ctx.console.error('rm: ' + f + ': ' + (e.message || e))
ctx.exitCode = 1
}
}
}