Files
bare-operating-system/packages/bare-os-seeder/kernel/bin/dd
T
Raven Scott beaff551cf Fixed dd /dev/zero truncation in packages/bare-os-coreutils/src/dd.js
if=/dev/zero with finite count*bs now produces requested size directly (not limited by VFS 64KiB pseudo-file).
Raw stdout now uses bareOsEmitRaw, improving pipeline correctness.
Fixed pipeline capture path for FIFO-relevant commands

echo and cat now emit via bareOsEmitRaw (packages/bare-os-coreutils/src/echo.js, packages/bare-os-coreutils/src/cat.js).
Shell pipeline now injects bareOsBinWrite into child contexts when output is captured, so raw writes are captured (packages/bare-os-booter/lib/shell.js).
Improved xattr behavior in packages/bare-os-coreutils/src/xattr.js

Added -p/--print NAME.
-w now logs written attribute name for visible success feedback.
Roundtrip behaviors preserved with sidecar metadata format.
Improved ACL UX in:

packages/bare-os-coreutils/src/getfacl.js
Explicit source header for sidecar and mode fallback (non-compact mode).
packages/bare-os-coreutils/src/setfacl.js
Added -m/--modify ENTRY in addition to stdin blob and -b.
Implemented dynamic ulimit output in packages/bare-os-coreutils/src/ulimit.js

Reads /proc/bare_os/rlimits.json when available.
Supports -a, -n, -Sn, -Hn.
Added shuf -e support in packages/bare-os-coreutils/src/shuf.js.

Added split byte-suffix parsing (k/K/m/M/g/G) and attached -bSIZE support in packages/bare-os-coreutils/src/split.js.
2026-04-27 08:15:50 -04:00

274 lines
7.7 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
}
function parseSize(v) {
const s = String(v || '').trim().toLowerCase()
const m = /^(\d+)([kmg]?)$/.exec(s)
if (!m) return null
const n = Number.parseInt(m[1], 10)
if (!Number.isFinite(n) || n < 0) return null
const mul = m[2] === 'k' ? 1024 : m[2] === 'm' ? 1024 * 1024 : m[2] === 'g' ? 1024 * 1024 * 1024 : 1
return n * mul
}
function concatU8(b4a, chunks) {
let total = 0
for (const c of chunks) total += c.byteLength
const out = b4a.alloc(total)
let off = 0
for (const c of chunks) {
out.set(c, off)
off += c.byteLength
}
return out
}
function parseConvList(v) {
return String(v || '')
.split(',')
.map((x) => x.trim().toLowerCase())
.filter(Boolean)
}
function padToBlock(u8, bs, b4a) {
if (bs <= 0) return u8
const rem = u8.byteLength % bs
if (rem === 0) return u8
const pad = bs - rem
const out = b4a.alloc(u8.byteLength + pad)
out.set(u8, 0)
return out
}
async function run(ctx, argv) {
/** @type {{ if?: string, of?: string, bs: number, count?: number, skip: number, seek: number, status: string, conv: string[] }} */
const opt = { bs: 512, skip: 0, seek: 0, status: 'progress', conv: [] }
for (let i = 1; i < argv.length; i++) {
const a = argv[i]
if (a === '--') continue
const eq = a.indexOf('=')
if (eq <= 0) {
ctx.console.error('dd: expected OPERAND=VALUE: ' + a)
ctx.exitCode = 1
return
}
const k = a.slice(0, eq)
const v = a.slice(eq + 1)
if (k === 'if') opt.if = v
else if (k === 'of') opt.of = v
else if (k === 'bs') {
const n = parseSize(v)
if (n == null || n <= 0) {
ctx.console.error('dd: invalid bs=' + v)
ctx.exitCode = 1
return
}
opt.bs = n
} else if (k === 'count') {
const n = Number.parseInt(v, 10)
if (!Number.isFinite(n) || n < 0) {
ctx.console.error('dd: invalid count=' + v)
ctx.exitCode = 1
return
}
opt.count = n
} else if (k === 'skip') {
const n = Number.parseInt(v, 10)
if (!Number.isFinite(n) || n < 0) {
ctx.console.error('dd: invalid skip=' + v)
ctx.exitCode = 1
return
}
opt.skip = n
} else if (k === 'seek') {
const n = Number.parseInt(v, 10)
if (!Number.isFinite(n) || n < 0) {
ctx.console.error('dd: invalid seek=' + v)
ctx.exitCode = 1
return
}
opt.seek = n
} else if (k === 'status') {
if (v !== 'none' && v !== 'progress') {
ctx.console.error('dd: unsupported status=' + v)
ctx.exitCode = 1
return
}
opt.status = v
} else if (k === 'conv') {
const list = parseConvList(v)
const allow = new Set(['notrunc', 'sync', 'noerror', 'nocreat'])
for (const c of list) {
if (!allow.has(c)) {
ctx.console.error('dd: unsupported conv=' + c)
ctx.exitCode = 1
return
}
}
opt.conv = list
} else {
ctx.console.error('dd: unsupported operand ' + k)
ctx.exitCode = 1
return
}
}
const finiteCount = opt.count == null ? null : opt.count
const wantedBytes =
finiteCount == null ? null : Math.max(0, finiteCount * opt.bs)
const skipBytes = Math.max(0, opt.skip * opt.bs)
let chunk
if (opt.if === '/dev/zero' && wantedBytes != null) {
chunk = ctx.b4a.alloc(wantedBytes)
} else if (opt.if === '/dev/urandom' && wantedBytes != null) {
const raw = await ctx.vfs.readFile('/dev/urandom')
if (!raw) {
ctx.console.error('dd: /dev/urandom: unavailable')
ctx.exitCode = 1
return
}
const seed = raw instanceof Uint8Array ? raw : ctx.b4a.from(raw)
chunk = ctx.b4a.alloc(wantedBytes)
for (let i = 0; i < wantedBytes; i++) chunk[i] = seed[i % seed.length]
} else {
let src
if (opt.if) {
src = await ctx.vfs.readFile(opt.if)
if (!src) {
ctx.console.error('dd: ' + opt.if + ': No such file or directory')
ctx.exitCode = 1
return
}
} else {
src = ctx.b4a.from(bareStdin(ctx), 'utf8')
}
const input = src instanceof Uint8Array ? src : ctx.b4a.from(src)
const start = Math.min(skipBytes, input.byteLength)
const end =
wantedBytes == null
? input.byteLength
: Math.min(input.byteLength, start + wantedBytes)
chunk = input.subarray(start, end)
}
if (opt.conv.includes('sync')) {
chunk = padToBlock(chunk, opt.bs, ctx.b4a)
}
if (!opt.of) {
if (!bareOsEmitRaw(ctx, chunk)) ctx.console.log(ctx.b4a.toString(chunk))
if (opt.status !== 'none') {
ctx.console.error(`${chunk.byteLength} bytes copied`)
}
return
}
const outOff = opt.seek * opt.bs
let prev = null
try {
prev = await ctx.vfs.readFile(opt.of)
} catch {
prev = null
}
const prevU8 = prev ? (prev instanceof Uint8Array ? prev : ctx.b4a.from(prev)) : ctx.b4a.alloc(0)
let out
if (opt.conv.includes('notrunc')) {
const want = Math.max(prevU8.byteLength, outOff + chunk.byteLength)
out = ctx.b4a.alloc(want)
if (prevU8.byteLength) out.set(prevU8, 0)
out.set(chunk, outOff)
} else {
out = concatU8(ctx.b4a, [ctx.b4a.alloc(outOff), chunk])
}
await ctx.vfs.writeFile(opt.of, out)
if (opt.status !== 'none') {
ctx.console.error(`${chunk.byteLength} bytes copied`)
}
}