Files
bare-operating-system/kernel/bin/pkg-swarm-index
T
Raven Scott 071edccfb3 Expand bounded awk/expr/test toward Issue 7; refresh man, profile 1.0.18,
posix matrix/dashboard, and syscalls/process_table schema alignment (v8).

Booter: replication_operator_sketch/corestore hints, HRPC allowlist tests,
Protomux cap channel 65536-byte bound + export, Wasm posix_profile_peek,
swarm-disk and security_posture docs.

Coreutils/kernel: pkg-swarm-index pathCapabilityEnvelopeVerify on get;
pathcap-verify --trusted failure hint; rebuild bins and sync seeder.

Docs: KERNEL_CONTRACT, kernel-extensions, capabilities index, environment
appendix (warm-cache tuning, cap channel, Wasm env), handbook observability,
vault threat model (multisig), developer-guide ctx/HRPC/Wasm, DOCUMENTATION
release-checklist note, release-checklist optional tier1 drift.

Changelog maintenance in bare-os-booter and bare-os-protocol.
2026-04-05 23:01:54 -04:00

228 lines
6.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
}
/**
* pkg-swarm-index — P2P package index: reads /etc/bare-os/pkg-index.json from VFS
* or uses ctx.bareOsHrpcRequest('bare_os','pkg_index_get', { key }) when available.
*/
async function readPkgIndexFromVfs(ctx) {
const buf = await ctx.vfs.readFile('/etc/bare-os/pkg-index.json')
if (!buf || !buf.byteLength) return null
return JSON.parse(ctx.b4a.toString(buf))
}
/**
* @param {Record<string, unknown>} ctx
* @param {string} key
*/
async function pkgIndexLookup(ctx, key) {
if (typeof ctx.bareOsHrpcRequest === 'function') {
try {
const r = await ctx.bareOsHrpcRequest('bare_os', 'pkg_index_get', {
key: key || ''
})
if (r && r.ok && r.json && typeof r.json === 'object') return r.json
} catch {
/* fall through */
}
}
const j = await readPkgIndexFromVfs(ctx)
if (!j || typeof j !== 'object') {
return { ok: false, reason: 'no_index' }
}
const pkgs =
j.packages && typeof j.packages === 'object'
? /** @type {Record<string, unknown>} */ (j.packages)
: {}
const k = String(key || '').trim()
if (!k) {
return {
ok: true,
path: '/etc/bare-os/pkg-index.json',
keys: Object.keys(pkgs).slice(0, 512)
}
}
const ent = Object.prototype.hasOwnProperty.call(pkgs, k) ? pkgs[k] : null
return {
ok: ent != null,
path: '/etc/bare-os/pkg-index.json',
key: k,
entry: ent
}
}
async function run(ctx, argv) {
const topic = String(
(ctx.env && ctx.env.BARE_OS_PKG_SWARM_TOPIC_HEX) || ''
).trim()
let sub = 'help'
let keyArg = ''
for (let i = 1; i < argv.length; i++) {
const a = argv[i]
if (a === '--help' || a === '-h') {
sub = 'help'
break
}
if (a === 'list' || a === 'get') {
sub = a
continue
}
if (!a.startsWith('-') && !keyArg) {
keyArg = a
continue
}
ctx.console.error('pkg-swarm-index: unknown option ' + a)
ctx.exitCode = 1
return
}
if (sub === 'help' || argv.length < 2) {
ctx.console.log(`pkg-swarm-index — P2P package index (drive manifest + HRPC)
usage:
pkg-swarm-index list # list package keys (VFS or bare_os.pkg_index_get)
pkg-swarm-index get <name@ver> # one entry
pkg-swarm-index --help
Environment:
BARE_OS_PKG_SWARM_TOPIC_HEX — optional 64-hex topic class for index peers.
Manifest path override on host: BARE_OS_PKG_INDEX_PATH (disk.os + HRPC).
See kernel/etc/bare-os/pkg-index.example.json and handbook ch.9.
${topic ? 'Topic pin prefix: ' + topic.slice(0, 16) + '…' : 'Topic pin unset.'}
`)
return
}
try {
if (sub === 'list') {
const r = await pkgIndexLookup(ctx, '')
ctx.console.log(JSON.stringify(r, null, 2))
return
}
if (sub === 'get') {
if (!keyArg) {
ctx.console.error('pkg-swarm-index: get requires name@version')
ctx.exitCode = 1
return
}
const r = await pkgIndexLookup(ctx, keyArg)
const ent =
r && typeof r === 'object' && r.entry && typeof r.entry === 'object'
? r.entry
: null
if (
ent &&
ent.pathCapabilityEnvelope != null &&
typeof ctx.bareOsVerifyPathCapabilityEnvelope === 'function'
) {
try {
r.pathCapabilityEnvelopeVerify =
ctx.bareOsVerifyPathCapabilityEnvelope(
ent.pathCapabilityEnvelope
)
} catch (e) {
r.pathCapabilityEnvelopeVerify = {
ok: false,
error: (e && e.message) || String(e)
}
}
}
ctx.console.log(JSON.stringify(r, null, 2))
if (!r.ok) ctx.exitCode = 1
return
}
} catch (e) {
ctx.console.error(
'pkg-swarm-index: ' + ((e && e.message) || String(e))
)
ctx.exitCode = 1
}
}
export { run }