This commit is contained in:
@@ -311,6 +311,132 @@ function bareOsHexEncode(u8) {
|
||||
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 }
|
||||
}
|
||||
|
||||
async function readProcJson(ctx, p) {
|
||||
const vfs = ctx.vfs
|
||||
if (!vfs || typeof vfs.readFile !== 'function') return null
|
||||
|
||||
Reference in New Issue
Block a user