This commit is contained in:
Raven Scott
2026-04-04 19:08:51 -04:00
parent 92368de550
commit b458c6031d
61 changed files with 4669 additions and 446 deletions
+205
View File
@@ -0,0 +1,205 @@
/* BARE_OS_BIN_API 1.0.0 — bump when staged /bin script semantics change (see developer guide). */
/** Shared helpers for drive-resident /bin scripts (prepended before each command). */
function bareStdin(ctx) {
return typeof ctx.shellStdin === 'string' ? ctx.shellStdin : ''
}
/** @param {number} mode @param {'file' | 'directory' | 'symlink'} type */
function bareFormatModeString(mode, type) {
const typeChar = type === 'directory' ? 'd' : type === 'symlink' ? 'l' : '-'
const perm = mode & 0o777
const r = (bit) => (perm & bit ? 'r' : '-')
const w = (bit) => (perm & bit ? 'w' : '-')
const x = (bit) => (perm & bit ? 'x' : '-')
return (
typeChar +
r(0o400) +
w(0o200) +
x(0o100) +
r(0o040) +
w(0o020) +
x(0o010) +
r(0o004) +
w(0o002) +
x(0o001)
)
}
/** @param {number} mtimeMs @param {number} [nowMs] */
function bareFormatLsMtime(mtimeMs, nowMs) {
const now = nowMs != null ? nowMs : Date.now()
const d = new Date(mtimeMs)
const months = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
const mon = months[d.getMonth()]
const day = String(d.getDate()).padStart(2, ' ')
const sixMo = 180 * 24 * 3600 * 1000
if (Math.abs(now - mtimeMs) > sixMo) {
const yr = String(d.getFullYear()).padStart(4, ' ')
return mon + ' ' + day + ' ' + yr
}
const hh = String(d.getHours()).padStart(2, '0')
const mm = String(d.getMinutes()).padStart(2, '0')
return mon + ' ' + day + ' ' + hh + ':' + mm
}
/** @param {number} size */
function barePosixBlocks(size) {
return Math.ceil(Number(size) / 512) || 0
}
/**
* Raw stdout for NUL/binary when **`process.stdout.write`** is missing.
* If **`ctx.bareOsBinWrite(Uint8Array|string)`** is set (tests / host), use it.
* @param {Record<string, unknown>} ctx
* @param {string | Uint8Array} chunk
* @returns {boolean}
*/
function bareOsEmitRaw(ctx, chunk) {
if (typeof ctx.bareOsBinWrite === 'function') {
const b4 = ctx.b4a
const u8 =
typeof chunk === 'string'
? b4 && typeof b4.from === 'function'
? b4.from(chunk)
: new TextEncoder().encode(chunk)
: chunk
ctx.bareOsBinWrite(u8 instanceof Uint8Array ? u8 : new Uint8Array(u8))
return true
}
const w = globalThis.process?.stdout?.write
if (typeof w === 'function') {
w.call(globalThis.process.stdout, chunk)
return true
}
return false
}
async function printMountTable(ctx) {
try {
const buf = await ctx.vfs.readFile('/proc/mounts')
if (buf) {
const s = ctx.b4a.toString(buf)
if (s) {
ctx.console.log(s.endsWith('\n') ? s.slice(0, -1) : s)
return
}
}
} catch {
/* fall through */
}
ctx.console.log('(no mounts)')
}
async function run(ctx, argv) {
const args = argv.slice(1)
if (!args.length) {
await printMountTable(ctx)
return
}
let type = 'hyperdrive'
let opts = ''
/** @type {string[]} */
const pos = []
for (let i = 0; i < args.length; i++) {
const a = args[i]
if (a === '-t') {
i++
if (i >= args.length) {
ctx.console.error('mount: option requires an argument -- t')
ctx.exitCode = 1
return
}
type = String(args[i] || '')
continue
}
if (a === '-o') {
i++
if (i >= args.length) {
ctx.console.error('mount: option requires an argument -- o')
ctx.exitCode = 1
return
}
opts = String(args[i] || '')
continue
}
if (a.startsWith('-')) {
ctx.console.error('mount: unsupported option ' + a)
ctx.exitCode = 1
return
}
pos.push(a)
}
if (type !== 'hyperdrive') {
ctx.console.error('mount: only -t hyperdrive is supported')
ctx.exitCode = 1
return
}
if (pos.length < 2) {
ctx.console.error(
'usage: mount [-t hyperdrive] [-o ro] <source-key|local> </mnt/label>'
)
ctx.exitCode = 1
return
}
const source = String(pos[0] || '').trim()
const target = String(pos[1] || '').trim()
const m = /^\/mnt\/([a-zA-Z0-9][a-zA-Z0-9._-]{0,62})$/.exec(target)
if (!m) {
ctx.console.error('mount: target must be /mnt/<label>')
ctx.exitCode = 1
return
}
const label = m[1]
const ro = opts
.split(',')
.map((x) => x.trim())
.filter(Boolean)
.includes('ro')
if (typeof ctx.bareOsSyscall === 'function') {
try {
const out = await ctx.bareOsSyscall('mount', {
source,
target,
label,
readOnly: ro
})
if (out && typeof out === 'object' && out.note) {
ctx.console.log(String(out.note))
}
return
} catch (e) {
ctx.console.error('mount: ' + (e?.message || String(e)))
ctx.exitCode = 1
return
}
}
if (typeof ctx.runHdms !== 'function') {
ctx.console.error('mount: hdms bridge unavailable')
ctx.exitCode = 1
return
}
try {
if (source === 'local') await ctx.runHdms(['hdms', 'create', label])
else await ctx.runHdms(['hdms', 'add', label, source])
} catch (e) {
ctx.console.error('mount: ' + (e?.message || String(e)))
ctx.exitCode = 1
}
}