Further Cleanup

This commit is contained in:
2026-08-18 16:43:40 -04:00
parent 45a99e5fde
commit 7e21c47b0f
92 changed files with 16037 additions and 17662 deletions
+126
View File
@@ -310,3 +310,129 @@ function bareOsHexEncode(u8) {
for (let i = 0; i < u8.length; i++) s += u8[i].toString(16).padStart(2, '0')
return s
}
/**
* Shared GNU-style checksum FILE / stdin loop used by *sum commands.
* @param {Record<string, unknown>} ctx
* @param {string[]} argv
* @param {{
* cmd: string,
* help?: string,
* digest: (u8: Uint8Array) => string | Promise<string>
* }} opts
*/
async function bareChecksumRun(ctx, argv, opts) {
const cmd = String(opts.cmd || 'checksum')
const help =
opts.help ||
'usage: ' +
cmd +
' [FILE]...\nWith no FILE, or when FILE is -, read standard input.'
const digest = opts.digest
const paths = []
for (let i = 1; i < argv.length; i++) {
const a = argv[i]
if (a === '-h' || a === '--help') {
ctx.console.log(help)
ctx.exitCode = 0
return
}
if (a.startsWith('-') && a !== '-') {
ctx.console.error(cmd + ': unsupported option ' + a)
ctx.exitCode = 1
return
}
paths.push(a)
}
const b4 = ctx.b4a
async function one(name, buf) {
const u8 = buf instanceof Uint8Array ? buf : new Uint8Array(buf)
try {
const hex = await digest(u8)
ctx.console.log(hex + ' ' + name)
} catch (e) {
ctx.console.error(cmd + ': ' + (e.message || e))
ctx.exitCode = 1
}
}
if (!paths.length || (paths.length === 1 && paths[0] === '-')) {
await one('-', b4.from(bareStdin(ctx)))
return
}
for (const p of paths) {
if (p === '-') {
await one('-', b4.from(bareStdin(ctx)))
continue
}
const b = await ctx.vfs.readFile(p)
if (!b) {
ctx.console.error(cmd + ': ' + p + ': No such file')
ctx.exitCode = 1
continue
}
await one(p, b)
}
}
/**
* WebCrypto hex digest; throws the same missing-subtle message as the old *sum files.
* @param {string} algo
* @param {string} missingMsg
* @returns {(u8: Uint8Array) => Promise<string>}
*/
function bareSubtleHexDigest(algo, missingMsg) {
return async function (u8) {
const subtle = globalThis.crypto?.subtle
if (!subtle || typeof subtle.digest !== 'function') {
throw new Error(missingMsg)
}
const hash = await subtle.digest(algo, u8)
return bareOsHexEncode(new Uint8Array(hash))
}
}
/**
* Owner/group name → uid/gid used by chown / chgrp.
* @param {string} spec
* @param {Record<string, string>} env
* @returns {number | null}
*/
function parseGroupSpec(spec, env) {
const s = String(spec).trim()
if (!s) return null
if (/^\d+$/.test(s)) {
const n = Number.parseInt(s, 10)
return Number.isFinite(n) ? n : null
}
const g = (env.GROUP || env.USER || 'guest').trim()
if (s === 'root') return 0
if (s === 'guest' || s === 'nobody') return 65534
if (g && s === g) {
const gid = Number.parseInt(String(env.GID ?? '65534'), 10)
return Number.isFinite(gid) ? gid : 65534
}
return null
}
/**
* User name / numeric uid used by chown.
* @param {string} spec
* @param {Record<string, string>} env
* @returns {{ uid: number | null, gid: number | null }}
*/
function parseIdSpec(spec, env) {
const s = String(spec).trim()
if (!s) return { uid: null, gid: null }
if (/^\d+$/.test(s)) {
const n = Number.parseInt(s, 10)
return { uid: Number.isFinite(n) ? n : null, gid: null }
}
const u = (env.USER || env.LOGNAME || '').trim()
if (s === 'root') return { uid: 0, gid: null }
if (s === 'guest' || s === 'nobody') return { uid: 65534, gid: null }
if (u && s === u) {
const uid = Number.parseInt(String(env.UID ?? '65534'), 10)
return { uid: Number.isFinite(uid) ? uid : 65534, gid: null }
}
return { uid: null, gid: null }
}
-22
View File
@@ -2,28 +2,6 @@
* chgrp — change file group (Hyperdrive metadata on writable paths).
*/
/**
* @param {string} spec
* @param {Record<string, string>} env
* @returns {number | null}
*/
function parseGroupSpec(spec, env) {
const s = String(spec).trim()
if (!s) return null
if (/^\d+$/.test(s)) {
const n = Number.parseInt(s, 10)
return Number.isFinite(n) ? n : null
}
const g = (env.GROUP || env.USER || 'guest').trim()
if (s === 'root') return 0
if (s === 'guest' || s === 'nobody') return 65534
if (g && s === g) {
const gid = Number.parseInt(String(env.GID ?? '65534'), 10)
return Number.isFinite(gid) ? gid : 65534
}
return null
}
async function run(ctx, argv) {
let i = 1
const files = []
-44
View File
@@ -2,50 +2,6 @@
* chown — change file owner and group (Hyperdrive metadata on writable paths).
*/
/**
* @param {string} spec
* @param {Record<string, string>} env
* @returns {{ uid: number | null, gid: number | null }}
*/
function parseIdSpec(spec, env) {
const s = String(spec).trim()
if (!s) return { uid: null, gid: null }
if (/^\d+$/.test(s)) {
const n = Number.parseInt(s, 10)
return { uid: Number.isFinite(n) ? n : null, gid: null }
}
const u = (env.USER || env.LOGNAME || '').trim()
if (s === 'root') return { uid: 0, gid: null }
if (s === 'guest' || s === 'nobody') return { uid: 65534, gid: null }
if (u && s === u) {
const uid = Number.parseInt(String(env.UID ?? '65534'), 10)
return { uid: Number.isFinite(uid) ? uid : 65534, gid: null }
}
return { uid: null, gid: null }
}
/**
* @param {string} spec
* @param {Record<string, string>} env
* @returns {number | null}
*/
function parseGroupSpec(spec, env) {
const s = String(spec).trim()
if (!s) return null
if (/^\d+$/.test(s)) {
const n = Number.parseInt(s, 10)
return Number.isFinite(n) ? n : null
}
const g = (env.GROUP || env.USER || 'guest').trim()
if (s === 'root') return 0
if (s === 'guest' || s === 'nobody') return 65534
if (g && s === g) {
const gid = Number.parseInt(String(env.GID ?? '65534'), 10)
return Number.isFinite(gid) ? gid : 65534
}
return null
}
async function run(ctx, argv) {
let i = 1
const files = []
+7 -46
View File
@@ -1,48 +1,9 @@
async function run(ctx, argv) {
const paths = []
for (let i = 1; i < argv.length; i++) {
const a = argv[i]
if (a === '-h' || a === '--help') {
ctx.console.log(
'usage: md5sum [FILE]...\n' +
'With no FILE, or when FILE is -, read standard input. Uses bundled MD5 (no Web Crypto MD5).'
)
ctx.exitCode = 0
return
}
if (a.startsWith('-') && a !== '-') {
ctx.console.error('md5sum: unsupported option ' + a)
ctx.exitCode = 1
return
}
paths.push(a)
}
const b4 = ctx.b4a
function one(name, buf) {
const u8 = buf instanceof Uint8Array ? buf : new Uint8Array(buf)
try {
const hex = bareMd5DigestBytes(u8)
ctx.console.log(hex + ' ' + name)
} catch (e) {
ctx.console.error('md5sum: ' + (e.message || e))
ctx.exitCode = 1
}
}
if (!paths.length || (paths.length === 1 && paths[0] === '-')) {
one('-', b4.from(bareStdin(ctx)))
return
}
for (const p of paths) {
if (p === '-') {
one('-', b4.from(bareStdin(ctx)))
continue
}
const b = await ctx.vfs.readFile(p)
if (!b) {
ctx.console.error('md5sum: ' + p + ': No such file')
ctx.exitCode = 1
continue
}
one(p, b)
}
await bareChecksumRun(ctx, argv, {
cmd: 'md5sum',
help:
'usage: md5sum [FILE]...\n' +
'With no FILE, or when FILE is -, read standard input. Uses bundled MD5 (no Web Crypto MD5).',
digest: (u8) => bareMd5DigestBytes(u8)
})
}
+7 -57
View File
@@ -1,59 +1,9 @@
async function sha1Hex(u8) {
const subtle = globalThis.crypto?.subtle
if (!subtle || typeof subtle.digest !== 'function') {
throw new Error('crypto.subtle.digest (SHA-1) is not available')
}
const hash = await subtle.digest('SHA-1', u8)
return [...new Uint8Array(hash)]
.map((b) => b.toString(16).padStart(2, '0'))
.join('')
}
async function run(ctx, argv) {
const paths = []
for (let i = 1; i < argv.length; i++) {
const a = argv[i]
if (a === '-h' || a === '--help') {
ctx.console.log(
'usage: sha1sum [FILE]...\n' +
'With no FILE, or when FILE is -, read standard input.'
)
ctx.exitCode = 0
return
}
if (a.startsWith('-') && a !== '-') {
ctx.console.error('sha1sum: unsupported option ' + a)
ctx.exitCode = 1
return
}
paths.push(a)
}
const b4 = ctx.b4a
async function one(name, buf) {
const u8 = buf instanceof Uint8Array ? buf : new Uint8Array(buf)
try {
const hex = await sha1Hex(u8)
ctx.console.log(hex + ' ' + name)
} catch (e) {
ctx.console.error('sha1sum: ' + (e.message || e))
ctx.exitCode = 1
}
}
if (!paths.length || (paths.length === 1 && paths[0] === '-')) {
await one('-', b4.from(bareStdin(ctx)))
return
}
for (const p of paths) {
if (p === '-') {
await one('-', b4.from(bareStdin(ctx)))
continue
}
const b = await ctx.vfs.readFile(p)
if (!b) {
ctx.console.error('sha1sum: ' + p + ': No such file')
ctx.exitCode = 1
continue
}
await one(p, b)
}
await bareChecksumRun(ctx, argv, {
cmd: 'sha1sum',
digest: bareSubtleHexDigest(
'SHA-1',
'crypto.subtle.digest (SHA-1) is not available'
)
})
}
+4 -46
View File
@@ -3,50 +3,8 @@
* Uses bundled SHA-224 (many runtimes omit SHA-224 in crypto.subtle.digest).
*/
async function run(ctx, argv) {
const paths = []
for (let i = 1; i < argv.length; i++) {
const a = argv[i]
if (a === '-h' || a === '--help') {
ctx.console.log(
'usage: sha224sum [FILE]...\n' +
'With no FILE, or when FILE is -, read standard input.'
)
ctx.exitCode = 0
return
}
if (a.startsWith('-') && a !== '-') {
ctx.console.error('sha224sum: unsupported option ' + a)
ctx.exitCode = 1
return
}
paths.push(a)
}
const b4 = ctx.b4a
function one(name, buf) {
const u8 = buf instanceof Uint8Array ? buf : new Uint8Array(buf)
try {
const hex = bareSha224DigestBytes(u8)
ctx.console.log(hex + ' ' + name)
} catch (e) {
ctx.console.error('sha224sum: ' + (e.message || e))
ctx.exitCode = 1
}
}
if (!paths.length || (paths.length === 1 && paths[0] === '-')) {
one('-', b4.from(bareStdin(ctx)))
return
}
for (const p of paths) {
if (p === '-') {
one('-', b4.from(bareStdin(ctx)))
continue
}
const b = await ctx.vfs.readFile(p)
if (!b) {
ctx.console.error('sha224sum: ' + p + ': No such file')
ctx.exitCode = 1
continue
}
one(p, b)
}
await bareChecksumRun(ctx, argv, {
cmd: 'sha224sum',
digest: (u8) => bareSha224DigestBytes(u8)
})
}
+7 -58
View File
@@ -1,63 +1,12 @@
/**
* sha256sum — compute SHA-256 checksums (hex), GNU-like output line.
*/
async function sha256Hex(u8) {
const subtle = globalThis.crypto?.subtle
if (!subtle || typeof subtle.digest !== 'function') {
throw new Error('crypto.subtle.digest (SHA-256) is not available')
}
const hash = await subtle.digest('SHA-256', u8)
return [...new Uint8Array(hash)]
.map((b) => b.toString(16).padStart(2, '0'))
.join('')
}
async function run(ctx, argv) {
const paths = []
for (let i = 1; i < argv.length; i++) {
const a = argv[i]
if (a === '-h' || a === '--help') {
ctx.console.log(
'usage: sha256sum [FILE]...\n' +
'With no FILE, or when FILE is -, read standard input.'
)
ctx.exitCode = 0
return
}
if (a.startsWith('-') && a !== '-') {
ctx.console.error('sha256sum: unsupported option ' + a)
ctx.exitCode = 1
return
}
paths.push(a)
}
const b4 = ctx.b4a
async function one(name, buf) {
const u8 = buf instanceof Uint8Array ? buf : new Uint8Array(buf)
try {
const hex = await sha256Hex(u8)
ctx.console.log(hex + ' ' + name)
} catch (e) {
ctx.console.error('sha256sum: ' + (e.message || e))
ctx.exitCode = 1
}
}
if (!paths.length || (paths.length === 1 && paths[0] === '-')) {
const buf = b4.from(bareStdin(ctx))
await one('-', buf)
return
}
for (const p of paths) {
if (p === '-') {
await one('-', b4.from(bareStdin(ctx)))
continue
}
const b = await ctx.vfs.readFile(p)
if (!b) {
ctx.console.error('sha256sum: ' + p + ': No such file')
ctx.exitCode = 1
continue
}
await one(p, b)
}
await bareChecksumRun(ctx, argv, {
cmd: 'sha256sum',
digest: bareSubtleHexDigest(
'SHA-256',
'crypto.subtle.digest (SHA-256) is not available'
)
})
}
+7 -57
View File
@@ -1,59 +1,9 @@
async function sha384Hex(u8) {
const subtle = globalThis.crypto?.subtle
if (!subtle || typeof subtle.digest !== 'function') {
throw new Error('crypto.subtle.digest (SHA-384) is not available')
}
const hash = await subtle.digest('SHA-384', u8)
return [...new Uint8Array(hash)]
.map((b) => b.toString(16).padStart(2, '0'))
.join('')
}
async function run(ctx, argv) {
const paths = []
for (let i = 1; i < argv.length; i++) {
const a = argv[i]
if (a === '-h' || a === '--help') {
ctx.console.log(
'usage: sha384sum [FILE]...\n' +
'With no FILE, or when FILE is -, read standard input.'
)
ctx.exitCode = 0
return
}
if (a.startsWith('-') && a !== '-') {
ctx.console.error('sha384sum: unsupported option ' + a)
ctx.exitCode = 1
return
}
paths.push(a)
}
const b4 = ctx.b4a
async function one(name, buf) {
const u8 = buf instanceof Uint8Array ? buf : new Uint8Array(buf)
try {
const hex = await sha384Hex(u8)
ctx.console.log(hex + ' ' + name)
} catch (e) {
ctx.console.error('sha384sum: ' + (e.message || e))
ctx.exitCode = 1
}
}
if (!paths.length || (paths.length === 1 && paths[0] === '-')) {
await one('-', b4.from(bareStdin(ctx)))
return
}
for (const p of paths) {
if (p === '-') {
await one('-', b4.from(bareStdin(ctx)))
continue
}
const b = await ctx.vfs.readFile(p)
if (!b) {
ctx.console.error('sha384sum: ' + p + ': No such file')
ctx.exitCode = 1
continue
}
await one(p, b)
}
await bareChecksumRun(ctx, argv, {
cmd: 'sha384sum',
digest: bareSubtleHexDigest(
'SHA-384',
'crypto.subtle.digest (SHA-384) is not available'
)
})
}
+7 -57
View File
@@ -1,59 +1,9 @@
async function sha512Hex(u8) {
const subtle = globalThis.crypto?.subtle
if (!subtle || typeof subtle.digest !== 'function') {
throw new Error('crypto.subtle.digest (SHA-512) is not available')
}
const hash = await subtle.digest('SHA-512', u8)
return [...new Uint8Array(hash)]
.map((b) => b.toString(16).padStart(2, '0'))
.join('')
}
async function run(ctx, argv) {
const paths = []
for (let i = 1; i < argv.length; i++) {
const a = argv[i]
if (a === '-h' || a === '--help') {
ctx.console.log(
'usage: sha512sum [FILE]...\n' +
'With no FILE, or when FILE is -, read standard input.'
)
ctx.exitCode = 0
return
}
if (a.startsWith('-') && a !== '-') {
ctx.console.error('sha512sum: unsupported option ' + a)
ctx.exitCode = 1
return
}
paths.push(a)
}
const b4 = ctx.b4a
async function one(name, buf) {
const u8 = buf instanceof Uint8Array ? buf : new Uint8Array(buf)
try {
const hex = await sha512Hex(u8)
ctx.console.log(hex + ' ' + name)
} catch (e) {
ctx.console.error('sha512sum: ' + (e.message || e))
ctx.exitCode = 1
}
}
if (!paths.length || (paths.length === 1 && paths[0] === '-')) {
await one('-', b4.from(bareStdin(ctx)))
return
}
for (const p of paths) {
if (p === '-') {
await one('-', b4.from(bareStdin(ctx)))
continue
}
const b = await ctx.vfs.readFile(p)
if (!b) {
ctx.console.error('sha512sum: ' + p + ': No such file')
ctx.exitCode = 1
continue
}
await one(p, b)
}
await bareChecksumRun(ctx, argv, {
cmd: 'sha512sum',
digest: bareSubtleHexDigest(
'SHA-512',
'crypto.subtle.digest (SHA-512) is not available'
)
})
}
+1
View File
@@ -10,3 +10,4 @@ async function run(ctx, argv) {
const msg = argv.slice(1).join(' ') || 'y'
const cap = bareOsEnvInt(ctx, 'BARE_OS_YES_MAX_LINES', 50000, 1, 200000)
for (let n = 0; n < cap; n++) ctx.console.log(msg)
}