Expose /proc/bare_os/metrics.prom (kernel counter OpenMetrics) and /proc/bare_os/protomux.json (alias registry snapshot). Extend VFS routing, readdir, pseudo watch, and bare_os_proc_index to schema 8; restore flat /proc entries for syscalls and process_table with sort order matching tests. Add ctx.bareOsAuditLogAppendBatch and bump BARE_OS_CTX_API_VERSION to 1.30.0 with d.ts and compatibility-matrix updates. Harden swarm peer ban backoff with jitter on the exponential window. Refresh conformance matrix, environment appendix, booter package doc, and handbook chapters for proc paths, param expansion V3, vault at rest, and baretop snapshot keys. Align baretop-snapshot with bareOsReadBareTopSnapshot.
210 lines
5.2 KiB
Plaintext
210 lines
5.2 KiB
Plaintext
/* 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 === '-l' || a === '--list') {
|
|
await printMountTable(ctx)
|
|
return
|
|
}
|
|
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
|
|
}
|
|
}
|