From c11e17573334dfb487a8e1cc25fa84fa9e138f5c Mon Sep 17 00:00:00 2001 From: Lily <129828376+Soccera1@users.noreply.github.com> Date: Tue, 28 Jul 2026 16:12:21 +1000 Subject: Refactor coreutils into single-call command modules (#1) * Refactor to single call --- src/shared/catalog.js | 27 + src/shared/checksum.js | 265 +++++++ src/shared/command.js | 28 + src/shared/common.js | 1238 +++++++++++++++++++++++++++++++++ src/shared/copy.js | 835 +++++++++++++++++++++++ src/shared/diagnostics.js | 550 +++++++++++++++ src/shared/filesystem.js | 739 ++++++++++++++++++++ src/shared/hash.js | 680 ++++++++++++++++++ src/shared/head-tail.js | 149 ++++ src/shared/help-options.js | 94 +++ src/shared/help.js | 631 +++++++++++++++++ src/shared/install.js | 282 ++++++++ src/shared/listing.js | 1517 +++++++++++++++++++++++++++++++++++++++++ src/shared/ownership.js | 194 ++++++ src/shared/paths.js | 92 +++ src/shared/process.js | 123 ++++ src/shared/runtime.js | 83 +++ src/shared/system.js | 65 ++ src/shared/tabs.js | 194 ++++++ src/shared/test-expression.js | 206 ++++++ src/shared/text.js | 165 +++++ src/shared/time.js | 779 +++++++++++++++++++++ 22 files changed, 8936 insertions(+) create mode 100644 src/shared/catalog.js create mode 100644 src/shared/checksum.js create mode 100644 src/shared/command.js create mode 100644 src/shared/common.js create mode 100644 src/shared/copy.js create mode 100644 src/shared/diagnostics.js create mode 100644 src/shared/filesystem.js create mode 100644 src/shared/hash.js create mode 100644 src/shared/head-tail.js create mode 100644 src/shared/help-options.js create mode 100644 src/shared/help.js create mode 100644 src/shared/install.js create mode 100644 src/shared/listing.js create mode 100644 src/shared/ownership.js create mode 100644 src/shared/paths.js create mode 100644 src/shared/process.js create mode 100644 src/shared/runtime.js create mode 100644 src/shared/system.js create mode 100644 src/shared/tabs.js create mode 100644 src/shared/test-expression.js create mode 100644 src/shared/text.js create mode 100644 src/shared/time.js (limited to 'src/shared') diff --git a/src/shared/catalog.js b/src/shared/catalog.js new file mode 100644 index 0000000..1d0f1de --- /dev/null +++ b/src/shared/catalog.js @@ -0,0 +1,27 @@ +export const commandNames = Object.freeze([ + "[", "arch", "b2sum", "base32", "base64", "basename", "basenc", "cat", + "chcon", "chgrp", "chmod", "chown", "chroot", "cksum", "comm", "coreutils", + "cp", "csplit", "cut", "date", "dd", "df", "dir", "dircolors", "dirname", "du", + "echo", "env", "expand", "expr", "factor", "false", "fmt", "fold", "ginstall", + "groups", "head", "hostid", "hostname", "id", "install", "join", "kill", "link", + "ln", "logname", "ls", "md5sum", "mkdir", "mkfifo", "mknod", "mktemp", "mv", + "nice", "nl", "nohup", "nproc", "numfmt", "od", "paste", "pathchk", "pinky", + "pr", "printenv", "printf", "ptx", "pwd", "readlink", "realpath", "rm", "rmdir", + "runcon", "seq", "sha1sum", "sha224sum", "sha256sum", "sha384sum", "sha512sum", + "shred", "shuf", "sleep", "sm3sum", "sort", "split", "stat", "stdbuf", "stty", + "sum", "sync", "tac", "tail", "tee", "test", "timeout", "touch", "tr", "true", + "truncate", "tsort", "tty", "uname", "unexpand", "uniq", "unlink", "uptime", + "users", "vdir", "wc", "who", "whoami", "yes", +]); + +const commandSet = new Set(commandNames); + +export function hasCommand(name) { + return commandSet.has(name); +} + +export async function loadCommand(name) { + if (!hasCommand(name)) return null; + const path = new URL(`../commands/${encodeURIComponent(name)}.js`, import.meta.url); + return (await import(path.href)).default; +} diff --git a/src/shared/checksum.js b/src/shared/checksum.js new file mode 100644 index 0000000..7e7e40f --- /dev/null +++ b/src/shared/checksum.js @@ -0,0 +1,265 @@ +import { readSync } from "node:fs"; +import { open } from "node:fs/promises"; +import { localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, pathDisplayName, shellEscapeLsName } from "./common.js"; +import { UsageError, stdout } from "./diagnostics.js"; + +export const BASE_ENCODING_LONG_OPTIONS = ["decode", "ignore-garbage", "wrap", "help", "version"]; + +export function baseEncodingMetaOption(args) { + return baseLikeMetaOption(args, BASE_ENCODING_LONG_OPTIONS, normalizeBaseEncodingLongOption); +} + +export function baseLikeMetaOption(args, longOptions, normalizeLongOption) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!longOptions.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + if (name === "wrap") { + const value = inlineValue ?? args[i + 1]; + if (value !== undefined) parseWrap(value); + if (inlineValue == null) i++; + } + else if (inlineValue != null && name !== "wrap") return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!["d", "i", "w"].includes(ch)) return null; + if (ch === "w") { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? args[i + 1] : inlineValue; + if (value !== undefined) parseWrap(value); + if (inlineValue === "") i++; + break; + } + } + } + return null; +} + +export function baseEncodingDiagnosticName(file) { + return shellEscapeLsName(pathDisplayName(file)); +} + +export function ensureSingleInputOperand(operands) { + if (operands.length > 1) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[1])}`, true); +} + +export function parseWrap(value) { + const text = String(value); + if (!/^\+?\d+$/.test(text)) throw new UsageError(`invalid wrap size: ${localeQuotedEscapedDiagnostic(text)}`); + const parsed = BigInt(text); + if (parsed > 9223372036854775807n) return 0; + const wrap = parsed > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(parsed); + return wrap; +} + +export function formatEncodedOutput(encoded, wrap) { + if (encoded === "") return ""; + if (wrap === 0) return encoded; + if (wrap >= encoded.length) return `${encoded}\n`; + return `${encoded.match(new RegExp(`.{1,${wrap}}`, "g"))?.join("\n") ?? ""}\n`; +} + +export function normalizeBaseEncodingLongOptions(args) { + return normalizeLongOptionsByPrefix(args, BASE_ENCODING_LONG_OPTIONS); +} + +export function normalizeBaseEncodingLongOption(arg) { + return normalizeLongOptionByPrefix(arg, BASE_ENCODING_LONG_OPTIONS); +} + +export function decodeBase64(text, ignoreGarbage = false, url = false) { + let cleaned = text.replace(/\s+/g, ""); + const alphabet = url ? "A-Za-z0-9_\\-=" : "A-Za-z0-9+/="; + if (ignoreGarbage) cleaned = cleaned.replace(new RegExp(`[^${alphabet}]`, "g"), ""); + else { + const invalid = cleaned.search(new RegExp(`[^${alphabet}]`)); + if (invalid !== -1) { + const prefix = decodeBase64Clean(cleaned.slice(0, invalid), url).bytes; + if (prefix.length) stdout(prefix); + throw new UsageError("invalid input"); + } + } + const decoded = decodeBase64Clean(cleaned, url); + if (!decoded.valid) { + if (decoded.bytes.length) stdout(decoded.bytes); + throw new UsageError("invalid input"); + } + return decoded.bytes; +} + +export function base64Alphabet(text, url = false) { + return url ? text.replaceAll("-", "+").replaceAll("_", "/") : text; +} + +export function decodeBase64Clean(text, url = false) { + const chunks = []; + let valid = true; + let i = 0; + while (i < text.length) { + let start = i; + while (i < text.length && text[i] !== "=") i++; + let base = text.slice(start, i); + let pads = ""; + while (i < text.length && text[i] === "=") pads += text[i++]; + if (base.length === 0 && pads.length > 0) { + valid = false; + break; + } + if (base.length === 0) continue; + const segment = decodeBase64Segment(base, pads, url); + if (segment.bytes.length) chunks.push(segment.bytes); + if (!segment.valid) { + valid = false; + break; + } + } + return { bytes: Buffer.concat(chunks), valid }; +} + +export function decodeBase64Segment(base, pads, url = false) { + const needPads = (4 - (base.length % 4)) % 4; + const bytes = base.length % 4 === 1 + ? Buffer.from(base64Alphabet(base.slice(0, base.length - 1), url), "base64") + : Buffer.from(base64Alphabet(base + "=".repeat(needPads), url), "base64"); + let valid = base.length % 4 !== 1; + if (pads.length) { + valid &&= pads.length <= 2; + valid &&= (base.length + pads.length) % 4 === 0; + if (pads.length === 1) valid &&= base.length % 4 === 3 && (base64DigitValue(base.at(-1), url) & 0b11) === 0; + else if (pads.length === 2) valid &&= base.length % 4 === 2 && (base64DigitValue(base.at(-1), url) & 0b1111) === 0; + else valid = false; + } else if (base.length % 4 === 2) { + valid &&= (base64DigitValue(base.at(-1), url) & 0b1111) === 0; + } else if (base.length % 4 === 3) { + valid &&= (base64DigitValue(base.at(-1), url) & 0b11) === 0; + } + return { bytes, valid }; +} + +export function base64DigitValue(ch, url = false) { + const alphabet = url ? "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + return alphabet.indexOf(ch); +} + +export async function forEachInputChunk(file, callback) { + const buffer = Buffer.allocUnsafe(256 * 1024); + if (file === "-") { + while (true) { + const n = readSync(0, buffer, 0, buffer.length, null); + if (n === 0) break; + callback(buffer.subarray(0, n)); + } + return; + } + const handle = await open(file, "r"); + try { + while (true) { + const { bytesRead } = await handle.read(buffer, 0, buffer.length, null); + if (bytesRead === 0) break; + callback(buffer.subarray(0, bytesRead)); + } + } finally { + await handle.close(); + } +} + +export async function sumForFile(file, sysv = false) { + let length = 0; + let sum = 0; + await forEachInputChunk(file, (chunk) => { + length += chunk.length; + for (const byte of chunk) { + if (sysv) sum = (sum + byte) % 0x100000000; + else sum = ((sum >> 1) + ((sum & 1) << 15) + byte) & 0xffff; + } + }); + if (sysv) { + sum = (sum & 0xffff) + Math.floor(sum / 0x10000); + sum = (sum & 0xffff) + Math.floor(sum / 0x10000); + } + return { sum, blocks: Math.ceil(length / (sysv ? 512 : 1024)) }; +} + +export function base32Encode(data) { + const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + let bits = 0; + let value = 0; + let out = ""; + for (const byte of data) { + value = (value << 8) | byte; + bits += 8; + while (bits >= 5) { + out += alphabet[(value >>> (bits - 5)) & 31]; + bits -= 5; + } + } + if (bits > 0) out += alphabet[(value << (5 - bits)) & 31]; + while (out.length % 8) out += "="; + return out; +} + +export function base32Decode(text) { + const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + const decoded = decodeBase32Clean(text, alphabet); + if (!decoded.valid) { + if (decoded.bytes.length) stdout(decoded.bytes); + throw new UsageError("invalid input"); + } + return decoded.bytes; +} + +export function decodeBase32Clean(text, alphabet) { + const chunks = []; + let valid = true; + let i = 0; + while (i < text.length) { + let start = i; + while (i < text.length && text[i] !== "=") i++; + const base = text.slice(start, i); + let pads = ""; + while (i < text.length && text[i] === "=") pads += text[i++]; + if (base.length === 0 && pads.length > 0) { + valid = false; + break; + } + if (base.length === 0) continue; + const segment = decodeBase32Segment(base, pads, alphabet); + if (segment.bytes.length) chunks.push(segment.bytes); + if (!segment.valid) { + valid = false; + break; + } + } + return { bytes: Buffer.concat(chunks), valid }; +} + +export function decodeBase32Segment(base, pads, alphabet) { + let bits = 0; + let value = 0; + const out = []; + for (const ch of base) { + const idx = alphabet.indexOf(ch); + if (idx === -1) { + return { bytes: Uint8Array.from(out), valid: false }; + } + value = (value << 5) | idx; + bits += 5; + if (bits >= 8) { + out.push((value >>> (bits - 8)) & 255); + bits -= 8; + } + } + const padByRemainder = new Map([[0, 0], [2, 6], [4, 4], [5, 3], [7, 1]]); + const expectedPads = padByRemainder.get(base.length % 8); + let valid = expectedPads !== undefined; + if (pads.length) valid &&= pads.length === expectedPads && (base.length + pads.length) % 8 === 0; + valid &&= bits === 0 || (value & ((1 << bits) - 1)) === 0; + return { bytes: Uint8Array.from(out), valid }; +} diff --git a/src/shared/command.js b/src/shared/command.js new file mode 100644 index 0000000..0d7666d --- /dev/null +++ b/src/shared/command.js @@ -0,0 +1,28 @@ +import { readFileSync } from "node:fs"; +import { executeCommand } from "./runtime.js"; +import { hasCommand } from "./catalog.js"; + +export function defineCommand(name, implementation, metaOption) { + if (!hasCommand(name)) throw new Error(`Unknown BNU command: ${name}`); + return (args) => executeCommand(name, implementation, metaOption, args); +} + +export async function runAsMain(command, args = singleCallArgs()) { + globalThis[Symbol.for("bnu.cli")] = true; + process.exit(await command(args)); +} + +// Bun consumes a leading `--` between the script name and its arguments. GNU +// utilities need to see that separator because it changes option parsing. +function singleCallArgs() { + const args = Bun.argv.slice(2); + try { + const parts = readFileSync("/proc/self/cmdline").subarray(0, -1).toString("binary").split("\0"); + let scriptIndex = parts.indexOf(Bun.argv[1]); + if (scriptIndex === -1 && parts.length > 1) scriptIndex = 1; + if (parts[scriptIndex + 1] === "--" && args[0] !== "--") return ["--", ...args]; + } catch { + // Non-Linux platforms retain Bun's decoded argument list. + } + return args; +} diff --git a/src/shared/common.js b/src/shared/common.js new file mode 100644 index 0000000..b90e08d --- /dev/null +++ b/src/shared/common.js @@ -0,0 +1,1238 @@ +import { CString, FFIType, dlopen, ptr, read } from "bun:ffi"; +import { closeSync, existsSync, constants as fsConstants, fstatSync, lstatSync, openSync, readFileSync, readSync, statSync } from "node:fs"; +import { access, readFile, readdir, stat } from "node:fs/promises"; +import { machine as osMachine } from "node:os"; +import { isAbsolute, join } from "node:path"; +import { UsageError, VERSION, encodeSurrogateEscapedString, stdout } from "./diagnostics.js"; + +export const libc = dlopen("libc.so.6", { + chdir: { args: [FFIType.cstring], returns: FFIType.i32 }, + chroot: { args: [FFIType.cstring], returns: FFIType.i32 }, + execve: { args: [FFIType.cstring, FFIType.ptr, FFIType.ptr], returns: FFIType.i32 }, + execvp: { args: [FFIType.cstring, FFIType.ptr], returns: FFIType.i32 }, + dlsym: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr }, + __errno_location: { args: [], returns: FFIType.ptr }, + chmod: { args: [FFIType.cstring, FFIType.u32], returns: FFIType.i32 }, + clock_settime: { args: [FFIType.i32, FFIType.ptr], returns: FFIType.i32 }, + fcntl: { args: [FFIType.i32, FFIType.i32], returns: FFIType.i32 }, + lseek: { args: [FFIType.i32, FFIType.i64, FFIType.i32], returns: FFIType.i64 }, + memset: { args: [FFIType.ptr, FFIType.i32, FFIType.u64], returns: FFIType.ptr }, + syscall: { args: [FFIType.u64, FFIType.u64, FFIType.u64, FFIType.u64, FFIType.u64], returns: FFIType.i64 }, + ioctl: { args: [FFIType.i32, FFIType.u64, FFIType.u64], returns: FFIType.i32 }, + kill: { args: [FFIType.i32, FFIType.i32], returns: FFIType.i32 }, + poll: { args: [FFIType.ptr, FFIType.u64, FFIType.i32], returns: FFIType.i32 }, + renameat2: { args: [FFIType.i32, FFIType.cstring, FFIType.i32, FFIType.cstring, FFIType.u32], returns: FFIType.i32 }, + sched_getscheduler: { args: [FFIType.i32], returns: FFIType.i32 }, + signal: { args: [FFIType.i32, FFIType.ptr], returns: FFIType.ptr }, + sync: { args: [], returns: FFIType.void }, + _exit: { args: [FFIType.i32], returns: FFIType.void }, + sethostname: { args: [FFIType.cstring, FFIType.u64], returns: FFIType.i32 }, + setgid: { args: [FFIType.u32], returns: FFIType.i32 }, + setgroups: { args: [FFIType.u64, FFIType.ptr], returns: FFIType.i32 }, + setuid: { args: [FFIType.u32], returns: FFIType.i32 }, + statfs: { args: [FFIType.cstring, FFIType.ptr], returns: FFIType.i32 }, + statx: { args: [FFIType.i32, FFIType.cstring, FFIType.i32, FFIType.u32, FFIType.ptr], returns: FFIType.i32 }, + tcgetattr: { args: [FFIType.i32, FFIType.ptr], returns: FFIType.i32 }, + tcsetattr: { args: [FFIType.i32, FFIType.i32, FFIType.ptr], returns: FFIType.i32 }, + mkfifo: { args: [FFIType.cstring, FFIType.u32], returns: FFIType.i32 }, + mknod: { args: [FFIType.cstring, FFIType.u32, FFIType.u64], returns: FFIType.i32 }, + opendir: { args: [FFIType.cstring], returns: FFIType.ptr }, + readdir: { args: [FFIType.ptr], returns: FFIType.ptr }, + closedir: { args: [FFIType.ptr], returns: FFIType.i32 }, + inotify_init1: { args: [FFIType.i32], returns: FFIType.i32 }, + inotify_add_watch: { args: [FFIType.i32, FFIType.cstring, FFIType.u32], returns: FFIType.i32 }, + inotify_rm_watch: { args: [FFIType.i32, FFIType.i32], returns: FFIType.i32 }, + getpriority: { args: [FFIType.i32, FFIType.u32], returns: FFIType.i32 }, + capget: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 }, + getxattr: { args: [FFIType.cstring, FFIType.cstring, FFIType.ptr, FFIType.u64], returns: FFIType.i64 }, + setxattr: { args: [FFIType.cstring, FFIType.cstring, FFIType.ptr, FFIType.u64, FFIType.i32], returns: FFIType.i32 }, + lsetxattr: { args: [FFIType.cstring, FFIType.cstring, FFIType.ptr, FFIType.u64, FFIType.i32], returns: FFIType.i32 }, + setpriority: { args: [FFIType.i32, FFIType.u32, FFIType.i32], returns: FFIType.i32 }, + setlocale: { args: [FFIType.i32, FFIType.cstring], returns: FFIType.ptr }, + nl_langinfo: { args: [FFIType.i32], returns: FFIType.ptr }, + strcoll: { args: [FFIType.cstring, FFIType.cstring], returns: FFIType.i32 }, + truncate: { args: [FFIType.cstring, FFIType.i64], returns: FFIType.i32 }, + utimensat: { args: [FFIType.i32, FFIType.cstring, FFIType.ptr, FFIType.i32], returns: FFIType.i32 }, +}); + +export let libselinux; + +export let libselinuxResolved = false; + +export function selinuxApi() { + if (libselinuxResolved) return libselinux; + libselinuxResolved = true; + try { + libselinux = dlopen("libselinux.so.1", { + context_free: { args: [FFIType.ptr], returns: FFIType.void }, + context_new: { args: [FFIType.cstring], returns: FFIType.ptr }, + context_range_set: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 }, + context_role_set: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 }, + context_str: { args: [FFIType.ptr], returns: FFIType.ptr }, + context_type_set: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 }, + context_user_set: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 }, + getcon: { args: [FFIType.ptr], returns: FFIType.i32 }, + getfilecon: { args: [FFIType.cstring, FFIType.ptr], returns: FFIType.i32 }, + lgetfilecon: { args: [FFIType.cstring, FFIType.ptr], returns: FFIType.i32 }, + freecon: { args: [FFIType.ptr], returns: FFIType.void }, + security_check_context: { args: [FFIType.cstring], returns: FFIType.i32 }, + security_compute_create: { args: [FFIType.cstring, FFIType.cstring, FFIType.u16, FFIType.ptr], returns: FFIType.i32 }, + setexeccon: { args: [FFIType.cstring], returns: FFIType.i32 }, + setfscreatecon: { args: [FFIType.ptr], returns: FFIType.i32 }, + setfilecon: { args: [FFIType.cstring, FFIType.cstring], returns: FFIType.i32 }, + lsetfilecon: { args: [FFIType.cstring, FFIType.cstring], returns: FFIType.i32 }, + string_to_security_class: { args: [FFIType.cstring], returns: FFIType.u16 }, + }); + } catch { + libselinux = null; + } + return libselinux; +} + +export const SEEK_SET = 0; + +export const SEEK_CUR = 1; + +export const enc = new TextEncoder(); + +export const SIGPIPE_MASK = 1n << 12n; + +export const BNU_SIGPIPE_DEFAULT_ENV = "BNU_SIGPIPE_DEFAULT"; + +export const AT_FDCWD = -100; + +export const AT_SYMLINK_NOFOLLOW = 0x100; + +export const STATX_ALL = 0x0fff; + +export function decodeSurrogateEscapedBytes(bytes) { + const decoder = new TextDecoder("utf-8", { fatal: true }); + try { + return decoder.decode(bytes); + } catch { + // Preserve undecodable bytes below while still decoding each maximal + // valid UTF-8 unit. The overwhelmingly common valid-input path above + // avoids building large strings one code point at a time. + } + let out = ""; + let start = 0; + while (start < bytes.length) { + let end = start + 1; + let decoded = null; + while (end <= bytes.length) { + try { + decoded = decoder.decode(bytes.subarray(start, end)); + break; + } catch { + end++; + } + } + if (decoded == null) { + out += String.fromCharCode(0xdc00 + bytes[start++]); + } else { + out += decoded; + start = end; + } + } + return out; +} + +export function parentIgnoresSigpipe() { + if (process.env[BNU_SIGPIPE_DEFAULT_ENV] === "1") return false; + try { + const status = readFileSync(`/proc/${process.ppid}/status`, "utf8"); + const ignored = status.match(/^SigIgn:\s*([0-9a-fA-F]+)/m)?.[1]; + return ignored != null && (BigInt(`0x${ignored}`) & SIGPIPE_MASK) !== 0n; + } catch { + return false; + } +} + +export function invalidOptionMessage(arg) { + return arg?.startsWith("--") ? `unrecognized option '${arg}'` : `invalid option -- '${String(arg ?? "").slice(1, 2)}'`; +} + +export function helpVersionOnlyMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (arg !== "-" && arg.startsWith("-") && !arg.startsWith("--")) return null; + if (!arg.startsWith("--")) continue; + const option = normalizeHelpVersionOnlyLongOption(arg); + if (option.includes("=") || !["--help", "--version"].includes(option)) return null; + return option; + } + return null; +} + +export function normalizeHelpVersionOnlyLongOptions(args) { + const out = []; + let end = false; + for (const arg of args) { + if (end || arg === "--" || !arg.startsWith("--")) { + out.push(arg); + if (arg === "--") end = true; + continue; + } + out.push(normalizeHelpVersionOnlyLongOption(arg)); + } + return out; +} + +export function normalizeHelpVersionOnlyLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const match = ["help", "version"].find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export function parseOptions(args, spec = {}) { + const opts = {}; + const operands = []; + let end = false; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (end || arg === "-" || !arg.startsWith("-")) { + operands.push(arg); + continue; + } + if (arg === "--") { + end = true; + continue; + } + if (arg.startsWith("--")) { + const [name, inlineValue] = arg.slice(2).split(/=(.*)/s, 2); + const mode = spec.long?.[name]; + if (mode === undefined) throw new UsageError(`unrecognized option '${arg}'`, true); + if (mode === "value" || mode === "value-array") { + if (inlineValue != null) setParsedOptionValue(opts, name, inlineValue, mode === "value-array"); + else if (i + 1 < args.length) setParsedOptionValue(opts, name, args[++i], mode === "value-array"); + else throw new UsageError(`option '--${name}' requires an argument`, true); + } else if (mode === "optional-value") { + opts[name] = inlineValue ?? true; + } else { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + opts[name] = true; + } + continue; + } + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + const mode = spec.short?.[ch]; + if (mode === undefined) throw new UsageError(`invalid option -- '${ch}'`, true); + if (mode === "value" || mode === "value-array") { + if (arg.slice(j + 1)) setParsedOptionValue(opts, ch, arg.slice(j + 1), mode === "value-array"); + else if (i + 1 < args.length) setParsedOptionValue(opts, ch, args[++i], mode === "value-array"); + else throw new UsageError(`option requires an argument -- '${ch}'`, true); + break; + } + if (mode === "optional-value") { + opts[ch] = arg.slice(j + 1) || true; + break; + } + opts[ch] = true; + } + } + return { opts, operands }; +} + +export function setParsedOptionValue(opts, name, value, append = false) { + if (!append || opts[name] == null) { + opts[name] = value; + } else if (Array.isArray(opts[name])) { + opts[name].push(value); + } else { + opts[name] = [opts[name], value]; + } +} + +export async function readAll(path) { + if (path === "-") return readStdinBytes(); + return new Uint8Array(await Bun.file(path).arrayBuffer()); +} + +export function readStdinBytes() { + const chunks = []; + const buffer = Buffer.allocUnsafe(64 * 1024); + let total = 0; + while (true) { + const n = readSync(0, buffer, 0, buffer.length, null); + if (n === 0) break; + chunks.push(Buffer.from(buffer.subarray(0, n))); + total += n; + } + const out = new Uint8Array(total); + let offset = 0; + for (const chunk of chunks) { + out.set(chunk, offset); + offset += chunk.length; + } + return out; +} + +export function readFdChunkViews(fd, accept, size = 64 * 1024) { + const buffer = Buffer.allocUnsafe(size); + while (true) { + const n = readSync(fd, buffer, 0, buffer.length, null); + if (n === 0) break; + accept(buffer.subarray(0, n)); + } +} + +export function readFdByteRecords(fd, sepByte, accept) { + const buffer = Buffer.allocUnsafe(64 * 1024); + let pending = []; + let pendingLength = 0; + const flush = (hasSep) => { + const bytes = new Uint8Array(pendingLength); + let offset = 0; + for (const chunk of pending) { + bytes.set(chunk, offset); + offset += chunk.length; + } + pending = []; + pendingLength = 0; + accept(bytes, hasSep); + }; + while (true) { + const n = readSync(fd, buffer, 0, buffer.length, null); + if (n === 0) break; + let start = 0; + for (let i = 0; i < n; i++) { + if (buffer[i] !== sepByte) continue; + pending.push(Buffer.from(buffer.subarray(start, i))); + pendingLength += i - start; + flush(true); + start = i + 1; + } + if (start < n) { + pending.push(Buffer.from(buffer.subarray(start, n))); + pendingLength += n - start; + } + } + if (pendingLength) flush(false); +} + +export function readStdinByteRecords(sepByte, accept) { + readFdByteRecords(0, sepByte, accept); +} + +export function fdStat(fd) { + try { + return fstatSync(fd); + } catch { + return null; + } +} + +export function wcFiles0SourceIsNonRegular(source) { + try { + return !(source === "-" ? fstatSync(0) : statSync(source)).isFile(); + } catch { + return false; + } +} + +export function splitFiles0ByteNames(bytes) { + if (bytes.byteLength === 0) return []; + const names = []; + let start = 0; + for (let i = 0; i < bytes.byteLength; i++) { + if (bytes[i] !== 0) continue; + names.push(Buffer.from(bytes.subarray(start, i))); + start = i + 1; + } + if (start < bytes.byteLength) names.push(Buffer.from(bytes.subarray(start))); + return names; +} + +export function wcFileNameIsDash(file) { + return file === "-" || (isBytePath(file) && Buffer.from(file).equals(Buffer.from("-"))); +} + +export function wcFileNameIsEmpty(file) { + return file === "" || (isBytePath(file) && file.byteLength === 0); +} + +export function displayWidth(ch) { + const code = ch.codePointAt(0) ?? 0; + if (code === 0 || ch === "\b" || ch === "\r" || ch === "\n" || ch === "\f") return 0; + if (isCombiningMark(code) || isZeroWidthFormat(code)) return 0; + if (isWideCodePoint(code)) return 2; + return 1; +} + +export function isZeroWidthFormat(code) { + return code === 0x061c || code === 0x180e || code === 0xfeff + || (code >= 0x200b && code <= 0x200f) + || (code >= 0x202a && code <= 0x202e) + || (code >= 0x2060 && code <= 0x2064) + || (code >= 0x2066 && code <= 0x206f) + || (code >= 0xfe00 && code <= 0xfe0f) + || (code >= 0xe0000 && code <= 0xe007f); +} + +export function isCombiningMark(code) { + return (code >= 0x0300 && code <= 0x036f) + || (code >= 0x1ab0 && code <= 0x1aff) + || (code >= 0x1dc0 && code <= 0x1dff) + || (code >= 0x20d0 && code <= 0x20ff) + || (code >= 0xfe20 && code <= 0xfe2f); +} + +export function isWideCodePoint(code) { + return (code >= 0x1100 && code <= 0x115f) + || code === 0x2329 || code === 0x232a + || (code >= 0x2e80 && code <= 0xa4cf && code !== 0x303f) + || (code >= 0xac00 && code <= 0xd7a3) + || (code >= 0xf900 && code <= 0xfaff) + || (code >= 0xfe10 && code <= 0xfe19) + || (code >= 0xfe30 && code <= 0xfe6f) + || (code >= 0xff00 && code <= 0xff60) + || (code >= 0xffe0 && code <= 0xffe6) + || (code >= 0x1f300 && code <= 0x1f64f) + || (code >= 0x1f900 && code <= 0x1f9ff); +} + +export function inRanges(index, ranges) { + return ranges.some(([start, end]) => index >= start && index <= end); +} + +export function concatBytes(chunks) { + const total = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0); + const out = new Uint8Array(total); + let offset = 0; + for (const chunk of chunks) { + out.set(chunk, offset); + offset += chunk.byteLength; + } + return out; +} + +export function isGb18030Locale() { + return /gb18030/i.test(process.env.LC_ALL ?? process.env.LC_CTYPE ?? process.env.LANG ?? ""); +} + +export function gb18030Units(bytes) { + const units = []; + for (let i = 0; i < bytes.length; i++) { + if (bytes[i] >= 0x80 && i + 1 < bytes.length) units.push(bytes.slice(i, ++i + 1)); + else units.push(bytes.slice(i, i + 1)); + } + return units; +} + +export function isUtf8Continuation(byte) { + return byte != null && byte >= 0x80 && byte <= 0xbf; +} + +export function textInputDiagnosticName(file) { + return shellEscapeLsName(pathDisplayName(file)); +} + +export function isUtf8Locale() { + return /utf-?8/i.test(process.env.LC_ALL || process.env.LC_CTYPE || process.env.LANG || ""); +} + +export function rawCommandArgs(command) { + try { + const parts = readFileSync("/proc/self/cmdline").subarray(0, -1).toString("binary").split("\0").map((part) => Buffer.from(part, "binary")); + const commandBytes = Buffer.from(command); + const index = parts.findIndex((part) => part.equals(commandBytes) || bufferPathBasename(part).equals(commandBytes)); + if (index !== -1) return parts.slice(index + 1); + const scriptIndex = parts.findIndex((part) => bufferPathBasename(part).toString() === "bnu.js"); + if (scriptIndex !== -1 && parts[scriptIndex + 1]?.equals(commandBytes)) return parts.slice(scriptIndex + 2); + return null; + } catch { + return null; + } +} + +export const LC_COLLATE = 3; + +export function initializeSortLocaleCollation() { + const locale = process.env.LC_ALL || process.env.LC_COLLATE || process.env.LANG || "C"; + if (locale === "C" || locale === "POSIX") return false; + return libc.symbols.setlocale(LC_COLLATE, cstr("")) !== null; +} + +export function compareSortLocaleText(a, b) { + const aa = encodeSurrogateEscapedString(a); + const bb = encodeSurrogateEscapedString(b); + // strcoll(3) is NUL-terminated. GNU sort still accepts embedded NUL bytes + // in newline-delimited records, so retain a length-aware byte comparison + // for that uncommon case. + if (aa.includes(0) || bb.includes(0)) return compareByteArrays(aa, bb); + const result = libc.symbols.strcoll(cstrPath(aa), cstrPath(bb)); + return result < 0 ? -1 : result > 0 ? 1 : 0; +} + +export function compareSortBytes(a, b) { + return compareByteArrays(encodeSurrogateEscapedString(a), encodeSurrogateEscapedString(b)); +} + +export function compareByteArrays(a, b) { + const length = Math.min(a.length, b.length); + for (let i = 0; i < length; i++) if (a[i] !== b[i]) return a[i] < b[i] ? -1 : 1; + return a.length < b.length ? -1 : a.length > b.length ? 1 : 0; +} + +export function isAsciiDigit(ch) { + return ch >= "0" && ch <= "9"; +} + +export function readStdinRecords(sep, accept) { + const sepByte = sep === "\0" ? 0 : 10; + const buffer = Buffer.allocUnsafe(64 * 1024); + let pending = []; + let pendingLength = 0; + const flush = () => { + const bytes = new Uint8Array(pendingLength); + let offset = 0; + for (const chunk of pending) { + bytes.set(chunk, offset); + offset += chunk.length; + } + pending = []; + pendingLength = 0; + accept(decodeSurrogateEscapedBytes(bytes)); + }; + while (true) { + const n = readSync(0, buffer, 0, buffer.length, null); + if (n === 0) break; + let start = 0; + for (let i = 0; i < n; i++) { + if (buffer[i] !== sepByte) continue; + pending.push(Buffer.from(buffer.subarray(start, i))); + pendingLength += i - start; + flush(); + start = i + 1; + } + if (start < n) { + pending.push(Buffer.from(buffer.subarray(start, n))); + pendingLength += n - start; + } + } + if (pendingLength) flush(); +} + +export function createFdRecordReader(fd, separator) { + const buffer = Buffer.allocUnsafe(64 * 1024); + let offset = 0; + let length = 0; + let eof = false; + let pending = []; + return { + next() { + while (true) { + for (let index = offset; index < length; index++) { + if (buffer[index] !== separator) continue; + const tail = Buffer.from(buffer.subarray(offset, index)); + offset = index + 1; + if (!pending.length) return tail; + pending.push(tail); + const record = Buffer.concat(pending); + pending = []; + return record; + } + if (offset < length) pending.push(Buffer.from(buffer.subarray(offset, length))); + if (eof) { + if (!pending.length) return null; + const record = Buffer.concat(pending); + pending = []; + return record; + } + length = readSync(fd, buffer, 0, buffer.length, null); + offset = 0; + if (length === 0) eof = true; + } + }, + }; +} + +export function normalizeLongOptionsByPrefix(args, longOptions) { + const out = []; + for (const arg of args) { + if (arg === "--") { + out.push(arg); + continue; + } + if (!arg.startsWith("--") || arg === "--") { + out.push(arg); + continue; + } + out.push(normalizeLongOptionByPrefix(arg, longOptions)); + } + return out; +} + +export function normalizeLongOptionByPrefix(arg, longOptions, reportAmbiguous = true) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (longOptions.includes(name)) return arg; + const matches = longOptions.filter((option) => option.startsWith(name)); + if (matches.length === 1) return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; + if (matches.length > 1 && reportAmbiguous) { + throw new UsageError(`option '${arg}' is ambiguous; possibilities: ${matches.map((option) => `'--${option}'`).join(" ")}`, true); + } + return arg; +} + +export function shellQuote(value) { + if (value === "") return "''"; + if (/[\u0080-\u009f]/.test(value)) return shellQuoteBytes(enc.encode(value)); + if (process.env.LC_ALL === "C" && /[^\x00-\x7f]/.test(value)) return shellQuoteBytes(enc.encode(value)); + if (/^[A-Za-z0-9_@%+=:,./~-]+$/.test(value) && !value.startsWith("~")) return value; + if (/^[^\s"'\\$`;&|()<>{}!*?\[\]#]+$/u.test(value) && !value.startsWith("~")) return value; + if (/[\x00-\x1f\x7f]/.test(value)) return shellQuoteControl(value); + if (!value.includes("'")) return `'${value}'`; + return `"${value.replace(/(["\\$`])/g, "\\$1")}"`; +} + +export function shellQuoteControl(value) { + let out = ""; + let segment = ""; + const flush = () => { + if (segment) { + out += `'${segment}'`; + segment = ""; + } else if (!out) { + out += "''"; + } + }; + for (const ch of value) { + const code = ch.codePointAt(0); + if (ch === "'") { + flush(); + out += "\\'''"; + } else if (code < 0x20 || code === 0x7f) { + flush(); + out += `$'${shellEscapeByte(code)}'`; + } else { + segment += ch; + } + } + flush(); + return out; +} + +export function shellQuoteBytes(bytes) { + return `''$'${[...bytes].map(shellEscapeByte).join("")}'`; +} + +export function shellEscapeByte(byte) { + if (byte === 7) return "\\a"; + if (byte === 8) return "\\b"; + if (byte === 9) return "\\t"; + if (byte === 10) return "\\n"; + if (byte === 13) return "\\r"; + return `\\${byte.toString(8).padStart(3, "0")}`; +} + +export function pad(value, width, flags = "") { + const n = Number(width); + if (!Number.isInteger(n) || value.length >= n) return value; + const fill = flags.includes("0") && !flags.includes("-") ? "0" : " "; + return flags.includes("-") ? value.padEnd(n, " ") : value.padStart(n, fill); +} + +export async function randomPicker(source, options = {}) { + if (source == null) return (max) => Math.floor(Math.random() * max); + const bytes = readRandomSourceBytes(source); + if (options.boundedBits && bytes.length === 0) { + const error = new Error("end of file"); + error.code = "EOF"; + throw error; + } + if (!options.boundedBits) { + let offset = 0; + return (max) => { + if (max <= 0) return 0; + if (!bytes.length) return 0; + let value = 0; + for (let i = 0; i < 4; i++) { + value = (value << 8) | bytes[offset++ % bytes.length]; + } + return (value >>> 0) % max; + }; + } + let bitOffset = 0; + const nextBit = () => { + if (bitOffset >= bytes.length * 8) { + const error = new Error("end of file"); + error.code = "EOF"; + throw error; + } + const bit = (bytes[bitOffset >> 3] >> (bitOffset & 7)) & 1; + bitOffset++; + return bit; + }; + return (max) => { + if (max <= 0) return 0; + if (!bytes.length) return 0; + const bits = Math.ceil(Math.log2(max)); + while (true) { + let value = 0; + for (let i = 0; i < bits; i++) value |= nextBit() << i; + if (value < max) return value; + } + }; +} + +export function readRandomSourceBytes(source) { + const fd = openSync(source, "r"); + try { + const buffer = Buffer.alloc(4096); + const bytesRead = readSync(fd, buffer, 0, buffer.length, null); + return buffer.subarray(0, bytesRead); + } finally { + closeSync(fd); + } +} + +export function attachDateNanoseconds(date, nanoseconds) { + date.__bnuNanoseconds = nanoseconds; + return date; +} + +export async function groupName(gid) { + const groups = await readGroup(); + for (const [name, id] of groups) if (id === gid) return name; + return String(gid); +} + +export async function userNameForUid(uid) { + const passwd = await readPasswd(); + for (const [name, row] of passwd) if (row.uid === Number(uid)) return name; + return null; +} + +export async function supplementaryGroupsForUser(user) { + const ids = []; + try { + const text = await readFile("/etc/group", "utf8"); + for (const line of text.split("\n")) { + const [,, gid, members = ""] = line.split(":"); + if (members.split(",").includes(user)) ids.push(Number(gid)); + } + } catch {} + return ids; +} + +export function uniqueNumbers(values) { + return [...new Set(values.map(Number))]; +} + +export function orderedGroupIds(primary, values) { + return [Number(primary), ...uniqueNumbers(values).filter((gid) => gid !== Number(primary))]; +} + +export function machineName() { + const map = { x64: "x86_64", arm64: "aarch64", ia32: "i686" }; + return osMachine?.() || map[process.arch] || process.arch; +} + +export function systemErrorMessage(error) { + if (error?.code === "ENOENT") return "No such file or directory"; + if (error?.code === "EACCES") return "Permission denied"; + if (error?.code === "EISDIR") return "Is a directory"; + if (error?.code === "ELOOP") return "Too many levels of symbolic links"; + if (error?.code === "ENOSPC") return "No space left on device"; + if (error?.code === "ENAMETOOLONG") return "File name too long"; + if (error?.code === "ENOTDIR") return "Not a directory"; + if (error?.code === "EBADF") return "Bad file descriptor"; + if (error?.code === "EINVAL") return "Invalid argument"; + return error?.message || String(error); +} + +export function selinuxRuntimeEnabled() { + if (process.platform !== "linux") return false; + if (smackRuntimeEnabled()) return true; + if (existsSync("/sys/fs/selinux/enforce")) return true; + try { + const result = Bun.spawnSync(["getenforce"], { stdout: "pipe", stderr: "ignore" }); + if (result.exitCode !== 0) return false; + return new TextDecoder().decode(result.stdout).trim() !== "Disabled"; + } catch { + return false; + } +} + +export function smackRuntimeEnabled() { + if (process.platform !== "linux") return false; + try { + const modules = readFileSync("/sys/kernel/security/lsm", "utf8").trim().split(","); + if (modules.includes("smack")) return true; + } catch {} + return existsSync("/sys/fs/smackfs"); +} + +export function processSecurityContext() { + if (smackRuntimeEnabled()) { + try { + const context = readFileSync("/proc/self/attr/current", "utf8").replace(/[\0\r\n]+$/, ""); + if (context) return context; + } catch {} + } + const lsmContext = lsmSelfSecurityContext(); + if (lsmContext != null) return lsmContext; + if (existsSync("/sys/fs/selinux/enforce")) { + const api = selinuxApi(); + if (api) { + const output = Buffer.alloc(8); + if (api.symbols.getcon(ptr(output)) === 0) { + const address = read.ptr(ptr(output), 0); + if (address) { + try { + const context = new CString(address).toString().replace(/[\0\r\n]+$/, ""); + if (context) return context; + } finally { + api.symbols.freecon(address); + } + } + } + } + try { + const context = readFileSync("/proc/self/attr/current", "utf8").replace(/[\0\r\n]+$/, ""); + if (context) return context; + } catch {} + } + // ps exposes the same process label on SELinux systems and also provides a + // useful fallback for minimal runtimes where libselinux is unavailable. + try { + const result = Bun.spawnSync(["ps", "-o", "label=", "-p", String(process.pid)], { stdout: "pipe", stderr: "ignore" }); + if (result.exitCode !== 0) return null; + return new TextDecoder().decode(result.stdout).trim() || null; + } catch { + return null; + } +} + +export function lsmSelfSecurityContext() { + if (process.platform !== "linux") return null; + // lsm_get_self_attr(LSM_ATTR_CURRENT, ...) is syscall 459 on Linux's + // current native ABIs. Unlike the legacy proc attribute, it can select the + // SELinux record correctly when several security modules are compiled in. + const size = Buffer.alloc(4); + libc.symbols.syscall(459, 100, 0, ptr(size), 0); + const required = size.readUInt32LE(0); + if (required < 32 || required > 1024 * 1024) return null; + const records = Buffer.alloc(required); + size.writeUInt32LE(required, 0); + const count = Number(libc.symbols.syscall(459, 100, ptr(records), ptr(size), 0)); + if (count <= 0) return null; + let offset = 0; + for (let index = 0; index < count && offset + 32 <= records.length; index++) { + const id = records.readBigUInt64LE(offset); + const length = Number(records.readBigUInt64LE(offset + 16)); + const contextLength = Number(records.readBigUInt64LE(offset + 24)); + if (length < 32 || offset + length > records.length || contextLength > length - 32) return null; + if (id === 101n) { + const context = records.subarray(offset + 32, offset + 32 + contextLength).toString("utf8").replace(/[\0\r\n]+$/, ""); + return context || null; + } + offset += length; + } + return null; +} + +export function lsUsesUtf8Locale() { + const locale = process.env.LC_ALL || process.env.LC_CTYPE || process.env.LANG || ""; + return /utf-?8/i.test(locale); +} + +export function localeQuotedDiagnostic(value) { + return lsUsesUtf8Locale() ? `\u2018${value}\u2019` : `'${value}'`; +} + +export function localeQuotedEscapedDiagnostic(value) { + return localeQuotedDiagnostic(lsEscapedName(value, { escapeDouble: false })); +} + +export function lsEscapedName(name, { escapeDouble = true, escapeSpaces = false } = {}) { + return [...String(name)].map((ch) => { + const code = ch.codePointAt(0); + if (ch === " " && escapeSpaces) return "\\ "; + if (ch === "\\") return "\\\\"; + if (ch === "\"" && escapeDouble) return "\\\""; + if (ch === "\n") return "\\n"; + if (ch === "\t") return "\\t"; + if (ch === "\r") return "\\r"; + if (ch === "\b") return "\\b"; + if (ch === "\f") return "\\f"; + if (ch === "\v") return "\\v"; + if (ch === "\x07") return "\\a"; + if (code < 32 || code === 127) return `\\${code.toString(8).padStart(3, "0")}`; + return ch; + }).join(""); +} + +export function shellEscapeLsName(name, always = false) { + const text = String(name); + if (!always && /^[A-Za-z0-9_.,@%+=/-]+$/.test(text)) return text; + if (!/[\x00-\x1f\x7f]/.test(text)) return shellQuoteLsName(text, always); + const parts = []; + let plain = ""; + let escaped = ""; + const flushPlain = () => { + if (plain) { + parts.push(`'${plain.replaceAll("'", "'\\''")}'`); + plain = ""; + } + }; + const flushEscaped = () => { + if (escaped) { + parts.push(`$'${escaped}'`); + escaped = ""; + } + }; + for (const ch of text) { + const code = ch.codePointAt(0); + if (code < 32 || code === 127) { + flushPlain(); + escaped += lsEscapedName(ch); + } else { + flushEscaped(); + plain += ch; + } + } + flushPlain(); + flushEscaped(); + if (!parts.length) return "''"; + if (always && parts.length === 1 && !parts[0].startsWith("$")) return parts[0]; + return parts.join(""); +} + +export function shellQuoteLsName(name, always = false, rendered = name) { + const text = String(name); + const out = String(rendered); + if (!always && !/[\s\\'"$`|&;<>(){}\[\]*?!]/.test(text)) return out; + if (out.includes("'") && !out.includes("\"")) return `"${out.replaceAll("\\", "\\\\").replaceAll("$", "\\$").replaceAll("`", "\\`").replaceAll("\"", "\\\"")}"`; + return `'${out.replaceAll("'", "'\\''")}'`; +} + +export function statAttachNanoseconds(statInfo, path, dereference) { + try { + const precise = nativeStatNanoseconds(path, dereference) ?? (dereference ? statSync(path, { bigint: true }) : lstatSync(path, { bigint: true })); + attachStatNanoseconds(statInfo, precise); + } catch {} + return statInfo; +} + +export function nativeStatNanoseconds(path, dereference) { + const buffer = Buffer.alloc(256); + const flags = dereference ? 0 : AT_SYMLINK_NOFOLLOW; + if (libc.symbols.statx(AT_FDCWD, cstrPath(path), flags, STATX_ALL, buffer) !== 0) return null; + const mask = buffer.readUInt32LE(0); + const fields = [ + ["atime", 0x0020, 64], + ["birthtime", 0x0800, 80], + ["ctime", 0x0080, 96], + ["mtime", 0x0040, 112], + ]; + const result = {}; + for (const [field, bit, offset] of fields) { + if ((mask & bit) === 0) continue; + const seconds = buffer.readBigInt64LE(offset); + const nanoseconds = BigInt(buffer.readUInt32LE(offset + 8)); + result[`${field}Ns`] = seconds * 1_000_000_000n + nanoseconds; + } + return result; +} + +export function attachStatNanoseconds(statInfo, precise) { + for (const field of ["atime", "mtime", "ctime", "birthtime"]) { + const value = precise[`${field}Ns`]; + if (value == null) continue; + statInfo[`${field}Ns`] = value; + statInfo[field] = dateFromNanoseconds(value); + } + return statInfo; +} + +export function dateFromNanoseconds(value) { + const total = BigInt(value); + const milliseconds = floorDivBigInt(total, 1_000_000n); + const nanoseconds = Number(((total % 1_000_000_000n) + 1_000_000_000n) % 1_000_000_000n); + return attachDateNanoseconds(new Date(Number(milliseconds)), nanoseconds); +} + +export async function readPasswd() { + const rows = new Map(); + try { + const text = await readFile("/etc/passwd", "utf8"); + for (const line of text.split("\n")) { + const [name,, uid, gid, gecos = "", home = "", shell = ""] = line.split(":"); + if (name) rows.set(name, { uid: Number(uid), gid: Number(gid), gecos, home, shell }); + } + } catch {} + return rows; +} + +export async function readGroup() { + const rows = new Map(); + try { + const text = await readFile("/etc/group", "utf8"); + for (const line of text.split("\n")) { + const [name,, gid] = line.split(":"); + if (name) rows.set(name, Number(gid)); + } + } catch {} + return rows; +} + +export function ddBufferIsZero(chunk) { + for (const byte of chunk) if (byte !== 0) return false; + return true; +} + +export function globMatch(pattern, value) { + let regex = ""; + for (let i = 0; i < pattern.length; i++) { + const ch = pattern[i]; + if (ch === "*") regex += ".*"; + else if (ch === "?") regex += "."; + else if (ch === "[") { + const end = pattern.indexOf("]", i + 1); + if (end === -1) regex += "\\["; + else { + let body = pattern.slice(i + 1, end); + if (body.startsWith("!")) body = `^${body.slice(1)}`; + regex += `[${body.replace(/\\/g, "\\\\")}]`; + i = end; + } + } else { + regex += ch.replace(/[.+^${}()|\\]/g, "\\$&"); + } + } + return new RegExp(`^${regex}$`).test(value); +} + +export function selinuxAllocatedString(api, invoke) { + const output = Buffer.alloc(8); + if (invoke(ptr(output)) < 0) return null; + const address = read.ptr(ptr(output), 0); + if (!address) return null; + try { + return new CString(address).toString().replace(/[\0\r\n]+$/, "") || null; + } finally { + api.symbols.freecon(address); + } +} + +export function hasSurrogateEscapedBytes(text) { + for (let i = 0; i < text.length; i++) { + const code = text.charCodeAt(i); + if (code >= 0xdc80 && code <= 0xdcff) return true; + } + return false; +} + +export function cstr(value) { + return Buffer.from(`${value}\0`); +} + +export function cstrPath(value) { + const bytes = isBytePath(value) ? Buffer.from(value) : Buffer.from(String(value)); + return Buffer.concat([bytes, Buffer.from([0])]); +} + +export function changeDirectory(path) { + if (libc.symbols.chdir(cstrPath(path)) !== 0) { + const error = new Error(`chdir '${pathDisplayName(path)}' failed`); + error.code = "EACCES"; + throw error; + } +} + +export function headTailDiagnosticName(file) { + return shellEscapeLsName(pathDisplayName(file), true); +} + +export function isWriteError(error) { + return error?.syscall === "write" || ["EPIPE", "ENOSPC"].includes(error?.code); +} + +export function nodeErrorMessage(error) { + if (error?.code === "ENOENT") return "No such file or directory"; + if (error?.code === "ENOSPC") return "No space left on device"; + return error?.message || String(error); +} + +export function writeEnvironment(env, sep) { + for (const [key, value] of Object.entries(env).filter(([key]) => key !== "_" && key !== "PWD").sort(([a], [b]) => a.localeCompare(b))) stdout(encodeEnvironmentEntry(`${key}=${value}${sep}`)); +} + +export function encodeEnvironmentEntry(entry) { + if (!/[\u00A0\uFFFD\uDC80-\uDCFF]/.test(entry)) return entry; + const parts = []; + let text = ""; + for (let i = 0; i < entry.length; i++) { + const code = entry.charCodeAt(i); + const raw = code === 0x00a0 || code === 0xfffd ? 0xa0 : code >= 0xdc80 && code <= 0xdcff ? code & 0xff : null; + if (raw == null) { + text += entry[i]; + continue; + } + if (text) { + parts.push(Buffer.from(text)); + text = ""; + } + parts.push(Buffer.from([raw])); + } + if (text) parts.push(Buffer.from(text)); + return Buffer.concat(parts); +} + +export async function booleanCommand(program, args, code) { + if (args.length === 1 && args[0] === "--help") { + showBooleanHelp(program, code); + return code; + } + if (args.length === 1 && args[0] === "--version") { + stdout(`${VERSION}\n`); + return code; + } + return code; +} + +export function showBooleanHelp(program, code) { + stdout(`Usage: ${program} [ignored command line arguments]\n`); + stdout(` or: ${program} OPTION\n`); + stdout(`Exit with a status code indicating ${code === 0 ? "success" : "failure"}.\n\n`); + stdout(" --help display this help and exit\n"); + stdout(" --version output version information and exit\n"); +} + +export async function resolveEnvCommand(command, env, cwd) { + const searchPath = Object.hasOwn(env, "PATH") ? String(env.PATH) : "/bin:/usr/bin"; + const candidates = command.includes("/") ? [command] : searchPath.split(":").map((dir) => join(dir || ".", command)); + for (const candidate of candidates) { + const full = isAbsolute(candidate) ? candidate : join(cwd, candidate); + const s = await stat(full).catch(() => null); + if (!s) continue; + if (s.isDirectory()) { + const error = new Error("Permission denied"); + error.code = "EACCES"; + throw error; + } + await access(full, fsConstants.X_OK).catch((error) => { + error.code = "EACCES"; + throw error; + }); + return command.includes("/") ? candidate : full; + } + const error = new Error("No such file or directory"); + error.code = "ENOENT"; + throw error; +} + +export function touchStatDate(statInfo, field) { + const date = statInfo[field]; + const value = statInfo[`${field}Ns`]; + return value == null ? date : attachDateNanoseconds(date, Number(((BigInt(value) % 1_000_000_000n) + 1_000_000_000n) % 1_000_000_000n)); +} + +export function libcErrno() { + const errnoPointer = libc.symbols.__errno_location(); + return errnoPointer ? read.i32(errnoPointer, 0) : 0; +} + +export function floorDivBigInt(value, divisor) { + const quotient = value / divisor; + return value >= 0n || value % divisor === 0n ? quotient : quotient - 1n; +} + +export async function readdirPathEntries(path) { + return await readdir(path, { encoding: "buffer" }); +} + +export function pathLikeJoin(parent, child) { + if (isBytePath(parent) || isBytePath(child)) return bufferPathJoin(parent, isBytePath(child) ? Buffer.from(child) : Buffer.from(String(child))); + return join(parent, child); +} + +export function isBytePath(path) { + return path instanceof Uint8Array; +} + +export function pathDisplayName(path) { + return isBytePath(path) ? decodeUtf8SurrogateEscaped(Buffer.from(path)) : String(path); +} + +export function decodeUtf8SurrogateEscaped(bytes) { + let out = ""; + for (let i = 0; i < bytes.length;) { + const b = bytes[i]; + if (b < 0x80) { + out += String.fromCharCode(b); + i++; + } else if (b >= 0xc2 && b <= 0xdf && i + 1 < bytes.length && isUtf8Continuation(bytes[i + 1])) { + out += String.fromCodePoint(((b & 0x1f) << 6) | (bytes[i + 1] & 0x3f)); + i += 2; + } else if (b >= 0xe0 && b <= 0xef && i + 2 < bytes.length && isUtf8Continuation(bytes[i + 1]) && isUtf8Continuation(bytes[i + 2]) + && !(b === 0xe0 && bytes[i + 1] < 0xa0) && !(b === 0xed && bytes[i + 1] >= 0xa0)) { + out += String.fromCodePoint(((b & 0x0f) << 12) | ((bytes[i + 1] & 0x3f) << 6) | (bytes[i + 2] & 0x3f)); + i += 3; + } else if (b >= 0xf0 && b <= 0xf4 && i + 3 < bytes.length && isUtf8Continuation(bytes[i + 1]) && isUtf8Continuation(bytes[i + 2]) && isUtf8Continuation(bytes[i + 3]) + && !(b === 0xf0 && bytes[i + 1] < 0x90) && !(b === 0xf4 && bytes[i + 1] >= 0x90)) { + out += String.fromCodePoint(((b & 0x07) << 18) | ((bytes[i + 1] & 0x3f) << 12) | ((bytes[i + 2] & 0x3f) << 6) | (bytes[i + 3] & 0x3f)); + i += 4; + } else { + out += String.fromCharCode(0xdc00 + b); + i++; + } + } + return out; +} + +export function bufferPathBasename(path) { + let end = path.length; + while (end > 1 && path[end - 1] === 0x2f) end--; + const slash = path.lastIndexOf(0x2f, end - 1); + return path.subarray(slash + 1, end); +} + +export function bufferPathJoin(dir, base) { + const normalizedDir = isBytePath(dir) ? Buffer.from(dir) : Buffer.from(String(dir)); + const sep = normalizedDir.length && normalizedDir[normalizedDir.length - 1] !== 0x2f ? Buffer.from("/") : Buffer.alloc(0); + return Buffer.concat([normalizedDir, sep, base]); +} + +export function parseGNUSize(value) { + const match = String(value).match(/^(\d+)([A-Za-z]*)$/); + if (!match) throw new UsageError(`invalid file size: ${value}`); + const n = BigInt(match[1]); + const suffix = match[2]; + const binary = { + "": 1n, b: 512n, + K: 1024n, k: 1024n, KiB: 1024n, kiB: 1024n, + M: 1024n ** 2n, m: 1024n ** 2n, MiB: 1024n ** 2n, miB: 1024n ** 2n, + G: 1024n ** 3n, g: 1024n ** 3n, GiB: 1024n ** 3n, giB: 1024n ** 3n, + T: 1024n ** 4n, t: 1024n ** 4n, TiB: 1024n ** 4n, tiB: 1024n ** 4n, + P: 1024n ** 5n, PiB: 1024n ** 5n, + E: 1024n ** 6n, EiB: 1024n ** 6n, + Z: 1024n ** 7n, ZiB: 1024n ** 7n, + Y: 1024n ** 8n, YiB: 1024n ** 8n, + }; + const decimal = { + KB: 1000n, kB: 1000n, MB: 1000n ** 2n, mB: 1000n ** 2n, + GB: 1000n ** 3n, gB: 1000n ** 3n, TB: 1000n ** 4n, tB: 1000n ** 4n, + PB: 1000n ** 5n, EB: 1000n ** 6n, ZB: 1000n ** 7n, YB: 1000n ** 8n, + }; + const scale = binary[suffix] ?? decimal[suffix]; + if (!scale) throw new UsageError(`invalid file size: ${value}`); + const amount = n * scale; + return amount > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(amount); +} + +export function statSyncNoThrow(path) { + try { + return statSync(path); + } catch { + return null; + } +} diff --git a/src/shared/copy.js b/src/shared/copy.js new file mode 100644 index 0000000..2add30b --- /dev/null +++ b/src/shared/copy.js @@ -0,0 +1,835 @@ +import { FFIType, linkSymbols, ptr } from "bun:ffi"; +import { closeSync, existsSync, mkdirSync, openSync, readdirSync, renameSync, unlinkSync } from "node:fs"; +import { chown, copyFile, link as fsLink, unlink as fsUnlink, lchown, lstat, mkdir, mkdtemp, open, readlink, realpath, rename, rm, stat, symlink, truncate, writeFile } from "node:fs/promises"; +import { isAbsolute, join, basename as pathBasename, dirname as pathDirname, resolve } from "node:path"; +import { AT_FDCWD, cstr, cstrPath, ddBufferIsZero, libc, libcErrno, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, pathDisplayName, pathLikeJoin, selinuxApi, shellEscapeLsName, statAttachNanoseconds, touchStatDate } from "./common.js"; +import { InvocationError, UsageError, stdout } from "./diagnostics.js"; +import { errnoMessage, openDirectoryPathEntries, restoreSelinuxSecurityContext, selinuxSecurityContext, setFileMode, setSelinuxSecurityContext, touchSetPathTimes } from "./filesystem.js"; + +export const SEEK_DATA = 3; + +export const SEEK_HOLE = 4; + +export let reflinkTempId = 0; + +export const FICLONE = 0x40049409; + +export function cpDuplicateDisplayName(src) { + return String(src).replace(/^\.\//, ""); +} + +export function cpRestrictiveDirectoryMode(mode) { + return (mode & 0o7000) | 0o700; +} + +export function stripTrailingSlashesPath(path) { + return String(path).replace(/\/+$/, "") || path; +} + +export async function cpUpdateDecision(src, target, opts, noDereference, overwriteMode = null) { + if (overwriteMode === "no-clobber" || (overwriteMode == null && (opts.n || opts["no-clobber"]))) { + if (await lstat(target).catch(() => null)) return { skip: true, fail: false }; + } + if (!(opts.u || opts.update != null)) return { skip: false, fail: false }; + const mode = cpUpdateMode(opts, overwriteMode); + if (mode === "all") return { skip: false, fail: false }; + const sourceInfo = await cpUpdateStat(src, noDereference); + const targetInfo = await cpUpdateStat(target, noDereference); + if (!targetInfo) return { skip: false, fail: false }; + if (mode === "none" || mode === "none-fail") return { skip: true, fail: mode === "none-fail" }; + if (!sourceInfo) return { skip: false, fail: false }; + if (sourceInfo.isDirectory()) return { skip: false, fail: false }; + if (sourceInfo.dev === targetInfo.dev && sourceInfo.ino === targetInfo.ino) return { skip: true, fail: false }; + return { skip: cpStatMtimeNs(targetInfo) >= cpStatMtimeNs(sourceInfo), fail: false }; +} + +export async function cpUpdateStat(path, noDereference) { + return (noDereference ? lstat(path, { bigint: true }) : stat(path, { bigint: true })).catch(() => null); +} + +export function cpStatMtimeNs(statInfo) { + if (statInfo.mtimeNs != null) return BigInt(statInfo.mtimeNs); + return BigInt(statInfo.mtimeMs) * 1_000_000n; +} + +export function cpUpdateMode(opts, overwriteMode = null) { + if (overwriteMode === "no-clobber" || (overwriteMode == null && (opts.n || opts["no-clobber"]))) return "none"; + if (opts.update === true || opts.update == null) return "older"; + const mode = String(opts.update); + if (mode === "older" || mode === "all" || mode === "none" || mode === "none-fail") return mode; + validateUpdateMode("cp", mode); +} + +export function validateUpdateMode(program, mode) { + if (["all", "none", "none-fail", "older"].includes(mode)) return; + const kind = mode === "" ? "ambiguous" : "invalid"; + throw new UsageError(`${kind} argument ${localeQuotedEscapedDiagnostic(mode)} for ${localeQuotedDiagnostic("--update")}\nValid arguments are:\n - ${localeQuotedDiagnostic("all")}\n - ${localeQuotedDiagnostic("none")}\n - ${localeQuotedDiagnostic("none-fail")}\n - ${localeQuotedDiagnostic("older")}`, true); +} + +export async function withPreservedCopyCreationContext(src, options, create) { + if (!options.preserveContextAtCreation || !options.selinuxEnabled || !existsSync("/sys/fs/selinux/enforce")) { + return { value: await create(), handled: false }; + } + const context = selinuxSecurityContext(src, Boolean(options.noDereferenceContext)); + const api = context != null ? selinuxApi() : null; + if (context == null && options.requirePreserveContext) { + throw new UsageError(`failed to get security context of '${pathDisplayName(src)}'`); + } + let contextSet = false; + if (api) { + const encoded = cstr(context); + if (api.symbols.security_check_context(encoded) === 0 && api.symbols.setfscreatecon(ptr(encoded)) === 0) { + contextSet = true; + } else if (options.requirePreserveContext) { + throw new UsageError(`failed to set default file creation context to '${context}': Invalid argument`); + } + } + try { + return { value: await create(), handled: true }; + } finally { + if (contextSet) api.symbols.setfscreatecon(null); + } +} + +export function copyMetadataAfterCreationContext(options, handled) { + return handled ? { ...options, preserveContext: false } : options; +} + +export function prepareRegularFilePreservedContext(src, dest, sourceStat, options) { + if (!options.preserveContextAtCreation || !options.selinuxEnabled || !existsSync("/sys/fs/selinux/enforce")) return false; + const context = selinuxSecurityContext(src, Boolean(options.noDereferenceContext)); + if (context == null) { + if (options.requirePreserveContext) throw new UsageError(`failed to get security context of '${src}'`); + return false; + } + const api = selinuxApi(); + let contextSet = false; + if (api) { + const encoded = cstr(context); + if (api.symbols.security_check_context(encoded) === 0 && api.symbols.setfscreatecon(ptr(encoded)) === 0) contextSet = true; + } + try { + // Create or truncate before copying any bytes. Besides keeping the + // thread-local fscreate context on this thread, this lets an explicitly + // requested --preserve=context fail with an empty destination on a + // fixed-context filesystem, as GNU cp does. + const fd = openSync(dest, "w", cpDefaultFileMode(options.useSourceModeForNewRegularFiles ? sourceStat.mode : undefined)); + closeSync(fd); + } finally { + if (contextSet) api.symbols.setfscreatecon(null); + } + const actual = selinuxSecurityContext(dest); + const preserved = actual === context || setSelinuxSecurityContext(dest, context); + if (!preserved && options.requirePreserveContext) throw new UsageError(`failed to set the security context of '${dest}'`); + // Archive/all preservation treats an unsupported context as ignorable; + // either way, do not attempt it again after data has been copied. + return true; +} + +export async function copyPath(src, dest, options, topLevel = false) { + const dereferenceSource = cpShouldDereferenceSource(src, options, topLevel); + if (options.hardLink) { + const srcInfo = await lstat(src).catch(() => null); + const destInfo = options.force || options.removeDestination ? null : await lstat(dest).catch(() => null); + if (!dereferenceSource && srcInfo?.isSymbolicLink() && destInfo?.isSymbolicLink() && await readlink(src).catch(() => null) === await readlink(dest).catch(() => null) && !(options.force || options.removeDestination)) return; + const dereferencedInfo = !dereferenceSource ? srcInfo : await stat(src).catch((error) => { + if (error?.code === "ENOENT") throw new UsageError(`cannot stat '${src}': No such file or directory`); + throw error; + }); + if (dereferencedInfo?.isDirectory()) { + if (!options.recursive) throw new UsageError(`-r not specified; omitting directory '${src}'`); + const destInfo = await lstat(dest).catch(() => null); + await mkdir(dest, { recursive: true, mode: cpInitialDirectoryCreateMode(dereferencedInfo.mode, options) }); + if (options.verbose && !destInfo && !topLevel) stdout(`created directory '${dest}'\n`); + if (options.oneFileSystem && !topLevel && dereferencedInfo.dev !== options.rootDev) { + if (options.preserveDirectoryMode && !destInfo) await setFileMode(dest, dereferencedInfo.mode & 0o7777).catch(() => {}); + return; + } + const dirSource = !dereferenceSource ? src : await realpath(src); + for await (const entry of iterateDirectoryPathEntries(dirSource)) await copyPath(pathLikeJoin(dirSource, entry), pathLikeJoin(dest, entry), options); + if (options.preserveDirectoryMode && !destInfo) await setFileMode(dest, dereferencedInfo.mode & 0o7777).catch(() => {}); + return; + } + if (options.force || options.removeDestination) await rm(dest, { force: true, recursive: true }); + let linkSource = src; + if (dereferenceSource) { + try { + linkSource = await realpath(src); + } catch (error) { + if (error?.code === "ENOENT") throw new UsageError(`cannot stat '${src}': No such file or directory`); + throw error; + } + } + try { + await fsLink(linkSource, dest); + } catch (error) { + if (error.code === "EEXIST") throw new UsageError(`cannot create hard link '${dest}' to '${src}'`); + throw error; + } + if (options.verbose) stdout(`'${dest}' => '${src}'\n`); + return; + } + if (options.symbolicLink) { + if (options.force || options.removeDestination) await rm(dest, { force: true, recursive: true }); + try { + await symlink(src, dest); + } catch (error) { + if (error.code === "EEXIST") throw new UsageError(`cannot create symbolic link '${dest}' to '${src}'`); + throw error; + } + if (options.verbose) stdout(`'${dest}' -> '${src}'\n`); + return; + } + const s = await lstat(src); + const sourceStat = dereferenceSource && s.isSymbolicLink() ? await stat(src).catch((error) => { + if (error?.code === "ENOENT") throw new UsageError(`cannot stat '${src}': No such file or directory`); + throw error; + }) : s; + statAttachNanoseconds(sourceStat, src, dereferenceSource); + const preservedLink = await cpMaybePreserveExistingLink(src, dest, sourceStat, s, dereferenceSource, options); + if (preservedLink) { + if (options.verbose) stdout(`'${src}' -> '${dest}'\n`); + return; + } + if (sourceStat.isDirectory()) { + if (!options.recursive) throw new UsageError(`-r not specified; omitting directory '${src}'`); + let destInfo = await lstat(dest).catch(() => null); + if (destInfo?.isSymbolicLink() && options.keepDirectorySymlink) { + const followed = await stat(dest).catch(() => null); + if (followed?.isDirectory()) destInfo = followed; + } + if (destInfo && !destInfo.isDirectory()) throw new UsageError(`cannot overwrite non-directory '${dest}' with directory '${src}'`); + const directoryCreation = destInfo + ? { handled: false } + : await withPreservedCopyCreationContext(src, options, () => mkdirSync(dest, { recursive: true, mode: cpInitialDirectoryCreateMode(sourceStat.mode, options) })); + if (destInfo) await mkdir(dest, { recursive: true, mode: cpInitialDirectoryCreateMode(sourceStat.mode, options) }); + if (options.verbose && !destInfo && !topLevel) stdout(`created directory '${dest}'\n`); + // GNU cp -x still creates a mountpoint directory, but does not descend + // into a directory that lives on another filesystem. + const crossFileSystemDirectory = options.oneFileSystem && !topLevel && sourceStat.dev !== options.rootDev; + const dirSource = dereferenceSource && s.isSymbolicLink() ? await realpath(src) : src; + let copyError = null; + if (!crossFileSystemDirectory) { + try { + for await (const entry of iterateDirectoryPathEntries(dirSource)) await copyPath(pathLikeJoin(dirSource, entry), pathLikeJoin(dest, entry), options); + } catch (error) { + copyError = error; + } + } + if (options.preserveDirectoryMode && !destInfo) await setFileMode(dest, sourceStat.mode & 0o7777).catch(() => {}); + // Restore directory metadata after descendants have been copied. This + // includes POSIX ACLs when mode preservation was requested; keeping the + // initial restrictive mode during traversal avoids making a directory + // prematurely accessible. + await applyCopyMetadata(dest, sourceStat, { ...copyMetadataAfterCreationContext(options, directoryCreation.handled), preserveMode: options.preserveMode, sourcePath: src }); + if (copyError) throw copyError; + return; + } else if (s.isSymbolicLink() && !dereferenceSource) { + const linkTarget = await readlink(src); + const destInfo = options.force || options.removeDestination ? null : await lstat(dest).catch(() => null); + if (destInfo?.isSymbolicLink() && await readlink(dest).catch(() => null) === linkTarget && !(options.force || options.removeDestination)) return; + if (options.force || options.removeDestination) await rm(dest, { force: true, recursive: true }); + const symlinkCreation = await withPreservedCopyCreationContext(src, { ...options, noDereferenceContext: true }, () => symlink(linkTarget, dest)); + if (options.preserveMetadata || options.preserveMode || options.preserveContext || options.restoreContext || options.explicitContext != null) { + await applySymlinkCopyMetadata(dest, sourceStat, { ...copyMetadataAfterCreationContext(options, symlinkCreation.handled), sourcePath: src }); + } + } else { + if (options.removeDestination) await rm(dest, { force: true }); + if (await cpDestinationIsDanglingSymlink(dest)) { + await cpHandleDanglingDestinationSymlink(src, dest, options); + return; + } + let destInfo = await lstat(dest).catch(() => null); + if (String(dest).endsWith("/") && !destInfo?.isDirectory()) throw new UsageError(`cannot create regular file '${dest}': Not a directory`); + if (destInfo?.isDirectory()) throw new UsageError(`cannot overwrite directory '${dest}' with non-directory '${src}'`); + if (cpShouldRecreateSpecialFile(sourceStat, options)) { + if (destInfo) await rm(dest, { force: true }); + const specialCreation = await withPreservedCopyCreationContext(src, options, () => cpCreateSpecialFile(dest, sourceStat)); + await applyCopyMetadata(dest, sourceStat, { ...copyMetadataAfterCreationContext(options, specialCreation.handled), preserveMode: true, sourcePath: src }); + } else if (options.attributesOnly) { + const attributeCreation = !destInfo + ? await withPreservedCopyCreationContext(src, options, () => writeFile(dest, "")) + : { handled: false }; + await applyCopyMetadata(dest, sourceStat, { ...copyMetadataAfterCreationContext(options, attributeCreation.handled), sourcePath: src }); + if (!options.preserveMode && !destInfo) await setFileMode(dest, cpDefaultFileMode(options.useSourceModeForNewRegularFiles ? sourceStat.mode : undefined)).catch(() => {}); + cpRememberPreservedLink(dest, sourceStat, options); + } else { + const contextPrepared = prepareRegularFilePreservedContext(src, dest, sourceStat, options); + const copyContents = async () => { + if (options.destinationWasReportedExisting && !destInfo) { + options.destinationWasReportedExisting = false; + try { + const staleTarget = await open(dest, "r+"); + await staleTarget.close(); + } catch (error) { + if (error?.code !== "ENOENT") throw error; + } + } + return cpCopyFileContents(src, dest, sourceStat, options); + }; + const copyWithRecovery = () => copyContents().catch(async (error) => { + if (error.code === "EEXIST" && await cpDestinationIsDanglingSymlink(dest)) return cpHandleDanglingDestinationSymlink(src, dest, options); + if ((error.code === "EEXIST" || error.code === "EACCES" || error.code === "EPERM") && options.force) { + await rm(dest, { force: true }); + destInfo = null; + return cpCopyFileContents(src, dest, sourceStat, options); + } + if (error.code === "EEXIST" && await cpCanWriteThroughDestinationSymlink(dest)) return cpCopyFileContents(src, dest, sourceStat, options); + throw error; + }); + const fileCreation = contextPrepared + ? { value: await copyWithRecovery(), handled: true } + : await withPreservedCopyCreationContext(src, options, copyWithRecovery); + if (!cpShouldReadUntilEof(src, sourceStat) && !cpCopiedSpecialContents(sourceStat, options)) await truncate(dest, sourceStat.size).catch(() => {}); + await applyCopyMetadata(dest, sourceStat, { ...copyMetadataAfterCreationContext(options, fileCreation.handled), sourcePath: src }); + if (!options.preserveMode && destInfo && !destInfo.isSymbolicLink()) await setFileMode(dest, destInfo.mode & 0o7777).catch(() => {}); + else if (!options.preserveMode && !destInfo) await setFileMode(dest, cpDefaultFileMode(options.useSourceModeForNewRegularFiles ? sourceStat.mode : undefined)).catch(() => {}); + cpRememberPreservedLink(dest, sourceStat, options); + if (options.debug) { + if (options.verbose && (!topLevel || !sourceStat.isDirectory())) stdout(`'${src}' -> '${dest}'\n`); + stdout(cpDebugLine(options)); + return; + } + } + } + if (options.verbose && (!topLevel || !sourceStat.isDirectory())) stdout(`'${src}' -> '${dest}'\n`); +} + +export async function cpCopyFileContents(src, dest, sourceStat, options = {}) { + if (cpShouldReadUntilEof(src, sourceStat) || cpCopiedSpecialContents(sourceStat, options)) { + await cpCopyStreamContents(src, dest); + return; + } + if (sourceStat?.isFile?.() && options.reflinkMode && options.reflinkMode !== "never") { + if (cpCloneFile(src, dest, sourceStat.mode)) { + options.reflinkAttempted = true; + return; + } + if (options.reflinkMode === "always") throw new UsageError(`failed to clone '${src}' from '${dest}': Operation not supported`); + } + if (options.sparseMode === "never") { + await cpCopyRegularFileContents(src, dest, sourceStat, { sparse: false }); + return; + } + if (options.sparseMode === "always" || cpSourceLooksSparse(sourceStat)) { + const sparseDetection = await cpCopyRegularFileContents(src, dest, sourceStat, { sparse: true, scanZeros: options.sparseMode === "always" }); + if (sparseDetection) options.sparseDetection = sparseDetection; + return; + } + await copyFile(src, dest); +} + +export function cpDebugLine(options) { + const offload = options.sparseMode === "never" || options.reflinkMode === "never" ? "avoided" : "unknown"; + const sparseDetection = options.sparseMode === "never" ? "no" : options.sparseDetection ?? "unknown"; + return `copy offload: ${offload}, reflink: ${options.reflinkAttempted ? "yes" : "no"}, sparse detection: ${sparseDetection}\n`; +} + +export function cpCloneFile(src, dest, sourceMode = 0o666) { + if (typeof src !== "string" || typeof dest !== "string") return false; + let sourceFd = -1; + let destFd = -1; + const temp = join(pathDirname(dest), `.${pathBasename(dest)}.bnu-reflink-${process.pid}-${reflinkTempId++}`); + let cloned = false; + try { + sourceFd = openSync(src, "r"); + destFd = openSync(temp, "wx", sourceMode & 0o777); + if (libc.symbols.ioctl(destFd, FICLONE, sourceFd) !== 0) return false; + cloned = true; + closeSync(destFd); + destFd = -1; + renameSync(temp, dest); + return true; + } catch { + return false; + } finally { + if (destFd >= 0) closeSync(destFd); + if (sourceFd >= 0) closeSync(sourceFd); + if (!cloned) { + try { + unlinkSync(temp); + } catch {} + } + } +} + +export async function cpCopyRegularFileContents(src, dest, sourceStat, options = {}) { + const destInfo = await stat(dest).catch(() => null); + if (destInfo && !destInfo.isFile()) { + await cpCopyStreamContents(src, dest); + return; + } + const source = await open(src, "r"); + const target = await open(dest, "w", 0o600); + const buffer = Buffer.allocUnsafe(64 * 1024); + let position = 0; + let copiedExtents = false; + try { + copiedExtents = options.sparse && await cpCopySparseExtents(source, target, sourceStat.size, buffer, options.scanZeros); + if (!copiedExtents) { + while (position < sourceStat.size) { + const { bytesRead } = await source.read(buffer, 0, Math.min(buffer.length, sourceStat.size - position), position); + if (bytesRead === 0) break; + const chunk = buffer.subarray(0, bytesRead); + if (!(options.sparse && ddBufferIsZero(chunk))) await target.write(chunk, 0, chunk.length, position); + position += bytesRead; + } + } + } finally { + await source.close().catch(() => {}); + await target.close().catch(() => {}); + } + await truncate(dest, sourceStat.size); + return options.scanZeros ? "zeros" : options.sparse && copiedExtents ? "SEEK_HOLE" : null; +} + +export async function cpCopySparseExtents(source, target, size, buffer, scanZeros = false) { + let offset = 0; + while (offset < size) { + const dataResult = libc.symbols.lseek(source.fd, BigInt(offset), SEEK_DATA); + if (dataResult < 0) { + const errno = libcErrno(); + return errno === 6; // ENXIO means there is no more data after the final hole. + } + const data = Number(dataResult); + const holeResult = libc.symbols.lseek(source.fd, BigInt(data), SEEK_HOLE); + if (holeResult < 0) return false; + const hole = Math.min(size, Number(holeResult)); + let position = data; + while (position < hole) { + const { bytesRead } = await source.read(buffer, 0, Math.min(buffer.length, hole - position), position); + if (bytesRead === 0) break; + const chunk = buffer.subarray(0, bytesRead); + if (!scanZeros || !ddBufferIsZero(chunk)) await target.write(chunk, 0, bytesRead, position); + position += bytesRead; + } + if (position < hole) return false; + offset = hole; + } + return true; +} + +export function cpSourceLooksSparse(sourceStat) { + return sourceStat?.isFile?.() && sourceStat.size > 0 && sourceStat.blocks != null && sourceStat.blocks * 512 < sourceStat.size; +} + +export async function cpCopyStreamContents(src, dest) { + const destExisted = Boolean(await lstat(dest).catch(() => null)); + const source = await open(src, "r"); + const target = await open(dest, "w", 0o600); + const buffer = Buffer.allocUnsafe(64 * 1024); + try { + if (!destExisted) await setFileMode(dest, 0o600).catch(() => {}); + while (true) { + const { bytesRead } = await source.read(buffer, 0, buffer.length, null); + if (bytesRead === 0) break; + await target.write(buffer.subarray(0, bytesRead)); + } + } finally { + await source.close().catch(() => {}); + await target.close().catch(() => {}); + } +} + +export function cpCopiedSpecialContents(sourceStat, options = {}) { + return Boolean(options.copyContents && !sourceStat?.isFile?.()); +} + +export function cpShouldReadUntilEof(src, sourceStat) { + if (sourceStat?.isCharacterDevice?.()) return true; + if (!sourceStat?.isFile?.()) return false; + const path = pathDisplayName(src); + return path === "/proc" || path.startsWith("/proc/") || path === "/sys" || path.startsWith("/sys/"); +} + +export async function cpMaybePreserveExistingLink(src, dest, sourceStat, linkStat, dereferenceSource, options) { + if (!options.preserveLinks || !options.linkMap || !sourceStat.isFile()) return false; + if (linkStat.isSymbolicLink() && !dereferenceSource) return false; + const key = cpLinkKey(sourceStat); + const previousDest = options.linkMap.get(key); + if (!previousDest) return false; + if (options.removeDestination || options.force) await rm(dest, { force: true, recursive: true }); + else if (await lstat(dest).catch(() => null)) await rm(dest, { force: true, recursive: true }); + await fsLink(previousDest, dest); + return true; +} + +export function cpRememberPreservedLink(dest, sourceStat, options) { + if (!options.preserveLinks || !options.linkMap || !sourceStat.isFile()) return; + const key = cpLinkKey(sourceStat); + if (!options.linkMap.has(key)) options.linkMap.set(key, dest); +} + +export function cpLinkKey(statInfo) { + return `${statInfo.dev}:${statInfo.ino}`; +} + +export function cpDirectoryCreateMode(sourceMode, options) { + if (!options.preserveDirectoryMode) return cpDefaultDirectoryMode(); + return (sourceMode & 0o7777) | 0o700; +} + +export function cpInitialDirectoryCreateMode(sourceMode, options) { + if (options.preserveMode || options.preserveMetadata) return cpRestrictiveDirectoryMode(sourceMode); + return cpDirectoryCreateMode(sourceMode, options); +} + +export function cpDefaultFileMode(sourceMode = 0o666) { + return (sourceMode & 0o777) & ~process.umask(); +} + +export function cpDefaultDirectoryMode() { + return 0o777 & ~process.umask(); +} + +export async function cpCreateFifo(path, mode) { + if (libc.symbols.mkfifo(cstr(path), mode) !== 0) throw new Error(`cannot create fifo '${path}'`); + await setFileMode(path, mode).catch(() => {}); +} + +export function cpShouldRecreateSpecialFile(sourceStat, options) { + return options.recursive && !options.copyContents + && (sourceStat.isFIFO() || sourceStat.isCharacterDevice() || sourceStat.isBlockDevice() || sourceStat.isSocket()); +} + +export async function cpCreateSpecialFile(path, sourceStat) { + if (sourceStat.isFIFO()) return cpCreateFifo(path, sourceStat.mode & 0o7777); + if (libc.symbols.mknod(cstrPath(path), sourceStat.mode >>> 0, sourceStat.rdev) !== 0) { + const errno = libcErrno(); + const error = new Error(`mknod failed (errno ${errno})`); + error.code = ({ 1: "EPERM", 13: "EACCES", 17: "EEXIST", 22: "EINVAL" })[errno] ?? "EIO"; + throw error; + } +} + +export function cpShouldDereferenceSource(src, options, topLevel) { + if (String(src).endsWith("/")) return true; + if (options.noDereference) return false; + if (options.dereferenceAll) return true; + if (options.dereferenceCommandLine && topLevel) return true; + if (options.hardLink && topLevel) return !options.preserveLinks; + return topLevel && !options.recursive; +} + +export async function cpCanWriteThroughDestinationSymlink(dest) { + const info = await lstat(dest).catch(() => null); + if (!info?.isSymbolicLink()) return false; + return Boolean(await stat(dest).catch(() => null)); +} + +export async function cpDestinationIsDanglingSymlink(dest) { + const info = await lstat(dest).catch(() => null); + if (!info?.isSymbolicLink()) return false; + try { + await stat(dest); + return false; + } catch (error) { + if (error?.code === "ENOENT" || error?.code === "ELOOP") return true; + throw new UsageError(`cannot stat '${dest}': ${errnoMessage(error)}`); + } +} + +export async function cpHandleDanglingDestinationSymlink(src, dest, options) { + const linkTarget = await readlink(dest); + if (process.env.POSIXLY_CORRECT) return copyFile(src, isAbsolute(linkTarget) ? linkTarget : join(pathDirname(dest), linkTarget)); + if (options.force && linkTarget === pathBasename(dest)) { + await rm(dest, { force: true }); + return copyFile(src, dest); + } + throw new UsageError(`not writing through dangling symlink '${dest}'`); +} + +export async function applyCopyMetadata(path, s, options) { + if (!options.preserveMetadata && !options.preserveMode && !options.preserveXattrs && !options.preserveContext && !options.restoreContext && options.explicitContext == null) return; + await applyCopySecurityContext(path, options.sourcePath, options); + if (options.preserveMode && options.sourcePath != null) await copyPosixAcl(options.sourcePath, path).catch(() => {}); + let ownershipPreserved = true; + if (options.preserveMetadata) { + let current = await stat(path).catch(() => null); + if (!current || current.uid !== s.uid) { + await chown(path, s.uid, current?.gid ?? s.gid).catch(() => { ownershipPreserved = false; }); + current = await stat(path).catch(() => current); + } + // Preserve the group independently. A non-root owner may be unable to + // assume the source UID but can still select a supplementary source GID. + if (!current || current.gid !== s.gid) { + await chown(path, current?.uid ?? s.uid, s.gid).catch(() => { ownershipPreserved = false; }); + current = await stat(path).catch(() => current); + } + ownershipPreserved = ownershipPreserved && current?.uid === s.uid && current?.gid === s.gid; + await touchSetPathTimes(path, touchStatDate(s, "atime"), touchStatDate(s, "mtime"), false).catch(() => {}); + } + // chown(2) clears security.capability. Apply xattrs only after ownership + // has reached its final value so --preserve=xattr/all retains file caps. + if (options.preserveXattrs && options.sourcePath != null) { + try { + await copyExtendedAttributes(options.sourcePath, path, Boolean(options.noDereferenceContext)); + } catch (error) { + if (options.requirePreserveXattrs) throw error; + } + } + // chown(2) clears setuid/setgid bits, so restore the source mode last. + // If ownership could not be preserved, GNU cp deliberately leaves those + // privilege bits cleared rather than granting them to the copying user. + if (options.preserveMode) { + const mode = (s.mode & 0o7777) & (ownershipPreserved ? 0o7777 : ~0o6000); + await setFileMode(path, mode).catch(() => {}); + } +} + +export async function copyPosixAcl(src, dest) { + let dumped; + try { + dumped = Bun.spawnSync(["getfacl", "--absolute-names", "--", String(src)], { stdout: "pipe", stderr: "ignore" }); + } catch { + return; + } + if (dumped.exitCode !== 0 || !dumped.stdout?.byteLength) return; + const lines = new TextDecoder().decode(dumped.stdout).split(/\r?\n/); + const fileLine = lines.findIndex((line) => line.startsWith("# file: ")); + if (fileLine < 0) return; + lines[fileLine] = `# file: ${dest}`; + const dir = await mkdtemp("/tmp/bnu-acl-"); + const restore = join(dir, "restore"); + try { + await writeFile(restore, lines.join("\n")); + Bun.spawnSync(["setfacl", "--restore", restore], { stdout: "ignore", stderr: "ignore" }); + } finally { + await rm(dir, { recursive: true, force: true }); + } +} + +export async function applySymlinkCopyMetadata(path, s, options) { + if (!options.preserveMetadata && !options.preserveMode && !options.preserveXattrs && !options.preserveContext && !options.restoreContext && options.explicitContext == null) return; + await applyCopySecurityContext(path, options.sourcePath, { ...options, noDereferenceContext: true }); + if (options.preserveXattrs && options.sourcePath != null) { + try { + await copyExtendedAttributes(options.sourcePath, path, true); + } catch (error) { + if (options.requirePreserveXattrs) throw error; + } + } + if (options.preserveMetadata) { + await touchSetPathTimes(path, touchStatDate(s, "atime"), touchStatDate(s, "mtime"), true).catch(() => {}); + await lchown(path, s.uid, s.gid).catch(() => {}); + } +} + +export async function applyCopySecurityContext(path, sourcePath, options) { + if (!options.selinuxEnabled) return; + const noDereference = Boolean(options.noDereferenceContext); + if (options.explicitContext != null) { + if (!setSelinuxSecurityContext(path, options.explicitContext, noDereference)) throw new UsageError(`failed to set the security context of '${path}'`); + return; + } + if (options.restoreContext) { + // -Z starts from the source label even when the destination already + // exists. restorecon may then replace it with a pathname default; if no + // default exists, GNU cp leaves this source label in place. + if (options.preserveContextAtCreation && sourcePath != null) { + const sourceContext = selinuxSecurityContext(sourcePath, noDereference); + if (sourceContext != null) setSelinuxSecurityContext(path, sourceContext, noDereference); + } + if (!restoreSelinuxSecurityContext(path, noDereference) && options.requireRestoreContext) { + throw new UsageError(`failed to restore the security context of '${path}'`); + } + return; + } + if (!options.preserveContext || sourcePath == null) return; + const context = selinuxSecurityContext(sourcePath, noDereference); + if (context == null) { + if (options.requirePreserveContext) throw new UsageError(`failed to get security context of '${sourcePath}'`); + return; + } + if (!setSelinuxSecurityContext(path, context, noDereference) && options.requirePreserveContext) { + throw new UsageError(`failed to set the security context of '${path}'`); + } +} + +export async function copyExtendedAttributes(src, dest, noDereference = false) { + const getArgs = ["getfattr", ...(noDereference ? ["-h"] : []), "--dump", "--absolute-names", "-m", "-", String(src)]; + const dumped = Bun.spawnSync(getArgs, { stdout: "pipe", stderr: "pipe" }); + if (dumped.exitCode !== 0) throw new UsageError(`setting attributes for '${dest}': ${xattrToolError(dumped.stderr)}`); + const text = new TextDecoder().decode(dumped.stdout ?? new Uint8Array()); + const attributes = text.split(/\r?\n/).filter((line) => /^[^#=][^=]*=/.test(line) + && !line.startsWith("security.selinux=") && !line.startsWith("system.posix_acl_")); + const destStat = noDereference ? null : await lstat(dest).catch(() => null); + const restoreMode = destStat && (destStat.mode & 0o200) === 0 ? destStat.mode & 0o7777 : null; + try { + if (restoreMode != null) await setFileMode(dest, restoreMode | 0o200); + for (const attribute of attributes) { + const equals = attribute.indexOf("="); + const name = attribute.slice(0, equals); + const value = attribute.slice(equals + 1); + const result = Bun.spawnSync(["setfattr", ...(noDereference ? ["-h"] : []), "-n", name, "-v", value, String(dest)], { stdout: "ignore", stderr: "pipe" }); + if (result.exitCode !== 0) throw new UsageError(`setting attributes for '${dest}': ${xattrToolError(result.stderr)}`); + } + } finally { + if (restoreMode != null) await setFileMode(dest, restoreMode).catch(() => {}); + } +} + +export function xattrToolError(output) { + const message = new TextDecoder().decode(output ?? new Uint8Array()).trim(); + return message.match(/: ([^:\n]+)$/)?.[1] || "Operation not supported"; +} + +export async function* iterateDirectoryPathEntries(path) { + yield* await openDirectoryPathEntries(path); +} + +export function lnQuotedName(name) { + return shellEscapeLsName(pathDisplayName(name), true); +} + +export async function areSameFile(a, b) { + const [as, bs] = await Promise.all([stat(a).catch(() => null), stat(b).catch(() => null)]); + return Boolean(as && bs && as.dev === bs.dev && as.ino === bs.ino); +} + +export async function ensureBackupDoesNotDestroySource(program, src, target, suffixOption, backupOption) { + const backup = backupFileName(target, suffixOption, backupOption); + if (!backup) return; + if (resolve(src) === resolve(backup) || await areSameFile(src, backup)) { + const action = program === "mv" ? "moved" : "copied"; + throw new UsageError(`backing up '${target}' might destroy source; '${src}' not ${action}`); + } +} + +export function backupSuffix(option) { + const suffix = option && option !== true && option !== "simple" ? option : process.env.SIMPLE_BACKUP_SUFFIX ?? "~"; + return String(suffix).includes("/") ? "~" : String(suffix); +} + +export async function backupDestination(target, suffixOption, backupOption = true) { + const s = await lstat(target).catch(() => null); + if (!s) return ""; + const backup = backupFileName(target, suffixOption, backupOption); + if (!backup) return ""; + await fsUnlink(backup).catch((error) => { + if (error?.code !== "ENOENT") throw error; + }); + await rename(target, backup); + return backup; +} + +export function backupFileName(target, suffixOption, backupOption = true) { + const mode = backupMode(backupOption); + if (mode === "none" || mode === "off") return ""; + if (mode === "numbered" || mode === "t") return nextNumberedBackupName(target); + if (mode === "existing" || mode === "nil") return existingNumberedBackupName(target) ? nextNumberedBackupName(target) : `${target}${backupSuffix(suffixOption)}`; + return `${target}${backupSuffix(suffixOption)}`; +} + +export const BACKUP_VALID_ARGUMENTS = [ + ["none", "off"], + ["simple", "never"], + ["existing", "nil"], + ["numbered", "t"], +]; + +export function validateBackupMode(program, backupOption) { + backupMode(backupOption, program); +} + +export function backupMode(backupOption = true, program = null) { + if (backupOption === true || backupOption == null) return backupModeFromEnvironment(program); + const mode = String(backupOption); + if (mode === "") return backupModeFromEnvironment(program); + if (backupModeIsValid(mode)) return mode; + if (program) throw new InvocationError(validBackupModeMessage(mode, "backup type"), 1, true); + return "existing"; +} + +export function backupModeFromEnvironment(program = null) { + const value = process.env.VERSION_CONTROL; + if (value == null || value === "") return "existing"; + if (backupModeIsValid(value)) return value; + if (program) throw new InvocationError(validBackupModeMessage(value, "$VERSION_CONTROL"), 1, true); + return "existing"; +} + +export function backupModeIsValid(value) { + return BACKUP_VALID_ARGUMENTS.some((group) => group.includes(value)); +} + +export function validBackupModeMessage(value, option) { + return `invalid argument ${localeQuotedEscapedDiagnostic(value)} for ${localeQuotedDiagnostic(option)}\nValid arguments are:\n${BACKUP_VALID_ARGUMENTS.map((group) => ` - ${group.map(localeQuotedDiagnostic).join(", ")}`).join("\n")}`; +} + +export function existingNumberedBackupName(target) { + const dir = pathDirname(target); + const base = pathBasename(target); + try { + return readdirSyncNoThrow(dir).some((name) => name.startsWith(`${base}.~`) && /^\.~\d+~$/.test(name.slice(base.length))); + } catch { + return false; + } +} + +export function nextNumberedBackupName(target) { + const dir = pathDirname(target); + const base = pathBasename(target); + let max = 0; + for (const name of readdirSyncNoThrow(dir)) { + if (!name.startsWith(`${base}.~`)) continue; + const match = name.slice(base.length).match(/^\.~(\d+)~$/); + if (match) max = Math.max(max, Number(match[1])); + } + return join(dir, `${base}.~${max + 1}~`); +} + +export async function resolveDestinationOperands(program, operands, opts) { + const explicitTarget = opts.t ?? opts["target-directory"]; + const noTargetDirectory = opts.T || opts["no-target-directory"]; + if (explicitTarget && noTargetDirectory) throw new UsageError("cannot combine --target-directory and --no-target-directory"); + if (explicitTarget) { + if (!operands.length) throw new UsageError("missing file operand", true); + const destStat = await stat(explicitTarget).catch((error) => { + if (program === "ln" && error?.code === "ENOENT") throw new UsageError(`failed to access ${lnQuotedName(explicitTarget)}: ${errnoMessage(error)}`); + if ((program === "cp" || program === "mv") && error?.code === "ENOENT") throw new UsageError(`target directory '${explicitTarget}': ${errnoMessage(error)}`); + if (error?.code !== "ENOENT") throw new UsageError(`target directory '${explicitTarget}': ${errnoMessage(error)}`); + return null; + }); + if (program === "ln" && !destStat?.isDirectory()) throw new UsageError(`target '${explicitTarget}' is not a directory`); + if (!destStat?.isDirectory()) throw new UsageError(`target directory '${explicitTarget}': Not a directory`); + return { sources: operands, dest: explicitTarget, destStat, useDirectoryTarget: true }; + } + if (operands.length < 2) { + if ((program === "cp" || program === "mv" || program === "ln") && operands.length === 0) throw new UsageError("missing file operand", true); + const suffix = operands.length === 1 ? ` after '${operands[0]}'` : ""; + throw new UsageError(`missing destination file operand${suffix}`, true); + } + const dest = operands.at(-1); + const sources = operands.slice(0, -1); + if (sources.length > 1 && noTargetDirectory) throw new UsageError(`extra operand '${dest}'`, true); + const destStat = program === "cp" ? await cpDestinationStat(dest) : await stat(dest).catch(() => null); + const useDirectoryTarget = !noTargetDirectory && Boolean(destStat?.isDirectory()); + if (sources.length > 1 && !useDirectoryTarget && !destStat) throw new UsageError(`target '${dest}': No such file or directory`); + if (sources.length > 1 && !useDirectoryTarget) throw new UsageError(`target '${dest}': Not a directory`); + return { sources, dest, destStat, useDirectoryTarget }; +} + +export let cpStatApi; + +export async function cpDestinationStat(path) { + if (!process.env.LD_PRELOAD) return await stat(path).catch(() => null); + if (!cpStatApi) { + const address = libc.symbols.dlsym(0, ptr(Buffer.from("fstatat\0"))); + cpStatApi = linkSymbols({ + fstatat: { ptr: address, args: [FFIType.i32, FFIType.cstring, FFIType.ptr, FFIType.i32], returns: FFIType.i32 }, + }).symbols; + } + const buffer = Buffer.alloc(144); + if (cpStatApi.fstatat(AT_FDCWD, cstrPath(path), ptr(buffer), 0) !== 0) return null; + const mode = buffer.readUInt32LE(24); + return { isDirectory: () => (mode & 0xf000) === 0x4000 }; +} + +export function readdirSyncNoThrow(path) { + try { + return readdirSync(path); + } catch { + return []; + } +} diff --git a/src/shared/diagnostics.js b/src/shared/diagnostics.js new file mode 100644 index 0000000..6c3527b --- /dev/null +++ b/src/shared/diagnostics.js @@ -0,0 +1,550 @@ +import { commandNames } from "./catalog.js"; +import { writeSync } from "node:fs"; +import { COMMAND_HELP_OPTIONS } from "./help-options.js"; +import { SPECIAL_HELP } from "./help.js"; +import { isOutputWriteError } from "./runtime.js"; + +export const VERSION = "bnu 9.11"; + +export class UsageError extends Error { + constructor(message, showHelp = false) { + super(message); + this.showHelp = showHelp; + } +} + +export class InvocationError extends UsageError { + constructor(message, code = 125, showHelp = true) { + super(message, showHelp); + this.code = code; + } +} + +export const emittedDiagnosticHints = new Set(); + +export function stdout(data = "") { + try { + writeAllSync(1, data instanceof Uint8Array ? data : String(data)); + } catch (error) { + if (error?.code === "EPIPE") process.exit(0); + throw error; + } +} + +export function stderr(data = "", ensureHint = false) { + const text = String(data); + writeAllSync(2, gnulyCorrectDiagnostics() ? text : enhanceDiagnostic(text, ensureHint)); +} + +export function gnulyCorrectDiagnostics() { + const value = process.env.GNULY_CORRECT; + return value !== undefined && !/^(?:|0|false)$/i.test(value); +} + +export function enhanceDiagnostic(text, ensureHint = false) { + if (!text.endsWith("\n") || /^(?:Hint|Warning):/m.test(text)) return text; + const firstLine = text.split("\n", 1)[0]; + if (!/^[^:\n]+:\s/.test(firstLine) || /:\s*(?:warning|debug):?\s/i.test(firstLine)) return text; + const program = firstLine.slice(0, firstLine.indexOf(":")); + const hint = diagnosticHint(program, text) ?? (ensureHint ? genericDiagnosticHint(program) : null); + if (!hint || emittedDiagnosticHints.has(hint)) return text; + emittedDiagnosticHints.add(hint); + const tryLine = text.indexOf("\nTry '"); + if (tryLine !== -1) return `${text.slice(0, tryLine + 1)}Hint: ${hint}\n${text.slice(tryLine + 1)}`; + return `${text}Hint: ${hint}\n`; +} + +export function diagnosticHint(program, diagnostic) { + if (program === "comm" && /^comm: -\n?$/.test(diagnostic)) { + return "Use standard input for only one input file; the other operand must name a file."; + } + const unknownCommand = diagnostic.match(/unknown command ['‘]([^'’]+)['’]/)?.[1]; + if (unknownCommand) { + const suggestion = closestValue(unknownCommand, commandNames); + return suggestion + ? `Did you mean '${suggestion}'? Run 'bnu --help' to list commands.` + : "Run 'bnu --help' to list available commands."; + } + const longOption = diagnostic.match(/unrecognized option ['‘](--[^'’=]+)(?:=[^'’]*)?['’]/)?.[1]; + if (longOption) { + const suggestion = closestLongOption(program, longOption); + return suggestion + ? `Did you mean '${suggestion}'? Run '${program} --help' to see all options.` + : `Check the option spelling, or run '${program} --help' to see all options.`; + } + if (/invalid option -- |option .* is ambiguous/.test(diagnostic)) { + return `Check the option spelling, or run '${program} --help' to see all options.`; + } + if (/option .*requires an argument|option requires an argument/.test(diagnostic)) { + const option = diagnostic.match(/option ['‘](--[^'’]+)['’] requires/)?.[1] + ?? diagnostic.match(/option requires an argument -- ['‘]([^'’]+)['’]/)?.[1]; + return option?.startsWith("--") + ? `Provide the missing value as '${option}=VALUE' or '${option} VALUE'.` + : option + ? `Provide the missing value after '-${option}'.` + : `Provide a value for the option. Run '${program} --help' for the expected form.`; + } + if (/doesn't allow an argument/.test(diagnostic)) { + const option = diagnostic.match(/option ['‘](--[^'’]+)['’]/)?.[1]; + return option ? `Use '${option}' by itself; this flag does not take a value.` : "Remove the value supplied to this flag."; + } + if (/missing operand|missing file operand|a command must be given/.test(diagnostic)) { + return commandUsageHint(program, "Add the required argument"); + } + if (/extra operand|extra argument|too many arguments/.test(diagnostic)) { + return commandUsageHint(program, "Remove the extra argument"); + } + if (/mutually exclusive|cannot combine|may not be combined|are incompatible|conflicting .* specifiers|cannot specify .* more than one source/.test(diagnostic)) { + return "Remove one of the conflicting options and try again."; + } + if (/ambiguous argument/.test(diagnostic)) { + return `Use a complete, unambiguous value. Run '${program} --help' for accepted values.`; + } + if (/Valid arguments are:/.test(diagnostic)) { + return "Choose one of the valid values listed above and try again."; + } + if (/multiple .* (?:specified|sources)|used more than once/.test(diagnostic)) { + return "Keep only one value for this setting, or remove the duplicate option."; + } + if (/only one .* may be specified|cannot .* in more than one way/.test(diagnostic)) { + return "Select one operating mode and remove the other mode options."; + } + if (/delimiter must be a single character|multi-character (?:tab|separator)|separator cannot be empty|empty (?:tab|record separator)/.test(diagnostic)) { + return "Use exactly one character as the delimiter or separator."; + } + if (/numbered from 1|field number is zero|character offset is zero/.test(diagnostic)) { + return "Use a position of 1 or greater; zero is not a valid field or character position."; + } + if (/invalid decreasing range|range-endpoints .* reverse|tab sizes must be ascending/.test(diagnostic)) { + return "Put the lower position first and list ranges in ascending order."; + } + if (/invalid range with no endpoint|invalid .* range|invalid field specification/.test(diagnostic)) { + return "Use a range such as 'N', 'N-M', 'N-', or '-M' with valid positive positions."; + } + if (/meaningless|meaningful only|only supported|does not support|only when|makes sense/.test(diagnostic)) { + return "Remove the option that has no effect here, or enable the mode that option requires."; + } + if (/no lines to repeat/.test(diagnostic)) { + return "Provide non-empty input, or remove --repeat."; + } + if (/missing (?:encoding type|mode)|must specify .* mode|must specify a list/.test(diagnostic)) { + return commandUsageHint(program, "Select the required mode"); + } + if (/template .* must end in X|too few X's in template|invalid (?:template|suffix).*directory separator/.test(diagnostic)) { + return "Use a template ending in at least three X characters and keep any suffix free of '/'."; + } + if (/out of range|overflow|value too (?:large|small)/i.test(diagnostic)) { + return "Choose a value within the supported range and try again."; + } + if (/invalid zero-length file name|empty file name/.test(diagnostic)) { + return "Remove the empty path entry from the input list."; + } + if (/when reading file names from standard input, no file name of '-' allowed/.test(diagnostic)) { + return "Remove '-' from the file-name list; standard input is already being used for that list."; + } + if (/No such file or directory|No such device or address/.test(diagnostic)) { + return "Check that the path exists and is spelled correctly."; + } + if (/Permission denied|Operation not permitted/.test(diagnostic)) { + return "Check the file permissions and whether this operation needs additional privileges."; + } + if (/Is a directory/.test(diagnostic)) { + return "Use a file instead, or select an option that supports directories."; + } + if (/Not a directory/.test(diagnostic)) { + return "Check that every parent component of the path is a directory."; + } + if (/Directory not empty/.test(diagnostic)) { + return "Remove the directory contents first, or use the command's recursive option if appropriate."; + } + if (/File exists/.test(diagnostic)) { + return "Choose a different destination or remove the existing one first."; + } + if (/No space left on device/.test(diagnostic)) { + return "Free some space on the destination filesystem and try again."; + } + if (/Disk quota exceeded/.test(diagnostic)) { + return "Free space within your quota or choose a different destination."; + } + if (/File too large/.test(diagnostic)) { + return "Use a smaller output or a filesystem that supports larger files."; + } + if (/File name too long/.test(diagnostic)) { + return "Shorten the file name or one of its parent directory names."; + } + if (/Read-only file system/.test(diagnostic)) { + return "Choose a writable destination or remount the filesystem read-write."; + } + if (/Too many open files/.test(diagnostic)) { + return "Close unused file descriptors or raise the process file-descriptor limit."; + } + if (/Cannot allocate memory|Out of memory/.test(diagnostic)) { + return "Reduce the input size or memory usage, or make more memory available."; + } + if (/Device or resource busy|Text file busy/.test(diagnostic)) { + return "Wait for the resource to become available or stop the process currently using it."; + } + if (/Invalid cross-device link/.test(diagnostic)) { + return "Use a copy followed by removal when moving data between filesystems."; + } + if (/Operation not supported|Not supported/.test(diagnostic)) { + return "Choose an operation supported by this filesystem or platform."; + } + if (/Bad file descriptor/.test(diagnostic)) { + return "Check that the referenced input or output descriptor is open and usable."; + } + if (/Input\/output error/.test(diagnostic)) { + return "Check the device and filesystem health, then retry the operation."; + } + if (/Too many levels of symbolic links/.test(diagnostic)) { + return "Check the path for a symbolic-link loop."; + } + if (/input is not in sorted order|: is not sorted:/.test(diagnostic)) { + return "Sort each input using the same comparison rules, then try again."; + } + if (/omitting directory/.test(diagnostic)) { + return `Add the recursive option if you intend ${program} to process a directory tree.`; + } + if (/are the same file/.test(diagnostic)) { + return "Choose a destination that is different from the source."; + } + if (/no such user|no such group/.test(diagnostic)) { + return "Check that the account name exists, or use a numeric ID where supported."; + } + if (/no login name/.test(diagnostic)) { + return "Run this from a login session, or use a command such as 'whoami' for the effective user."; + } + if (/cannot skip past end|cannot skip to specified offset/.test(diagnostic)) { + return "Reduce the requested offset so it falls within the available input."; + } + if (/input contains a loop/.test(diagnostic)) { + return "Remove a dependency cycle from the input graph and try again."; + } + if (/input contains an odd number of tokens/.test(diagnostic)) { + return "Provide dependency tokens in pairs: one item followed by the item that depends on it."; + } + if (/not listing already-listed directory/.test(diagnostic)) { + return "Check the directory tree for a cycle, usually caused by a symbolic link."; + } + if (/no files remaining/.test(diagnostic)) { + return "Check the input paths, or use --retry when following names that may reappear."; + } + if (/no file systems processed/.test(diagnostic)) { + return "Check the requested paths and filesystem-type filters."; + } + if (/couldn't get boot time/.test(diagnostic)) { + return "Check that the system exposes readable boot-time accounting information."; + } + if (/no input from|cannot read file names from/.test(diagnostic)) { + return "Check that the file-name list is readable and contains valid entries."; + } + if (/using '-' to denote standard input does not work in file system mode/.test(diagnostic)) { + return "Name a filesystem path instead of '-' when using filesystem mode."; + } + if (/regular expression has length zero|invalid (?:regular expression|pattern)/.test(diagnostic)) { + return "Use a non-empty, valid regular expression for the selected command."; + } + if (/match not found/.test(diagnostic)) { + return "Adjust the pattern so it matches the input, or remove that split point."; + } + if (/line number .* smaller than preceding|line number .* same as preceding/.test(diagnostic)) { + return "List split line numbers once each and in strictly increasing order."; + } + if (/starting page number .* exceeds page count/.test(diagnostic)) { + return "Choose a starting page that exists in the input."; + } + if (/closing delimiter .* missing|integer expected after delimiter/.test(diagnostic)) { + return "Close the pattern delimiter and place any numeric offset after it."; + } + if (/syntax error|unexpected argument|expecting ['‘]?\)|Unmatched|Trailing backslash/.test(diagnostic)) { + return "Correct the expression syntax, balancing parentheses, escapes, and operators."; + } + if (/non-integer argument/.test(diagnostic)) { + return "Use integer operands for arithmetic operations."; + } + if (/division by zero/.test(diagnostic)) { + return "Use a nonzero divisor."; + } + if (/format .* (?:ends in %|has no % directive|has too many % directives|has unknown .* directive)|invalid precision in format/.test(diagnostic)) { + return invalidValueHint(program, diagnostic); + } + if (/checksum.*(?:did NOT match|mismatch)|computed checksum.*not match/i.test(diagnostic)) { + return "Verify that the file and expected checksum came from the same source."; + } + if (/no properly formatted checksum lines found|improperly formatted .* checksum line/.test(diagnostic)) { + return "Check the checksum file format and selected checksum algorithm."; + } + if (/failed to set locale/.test(diagnostic)) { + return "Select an installed locale, for example by setting LC_ALL=C."; + } + if (/Host name lookup failure/.test(diagnostic)) { + return "Check the host name and DNS or hosts-file configuration."; + } + if (/can't open/.test(diagnostic)) { + return "Check that the file exists and is readable."; + } + if (/this system doesn't provide/.test(diagnostic)) { + return "Choose a data type or feature supported by this platform."; + } + if (/cannot fstat|failed to stat|cannot stat|cannot access|failed to access/.test(diagnostic)) { + return "Check that the path exists and that its metadata is accessible."; + } + if (/cannot unlink|cannot remove|failed to remove/.test(diagnostic)) { + return "Check that the path exists, is writable, and is not protected or in use."; + } + if (/failed to run command|cannot run|could not run|failed to execute|not found in PATH/.test(diagnostic)) { + return "Check that the command exists, is executable, and is available on PATH."; + } + if (/cannot create link|failed to create link/.test(diagnostic)) { + return "Check the source, destination, permissions, and filesystem link support."; + } + if (/input file is output file/.test(diagnostic)) { + return "Choose an output path different from the input path."; + } + if (/cannot (?:copy|move) a directory.*into itself|cannot move .* subdirectory of itself/.test(diagnostic)) { + return "Choose a destination outside the source directory tree."; + } + if (/will not overwrite just-created/.test(diagnostic)) { + return "Give each source a distinct destination name instead of targeting the same newly created path."; + } + if (/target .* is not a directory|target directory .*Not a directory|destination must be a directory/.test(diagnostic)) { + return "Use an existing directory as the destination, or provide only one source file."; + } + if (/not writing through dangling symlink/.test(diagnostic)) { + return "Repair or remove the dangling destination symlink, then try again."; + } + if (/inter-device move failed.*--no-copy specified/.test(diagnostic)) { + return "Remove --no-copy to allow a copy-and-delete move between filesystems."; + } + if (/cannot do --relative without --symbolic/.test(diagnostic)) { + return "Add --symbolic when using --relative, or remove --relative."; + } + if (/preserve-root/.test(diagnostic) && /unrecognized|may not abbreviate/.test(diagnostic)) { + return "Use the full option '--no-preserve-root', or use '--preserve-root=all'."; + } + if (/dangerous to operate recursively/.test(diagnostic)) { + return "Verify the path carefully; use --no-preserve-root only if removing this protected root is intentional."; + } + if (/skipping .*different device|--preserve-root=all is in effect/.test(diagnostic)) { + return "Remove --one-file-system/--preserve-root=all only if crossing this filesystem boundary is intentional."; + } + if (/--skip-chdir only permitted/.test(diagnostic)) { + return "Use --skip-chdir only with NEWROOT '/', or remove --skip-chdir."; + } + if (/security context|SELinux|SMACK/.test(diagnostic) && /cannot|failed|invalid/.test(diagnostic)) { + return "Use a valid security label and verify that the required security module is enabled."; + } + if (/backing up .* might destroy source/.test(diagnostic)) { + return "Choose a different backup suffix or move the source away from the backup path."; + } + if (/through just-created symlink/.test(diagnostic)) { + return "Remove the conflicting symbolic link or choose a different destination tree."; + } + if (/not replacing|cannot overwrite/.test(diagnostic)) { + return `Choose a different destination or review '${program} --help' for overwrite options.`; + } + if (/\binvalid\b|not a valid|\bunsupported\b/i.test(diagnostic)) { + return invalidValueHint(program, diagnostic); + } + if (/cannot open|failed to open|error reading|read error/.test(diagnostic)) { + return "Check that the input exists and is readable."; + } + if (/cannot create|failed to create|error writing|write error/.test(diagnostic)) { + return "Check that the destination exists, is writable, and has enough free space."; + } + if (/ignoring non-option arguments/.test(diagnostic)) { + return `Remove the operands; '${program}' does not use positional arguments.`; + } + if (informationalDiagnostic(program, diagnostic)) return null; + if (/(?:^|:\s)(?:cannot|could not|failed|error|invalid|unrecognized|unknown|missing|extra|refusing|unable|must|requires?|may not)\b/im.test(diagnostic)) { + return genericDiagnosticHint(program); + } + // Direct command implementations sometimes return terse GNU diagnostics + // without a standard error keyword. Once progress/debug output is excluded, + // still give those failures command-specific recovery guidance. + return invalidValueHint(program, diagnostic); +} + +export function genericDiagnosticHint(program) { + return `Review the arguments and current system state, then run '${program} --help' if the problem persists.`; +} + +export function commandUsageHint(program, action) { + const helpProgram = program === "ginstall" ? "install" : program; + const usage = SPECIAL_HELP[helpProgram]?.usage?.split("\n", 1)[0]?.replace(/^Usage:\s*/, ""); + return usage + ? `${action}. Expected form: ${usage}.` + : `${action}. Run '${program} --help' for the expected form.`; +} + +export function informationalDiagnostic(program, diagnostic) { + if (program === "sort" && /text ordering performed|key \d+ has zero width|numeric and spans multiple fields|leading blanks are significant|field separator|numbers use|options? .* (?:is|are) ignored|only applies to last-resort comparison/.test(diagnostic)) return true; + if (program === "date" && (/only using last of multiple/.test(diagnostic) || (/date: output format:/.test(diagnostic) && !/date: error:/.test(diagnostic)))) return true; + if (program === "cksum" && /using generic checksum implementation/.test(diagnostic)) return true; + if (program === "numfmt" && /--header ignored|grouping has no effect|no conversion option specified|failed to convert some/.test(diagnostic)) return true; + if (program === "timeout" && /sending signal/.test(diagnostic)) return true; + if (program === "shred" && /: (?:pass \d+\/\d+|removing|renamed to|removed)/.test(diagnostic)) return true; + if (program === "tail" && /using .* mode|has appeared;|has been replaced;|file truncated|reverting to polling/.test(diagnostic)) return true; + if (program === "executing" || program === "argv0") return true; + return false; +} + +export function invalidValueHint(program, diagnostic) { + if (/Valid arguments are:/.test(diagnostic)) return "Choose one of the valid values listed above and try again."; + const hints = { + basename: "Check the suffix and file-name arguments shown by 'basename --help'.", + basenc: "Check that the input uses the selected encoding and has valid padding.", + base32: "Check that the input is valid base32 data and has correct padding.", + base64: "Check that the input is valid base64 data and has correct padding.", + cat: "Check the input paths and use '-' only when the data should come from standard input.", + chcon: "Use a complete security context, or valid user, role, type, and range components.", + chroot: "Provide a valid new root, user/group specification, and executable command.", + chmod: "Use an octal mode such as '755' or a symbolic mode such as 'u+x'.", + chown: "Use OWNER, OWNER:GROUP, or a numeric user and group ID.", + chgrp: "Use an existing group name or a numeric group ID.", + cksum: "Choose a supported algorithm and a digest length valid for that algorithm.", + csplit: "Use a positive line number or a valid /REGEXP/ split pattern.", + comm: "Provide exactly two sorted inputs and select valid output columns.", + cp: "Check the copy mode, preservation attributes, and source/destination form.", + cut: "Use positive byte, character, or field positions such as '1-3,7'.", + date: "Use a recognized date such as '2026-07-13 14:30' or an ISO 8601 value.", + dd: "Use NAME=VALUE operands and a byte count such as '4K', '1M', or a positive integer.", + df: "Use a supported output field, filesystem type, or block-size value.", + dircolors: "Check the terminal type and dircolors database syntax.", + du: "Use a valid depth, threshold, time style, or block-size value.", + env: "Check the environment assignment, signal name, or quoting in the -S string.", + expand: "Use positive, ascending tab stops or a positive repeating tab size.", + expr: "Use integer operands where arithmetic is required and check the expression syntax.", + factor: "Provide one or more positive integers.", + fmt: "Use a positive width, with the goal width no larger than the maximum width.", + fold: "Use a positive line width and select at most one width-counting mode.", + groups: "Use an existing user name, or omit it to inspect the current process.", + head: "Use a nonnegative count, optionally with a size suffix such as K or M.", + hostname: "Provide a readable file or a valid host name supported by the selected operation.", + id: "Select a compatible output mode and use an existing user name or numeric ID.", + install: "Check the mode, owner/group, context, and source/destination form.", + join: "Use file number 1 or 2 and field numbers starting at 1.", + kill: "Use a valid signal name/number and a numeric process ID.", + mkdir: "Use an octal mode such as '755' or a symbolic mode such as 'u=rwx'.", + mkfifo: "Use an octal mode such as '644' or a symbolic mode.", + mknod: "Use type 'b', 'c', 'u', or 'p' and valid major/minor device numbers where required.", + mktemp: "Use a template ending in at least three X characters, for example 'tmp.XXXXXX'.", + mv: "Check the update/backup mode and provide a valid source and destination.", + nice: "Use an integer adjustment and provide a command to execute.", + nl: "Check the numbering style, numeric width, and regular-expression syntax.", + numfmt: "Use a valid numeric field/range and a supported scaling or format value.", + nproc: "Use a nonnegative integer with --ignore.", + od: "Use a supported type string and an address radix of d, o, x, or n.", + paste: "Use a valid delimiter list; escape a trailing backslash.", + pr: "Use positive page dimensions and a page range such as '2' or '2:5'.", + printf: "Check the conversion directive and ensure each '%' starts a supported format conversion.", + pwd: "Use only -L or -P; remove positional arguments.", + readlink: "Choose a supported canonicalization mode and provide a valid path.", + realpath: "Choose a supported canonicalization mode and provide a valid path.", + rm: "Check the interactive/preserve-root mode and the recursive removal options.", + rmdir: "Name empty directories, or use --ignore-fail-on-non-empty when appropriate.", + runcon: "Provide a valid security context or context components followed by a command.", + seq: "Use finite numeric operands, a nonzero increment, and a floating-point output format.", + shuf: "Use a positive count or an input range in LO-HI form.", + shred: "Use a positive pass count, valid size, and supported removal mode.", + sleep: "Use one or more nonnegative durations, optionally suffixed with s, m, h, or d.", + sort: "Check the key syntax, numeric size, and selected ordering mode.", + split: "Use a positive chunk size/count and a suffix that does not contain '/'.", + stdbuf: "Use buffering mode 'L', '0', or a positive size such as '4K'.", + stty: "Use a supported terminal mode, control character, or numeric speed.", + stat: "Use a supported format, cache policy, or filesystem mode.", + tail: "Use a nonnegative count, valid PID, or supported --follow mode.", + tee: "Use a supported --output-error mode and writable output files.", + test: "Check the expression arity, operators, integer operands, and balanced parentheses.", + timeout: "Use a nonnegative duration such as '30s', '5m', or '1h'.", + touch: "Use a recognized date, [[CC]YY]MMDDhhmm[.ss], or a valid reference file.", + tr: "Check the character sets, ranges, classes, escapes, and repeat counts.", + truncate: "Use an absolute size or a signed adjustment such as '+1K' or '-512'.", + tsort: "Provide whitespace-separated dependency pairs and remove dependency cycles.", + uniq: "Use nonnegative skip/count values and a supported grouping method.", + unexpand: "Use positive, ascending tab stops or a positive repeating tab size.", + wc: "Use a supported count/total mode and valid input or --files0-from list.", + }; + return hints[program] ?? `Check the value's format. Run '${program} --help' for accepted values.`; +} + +export function closestLongOption(program, badOption) { + const helpProgram = program === "ginstall" ? "install" : program; + const options = [...new Set([...(COMMAND_HELP_OPTIONS[helpProgram] ?? []), "--help", "--version"])] + .filter((option) => option.startsWith("--")) + .map((option) => option.split("=", 1)[0]); + const closest = closestValue(badOption.slice(2), options.map((option) => option.slice(2))); + return closest == null ? null : `--${closest}`; +} + +export function closestValue(value, candidates) { + if (!candidates.length) return null; + let best = null; + let bestDistance = Infinity; + for (const candidate of candidates) { + const distance = editDistance(value, candidate); + if (distance < bestDistance) { + best = candidate; + bestDistance = distance; + } + } + return bestDistance <= Math.max(1, Math.floor(value.length / 3)) ? best : null; +} + +export function editDistance(left, right) { + let previous = Array.from({ length: right.length + 1 }, (_, index) => index); + for (let i = 1; i <= left.length; i++) { + const current = [i]; + for (let j = 1; j <= right.length; j++) { + current[j] = Math.min( + current[j - 1] + 1, + previous[j] + 1, + previous[j - 1] + (left[i - 1] === right[j - 1] ? 0 : 1), + ); + } + previous = current; + } + return previous[right.length]; +} + +export function writeAllSync(fd, data) { + const buffer = data instanceof Uint8Array ? data : encodeSurrogateEscapedString(String(data)); + let offset = 0; + while (offset < buffer.length) { + const written = writeSync(fd, buffer, offset, buffer.length - offset); + if (written === 0) throw Object.assign(new Error("write returned 0"), { code: "EIO" }); + offset += written; + } +} + +export function encodeSurrogateEscapedString(text) { + const chunks = []; + let plain = ""; + const flush = () => { + if (plain) { + chunks.push(Buffer.from(plain)); + plain = ""; + } + }; + for (let i = 0; i < text.length; i++) { + const code = text.charCodeAt(i); + if (code >= 0xdc80 && code <= 0xdcff) { + flush(); + chunks.push(Buffer.from([code - 0xdc00])); + } else { + plain += text[i]; + } + } + flush(); + return Buffer.concat(chunks); +} + +export function fail(program, message, code = 1) { + try { + stderr(`${program}: ${message}\n`, true); + } catch (error) { + if (isOutputWriteError(error)) return code; + throw error; + } + return code; +} diff --git a/src/shared/filesystem.js b/src/shared/filesystem.js new file mode 100644 index 0000000..25b5c05 --- /dev/null +++ b/src/shared/filesystem.js @@ -0,0 +1,739 @@ +import { FFIType, linkSymbols, ptr } from "bun:ffi"; +import { existsSync, lstatSync, mkdirSync, readSync } from "node:fs"; +import { chmod as fsChmod, lstat, lutimes, mkdir, opendir, realpath, stat, utimes } from "node:fs/promises"; +import { isAbsolute, dirname as pathDirname, resolve } from "node:path"; +import { AT_FDCWD, AT_SYMLINK_NOFOLLOW, cstr, cstrPath, floorDivBigInt, invalidOptionMessage, isBytePath, libc, libcErrno, localeQuotedEscapedDiagnostic, lsEscapedName, lsUsesUtf8Locale, parseGNUSize, pathDisplayName, rawCommandArgs, readGroup, readPasswd, selinuxAllocatedString, selinuxApi, selinuxRuntimeEnabled, shellEscapeLsName, smackRuntimeEnabled, systemErrorMessage } from "./common.js"; +import { UsageError, stderr, stdout } from "./diagnostics.js"; + +export let stdinLineBuffer = ""; + +export function invalidClusterOptionMessage(arg, validOptions) { + if (arg?.startsWith("--")) return invalidOptionMessage(arg); + const option = [...String(arg ?? "").slice(1)].find((ch) => !validOptions.has(ch)); + return `invalid option -- '${option ?? String(arg ?? "").slice(1, 2)}'`; +} + +export const SPECIAL_FILE_LONG_OPTIONS = ["mode", "context", "help", "version"]; + +export function specialFileMetaOption(program, args) { + let sawContextValue = false; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (!arg.startsWith("--")) continue; + const option = normalizeSpecialFileLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!SPECIAL_FILE_LONG_OPTIONS.includes(name)) return null; + if (option.includes("=")) { + if (name === "help" || name === "version") return null; + if (name === "context") sawContextValue = true; + continue; + } + if (option === "--help" || option === "--version") { + if (sawContextValue && !selinuxRuntimeEnabled()) stderr(`${program}: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n`); + return option; + } + if (name === "mode") i++; + } + return null; +} + +export function validateBlockSizeMetaOption(option, value) { + try { + parseGNUBlockSize(value); + } catch { + throw new UsageError(gnuBlockSizeErrorMessage(option, value), false); + } +} + +export function processCwdOrNull() { + try { + return process.cwd(); + } catch { + return null; + } +} + +export function fileTypeChar(s) { + if (s.isDirectory()) return "d"; + if (s.isSymbolicLink()) return "l"; + if (s.isBlockDevice()) return "b"; + if (s.isCharacterDevice()) return "c"; + if (s.isFIFO()) return "p"; + if (s.isSocket()) return "s"; + return "-"; +} + +export function modeString(s) { + const mode = s.mode; + const chars = ["r", "w", "x"]; + let out = fileTypeChar(s); + for (let shift = 6; shift >= 0; shift -= 3) { + for (let bit = 2; bit >= 0; bit--) { + if (bit === 0 && shift === 6 && (mode & 0o4000)) out += mode & 0o100 ? "s" : "S"; + else if (bit === 0 && shift === 3 && (mode & 0o2000)) out += mode & 0o010 ? "s" : "S"; + else if (bit === 0 && shift === 0 && (mode & 0o1000)) out += mode & 0o001 ? "t" : "T"; + else out += mode & (1 << (shift + bit)) ? chars[2 - bit] : "-"; + } + } + return out; +} + +export function blocksFor(s) { + return s.blocks ?? Math.ceil(s.size / 512); +} + +export function defaultGNUBlockSize() { + return process.env.POSIXLY_CORRECT ? 512 : 1024; +} + +export function optionValues(value) { + if (value == null) return []; + return Array.isArray(value) ? value : [value]; +} + +export function selinuxSecurityContext(path, noDereference = false) { + if (process.platform !== "linux") return null; + // A disk can retain stale SELinux attributes after being booted with SMACK. + // GNU coreutils selects the active LSM, so prefer SMACK64 whenever SMACK is + // active rather than returning an unrelated security.selinux attribute. + if (smackRuntimeEnabled()) return securityXattrText(path, "security.SMACK64", noDereference); + const api = selinuxApi(); + if (api) { + const getter = selinuxInterposedContextGetter(api, noDereference); + return selinuxAllocatedString(api, (output) => getter(cstrPath(path), output)); + } + try { + const result = Bun.spawnSync(["getfattr", ...(noDereference ? ["-h"] : []), "--only-values", "-n", "security.selinux", "--", String(path)], { stdout: "pipe", stderr: "ignore" }); + if (result.exitCode !== 0 || !result.stdout?.byteLength) return null; + // Linux stores security.selinux as a NUL-terminated context. getfattr's + // --only-values output preserves that terminator, unlike ordinary text + // attributes, so remove it along with any line ending. + const value = new TextDecoder().decode(result.stdout).replace(/[\0\r\n]+$/, ""); + return value || null; + } catch { + return null; + } +} + +export function securityXattrText(path, name, noDereference = false) { + try { + const result = Bun.spawnSync(["getfattr", ...(noDereference ? ["-h"] : []), "--only-values", "-n", name, "--", String(path)], { stdout: "pipe", stderr: "ignore" }); + if (result.exitCode !== 0 || !result.stdout?.byteLength) return null; + return new TextDecoder().decode(result.stdout).replace(/[\0\r\n]+$/, "") || null; + } catch { + return null; + } +} + +export const selinuxContextGetters = new Map(); + +export function selinuxInterposedContextGetter(api, noDereference) { + const name = noDereference ? "lgetfilecon" : "getfilecon"; + if (selinuxContextGetters.has(name)) return selinuxContextGetters.get(name); + const address = libc.symbols.dlsym(0, ptr(Buffer.from(`${name}\0`))); + const getter = address + ? linkSymbols({ [name]: { ptr: address, args: [FFIType.cstring, FFIType.ptr], returns: FFIType.i32 } }).symbols[name] + : api.symbols[name]; + selinuxContextGetters.set(name, getter); + return getter; +} + +export let lastSecurityContextErrno = 0; + +export function setSelinuxSecurityContext(path, context, noDereference = false) { + if (smackRuntimeEnabled()) { + const value = Buffer.from(String(context)); + const errnoPointer = libc.symbols.__errno_location(); + if (errnoPointer) libc.symbols.memset(errnoPointer, 0, 4); + const setter = noDereference ? libc.symbols.lsetxattr : libc.symbols.setxattr; + const result = setter(cstrPath(path), cstr("security.SMACK64"), ptr(value), BigInt(value.byteLength), 0); + lastSecurityContextErrno = result === 0 ? 0 : libcErrno(); + return result === 0; + } + lastSecurityContextErrno = 0; + const api = existsSync("/sys/fs/selinux/enforce") ? selinuxApi() : null; + if (api) { + if (api.symbols.security_check_context(cstr(context)) !== 0) return false; + const setter = noDereference ? api.symbols.lsetfilecon : api.symbols.setfilecon; + return setter(cstrPath(path), cstr(context)) === 0; + } + try { + const result = Bun.spawnSync(["setfattr", ...(noDereference ? ["-h"] : []), "-n", "security.selinux", "-v", String(context), "--", String(path)], { stdout: "ignore", stderr: "ignore" }); + return result.exitCode === 0; + } catch { + return false; + } +} + +export function restoreSelinuxSecurityContext(path, noDereference = false, recursive = false) { + try { + const result = Bun.spawnSync(["restorecon", ...(noDereference ? ["-h"] : []), ...(recursive ? ["-R"] : []), "-F", "--", String(path)], { stdout: "ignore", stderr: "ignore" }); + return result.exitCode === 0; + } catch { + return false; + } +} + +export function selinuxCreationOptions(program, opts) { + const enabled = selinuxRuntimeEnabled(); + const explicitContext = typeof opts.context === "string" && enabled ? opts.context : null; + if (typeof opts.context === "string" && !enabled) { + stderr(`${program}: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n`); + } + return { + enabled, + explicitContext, + restoreContext: enabled && (opts.Z || opts.context === true), + }; +} + +export async function withSelinuxCreationContext(program, options, create) { + const api = options.explicitContext != null && existsSync("/sys/fs/selinux/enforce") ? selinuxApi() : null; + let defaultContextSet = false; + if (api) { + if (api.symbols.security_check_context(cstr(options.explicitContext)) !== 0 + || api.symbols.setfscreatecon(ptr(cstr(options.explicitContext))) !== 0) { + throw new UsageError(`failed to set default file creation context to '${options.explicitContext}': Invalid argument`); + } + defaultContextSet = true; + } + const created = async (path) => { + if (options.explicitContext != null && !defaultContextSet) { + if (!setSelinuxSecurityContext(path, options.explicitContext)) { + const message = lastSecurityContextErrno === 1 ? "Operation not permitted" : "Invalid argument"; + throw new UsageError(`failed to set default file creation context to '${options.explicitContext}': ${message}`); + } + } else if (options.restoreContext && !restoreSelinuxSecurityContext(path)) { + throw new UsageError(`failed to restore context for '${path}'`); + } + }; + try { + // setfscreatecon(3) is thread-local. Bun's promise-based mkdir may run + // the syscall on a worker thread, so callers that create directories must + // use their synchronous path while an explicit creation context is set. + return await create(created, defaultContextSet); + } finally { + if (defaultContextSet) api.symbols.setfscreatecon(null); + } +} + +export function parseGNUBlockSize(value) { + value = String(value).replace(/^'/, ""); + if (value === "human-readable" || value === "si") return 1; + return /^\D+$/.test(String(value)) ? parseGNUSize(`1${value}`) : parseGNUSize(value); +} + +export function gnuBlockSizeErrorMessage(option, value) { + const text = String(value).replace(/^'/, ""); + if (/^\+?\d+[A-Za-z]+$/.test(text)) return `invalid suffix in ${option} argument '${value}'`; + return `invalid ${option} argument '${value}'`; +} + +export function parseGNUBlockSizeEnvInfo(value, defaultSize) { + if (value == null || value === "") return { size: defaultSize, fallback: "default" }; + try { + return { size: parseGNUBlockSize(value), fallback: null }; + } catch { + const match = String(value).replace(/^'/, "").match(/^\+?(\d+)/); + if (match) return { size: Number(match[1]), fallback: "numeric" }; + return { size: defaultSize, fallback: "default" }; + } +} + +export function blockSizeSpecialMode(value) { + const text = value == null ? "" : String(value).replace(/^'/, ""); + if (text === "human-readable") return "human"; + if (text === "si") return "si"; + return null; +} + +export function applyBlockSizeSpecialMode(opts, value) { + const mode = blockSizeSpecialMode(value); + if (mode === "human") opts["human-readable"] = true; + if (mode === "si") opts.si = true; +} + +export function humanSizeWithUnits(bytes, base, units) { + let value = bytes; + let unit = 0; + while (value >= base && unit < units.length - 1) { + value /= base; + unit++; + } + if (unit === 0) return `${value}${units[unit]}`; + const digits = value >= 10 ? 0 : 1; + const factor = 10 ** digits; + const rounded = Math.ceil(value * factor) / factor; + return `${rounded.toFixed(digits)}${units[unit]}`; +} + +export async function resolveUser(value) { + if (value == null || value === "") return null; + if (/^\+\d+$/.test(value)) return Number(value.slice(1)); + if (/^\d+$/.test(value)) return Number(value); + const user = (await readPasswd()).get(value); + if (!user) throw new UsageError(`invalid user: ${localeQuotedEscapedDiagnostic(value)}`); + return user.uid; +} + +export async function resolveGroup(value) { + if (value == null || value === "") return null; + if (/^\+\d+$/.test(value)) return Number(value.slice(1)); + if (/^\d+$/.test(value)) return Number(value); + const gid = (await readGroup()).get(value); + if (gid == null) throw new UsageError(`invalid group: ${localeQuotedEscapedDiagnostic(value)}`); + return gid; +} + +export async function ensureSpecialFileCreatable(program, path, kind) { + try { + await lstat(path); + throw new UsageError(specialFileCreationMessage(program, path, kind, "File exists")); + } catch (error) { + if (error instanceof UsageError) throw error; + if (error?.code === "ENOTDIR") throw new UsageError(specialFileCreationMessage(program, path, kind, "Not a directory")); + if (error?.code !== "ENOENT") throw new UsageError(specialFileCreationMessage(program, path, kind, systemErrorMessage(error))); + } + const dir = pathDirname(path); + if (dir && dir !== ".") { + try { + const parent = await stat(dir); + if (!parent.isDirectory()) throw new UsageError(specialFileCreationMessage(program, path, kind, "Not a directory")); + } catch (error) { + if (error instanceof UsageError) throw error; + throw new UsageError(specialFileCreationMessage(program, path, kind, systemErrorMessage(error))); + } + } +} + +export function specialFileCreationMessage(program, path, kind, message) { + if (program === "mknod") return `${mknodDiagnosticName(path)}: ${message}`; + return `cannot create ${kind} ${specialFileQuotedName(path)}: ${message}`; +} + +export function specialFileQuotedName(path) { + return shellEscapeLsName(pathDisplayName(path), true); +} + +export function mknodDiagnosticName(path) { + return shellEscapeLsName(pathDisplayName(path)); +} + +export async function setFileMode(path, mode) { + if (isBytePath(path)) { + await fsChmod(path, mode); + return; + } + if (libc.symbols.chmod(cstr(path), mode) !== 0) { + const errno = libcErrno(); + const error = new Error(`chmod '${path}' failed (errno ${errno})`); + error.code = ({ 1: "EPERM", 2: "ENOENT", 13: "EACCES", 20: "ENOTDIR", 30: "EROFS" })[errno] ?? "EIO"; + throw error; + } +} + +export const TOUCH_NOW = Symbol("touch-now"); + +export const TOUCH_OMIT = Symbol("touch-omit"); + +export async function touchSetPathTimes(path, atime, mtime, noDereference) { + if (touchSetPathTimesNative(path, atime, mtime, noDereference)) return; + if (atime === TOUCH_NOW || atime === TOUCH_OMIT || mtime === TOUCH_NOW || mtime === TOUCH_OMIT) { + const errno = libcErrno(); + const error = new Error(`utimensat failed (errno ${errno})`); + error.code = ({ + 1: "EPERM", + 2: "ENOENT", + 5: "EIO", + 9: "EBADF", + 13: "EACCES", + 20: "ENOTDIR", + 21: "EISDIR", + 22: "EINVAL", + 28: "ENOSPC", + 30: "EROFS", + 36: "ENAMETOOLONG", + 40: "ELOOP", + })[errno] ?? "EIO"; + throw error; + } + return (noDereference ? lutimes : utimes)(path, atime, mtime); +} + +export function touchSetPathTimesNative(path, atime, mtime, noDereference) { + const times = Buffer.alloc(32); + writeTimespec(times, 0, atime); + writeTimespec(times, 16, mtime); + return libc.symbols.utimensat(AT_FDCWD, cstrPath(path), times, noDereference ? AT_SYMLINK_NOFOLLOW : 0) === 0; +} + +export function writeTimespec(buffer, offset, date) { + if (date === TOUCH_NOW || date === TOUCH_OMIT) { + buffer.writeBigInt64LE(0n, offset); + buffer.writeBigInt64LE(BigInt(date === TOUCH_NOW ? 1_073_741_823 : 1_073_741_822), offset + 8); + return; + } + const total = dateAbsoluteNanoseconds(date); + const sec = floorDivBigInt(total, 1_000_000_000n); + const nsec = ((total % 1_000_000_000n) + 1_000_000_000n) % 1_000_000_000n; + buffer.writeBigInt64LE(sec, offset); + buffer.writeBigInt64LE(nsec, offset + 8); +} + +export function dateAbsoluteNanoseconds(date) { + const milliseconds = BigInt(date.getTime()); + const seconds = floorDivBigInt(milliseconds, 1000n); + const millisecondFraction = Number(((milliseconds % 1000n) + 1000n) % 1000n); + const nanoseconds = BigInt(date.__bnuNanoseconds ?? millisecondFraction * 1_000_000); + return seconds * 1_000_000_000n + nanoseconds; +} + +export function invalidModeDiagnosticValue(message) { + return message.match(/^invalid mode: '(.+)'$/)?.[1] + ?? message.match(/^invalid mode: \u2018(.+)\u2019$/)?.[1] + ?? null; +} + +export function normalizeSpecialFileLongOptions(args) { + const out = []; + let end = false; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (end || arg === "--" || !arg.startsWith("--")) { + out.push(arg); + if (arg === "--") end = true; + continue; + } + const option = normalizeSpecialFileLongOption(arg); + out.push(option); + if (option === "--mode" && i + 1 < args.length) out.push(args[++i]); + } + return out; +} + +export function normalizeSpecialFileLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const matches = SPECIAL_FILE_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length === 0) return arg; + return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; +} + +export function parseSpecialFileCreationMode(spec) { + try { + return parseCreationMode(spec, 0o666); + } catch (error) { + if (error instanceof UsageError && invalidModeDiagnosticValue(error.message) != null) throw new UsageError("invalid mode", false); + throw error; + } +} + +export function rawOperandPlan(command, args, operands, spec = {}) { + const raw = rawCommandArgs(command); + if (!raw) return null; + const rawOperands = parseRawOperands(raw, spec); + if (rawOperands.length !== operands.length) return null; + if (!rawOperands.some((operand, index) => rawPathNeedsBytes(operand) || !operand.equals(Buffer.from(operands[index])))) return null; + return rawOperands; +} + +export function parseRawOperands(rawArgs, spec = {}) { + const operands = []; + const valueOptions = new Set(spec.valueOptions ?? []); + const shortValueOptions = new Set(spec.shortValueOptions ?? []); + for (let i = 0; i < rawArgs.length; i++) { + const arg = rawArgs[i]; + if (arg.equals(Buffer.from("--"))) { + operands.push(...rawArgs.slice(i + 1)); + break; + } + if (!rawArgLooksLikeOption(arg)) { + operands.push(arg); + continue; + } + const text = arg.toString(); + if (valueOptions.has(text)) { + i++; + continue; + } + if ([...valueOptions].some((option) => text.startsWith(`${option}=`))) continue; + if (arg.length > 2 && arg[0] === 0x2d && arg[1] !== 0x2d) { + const letters = arg.toString().slice(1); + const valueIndex = [...letters].findIndex((ch) => shortValueOptions.has(ch)); + if (valueIndex !== -1 && valueIndex === letters.length - 1) i++; + } + } + return operands; +} + +export async function mkdirParents(dir, mode, verbose, created = async () => {}, sameThread = false) { + const absolute = isAbsolute(dir); + const parts = String(dir).split("/").filter((part) => part !== ""); + let current = absolute ? "/" : ""; + for (let i = 0; i < parts.length; i++) { + const part = parts[i]; + if (part === ".") continue; + if (part === "..") { + current = current ? pathDirname(current) : ".."; + continue; + } + current = current === "" || current === "/" ? `${current}${part}` : `${current}/${part}`; + const isFinal = i === parts.length - 1 || parts.slice(i + 1).every((tail) => tail === "."); + const createMode = isFinal ? mode : defaultDirectoryMode(); + try { + if (sameThread) mkdirSync(current, { mode: createMode }); + else await mkdir(current, { mode: createMode }); + if (createMode !== undefined) await setFileMode(current, createMode); + await created(current); + if (verbose) stdout(`mkdir: created directory ${mkdirVerboseName(current)}\n`); + } catch (error) { + if (error?.code !== "EEXIST") throw new UsageError(`cannot create directory ${mkdirDiagnosticName(current)}: ${mkdirErrorMessage(error)}`); + const s = await stat(current); + if (!s.isDirectory()) throw new UsageError(`cannot create directory ${mkdirDiagnosticName(current)}: Not a directory`); + } + } +} + +export function mkdirVerboseName(path) { + return shellEscapeLsName(pathDisplayName(path), true); +} + +export function mkdirDiagnosticName(path) { + const text = pathDisplayName(path); + const escaped = lsEscapedName(text, { escapeDouble: false }); + return lsUsesUtf8Locale() ? `\u2018${escaped}\u2019` : `'${escaped.replaceAll("'", "\\'")}'`; +} + +export function mkdirErrorMessage(error) { + if (error?.code === "EEXIST") return "File exists"; + return systemErrorMessage(error); +} + +export function defaultDirectoryMode() { + return (0o777 & ~process.umask()) | 0o300; +} + +export function parseCreationMode(spec, base = 0o777) { + return /^[0-7]+$/.test(spec) ? Number.parseInt(spec, 8) : parseModeSpec(spec, base, true).mode; +} + +export function linkQuotedName(path) { + return shellEscapeLsName(pathDisplayName(path), true); +} + +export function rmLibcError(errno) { + const error = new Error(({ + 5: "Input/output error", + 10: "No child processes", + 13: "Permission denied", + 22: "Invalid argument", + 38: "Function not implemented", + })[errno] ?? `error ${errno}`); + error.code = ({ 5: "EIO", 10: "ECHILD", 13: "EACCES", 22: "EINVAL", 38: "ENOSYS" })[errno] ?? "EIO"; + return error; +} + +export function confirmRemoval(prompt) { + stderr(prompt); + const bytes = new Uint8Array(1024); + while (!stdinLineBuffer.includes("\n")) { + const n = readSync(0, bytes, 0, bytes.length, null); + if (!n) break; + stdinLineBuffer += Buffer.from(bytes.subarray(0, n)).toString("utf8"); + } + const newline = stdinLineBuffer.indexOf("\n"); + const text = newline === -1 ? stdinLineBuffer : stdinLineBuffer.slice(0, newline); + stdinLineBuffer = newline === -1 ? "" : stdinLineBuffer.slice(newline + 1); + return /^[yY]/.test(text.trimStart()); +} + +export function isWriteProtected(s) { + return !s.isSymbolicLink() && !(s.mode & 0o200); +} + +export function rawPathNeedsBytes(path) { + return Buffer.from(path).some((byte) => byte >= 0x80); +} + +export function rawArgLooksLikeOption(arg) { + return arg.length > 1 && arg[0] === 0x2d; +} + +export async function openDirectoryPathEntries(path) { + const directory = await opendir(path, { encoding: "buffer" }); + return { + async *[Symbol.asyncIterator]() { + try { + while (true) { + const entry = await directory.read(); + if (!entry) break; + // Bun returns the encoded name directly for encoding:"buffer", + // while Node returns a Dirent whose name uses that encoding. + yield entry.name ?? entry; + } + } finally { + try { + await directory.close(); + } catch (error) { + if (error?.code !== "ERR_DIR_CLOSED") throw error; + } + } + } + }; +} + +export function parseModeSpec(spec, current, isDirectory = false, parseOptions = {}) { + if (spec === "--") return { mode: current, ok: true }; + if (spec.includes(",")) { + let mode = current; + let ok = true; + for (const clause of spec.split(",")) { + const parsed = parseModeSpec(clause, mode, isDirectory, parseOptions); + mode = parsed.mode; + ok &&= parsed.ok; + } + return { mode, ok }; + } + if (/^[0-7]+$/.test(spec)) { + const next = Number.parseInt(spec, 8); + const preserveDirectorySpecialBits = isDirectory && next <= 0o777 && !spec.startsWith("00"); + return { mode: preserveDirectorySpecialBits ? next | (current & 0o6000) : next, ok: true }; + } + if (/^=[0-7]+$/.test(spec)) return { mode: Number.parseInt(spec.slice(1), 8), ok: true }; + if (/^\+[0-7]+$/.test(spec)) return { mode: current | Number.parseInt(spec.slice(1), 8), ok: true }; + if (/^-[0-7]+$/.test(spec)) return { mode: current & ~Number.parseInt(spec.slice(1), 8), ok: true }; + let mode = current; + let ok = true; + for (const clause of expandSymbolicModeClauses(spec)) { + const match = clause.match(/^([ugoa]*)([+=-])([rwxXstugo]*)$/); + if (!match) throw new UsageError(`invalid mode: ${localeQuotedEscapedDiagnostic(spec)}`, true); + if (/[ugo]/.test(match[3]) && match[3].length !== 1) throw new UsageError(`invalid mode: ${localeQuotedEscapedDiagnostic(spec)}`, true); + const explicitWho = match[1] !== ""; + const who = match[1] || "a"; + const op = match[2]; + const perms = symbolicBits(match[3], who, mode, isDirectory); + const mask = whoMask(who); + const setMask = explicitWho || parseOptions.ignoreUmask ? mask : mask & ~process.umask(); + if (op === "=") mode = (mode & ~mask) | (perms & setMask); + else if (op === "+") mode |= perms & setMask; + else { + mode &= ~(perms & setMask); + if (!explicitWho && (mode & perms & (mask & ~setMask))) ok = false; + } + } + return { mode, ok }; +} + +export function expandSymbolicModeClauses(spec) { + const clauses = []; + for (const clause of spec.split(",")) { + const compound = clause.match(/^([ugoa]*)=([+-][rwxXstugo]+)$/); + if (compound) { + clauses.push(`${compound[1]}=`, `${compound[1]}${compound[2]}`); + } else { + clauses.push(clause); + } + } + return clauses; +} + +export function errnoMessage(error) { + if (error?.code === "EACCES") return "Permission denied"; + if (error?.code === "ENOENT") return "No such file or directory"; + if (error?.code === "ENOTDIR") return "Not a directory"; + if (error?.code === "EISDIR") return "Is a directory"; + if (error?.code === "ENOTEMPTY") return "Directory not empty"; + if (error?.code === "EEXIST") return "Directory not empty"; + if (error?.code === "EPERM") return "Operation not permitted"; + if (error?.code === "EROFS") return "Read-only file system"; + if (error?.code === "EINVAL") return "Invalid argument"; + return error?.message || String(error); +} + +export async function referenceStat(path) { + try { + return await stat(path); + } catch (error) { + throw new UsageError(`failed to get attributes of ${chmodQuotedName(path)}: ${errnoMessage(error)}`); + } +} + +export function chmodQuotedName(path) { + return shellEscapeLsName(pathDisplayName(path), true); +} + +export function isAccessError(error) { + return error?.code === "EACCES" || error?.code === "ENOENT" || error?.code === "ENOTDIR"; +} + +export function lstatSyncNoThrow(path) { + try { + return lstatSync(path); + } catch { + return null; + } +} + +export async function isRecursiveRootTarget(path) { + try { + return await realpath(path) === "/"; + } catch { + return resolve(path) === "/"; + } +} + +export function preserveRootError(command, path) { + const error = new Error("preserve-root"); + error.preserveRoot = true; + error.path = path; + error.rootPath = path === "/" ? null : "/"; + return error; +} + +export function preserveRootMessage(command, path, rootPath = null) { + const target = rootPath ? `'${path}' (same as '${rootPath}')` : `'${path}'`; + return `${command}: it is dangerous to operate recursively on ${target}\n${command}: use --no-preserve-root to override this failsafe\n`; +} + +export function whoMask(who) { + let mask = 0; + if (who.includes("a") || who.includes("u")) mask |= 0o4700; + if (who.includes("a") || who.includes("g")) mask |= 0o2070; + if (who.includes("a") || who.includes("o")) mask |= 0o1007; + return mask; +} + +export function symbolicBits(perms, who, current, isDirectory = false) { + let bits = 0; + const apply = (u, g, o) => { + if (who.includes("a") || who.includes("u")) bits |= u; + if (who.includes("a") || who.includes("g")) bits |= g; + if (who.includes("a") || who.includes("o")) bits |= o; + }; + for (const ch of perms) { + if (ch === "r") apply(0o400, 0o040, 0o004); + else if (ch === "w") apply(0o200, 0o020, 0o002); + else if (ch === "x") apply(0o100, 0o010, 0o001); + else if (ch === "X") { + if (isDirectory || (current & 0o111)) apply(0o100, 0o010, 0o001); + } + else if (ch === "s") apply(0o4000, 0o2000, 0); + else if (ch === "t") apply(0, 0, 0o1000); + else if (ch === "u") bits |= copyClassBits(current, 6, who); + else if (ch === "g") bits |= copyClassBits(current, 3, who); + else if (ch === "o") bits |= copyClassBits(current, 0, who); + } + return bits; +} + +export function copyClassBits(mode, shift, who) { + const classBits = (mode >> shift) & 7; + let bits = 0; + if (who.includes("a") || who.includes("u")) bits |= classBits << 6; + if (who.includes("a") || who.includes("g")) bits |= classBits << 3; + if (who.includes("a") || who.includes("o")) bits |= classBits; + return bits; +} diff --git a/src/shared/hash.js b/src/shared/hash.js new file mode 100644 index 0000000..c850f90 --- /dev/null +++ b/src/shared/hash.js @@ -0,0 +1,680 @@ +import { createHash } from "node:crypto"; +import { fstatSync } from "node:fs"; +import { forEachInputChunk } from "./checksum.js"; +import { createFdRecordReader, decodeSurrogateEscapedBytes, hasSurrogateEscapedBytes, isWriteError, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, nodeErrorMessage, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions, pathDisplayName, readAll, systemErrorMessage, textInputDiagnosticName } from "./common.js"; +import { UsageError, VERSION, encodeSurrogateEscapedString, stderr, stdout } from "./diagnostics.js"; + +export const CKSUM_LONG_OPTIONS = ["algorithm", "base64", "check", "length", "raw", "tag", "untagged", "zero", "ignore-missing", "quiet", "status", "strict", "warn", "debug", "binary", "text", "help", "version"]; + +export const HASH_LONG_OPTIONS = ["check", "binary", "text", "tag", "length", "status", "quiet", "warn", "strict", "ignore-missing", "zero", "help", "version"]; + +export function hashMetaOption(program, args) { + const supportsLength = program === "b2sum"; + const longOptions = supportsLength ? HASH_LONG_OPTIONS : HASH_LONG_OPTIONS.filter((option) => option !== "length"); + return checksumMetaOption(args, longOptions, normalizeHashLongOption, supportsLength ? new Set(["length"]) : new Set(), supportsLength ? new Set(["l"]) : new Set(), supportsLength ? new Set(["b", "c", "l", "t", "w", "z"]) : new Set(["b", "c", "t", "w", "z"])); +} + +export function checksumMetaOption(args, longOptions, normalizeLongOption, longValueOptions, shortValueOptions, shortKnownOptions) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!longOptions.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + if (longOptions === CKSUM_LONG_OPTIONS && name === "algorithm" && inlineValue != null) validateCksumAlgorithmOption(inlineValue); + const value = inlineValue ?? (longValueOptions.has(name) ? args[i + 1] : undefined); + if (name === "length" && value !== undefined) validateShaDigestLengthSyntax(value); + if (inlineValue == null && longValueOptions.has(name)) i++; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? args[i + 1] : inlineValue; + validateChecksumMetaShortValue(ch, value, longOptions); + if (inlineValue === "") i++; + break; + } + } + } + return null; +} + +export function validateChecksumMetaShortValue(ch, value, longOptions) { + if (value === undefined) return; + if (ch === "a" && longOptions === CKSUM_LONG_OPTIONS) validateCksumAlgorithmOption(value); + if (ch === "l") validateShaDigestLengthSyntax(value); +} + +export async function fileDigest(file, algorithm) { + if (algorithm === "sm3") return sm3DigestFile(file); + const hash = createHash(algorithm); + await forEachInputChunk(file, (chunk) => hash.update(chunk)); + return hash.digest(); +} + +export async function sm3DigestFile(file) { + const state = { + h: [0x7380166f, 0x4914b2b9, 0x172442d7, 0xda8a0600, 0xa96f30bc, 0x163138aa, 0xe38dee4d, 0xb0fb0e4e], + buffer: Buffer.alloc(0), + length: 0n, + }; + await forEachInputChunk(file, (chunk) => sm3Update(state, chunk)); + return sm3Final(state); +} + +export function sm3Update(state, chunk) { + state.length += BigInt(chunk.length); + let data = Buffer.concat([state.buffer, chunk]); + let offset = 0; + while (offset + 64 <= data.length) { + sm3Compress(state.h, data.subarray(offset, offset + 64)); + offset += 64; + } + state.buffer = data.subarray(offset); +} + +export function sm3Final(state) { + const bitLength = state.length * 8n; + const padLength = Number((56n - ((state.length + 1n) % 64n) + 64n) % 64n); + const padding = Buffer.alloc(1 + padLength + 8); + padding[0] = 0x80; + padding.writeBigUInt64BE(bitLength, 1 + padLength); + sm3Update(state, padding); + const out = Buffer.alloc(32); + state.h.forEach((word, index) => out.writeUInt32BE(word >>> 0, index * 4)); + return out; +} + +export function sm3Compress(h, block) { + const w = new Array(68); + const w1 = new Array(64); + for (let i = 0; i < 16; i++) w[i] = block.readUInt32BE(i * 4); + for (let j = 16; j < 68; j++) w[j] = (sm3P1((w[j - 16] ^ w[j - 9] ^ rotl32(w[j - 3], 15)) >>> 0) ^ rotl32(w[j - 13], 7) ^ w[j - 6]) >>> 0; + for (let j = 0; j < 64; j++) w1[j] = (w[j] ^ w[j + 4]) >>> 0; + let [a, b, c, d, e, f, g, hh] = h; + for (let j = 0; j < 64; j++) { + const t = j < 16 ? 0x79cc4519 : 0x7a879d8a; + const ss1 = rotl32((rotl32(a, 12) + e + rotl32(t, j % 32)) >>> 0, 7); + const ss2 = (ss1 ^ rotl32(a, 12)) >>> 0; + const tt1 = (sm3FF(a, b, c, j) + d + ss2 + w1[j]) >>> 0; + const tt2 = (sm3GG(e, f, g, j) + hh + ss1 + w[j]) >>> 0; + d = c; + c = rotl32(b, 9); + b = a; + a = tt1; + hh = g; + g = rotl32(f, 19); + f = e; + e = sm3P0(tt2); + } + for (let i = 0, values = [a, b, c, d, e, f, g, hh]; i < 8; i++) h[i] = (h[i] ^ values[i]) >>> 0; +} + +export function rotl32(value, bits) { + return ((value << bits) | (value >>> (32 - bits))) >>> 0; +} + +export function sm3P0(x) { + return (x ^ rotl32(x, 9) ^ rotl32(x, 17)) >>> 0; +} + +export function sm3P1(x) { + return (x ^ rotl32(x, 15) ^ rotl32(x, 23)) >>> 0; +} + +export function sm3FF(x, y, z, j) { + return j < 16 ? (x ^ y ^ z) >>> 0 : ((x & y) | (x & z) | (y & z)) >>> 0; +} + +export function sm3GG(x, y, z, j) { + return j < 16 ? (x ^ y ^ z) >>> 0 : ((x & y) | (~x & z)) >>> 0; +} + +export const BLAKE2B_IV = [ + 0x6a09e667f3bcc908n, 0xbb67ae8584caa73bn, 0x3c6ef372fe94f82bn, 0xa54ff53a5f1d36f1n, + 0x510e527fade682d1n, 0x9b05688c2b3e6c1fn, 0x1f83d9abfb41bd6bn, 0x5be0cd19137e2179n, +]; + +export const BLAKE2B_SIGMA = [ + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], + [14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3], + [11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4], + [7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8], + [9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13], + [2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9], + [12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11], + [13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10], + [6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5], + [10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0], + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], + [14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3], +]; + +export const MASK64 = 0xffffffffffffffffn; + +export async function blake2bDigestFile(file, outBytes = 64) { + const state = { + h: [...BLAKE2B_IV], + buffer: Buffer.alloc(0), + length: 0n, + outBytes, + }; + state.h[0] ^= 0x01010000n ^ BigInt(outBytes); + await forEachInputChunk(file, (chunk) => blake2bUpdate(state, chunk)); + return blake2bFinal(state); +} + +export function blake2bUpdate(state, chunk) { + let data = Buffer.concat([state.buffer, chunk]); + let offset = 0; + while (offset + 128 < data.length) { + state.length += 128n; + blake2bCompress(state.h, data.subarray(offset, offset + 128), state.length, false); + offset += 128; + } + state.buffer = data.subarray(offset); +} + +export function blake2bFinal(state) { + state.length += BigInt(state.buffer.length); + const block = Buffer.alloc(128); + state.buffer.copy(block); + blake2bCompress(state.h, block, state.length, true); + const out = Buffer.alloc(64); + state.h.forEach((word, index) => out.writeBigUInt64LE(word, index * 8)); + return out.subarray(0, state.outBytes); +} + +export function blake2bCompress(h, block, count, last) { + const m = Array.from({ length: 16 }, (_, i) => block.readBigUInt64LE(i * 8)); + const v = [...h, ...BLAKE2B_IV]; + v[12] ^= count & MASK64; + v[13] ^= count >> 64n; + if (last) v[14] ^= MASK64; + const mix = (a, b, c, d, x, y) => { + v[a] = (v[a] + v[b] + x) & MASK64; + v[d] = rotr64(v[d] ^ v[a], 32n); + v[c] = (v[c] + v[d]) & MASK64; + v[b] = rotr64(v[b] ^ v[c], 24n); + v[a] = (v[a] + v[b] + y) & MASK64; + v[d] = rotr64(v[d] ^ v[a], 16n); + v[c] = (v[c] + v[d]) & MASK64; + v[b] = rotr64(v[b] ^ v[c], 63n); + }; + for (const s of BLAKE2B_SIGMA) { + mix(0, 4, 8, 12, m[s[0]], m[s[1]]); + mix(1, 5, 9, 13, m[s[2]], m[s[3]]); + mix(2, 6, 10, 14, m[s[4]], m[s[5]]); + mix(3, 7, 11, 15, m[s[6]], m[s[7]]); + mix(0, 5, 10, 15, m[s[8]], m[s[9]]); + mix(1, 6, 11, 12, m[s[10]], m[s[11]]); + mix(2, 7, 8, 13, m[s[12]], m[s[13]]); + mix(3, 4, 9, 14, m[s[14]], m[s[15]]); + } + for (let i = 0; i < 8; i++) h[i] = (h[i] ^ v[i] ^ v[i + 8]) & MASK64; +} + +export function rotr64(value, bits) { + return ((value >> bits) | (value << (64n - bits))) & MASK64; +} + +export const CKSUM_ALGORITHMS = ["bsd", "sysv", "crc", "crc32b", "md5", "sha1", "sha224", "sha256", "sha384", "sha512", "sha2", "sha3", "blake2b", "sm3"]; + +export function validateCksumAlgorithmOption(value) { + if (CKSUM_ALGORITHMS.includes(value)) return; + throw new UsageError(`invalid argument ${localeQuotedEscapedDiagnostic(value)} for ${localeQuotedDiagnostic("--algorithm")}\nValid arguments are:\n${CKSUM_ALGORITHMS.map((algorithm) => ` - ${localeQuotedDiagnostic(algorithm)}`).join("\n")}`, true); +} + +export function optionAppearsAfter(args, later, earlier) { + const laterIndex = args.lastIndexOf(later); + const earlierIndex = args.lastIndexOf(earlier); + return laterIndex !== -1 && earlierIndex !== -1 && laterIndex > earlierIndex; +} + +export function cksumHashAlgorithm(name, opts = {}) { + const length = opts.l ?? opts.length; + if (name === "md5") return "md5"; + if (name === "sha1") return "sha1"; + if (["sha224", "sha256", "sha384", "sha512"].includes(name)) return name; + if (name === "sha2") { + validateShaDigestLengthSyntax(length); + if (!["224", "256", "384", "512"].includes(String(length))) throw invalidDigestLengthError("cksum", "SHA2", length, "digest length for 'SHA2' must be 224, 256, 384, or 512"); + return `sha${length}`; + } + if (name === "sha3") { + validateShaDigestLengthSyntax(length); + if (!["224", "256", "384", "512"].includes(String(length))) throw invalidDigestLengthError("cksum", "SHA3", length, "digest length for 'SHA3' must be 224, 256, 384, or 512"); + return `sha3-${length}`; + } + if (name === "blake2b") { + if (length != null) validateBlake2bLength(opts.program ?? "cksum", length); + return "blake2b512"; + } + if (name === "sm3") return "sm3"; + throw new UsageError(`invalid checksum algorithm: ${name}`); +} + +export function validateShaDigestLengthSyntax(length) { + const text = String(length); + if (/^\d+$/.test(text)) return; + if (/^-\d+$/.test(text)) throw new UsageError(`invalid length: ${localeQuotedEscapedDiagnostic(length)}: Value too large for defined data type`, false); + throw new UsageError(`invalid length: ${localeQuotedEscapedDiagnostic(length)}`, false); +} + +export function cksumAlgorithmForDigest(name, digestBytes, opts = {}) { + const length = opts.l ?? opts.length; + if (name === "sha2") { + const bits = length ?? String(digestBytes * 8); + return cksumHashAlgorithm(name, { ...opts, l: bits }); + } + if (name === "sha3") { + const bits = length ?? String(digestBytes * 8); + return cksumHashAlgorithm(name, { ...opts, l: bits }); + } + return cksumHashAlgorithm(name, opts); +} + +export function checksumDiagnosticName(file) { + return textInputDiagnosticName(file); +} + +export function hashCommand(algorithm) { + return async (args) => { + if (algorithm !== "blake2b512") rejectHashLengthOption(args); + args = normalizeHashLongOptions(args); + if (args.length === 1 && args[0] === "--version") { + stdout(`${VERSION}\n`); + return 0; + } + const { opts, operands } = parseOptions(args, { short: { c: false, b: false, t: false, l: "value", w: false, z: false }, long: { check: false, binary: false, text: false, tag: false, length: "value", status: false, quiet: false, warn: false, strict: false, "ignore-missing": false, zero: false, help: false, version: false } }); + if (opts.w) opts.warn = true; + opts.warnAfterStatus = optionAppearsAfter(args, "--warn", "--status") || optionAppearsAfter(args, "-w", "--status"); + const digestBytes = hashDigestBytes(algorithm, opts); + validateChecksumCheckOnlyOptions(opts); + if (opts.c || opts.check) { + if (opts.tag) throw new UsageError("the --tag option is meaningless when verifying checksums"); + return checkHashes(algorithm, operands.length ? operands : ["-"], { ...opts, program: hashProgramName(algorithm) }); + } + if (opts.tag && (opts.t || opts.text)) throw new UsageError("--tag does not support --text mode"); + const files = operands.length ? operands : ["-"]; + let failed = false; + for (const file of files) { + let digest; + try { + digest = (digestBytes ? await blake2bDigestFile(file, digestBytes) : await fileDigest(file, algorithm)).toString("hex"); + } catch (error) { + const program = hashProgramName(algorithm); + stderr(file === "-" ? `${program}: ${nodeErrorMessage(error)}\n` : `${program}: ${checksumDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + continue; + } + const sep = opts.z || opts.zero ? "\0" : "\n"; + const displayFile = file === "-" ? "-" : file; + const renderedFile = opts.z || opts.zero ? displayFile : escapeChecksumFilename(displayFile); + const escapePrefix = !(opts.z || opts.zero) && shouldEscapeChecksumFilename(displayFile) ? "\\" : ""; + if (opts.tag) stdout(`${escapePrefix}${hashTagName(algorithm, opts)} (${renderedFile}) = ${digest}${sep}`); + else stdout(`${escapePrefix}${digest}${opts.b || opts.binary ? " *" : " "}${renderedFile}${sep}`); + } + return failed ? 1 : 0; + }; +} + +export function rejectHashLengthOption(args) { + for (const arg of args) { + if (arg === "--") return; + if (arg.startsWith("--")) { + const name = arg.slice(2).split("=", 1)[0]; + if ("length".startsWith(name)) throw new UsageError(`unrecognized option '${arg}'`, true); + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (const ch of arg.slice(1)) { + if (ch === "l") throw new UsageError("invalid option -- 'l'", true); + if (ch === "b" || ch === "c" || ch === "t" || ch === "w" || ch === "z") continue; + break; + } + } +} + +export function validateChecksumCheckOnlyOptions(opts) { + if (opts.c || opts.check) return; + for (const option of ["ignore-missing", "status", "quiet", "strict", "warn"]) { + if (opts[option]) throw new UsageError(`the --${option} option is meaningful only when verifying checksums`, true); + } +} + +export function hashDigestBytes(algorithm, opts = {}) { + if (algorithm !== "blake2b512") return null; + const length = opts.l ?? opts.length ?? "512"; + validateBlake2bLength("b2sum", length); + return Number(length) === 0 ? 64 : Number(length) / 8; +} + +export function hashProgramName(algorithm) { + if (algorithm === "md5") return "md5sum"; + if (algorithm === "blake2b512") return "b2sum"; + return `${algorithm}sum`; +} + +export function normalizeHashLongOptions(args) { + return normalizeLongOptionsByPrefix(args, HASH_LONG_OPTIONS); +} + +export function normalizeHashLongOption(arg) { + return normalizeLongOptionByPrefix(arg, HASH_LONG_OPTIONS); +} + +export function hashTagName(algorithm, opts = {}) { + if (algorithm === "md5") return "MD5"; + if (algorithm === "blake2b512") { + const length = opts.l ?? opts.length; + return length != null && !["0", "512"].includes(String(length)) ? `BLAKE2b-${length}` : "BLAKE2b"; + } + return algorithm.toUpperCase(); +} + +export async function checkHashes(algorithm, files, opts = {}) { + if (files.length === 1 && files[0] === "-" && !fstatSync(0).isFile()) { + return checkHashesStream(algorithm, opts); + } + let checksumFailures = 0; + let readFailures = 0; + let malformed = 0; + let properLines = 0; + let verified = 0; + let emptyLists = 0; + const checksumName = algorithm.toUpperCase(); + let lastChecksumName = checksumName; + let checksumLineStyle = null; + for (const listFile of files) { + let text; + try { + text = decodeSurrogateEscapedBytes(await readAll(listFile)); + } catch (error) { + readFailures++; + stderr(`${diagnosticPrefix(opts)}${listFile}: ${fileReadErrorMessage(error)}\n`); + continue; + } + let fileProperLines = 0; + let lineNo = 0; + for (const raw of text.split(/\n/)) { + lineNo++; + const line = raw.replace(/\r$/, "").replace(/^[ \t]*\\/, ""); + if (!line || line.startsWith("#") || line.startsWith("-----BEGIN ") || line.startsWith("-----END ") || /^Hash: /.test(line)) continue; + const parsedLine = parseChecksumLine(line, algorithm, checksumLineStyle); + if (parsedLine?.skip) continue; + if (!parsedLine) { + malformed++; + if (opts.warn && (!opts.status || opts.warnAfterStatus)) stderr(`${diagnosticPrefix(opts)}${listFile}: ${lineNo}: improperly formatted ${lastChecksumName} checksum line\n`); + continue; + } + const parsed = parseExpectedChecksum(parsedLine.token, parsedLine.algorithm, { ...opts, ...(parsedLine.opts ?? {}) }); + if (!parsed) { + malformed++; + if (opts.warn && (!opts.status || opts.warnAfterStatus)) stderr(`${diagnosticPrefix(opts)}${listFile}: ${lineNo}: improperly formatted ${checksumDisplayName(parsedLine.algorithm, parsedLine.opts)} checksum line\n`); + continue; + } + if (parsedLine.style && checksumLineStyle == null) checksumLineStyle = parsedLine.style; + lastChecksumName = checksumDisplayName(parsedLine.algorithm, parsedLine.opts); + const { expected, hashAlgorithm, blake2bBytes } = parsed; + const file = checksumPathFromFilename(unescapeChecksumFilename(parsedLine.file)); + properLines++; + fileProperLines++; + try { + const actualDigest = blake2bBytes ? await blake2bDigestFile(file, blake2bBytes) : await fileDigest(file, hashAlgorithm); + const actual = actualDigest.slice(0, expected.length / 2).toString("hex"); + const ok = actual === expected; + verified++; + if (!ok) checksumFailures++; + if (!(opts.status || (opts.quiet && ok))) stdout(`${checksumDisplayFile(file)}: ${ok ? "OK" : "FAILED"}\n`); + } catch (error) { + if (opts["ignore-missing"]) continue; + readFailures++; + if (!opts.status) stderr(`${diagnosticPrefix(opts)}${pathDisplayName(file)}: ${fileReadErrorMessage(error)}\n`); + if (!opts.status) stdout(`${checksumDisplayFile(file)}: FAILED open or read\n`); + } + } + if (fileProperLines === 0) { + emptyLists++; + if (!opts.status) stderr(`${diagnosticPrefix(opts)}${listFile}: no properly formatted checksum lines found\n`); + } + } + if (properLines === 0) { + return 1; + } + const showStatusWarnings = !opts.status || opts.warnAfterStatus; + if (malformed && showStatusWarnings && !opts.quiet) stderr(`${diagnosticPrefix(opts)}WARNING: ${malformed} line${malformed === 1 ? " is" : "s are"} improperly formatted\n`); + if (readFailures && showStatusWarnings) stderr(`${diagnosticPrefix(opts)}WARNING: ${readFailures} listed file${readFailures === 1 ? "" : "s"} could not be read\n`); + if (checksumFailures && showStatusWarnings) stderr(`${diagnosticPrefix(opts)}WARNING: ${checksumFailures} computed checksum${checksumFailures === 1 ? "" : "s"} did NOT match\n`); + if (verified === 0 && readFailures === 0) { + if (showStatusWarnings) stderr(`${diagnosticPrefix(opts)}${files[0]}: no file was verified\n`); + return 1; + } + return checksumFailures || readFailures || emptyLists || (opts.strict && malformed) ? 1 : 0; +} + +export async function checkHashesStream(requestedAlgorithm, opts = {}) { + const reader = createFdRecordReader(0, 10); + let checksumFailures = 0; + let readFailures = 0; + let malformed = 0; + let properLines = 0; + let verified = 0; + let lineNo = 0; + let checksumLineStyle = null; + let lastChecksumName = requestedAlgorithm.toUpperCase(); + while (true) { + const bytes = reader.next(); + if (bytes == null) break; + lineNo++; + const raw = decodeSurrogateEscapedBytes(bytes); + const line = raw.replace(/\r$/, "").replace(/^[ \t]*\\/, ""); + if (!line || line.startsWith("#") || line.startsWith("-----BEGIN ") || line.startsWith("-----END ") || /^Hash: /.test(line)) continue; + const parsedLine = parseChecksumLine(line, requestedAlgorithm, checksumLineStyle); + if (parsedLine?.skip) continue; + if (!parsedLine) { + malformed++; + if (opts.warn && (!opts.status || opts.warnAfterStatus)) stderr(`${diagnosticPrefix(opts)}-: ${lineNo}: improperly formatted ${lastChecksumName} checksum line\n`); + continue; + } + const parsed = parseExpectedChecksum(parsedLine.token, parsedLine.algorithm, { ...opts, ...(parsedLine.opts ?? {}) }); + if (!parsed) { + malformed++; + if (opts.warn && (!opts.status || opts.warnAfterStatus)) stderr(`${diagnosticPrefix(opts)}-: ${lineNo}: improperly formatted ${checksumDisplayName(parsedLine.algorithm, parsedLine.opts)} checksum line\n`); + continue; + } + if (parsedLine.style && checksumLineStyle == null) checksumLineStyle = parsedLine.style; + lastChecksumName = checksumDisplayName(parsedLine.algorithm, parsedLine.opts); + properLines++; + const file = checksumPathFromFilename(unescapeChecksumFilename(parsedLine.file)); + const { expected, hashAlgorithm, blake2bBytes } = parsed; + try { + const digest = blake2bBytes ? await blake2bDigestFile(file, blake2bBytes) : await fileDigest(file, hashAlgorithm); + const actual = digest.slice(0, expected.length / 2).toString("hex"); + const ok = actual === expected; + verified++; + if (!ok) checksumFailures++; + if (!(opts.status || (opts.quiet && ok))) stdout(`${checksumDisplayFile(file)}: ${ok ? "OK" : "FAILED"}\n`); + } catch (error) { + if (isWriteError(error)) throw error; + if (opts["ignore-missing"]) continue; + readFailures++; + if (!opts.status) stderr(`${diagnosticPrefix(opts)}${pathDisplayName(file)}: ${fileReadErrorMessage(error)}\n`); + if (!opts.status) stdout(`${checksumDisplayFile(file)}: FAILED open or read\n`); + } + } + if (properLines === 0) { + if (!opts.status) stderr(`${diagnosticPrefix(opts)}-: no properly formatted checksum lines found\n`); + return 1; + } + const showStatusWarnings = !opts.status || opts.warnAfterStatus; + if (malformed && showStatusWarnings && !opts.quiet) stderr(`${diagnosticPrefix(opts)}WARNING: ${malformed} line${malformed === 1 ? " is" : "s are"} improperly formatted\n`); + if (readFailures && showStatusWarnings) stderr(`${diagnosticPrefix(opts)}WARNING: ${readFailures} listed file${readFailures === 1 ? "" : "s"} could not be read\n`); + if (checksumFailures && showStatusWarnings) stderr(`${diagnosticPrefix(opts)}WARNING: ${checksumFailures} computed checksum${checksumFailures === 1 ? "" : "s"} did NOT match\n`); + if (verified === 0 && readFailures === 0) { + if (showStatusWarnings) stderr(`${diagnosticPrefix(opts)}-: no file was verified\n`); + return 1; + } + return checksumFailures || readFailures || (opts.strict && malformed) ? 1 : 0; +} + +export function parseChecksumLine(line, requestedAlgorithm, style = null) { + const tag = line.match(/^([A-Za-z0-9-]+) \((.*)\) = (\S+)$/) ?? line.match(/^([A-Za-z0-9-]+)\((.*)\)= (\S+)$/); + if (tag) { + const algorithm = checksumAlgorithmFromTag(tag[1]); + if (!algorithm) return null; + if (requestedAlgorithm !== "auto" && !checksumAlgorithmMatches(requestedAlgorithm, algorithm)) return { skip: true }; + return { algorithm: algorithm.name, token: tag[3], file: tag[2], opts: algorithm.opts }; + } + const token = line.match(/^(\S+)/)?.[1]; + if (!token || line.length <= token.length || line[token.length] !== " ") return null; + const rest = line.slice(token.length); + const algorithm = requestedAlgorithm === "auto" ? "auto" : requestedAlgorithm; + if (style === "bsd-alternate") return { algorithm, token, file: rest.slice(1), style: "bsd-alternate" }; + if (rest.startsWith(" ")) return { algorithm, token, file: rest.slice(2), style: "gnu" }; + if (rest.startsWith(" *")) return { algorithm, token, file: rest.slice(2), style: "gnu" }; + if (style == null && requestedAlgorithm !== "auto") return { algorithm, token, file: rest.slice(1), style: "bsd-alternate" }; + return null; +} + +export function shouldEscapeChecksumFilename(file) { + return String(file).includes("\\") || String(file).includes("\n") || String(file).includes("\r"); +} + +export function escapeChecksumFilename(file) { + return String(file).replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/\r/g, "\\r"); +} + +export function unescapeChecksumFilename(file) { + return String(file).replace(/\\([\\nr])/g, (_, ch) => ch === "n" ? "\n" : ch === "r" ? "\r" : "\\"); +} + +export function checksumPathFromFilename(file) { + return hasSurrogateEscapedBytes(file) ? Buffer.from(encodeSurrogateEscapedString(file)) : file; +} + +export function checksumDisplayFile(file) { + const text = pathDisplayName(file); + return /[\\\n\r\t]/.test(text) || hasSurrogateEscapedBytes(text) ? checksumShellQuote(text) : text; +} + +export function checksumShellQuote(file) { + let out = ""; + let plain = ""; + const flush = () => { + if (!plain) return; + out += `'${plain.replace(/'/g, "'\\''")}'`; + plain = ""; + }; + for (const ch of String(file)) { + const code = ch.charCodeAt(0); + if (ch === "\n" || ch === "\t" || ch === "\r") { + flush(); + out += `$'\\${ch === "\n" ? "n" : ch === "\t" ? "t" : "r"}'`; + } else if (code >= 0xdc80 && code <= 0xdcff) { + flush(); + out += `$'\\${(code - 0xdc00).toString(8).padStart(3, "0")}'`; + } else { + plain += ch; + } + } + flush(); + return out; +} + +export function checksumAlgorithmMatches(requested, tagged) { + if (requested === tagged.name) return true; + if (requested === "blake2b512" && tagged.name === "blake2b") return true; + if (requested === "sha2" && /^sha(224|256|384|512)$/.test(tagged.name)) return true; + return false; +} + +export function checksumAlgorithmFromTag(tag) { + const upper = tag.toUpperCase(); + if (upper === "MD5") return { name: "md5" }; + if (upper === "SHA1") return { name: "sha1" }; + const sha = upper.match(/^SHA(224|256|384|512)$/); + if (sha) return { name: `sha${sha[1]}` }; + const sha2 = upper.match(/^SHA2-(224|256|384|512)$/); + if (sha2) return { name: "sha2", opts: { l: sha2[1] } }; + const sha3 = upper.match(/^SHA3-(224|256|384|512)$/); + if (sha3) return { name: "sha3", opts: { l: sha3[1] } }; + const blake = tag.match(/^BLAKE2b(?:-(\d+))?$/); + if (blake) return { name: "blake2b", opts: blake[1] ? { l: blake[1], tagged: true } : { tagged: true } }; + if (upper === "SM3") return { name: "sm3", opts: { tagged: true } }; + return null; +} + +export function diagnosticPrefix(opts = {}) { + return opts.program ? `${opts.program}: ` : ""; +} + +export function checksumDisplayName(algorithm, opts = {}) { + if (algorithm === "blake2b") return "BLAKE2b"; + if (algorithm === "sha2") return `SHA2-${opts?.l ?? opts?.length ?? ""}`.replace(/-$/, ""); + if (algorithm === "sha3") return `SHA3-${opts?.l ?? opts?.length ?? ""}`.replace(/-$/, ""); + return String(algorithm).toUpperCase(); +} + +export function invalidDigestLengthError(program, algorithm, length, message) { + const quotedMessage = message.replaceAll(`'${algorithm}'`, localeQuotedDiagnostic(algorithm)); + const error = new UsageError(`invalid length: ${localeQuotedDiagnostic(length)}\n${program}: ${quotedMessage}`, false); + return error; +} + +export function validateBlake2bLength(program, length) { + const text = String(length); + if (!/^\d+$/.test(text)) { + if (/^-\d+$/.test(text)) throw new UsageError(`invalid length: ${localeQuotedEscapedDiagnostic(length)}: Value too large for defined data type`, false); + throw new UsageError(`invalid length: ${localeQuotedEscapedDiagnostic(length)}`, false); + } + const n = Number(text); + if (!Number.isSafeInteger(n) || n > 512) throw invalidDigestLengthError(program, "BLAKE2b", length, "maximum digest length for 'BLAKE2b' is 512 bits"); + if (n % 8 !== 0) throw invalidDigestLengthError(program, "BLAKE2b", length, "length is not a multiple of 8"); +} + +export function parseExpectedChecksum(token, algorithm, opts = {}) { + if (/^[0-9a-fA-F]+$/.test(token) && token.length % 2 === 0) { + const digestBytes = token.length / 2; + if (!validChecksumDigestLength(algorithm, digestBytes, opts)) return null; + if (algorithm === "blake2b" || algorithm === "blake2b512") return { expected: token.toLowerCase(), hashAlgorithm: "blake2b512", blake2bBytes: digestBytes }; + return { expected: token.toLowerCase(), hashAlgorithm: cksumAlgorithmForDigest(algorithm, digestBytes, opts) }; + } + if (!/^[A-Za-z0-9+/]+={0,2}$/.test(token) || token.length % 4 !== 0) return null; + let digest; + try { + digest = Buffer.from(token, "base64"); + } catch { + return null; + } + if (digest.length === 0) return null; + try { + if (!validChecksumDigestLength(algorithm, digest.length, opts)) return null; + if (algorithm === "blake2b" || algorithm === "blake2b512") return { expected: digest.toString("hex"), hashAlgorithm: "blake2b512", blake2bBytes: digest.length }; + return { expected: digest.toString("hex"), hashAlgorithm: cksumAlgorithmForDigest(algorithm, digest.length, opts) }; + } catch { + return null; + } +} + +export function validChecksumDigestLength(algorithm, digestBytes, opts = {}) { + const fixed = { md5: 16, sha1: 20, sha224: 28, sha256: 32, sha384: 48, sha512: 64, sm3: 32 }; + if (fixed[algorithm] != null) return digestBytes === fixed[algorithm]; + if (algorithm === "sha2" || algorithm === "sha3") return [28, 32, 48, 64].includes(digestBytes); + if (algorithm === "blake2b" || algorithm === "blake2b512") { + if (opts.tagged && opts.l == null && opts.length == null) return digestBytes === 64; + return digestBytes >= 1 && digestBytes <= 64; + } + if (algorithm === "auto") return false; + return true; +} + +export function fileReadErrorMessage(error) { + return systemErrorMessage(error); +} diff --git a/src/shared/head-tail.js b/src/shared/head-tail.js new file mode 100644 index 0000000..c3789cb --- /dev/null +++ b/src/shared/head-tail.js @@ -0,0 +1,149 @@ +import { open, stat } from "node:fs/promises"; +import { UsageError, stdout } from "./diagnostics.js"; + +export function parseByteCount(value) { + const match = String(value).match(/^(\d+)([A-Za-z]*)$/); + if (!match) throw new UsageError(`invalid byte count: ${value}`); + const n = BigInt(match[1]); + const scale = { + "": 1n, b: 512n, + k: 1024n, K: 1024n, KiB: 1024n, kiB: 1024n, + m: 1024n ** 2n, M: 1024n ** 2n, MiB: 1024n ** 2n, miB: 1024n ** 2n, + g: 1024n ** 3n, G: 1024n ** 3n, GiB: 1024n ** 3n, giB: 1024n ** 3n, + t: 1024n ** 4n, T: 1024n ** 4n, TiB: 1024n ** 4n, tiB: 1024n ** 4n, + P: 1024n ** 5n, PiB: 1024n ** 5n, + E: 1024n ** 6n, EiB: 1024n ** 6n, + Z: 1024n ** 7n, ZiB: 1024n ** 7n, + Y: 1024n ** 8n, YiB: 1024n ** 8n, + R: 1024n ** 9n, RiB: 1024n ** 9n, + Q: 1024n ** 10n, QiB: 1024n ** 10n, + KB: 1000n, kB: 1000n, MB: 1000n ** 2n, mB: 1000n ** 2n, + GB: 1000n ** 3n, gB: 1000n ** 3n, TB: 1000n ** 4n, tB: 1000n ** 4n, + PB: 1000n ** 5n, EB: 1000n ** 6n, ZB: 1000n ** 7n, YB: 1000n ** 8n, + RB: 1000n ** 9n, QB: 1000n ** 10n, + }[match[2]]; + if (!scale) throw new UsageError(`invalid byte count: ${value}`); + const amount = n * scale; + return amount > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(amount); +} + +export async function isTailStreamingDevice(file) { + const s = await stat(file).catch(() => null); + return !!(s?.isCharacterDevice?.() || s?.isBlockDevice?.()); +} + +export async function tailFirstBytes(file, count) { + if (count <= 0) return; + const handle = await open(file, "r"); + const buffer = Buffer.allocUnsafe(Math.min(64 * 1024, count)); + let remaining = count; + try { + while (remaining > 0) { + const { bytesRead } = await handle.read(buffer, 0, Math.min(buffer.length, remaining), null); + if (bytesRead === 0) break; + stdout(buffer.subarray(0, bytesRead)); + remaining -= bytesRead; + } + } finally { + await handle.close().catch(() => {}); + } +} + +export function splitDelimitedByteRecords(data, delimiter) { + const records = []; + const sep = delimiter.charCodeAt(0); + let start = 0; + for (let i = 0; i < data.length; i++) { + if (data[i] !== sep) continue; + records.push(Buffer.from(data.subarray(start, i + 1))); + start = i + 1; + } + if (start < data.length) records.push(Buffer.from(data.subarray(start))); + return records; +} + +export function normalizeHeadTailArgs(args, command) { + const normalized = []; + let end = false; + let valueFor = null; + for (const arg of args) { + if (end) { + normalized.push(arg); + continue; + } + if (valueFor) { + normalized.push(arg); + valueFor = null; + continue; + } + if (arg === "--") { + end = true; + normalized.push(arg); + } else if (arg === "-n" || arg === "-c" || arg === "--lines" || arg === "--bytes") { + valueFor = arg; + normalized.push(arg); + } else if (command === "tail" && /^-\d+[cbk].+/.test(arg)) { + throw new UsageError(`option used in invalid context -- ${arg[1]}`); + } else if (command === "tail" && /^([+-])(\d+)([cbk])$/.test(arg)) { + const [, sign, count, suffix] = arg.match(/^([+-])(\d+)([cbk])$/); + normalized.push("-c", `${sign}${count}${suffix === "c" ? "" : suffix}`); + } else if (command === "tail" && /^([+-])(\d+)l$/.test(arg)) { + const [, sign, count] = arg.match(/^([+-])(\d+)l$/); + normalized.push("-n", `${sign}${count}`); + } else if (command === "tail" && arg === "+c") { + normalized.push("-c", "+10"); + } else if (command === "tail" && arg === "+l") { + normalized.push("-n", "+10"); + } else if (command === "tail" && arg === "-l") { + normalized.push("-n", "10"); + } else if (command === "tail" && arg === "-b") { + normalized.push("-c", "10b"); + } else if (/^-\d+[cbk]$/.test(arg)) { + const suffix = arg.at(-1); + normalized.push("-c", suffix === "c" ? arg.slice(1, -1) : arg.slice(1)); + } else if (/^-\d/.test(arg)) { + normalized.push("-n", arg.slice(1)); + } else if (command === "tail" && /^\+\d+$/.test(arg)) { + normalized.push("-n", arg); + } else { + normalized.push(arg); + } + } + return normalized; +} + +export function headTailHeaderMode(args) { + let mode = null; + let end = false; + let valueFor = null; + for (const arg of args) { + if (end) continue; + if (valueFor) { + valueFor = null; + continue; + } + if (arg === "--") { + end = true; + continue; + } + if (arg === "-n" || arg === "-c" || arg === "--lines" || arg === "--bytes" || arg === "--sleep" || arg === "--sleep-interval" || arg === "--max-unchanged-stats" || arg === "--pid") { + valueFor = arg; + continue; + } + if (arg === "--quiet" || arg === "--silent") mode = "quiet"; + else if (arg === "--verbose") mode = "verbose"; + else if (/^-[^-]/.test(arg)) { + for (const ch of arg.slice(1)) { + if (ch === "q") mode = "quiet"; + else if (ch === "v") mode = "verbose"; + } + } + } + return mode; +} + +export function headTailShouldPrintHeader(mode, fileCount) { + if (mode === "verbose") return true; + if (mode === "quiet") return false; + return fileCount > 1; +} diff --git a/src/shared/help-options.js b/src/shared/help-options.js new file mode 100644 index 0000000..3717676 --- /dev/null +++ b/src/shared/help-options.js @@ -0,0 +1,94 @@ +export const HASH_HELP_OPTIONS = ["--binary", "--check", "--ignore-missing", "--quiet", "--status", "--strict", "--tag", "--text", "--warn", "--zero", "-b", "-c", "-t", "-w", "-z"]; + +export const B2SUM_HELP_OPTIONS = ["--binary", "--check", "--ignore-missing", "--length", "--quiet", "--status", "--strict", "--tag", "--text", "--warn", "--zero", "-b", "-c", "-l", "-t", "-w", "-z"]; + +export const BASE_ENCODING_HELP_OPTIONS = ["--decode", "--ignore-garbage", "--wrap", "-d", "-i", "-w"]; + +export const COMMAND_HELP_OPTIONS = { + basename: ["--multiple", "--suffix", "--zero", "-a", "-s", "-z"], + base32: BASE_ENCODING_HELP_OPTIONS, + base64: BASE_ENCODING_HELP_OPTIONS, + basenc: ["--base16", "--base2lsbf", "--base2msbf", "--base32", "--base32hex", "--base58", "--base64", "--base64url", "--decode", "--ignore-garbage", "--wrap", "--z85", "-d", "-i", "-w"], + b2sum: B2SUM_HELP_OPTIONS, + cat: ["--number", "--number-nonblank", "--show-all", "--show-ends", "--show-nonprinting", "--show-tabs", "--squeeze-blank", "-A", "-E", "-T", "-b", "-e", "-n", "-s", "-t", "-u", "-v"], + chcon: ["--dereference", "--no-dereference", "--no-preserve-root", "--preserve-root", "--range", "--recursive", "--reference", "--role", "--type", "--user", "--verbose", "-R", "-h", "-l", "-r", "-t", "-u", "-v"], + chmod: ["--changes", "--dereference", "--no-dereference", "--no-preserve-root", "--preserve-root", "--quiet", "--recursive", "--reference", "--silent", "--verbose", "-H", "-L", "-P", "-R", "-c", "-f", "-h", "-v"], + chown: ["--changes", "--dereference", "--from", "--no-dereference", "--no-preserve-root", "--preserve-root", "--quiet", "--recursive", "--reference", "--silent", "--verbose", "-H", "-L", "-P", "-R", "-c", "-f", "-h", "-v"], + chgrp: ["--changes", "--dereference", "--from", "--no-dereference", "--no-preserve-root", "--preserve-root", "--quiet", "--recursive", "--reference", "--silent", "--verbose", "-H", "-L", "-P", "-R", "-c", "-f", "-h", "-v"], + chroot: ["--groups", "--skip-chdir", "--userspec"], + cksum: ["--algorithm", "--base64", "--check", "--debug", "--ignore-missing", "--length", "--quiet", "--raw", "--status", "--strict", "--tag", "--untagged", "--warn", "--zero", "-a", "-c", "-l", "-w", "-z"], + comm: ["--check-order", "--nocheck-order", "--output-delimiter", "--total", "--zero-terminated", "-1", "-2", "-3", "-z"], + cp: ["--archive", "--attributes-only", "--backup", "--context", "--copy-contents", "--debug", "--dereference", "--force", "--interactive", "--keep-directory-symlink", "--link", "--no-clobber", "--no-dereference", "--no-preserve", "--no-target-directory", "--one-file-system", "--parents", "--preserve", "--recursive", "--reflink", "--remove-destination", "--sparse", "--strip-trailing-slashes", "--suffix", "--symbolic-link", "--target-directory", "--update", "--verbose", "-H", "-L", "-P", "-R", "-S", "-T", "-Z", "-a", "-b", "-d", "-f", "-i", "-l", "-n", "-p", "-r", "-s", "-t", "-u", "-v", "-x"], + csplit: ["--digits", "--elide-empty-files", "--keep-files", "--prefix", "--quiet", "--silent", "--suffix-format", "--suppress-matched", "-b", "-f", "-k", "-n", "-s", "-z"], + cut: ["--bytes", "--characters", "--complement", "--delimiter", "--fields", "--no-partial", "--only-delimited", "--output-delimiter", "--whitespace-delimited", "--zero-terminated", "-F", "-O", "-b", "-c", "-d", "-f", "-n", "-s", "-w", "-z"], + date: ["--date", "--debug", "--file", "--iso-8601", "--reference", "--resolution", "--rfc-3339", "--rfc-email", "--set", "--universal", "--utc", "-I", "-R", "-d", "-f", "-r", "-s", "-u"], + df: ["--all", "--block-size", "--exclude-type", "--human-readable", "--inodes", "--local", "--no-sync", "--output", "--portability", "--print-type", "--si", "--sync", "--total", "--type", "-B", "-H", "-P", "-T", "-a", "-h", "-i", "-k", "-l", "-t", "-x"], + dir: ["--all", "--almost-all", "--author", "--block-size", "--classify", "--color", "--context", "--dereference", "--dereference-command-line", "--dereference-command-line-symlink-to-dir", "--directory", "--dired", "--escape", "--file-type", "--format", "--full-time", "--group-directories-first", "--hide", "--hide-control-chars", "--human-readable", "--hyperlink", "--ignore", "--ignore-backups", "--indicator-style", "--inode", "--kibibytes", "--literal", "--no-group", "--numeric-uid-gid", "--quote-name", "--quoting-style", "--recursive", "--reverse", "--show-control-chars", "--si", "--size", "--sort", "--tabsize", "--time", "--time-style", "--width", "--zero", "-1", "-C", "-S", "-U", "-X", "-c", "-g", "-l", "-m", "-o", "-p", "-t", "-u", "-v", "-x", "-A", "-B", "-D", "-F", "-G", "-H", "-I", "-L", "-N", "-Q", "-R", "-T", "-Z", "-a", "-b", "-d", "-f", "-h", "-i", "-k", "-n", "-q", "-r", "-s", "-w"], + dircolors: ["--bourne-shell", "--c-shell", "--csh", "--print-database", "--print-ls-colors", "--sh", "-b", "-c", "-p"], + dirname: ["--zero", "-z"], + du: ["--all", "--apparent-size", "--block-size", "--bytes", "--count-links", "--dereference", "--dereference-args", "--exclude", "--exclude-from", "--files0-from", "--human-readable", "--inodes", "--max-depth", "--no-dereference", "--null", "--one-file-system", "--separate-dirs", "--si", "--summarize", "--threshold", "--time", "--time-style", "--total", "-0", "-A", "-B", "-D", "-H", "-L", "-P", "-S", "-X", "-a", "-b", "-c", "-d", "-h", "-k", "-l", "-m", "-s", "-t", "-x"], + env: ["--argv0", "--block-signal", "--chdir", "--debug", "--default-signal", "--ignore-environment", "--ignore-signal", "--list-signal-handling", "--null", "--split-string", "--unset", "-0", "-C", "-S", "-a", "-i", "-u", "-v"], + expand: ["--initial", "--tabs", "-i", "-t"], + factor: ["--exponents", "-h"], + fmt: ["--crown-margin", "--goal", "--prefix", "--split-only", "--tagged-paragraph", "--uniform-spacing", "--width", "-c", "-g", "-p", "-s", "-t", "-u", "-w"], + fold: ["--bytes", "--characters", "--spaces", "--width", "-b", "-c", "-s", "-w"], + head: ["--bytes", "--lines", "--quiet", "--silent", "--verbose", "--zero-terminated", "-c", "-n", "-q", "-v", "-z"], + id: ["--context", "--group", "--groups", "--name", "--real", "--user", "--zero", "-G", "-Z", "-g", "-n", "-r", "-u", "-z"], + install: ["--backup", "--compare", "--context", "--debug", "--directory", "--group", "--mode", "--no-target-directory", "--owner", "--preserve-context", "--preserve-timestamps", "--strip", "--strip-program", "--suffix", "--target-directory", "--verbose", "-C", "-D", "-S", "-T", "-Z", "-b", "-c", "-d", "-g", "-m", "-o", "-p", "-s", "-t", "-v"], + join: ["--check-order", "--header", "--ignore-case", "--nocheck-order", "--zero-terminated", "-1", "-2", "-a", "-e", "-i", "-j", "-o", "-t", "-v", "-z"], + kill: ["--list", "--signal", "--table", "-l", "-s", "-t"], + ln: ["--backup", "--directory", "--force", "--interactive", "--logical", "--no-dereference", "--no-target-directory", "--physical", "--relative", "--suffix", "--symbolic", "--target-directory", "--verbose", "-F", "-L", "-P", "-S", "-T", "-b", "-d", "-f", "-i", "-n", "-r", "-s", "-t", "-v"], + ls: ["--all", "--almost-all", "--author", "--block-size", "--classify", "--color", "--context", "--dereference", "--dereference-command-line", "--dereference-command-line-symlink-to-dir", "--directory", "--dired", "--escape", "--file-type", "--format", "--full-time", "--group-directories-first", "--hide", "--hide-control-chars", "--human-readable", "--hyperlink", "--ignore", "--ignore-backups", "--indicator-style", "--inode", "--kibibytes", "--literal", "--no-group", "--numeric-uid-gid", "--quote-name", "--quoting-style", "--recursive", "--reverse", "--show-control-chars", "--si", "--size", "--sort", "--tabsize", "--time", "--time-style", "--width", "--zero", "-1", "-C", "-S", "-U", "-X", "-c", "-g", "-l", "-m", "-o", "-p", "-t", "-u", "-v", "-x", "-A", "-B", "-D", "-F", "-G", "-H", "-I", "-L", "-N", "-Q", "-R", "-T", "-Z", "-a", "-b", "-d", "-f", "-h", "-i", "-k", "-n", "-q", "-r", "-s", "-w"], + md5sum: HASH_HELP_OPTIONS, + mkdir: ["--context", "--mode", "--parents", "--verbose", "-Z", "-m", "-p", "-v"], + mkfifo: ["--context", "--mode", "-Z", "-m"], + mknod: ["--context", "--mode", "-Z", "-m"], + mktemp: ["--directory", "--dry-run", "--quiet", "--suffix", "--tmpdir", "-d", "-p", "-q", "-u"], + mv: ["--backup", "--context", "--debug", "--exchange", "--force", "--interactive", "--no-clobber", "--no-copy", "--no-target-directory", "--strip-trailing-slashes", "--suffix", "--target-directory", "--update", "--verbose", "-S", "-T", "-Z", "-b", "-f", "-i", "-n", "-t", "-u", "-v"], + nice: ["--adjustment", "-n"], + nl: ["--body-numbering", "--footer-numbering", "--header-numbering", "--join-blank-lines", "--line-increment", "--no-renumber", "--number-format", "--number-separator", "--number-width", "--section-delimiter", "--starting-line-number", "-b", "-d", "-f", "-h", "-i", "-l", "-n", "-p", "-s", "-v", "-w"], + nproc: ["--all", "--ignore"], + numfmt: ["--debug", "--delimiter", "--field", "--format", "--from", "--from-unit", "--grouping", "--header", "--invalid", "--padding", "--round", "--suffix", "--to", "--to-unit", "--unit-separator", "--zero-terminated", "-d", "-z"], + od: ["--address-radix", "--endian", "--format", "--output-duplicates", "--read-bytes", "--skip-bytes", "--strings", "--traditional", "--width", "-A", "-N", "-S", "-a", "-b", "-c", "-d", "-f", "-i", "-l", "-j", "-o", "-s", "-t", "-v", "-w", "-x"], + paste: ["--delimiters", "--serial", "--zero-terminated", "-d", "-s", "-z"], + pathchk: ["--portability", "-p", "-P"], + pinky: ["--lookup", "-b", "-f", "-h", "-i", "-l", "-p", "-q", "-s", "-w"], + pr: ["--across", "--columns", "--date-format", "--double-space", "--expand-tabs", "--first-line-number", "--form-feed", "--header", "--indent", "--join-lines", "--length", "--merge", "--no-file-warnings", "--number-lines", "--omit-header", "--omit-pagination", "--output-tabs", "--page-width", "--pages", "--sep-string", "--separator", "--show-control-chars", "--show-nonprinting", "--width", "-D", "-F", "-J", "-N", "-S", "-T", "-W", "-a", "-c", "-d", "-e", "-f", "-h", "-i", "-l", "-m", "-n", "-o", "-r", "-s", "-t", "-v", "-w"], + printenv: ["--null", "-0"], + ptx: ["--auto-reference", "--break-file", "--flag-truncation", "--format", "--gap-size", "--ignore-case", "--ignore-file", "--macro-name", "--only-file", "--references", "--right-side-refs", "--sentence-regexp", "--traditional", "--typeset-mode", "--width", "--word-regexp", "-A", "-F", "-G", "-M", "-R", "-S", "-W", "-b", "-f", "-g", "-i", "-o", "-r", "-t", "-w"], + pwd: ["--logical", "--physical", "-L", "-P"], + readlink: ["--canonicalize", "--canonicalize-existing", "--canonicalize-missing", "--no-newline", "--quiet", "--silent", "--verbose", "--zero", "-e", "-f", "-m", "-n", "-q", "-s", "-v", "-z"], + realpath: ["--canonicalize", "--canonicalize-existing", "--canonicalize-missing", "--logical", "--no-symlinks", "--physical", "--quiet", "--relative-base", "--relative-to", "--strip", "--zero", "-E", "-L", "-P", "-e", "-m", "-q", "-s", "-z"], + rm: ["--dir", "--force", "--interactive", "--no-preserve-root", "--one-file-system", "--preserve-root", "--recursive", "--verbose", "-I", "-R", "-d", "-f", "-i", "-r", "-v"], + rmdir: ["--ignore-fail-on-non-empty", "--parents", "--verbose", "-p", "-v"], + runcon: ["--compute", "--range", "--role", "--type", "--user", "-c", "-l", "-r", "-t", "-u"], + seq: ["--equal-width", "--format", "--separator", "-f", "-s", "-w"], + sha1sum: HASH_HELP_OPTIONS, + sha224sum: HASH_HELP_OPTIONS, + sha256sum: HASH_HELP_OPTIONS, + sha384sum: HASH_HELP_OPTIONS, + sha512sum: HASH_HELP_OPTIONS, + sm3sum: HASH_HELP_OPTIONS, + shred: ["--exact", "--force", "--iterations", "--random-source", "--remove", "--size", "--verbose", "--zero", "-f", "-n", "-s", "-u", "-v", "-x", "-z"], + shuf: ["--echo", "--head-count", "--input-range", "--output", "--random-source", "--repeat", "--zero-terminated", "-e", "-i", "-n", "-o", "-r", "-z"], + sort: ["--batch-size", "--buffer-size", "--check", "--compress-program", "--debug", "--dictionary-order", "--field-separator", "--files0-from", "--general-numeric-sort", "--human-numeric-sort", "--ignore-case", "--ignore-leading-blanks", "--ignore-nonprinting", "--key", "--merge", "--month-sort", "--numeric-sort", "--output", "--parallel", "--random-sort", "--random-source", "--reverse", "--sort", "--stable", "--temporary-directory", "--unique", "--version-sort", "--zero-terminated", "-C", "-M", "-R", "-S", "-T", "-V", "-b", "-c", "-d", "-f", "-g", "-h", "-i", "-k", "-m", "-n", "-o", "-r", "-s", "-t", "-u", "-z"], + split: ["--additional-suffix", "--bytes", "--elide-empty-files", "--filter", "--hex-suffixes", "--line-bytes", "--lines", "--number", "--numeric-suffixes", "--separator", "--suffix-length", "--unbuffered", "--verbose", "-C", "-a", "-b", "-d", "-e", "-l", "-n", "-t", "-u", "-x"], + stat: ["--cached", "--dereference", "--file-system", "--format", "--printf", "--terse", "-L", "-c", "-f", "-t"], + stdbuf: ["--error", "--input", "--output", "-e", "-i", "-o"], + stty: ["--all", "--file", "--save", "-F", "-a", "-g"], + sync: ["--data", "--file-system", "-d", "-f"], + tac: ["--before", "--regex", "--separator", "-b", "-r", "-s"], + tail: ["--bytes", "--debug", "--follow", "--lines", "--max-unchanged-stats", "--pid", "--quiet", "--retry", "--silent", "--sleep-interval", "--verbose", "--zero-terminated", "-F", "-c", "-f", "-n", "-q", "-s", "-v", "-z"], + tee: ["--append", "--ignore-interrupts", "--output-error", "-a", "-i", "-p"], + timeout: ["--foreground", "--kill-after", "--preserve-status", "--signal", "--verbose", "-f", "-k", "-p", "-s", "-v"], + touch: ["--date", "--no-create", "--no-dereference", "--reference", "--time", "-a", "-c", "-d", "-f", "-h", "-m", "-r", "-t"], + tr: ["--complement", "--delete", "--squeeze-repeats", "--truncate-set1", "-C", "-c", "-d", "-s", "-t"], + truncate: ["--io-blocks", "--no-create", "--reference", "--size", "-c", "-o", "-r", "-s"], + tty: ["--quiet", "--silent", "-s"], + uname: ["--all", "--hardware-platform", "--kernel-name", "--kernel-release", "--kernel-version", "--machine", "--nodename", "--operating-system", "--processor", "-a", "-i", "-m", "-n", "-o", "-p", "-r", "-s", "-v"], + unexpand: ["--all", "--first-only", "--tabs", "-a", "-t"], + uniq: ["--all-repeated", "--check-chars", "--count", "--group", "--ignore-case", "--repeated", "--skip-chars", "--skip-fields", "--unique", "--zero-terminated", "-D", "-c", "-d", "-f", "-i", "-s", "-u", "-w", "-z"], + vdir: ["--all", "--almost-all", "--author", "--block-size", "--classify", "--color", "--context", "--dereference", "--dereference-command-line", "--dereference-command-line-symlink-to-dir", "--directory", "--dired", "--escape", "--file-type", "--format", "--full-time", "--group-directories-first", "--hide", "--hide-control-chars", "--human-readable", "--hyperlink", "--ignore", "--ignore-backups", "--indicator-style", "--inode", "--kibibytes", "--literal", "--no-group", "--numeric-uid-gid", "--quote-name", "--quoting-style", "--recursive", "--reverse", "--show-control-chars", "--si", "--size", "--sort", "--tabsize", "--time", "--time-style", "--width", "--zero", "-1", "-C", "-S", "-U", "-X", "-c", "-g", "-l", "-m", "-o", "-p", "-t", "-u", "-v", "-x", "-A", "-B", "-D", "-F", "-G", "-H", "-I", "-L", "-N", "-Q", "-R", "-T", "-Z", "-a", "-b", "-d", "-f", "-h", "-i", "-k", "-n", "-q", "-r", "-s", "-w"], + wc: ["--bytes", "--chars", "--debug", "--files0-from", "--lines", "--max-line-length", "--total", "--words", "-L", "-c", "-l", "-m", "-w"], + who: ["--all", "--boot", "--count", "--dead", "--heading", "--login", "--lookup", "--mesg", "--message", "--process", "--runlevel", "--short", "--time", "--users", "--writable", "-H", "-T", "-a", "-b", "-d", "-l", "-m", "-p", "-q", "-r", "-s", "-t", "-u", "-w"], +}; diff --git a/src/shared/help.js b/src/shared/help.js new file mode 100644 index 0000000..2979090 --- /dev/null +++ b/src/shared/help.js @@ -0,0 +1,631 @@ +import { stdout } from "./diagnostics.js"; +import { COMMAND_HELP_OPTIONS } from "./help-options.js"; + +export const SPECIAL_HELP = { + arch: { + usage: "Usage: arch [OPTION]...", + description: "Print machine architecture.", + }, + basename: { + usage: "Usage: basename NAME [SUFFIX]\n or: basename OPTION... NAME...", + description: "Print NAME with any leading directory components removed.\nIf specified, also remove a trailing SUFFIX.", + options: ["-a, --multiple", "-s, --suffix=SUFFIX", "-z, --zero"], + }, + base32: { + usage: "Usage: base32 [OPTION]... [FILE]", + description: "Base32 encode or decode FILE, or standard input, to standard output.", + options: COMMAND_HELP_OPTIONS.base32, + }, + base64: { + usage: "Usage: base64 [OPTION]... [FILE]", + description: "Base64 encode or decode FILE, or standard input, to standard output.", + options: COMMAND_HELP_OPTIONS.base64, + }, + b2sum: { + usage: "Usage: b2sum [OPTION]... [FILE]...", + description: "Print or check BLAKE2b (512-bit) checksums.\nLegacy interface to the cksum utility.", + options: COMMAND_HELP_OPTIONS.b2sum, + }, + basenc: { + usage: "Usage: basenc [OPTION]... [FILE]", + description: "basenc encode or decode FILE, or standard input, to standard output.", + options: COMMAND_HELP_OPTIONS.basenc, + }, + cat: { + usage: "Usage: cat [OPTION]... [FILE]...", + description: "Concatenate FILE(s) to standard output.", + options: COMMAND_HELP_OPTIONS.cat, + }, + comm: { + usage: "Usage: comm [OPTION]... FILE1 FILE2", + description: "Compare sorted files FILE1 and FILE2 line by line.", + options: COMMAND_HELP_OPTIONS.comm, + }, + chgrp: { + usage: "Usage: chgrp [OPTION]... GROUP FILE...\n or: chgrp [OPTION]... --reference=RFILE FILE...", + description: "Change the group of each FILE to GROUP.", + options: COMMAND_HELP_OPTIONS.chgrp, + }, + chmod: { + usage: "Usage: chmod [OPTION]... MODE[,MODE]... FILE...\n or: chmod [OPTION]... OCTAL-MODE FILE...\n or: chmod [OPTION]... --reference=RFILE FILE...", + description: "Change the mode of each FILE to MODE.", + options: COMMAND_HELP_OPTIONS.chmod, + }, + chown: { + usage: "Usage: chown [OPTION]... [OWNER][:[GROUP]] FILE...\n or: chown [OPTION]... --reference=RFILE FILE...", + description: "Change the owner and/or group of each FILE to OWNER and/or GROUP.", + options: COMMAND_HELP_OPTIONS.chown, + }, + chcon: { + usage: "Usage: chcon [OPTION]... CONTEXT FILE...\n or: chcon [OPTION]... [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE...\n or: chcon [OPTION]... --reference=RFILE FILE...", + description: "Change the SELinux security context of each FILE to CONTEXT.", + options: COMMAND_HELP_OPTIONS.chcon, + }, + chroot: { + usage: "Usage: chroot [OPTION]... NEWROOT [COMMAND [ARG]...]", + description: "Run COMMAND with root directory set to NEWROOT.", + options: COMMAND_HELP_OPTIONS.chroot, + }, + cp: { + usage: "Usage: cp [OPTION]... [-T] SOURCE DEST\n or: cp [OPTION]... SOURCE... DIRECTORY\n or: cp [OPTION]... -t DIRECTORY SOURCE...", + description: "Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.", + options: COMMAND_HELP_OPTIONS.cp, + }, + csplit: { + usage: "Usage: csplit [OPTION]... FILE PATTERN...", + description: "Output pieces of FILE separated by PATTERN(s) to files 'xx00', 'xx01', ...,\nand output byte counts of each piece to standard output.", + options: COMMAND_HELP_OPTIONS.csplit, + }, + cut: { + usage: "Usage: cut OPTION... [FILE]...", + description: "Print selected parts of lines from each FILE to standard output.", + options: COMMAND_HELP_OPTIONS.cut, + }, + date: { + usage: "Usage: date [OPTION]... [+FORMAT]\n or: date [OPTION]... MMDDhhmm[[CC]YY][.ss]", + description: "Display date and time in the given FORMAT.\nWith -s, or with MMDDhhmm[[CC]YY][.ss], set the date and time first.", + options: COMMAND_HELP_OPTIONS.date, + }, + dd: { + usage: "Usage: dd [OPERAND]...\n or: dd OPTION", + description: "Copy a file, converting and formatting according to the operands.", + }, + df: { + usage: "Usage: df [OPTION]... [FILE]...", + description: "Show information about the file system on which each FILE resides,\nor all file systems by default.", + options: COMMAND_HELP_OPTIONS.df, + }, + dir: { + usage: "Usage: dir [OPTION]... [FILE]...", + description: "List information about the FILEs (the current directory by default).\nSort entries alphabetically if none of -cftuvSUX nor --sort is specified.", + options: COMMAND_HELP_OPTIONS.dir, + }, + dircolors: { + usage: "Usage: dircolors [OPTION]... [FILE]", + description: "Output commands to set the LS_COLORS environment variable.", + options: COMMAND_HELP_OPTIONS.dircolors, + }, + dirname: { + usage: "Usage: dirname [OPTION] NAME...", + description: "Output each NAME with its last non-slash component and trailing slashes\nremoved; if NAME contains no /'s, output '.' (meaning the current directory).", + options: ["-z, --zero"], + }, + du: { + usage: "Usage: du [OPTION]... [FILE]...\n or: du [OPTION]... --files0-from=F", + description: "Summarize device usage of the set of FILEs, recursively for directories.", + options: COMMAND_HELP_OPTIONS.du, + }, + env: { + usage: "Usage: env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]", + description: "Set each NAME to VALUE in the environment and run COMMAND.", + options: COMMAND_HELP_OPTIONS.env, + }, + expr: { + usage: "Usage: expr EXPRESSION\n or: expr OPTION", + description: "", + options: [], + }, + expand: { + usage: "Usage: expand [OPTION]... [FILE]...", + description: "Convert tabs in each FILE to spaces, writing to standard output.", + options: COMMAND_HELP_OPTIONS.expand, + }, + fold: { + usage: "Usage: fold [OPTION]... [FILE]...", + description: "Wrap input lines in each FILE, writing to standard output.", + options: COMMAND_HELP_OPTIONS.fold, + }, + groups: { + usage: "Usage: groups [OPTION]... [USERNAME]...", + description: "Print group memberships for each USERNAME or, if no USERNAME is specified, for\nthe current process (which may differ if the groups database has changed).", + }, + ginstall: { + usage: "Usage: install [OPTION]... [-T] SOURCE DEST\n or: install [OPTION]... SOURCE... DIRECTORY\n or: install [OPTION]... -t DIRECTORY SOURCE...\n or: install [OPTION]... -d DIRECTORY...", + description: "Copy files and set attributes.", + options: COMMAND_HELP_OPTIONS.install, + }, + hostid: { + usage: "Usage: hostid [OPTION]", + description: "Print the numeric identifier (in hexadecimal) for the current host.", + }, + head: { + usage: "Usage: head [OPTION]... [FILE]...", + description: "Print the first 10 lines of each FILE to standard output.\nWith more than one FILE, precede each with a header giving the file name.", + options: COMMAND_HELP_OPTIONS.head, + }, + id: { + usage: "Usage: id [OPTION]... [USER]...", + description: "Print user and group information for each specified USER,\nor (when USER omitted) for the current process.", + options: COMMAND_HELP_OPTIONS.id, + }, + install: { + usage: "Usage: install [OPTION]... [-T] SOURCE DEST\n or: install [OPTION]... SOURCE... DIRECTORY\n or: install [OPTION]... -t DIRECTORY SOURCE...\n or: install [OPTION]... -d DIRECTORY...", + description: "Copy files and set attributes.", + options: COMMAND_HELP_OPTIONS.install, + }, + join: { + usage: "Usage: join [OPTION]... FILE1 FILE2", + description: "For each pair of input lines with identical join fields, write a line to\nstandard output.", + options: COMMAND_HELP_OPTIONS.join, + }, + kill: { + usage: "Usage: kill [-s SIGNAL | --signal SIGNAL | -SIGNAL] PID...\n or: kill [-l | --list | -t | --table] [SIGNAL]...", + description: "Send a signal to processes, or list information about signals.", + options: [ + "-s, --signal=SIGNAL specify the name or number of the signal to be sent", + "-l, --list[=SIGNAL] list signal names, or convert signal names and numbers", + "-t, --table print a table of signal numbers, names, and descriptions", + ], + }, + ls: { + usage: "Usage: ls [OPTION]... [FILE]...", + description: "List information about the FILEs (the current directory by default).\nSort entries alphabetically if none of -cftuvSUX nor --sort is specified.", + options: COMMAND_HELP_OPTIONS.ls, + }, + logname: { + usage: "Usage: logname [OPTION]", + description: "Print the user's login name.", + }, + md5sum: { + usage: "Usage: md5sum [OPTION]... [FILE]...", + description: "Print or check MD5 (128-bit) checksums.\nLegacy interface to the cksum utility.", + options: COMMAND_HELP_OPTIONS.md5sum, + }, + factor: { + usage: "Usage: factor [OPTION] [NUMBER]...", + description: "Print the prime factors of each specified integer NUMBER. If none\nare specified on the command line, read them from standard input.", + options: ["-h, --exponents"], + }, + fmt: { + usage: "Usage: fmt [-WIDTH] [OPTION]... [FILE]...", + description: "Reformat each paragraph in the FILE(s), writing to standard output.\nThe option -WIDTH is an abbreviated form of --width=DIGITS.", + options: COMMAND_HELP_OPTIONS.fmt, + }, + mkdir: { + usage: "Usage: mkdir [OPTION]... DIRECTORY...", + description: "Create the DIRECTORY(ies), if they do not already exist.\n\nMandatory arguments to long options are mandatory for short options too.", + options: [ + "-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask", + "-p, --parents no error if existing, make parent directories as needed,\n with their file modes unaffected by any -m option", + "-v, --verbose print a message for each created directory", + "-Z set SELinux security context of each created directory\n to the default type", + "--context[=CTX] like -Z, or if CTX is specified then set the\n SELinux or SMACK security context to CTX", + ], + }, + mkfifo: { + usage: "Usage: mkfifo [OPTION]... NAME...", + description: "Create named pipes (FIFOs) with the given NAMEs.\n\nMandatory arguments to long options are mandatory for short options too.", + options: [ + "-m, --mode=MODE set file permission bits to MODE, not a=rw - umask", + "-Z set the SELinux security context to default type", + "--context[=CTX] like -Z, or if CTX is specified then set the\n SELinux or SMACK security context to CTX", + ], + }, + mktemp: { + usage: "Usage: mktemp [OPTION]... [TEMPLATE]", + description: "Create a temporary file or directory, safely, and print its name.\nTEMPLATE must contain at least 3 consecutive 'X's in last component.\nIf TEMPLATE is not specified, use tmp.XXXXXXXXXX, and --tmpdir is implied.\nFiles are created u+rw, and directories u+rwx, minus umask restrictions.", + options: [ + "-d, --directory create a directory, not a file", + "-u, --dry-run do not create anything; merely print a name (unsafe)", + "-q, --quiet suppress diagnostics about file/dir-creation failure", + "--suffix=SUFF append SUFF to TEMPLATE; SUFF must not contain a slash.\n This option is implied if TEMPLATE does not end in X", + "-p DIR, --tmpdir[=DIR]\n interpret TEMPLATE relative to DIR;\n if DIR is not specified, use $TMPDIR if set, else /tmp.\n With this option, TEMPLATE must not be an absolute name;\n unlike with -t, TEMPLATE may contain slashes,\n but mktemp creates only the final component", + "-t interpret TEMPLATE as a single file name component,\n relative to a directory: $TMPDIR, if set;\n else the directory specified via -p; else /tmp [deprecated]", + ], + }, + mv: { + usage: "Usage: mv [OPTION]... [-T] SOURCE DEST\n or: mv [OPTION]... SOURCE... DIRECTORY\n or: mv [OPTION]... -t DIRECTORY SOURCE...", + description: "Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.", + options: COMMAND_HELP_OPTIONS.mv, + }, + nice: { + usage: "Usage: nice [OPTION] [COMMAND [ARG]...]", + description: "Run COMMAND with an adjusted niceness, which affects process scheduling.\nWith no COMMAND, print the current niceness.", + options: COMMAND_HELP_OPTIONS.nice, + }, + nohup: { + usage: "Usage: nohup COMMAND [ARG]...\n or: nohup OPTION", + description: "Run COMMAND, ignoring hangup signals.", + }, + ln: { + usage: "Usage: ln [OPTION]... [-T] TARGET LINK_NAME\n or: ln [OPTION]... TARGET\n or: ln [OPTION]... TARGET... DIRECTORY\n or: ln [OPTION]... -t DIRECTORY TARGET...", + description: "Create links between files.", + options: COMMAND_HELP_OPTIONS.ln, + }, + mknod: { + usage: "Usage: mknod [OPTION]... NAME TYPE [MAJOR MINOR]", + description: "Create the special file NAME of the given TYPE.\n\nMandatory arguments to long options are mandatory for short options too.", + options: [ + "-m, --mode=MODE set file permission bits to MODE, not a=rw - umask", + "-Z set the SELinux security context to default type", + "--context[=CTX] like -Z, or if CTX is specified then set the\n SELinux or SMACK security context to CTX", + ], + after: "Both MAJOR and MINOR must be specified when TYPE is b, c, or u, and they\nmust be omitted when TYPE is p. If MAJOR or MINOR begins with 0x or 0X,\nit is interpreted as hexadecimal; otherwise, if it begins with 0, as octal;\notherwise, as decimal. TYPE may be:\n\n b create a block (buffered) special file\n c, u create a character (unbuffered) special file\n p create a FIFO\n\nYour shell may have its own version of mknod, which usually supersedes\nthe version described here. Please refer to your shell's documentation\nfor details about the options it supports.", + }, + nproc: { + usage: "Usage: nproc [OPTION]...", + description: "Print the number of processing units available to the current process,\nwhich may be less than the number of online processors.\nIf the 'OMP_NUM_THREADS' or 'OMP_THREAD_LIMIT' environment variables are set,\nthen they will determine the minimum and maximum returned value respectively.", + options: [ + "--all\n print the number of installed processors,\n disregarding any OpenMP environment variables, or CPU quotas.", + "--ignore=N\n if possible, exclude N processing units.\n The result is guaranteed to be at least 1.", + ], + }, + nl: { + usage: "Usage: nl [OPTION]... [FILE]...", + description: "Write each FILE to standard output, with line numbers added.", + options: COMMAND_HELP_OPTIONS.nl, + }, + numfmt: { + usage: "Usage: numfmt [OPTION]... [NUMBER]...", + description: "Reformat NUMBER(s), or the numbers from standard input if none are specified.", + options: COMMAND_HELP_OPTIONS.numfmt, + }, + od: { + usage: "Usage: od [OPTION]... [FILE]...\n or: od [-abcdfilosx]... [FILE] [[+]OFFSET[.][b]]\n or: od --traditional [OPTION]... [FILE] [[+]OFFSET[.][b] [+][LABEL][.][b]]", + description: "Write an unambiguous representation, octal bytes by default,\nof FILE to standard output.", + options: COMMAND_HELP_OPTIONS.od, + }, + pathchk: { + usage: "Usage: pathchk [OPTION]... NAME...", + description: "Diagnose invalid or non-portable file names.", + options: [ + "-p check for most POSIX systems", + "-P check for empty names and leading \"-\"", + "--portability\n check for all POSIX systems (equivalent to -p -P)", + ], + }, + paste: { + usage: "Usage: paste [OPTION]... [FILE]...", + description: "Write lines consisting of the sequentially corresponding lines from\neach FILE, separated by TABs, to standard output.", + options: COMMAND_HELP_OPTIONS.paste, + }, + pinky: { + usage: "Usage: pinky [OPTION]... [USER]...", + description: "", + options: COMMAND_HELP_OPTIONS.pinky, + }, + printenv: { + usage: "Usage: printenv [OPTION] [VARIABLE]...", + description: "Print the values of the specified environment VARIABLE(s).\nIf no VARIABLE is specified, print name and value pairs for them all.", + options: ["-0, --null end each output line with NUL, not newline"], + after: "Your shell may have its own version of printenv, which usually supersedes\nthe version described here. Please refer to your shell's documentation\nfor details about the options it supports.", + }, + printf: { + usage: "Usage: printf FORMAT [ARGUMENT]...\n or: printf OPTION", + description: "Print ARGUMENT(s) according to FORMAT, or execute according to OPTION:", + options: [], + }, + ptx: { + usage: "Usage: ptx [OPTION]... [INPUT]... (without -G)\n or: ptx -G [OPTION]... [INPUT [OUTPUT]]", + description: "Output a permuted index, including context, of the words in the input files.", + options: COMMAND_HELP_OPTIONS.ptx, + }, + pr: { + usage: "Usage: pr [OPTION]... [FILE]...", + description: "Paginate or columnate FILE(s) for printing.", + options: COMMAND_HELP_OPTIONS.pr, + }, + pwd: { + usage: "Usage: pwd [OPTION]...", + description: "Print the full filename of the current working directory.", + options: ["-L, --logical", "-P, --physical"], + }, + readlink: { + usage: "Usage: readlink [OPTION]... FILE...", + description: "Print value of a symbolic link or canonical file name", + options: [ + "-f, --canonicalize\n canonicalize by following every symlink\n in every component of the given name recursively;\n all but the last component must exist", + "-e, --canonicalize-existing\n canonicalize by following every symlink\n in every component of the given name recursively;\n all components must exist", + "-m, --canonicalize-missing\n canonicalize by following every symlink\n in every component of the given name recursively,\n without requirements on components existence", + "-n, --no-newline\n do not output the trailing delimiter", + "-q, --quiet\n suppress most error messages\n (on by default if POSIXLY_CORRECT is not set)", + "-s, --silent\n suppress most error messages\n (on by default if POSIXLY_CORRECT is not set)", + "-v, --verbose\n report error messages\n (on by default if POSIXLY_CORRECT is set)", + "-z, --zero\n end each output line with NUL, not newline", + ], + }, + realpath: { + usage: "Usage: realpath [OPTION]... FILE...", + description: "Print the resolved absolute file name.", + options: [ + "-E, --canonicalize all but the last component must exist (default)", + "-e, --canonicalize-existing all components of the path must exist", + "-m, --canonicalize-missing no path components need exist or be a directory", + "-L, --logical resolve '..' components before symlinks", + "-P, --physical resolve symlinks as encountered (default)", + "-q, --quiet suppress most error messages", + "--relative-to=DIR print the resolved path relative to DIR", + "--relative-base=DIR print absolute paths unless paths below DIR", + "-s, --strip, --no-symlinks don't expand symlinks", + "-z, --zero end each output line with NUL, not newline", + ], + }, + rmdir: { + usage: "Usage: rmdir [OPTION]... DIRECTORY...", + description: "Remove the DIRECTORY(ies), if they are empty.", + options: ["--ignore-fail-on-non-empty", "-p, --parents", "-v, --verbose"], + }, + runcon: { + usage: "Usage: runcon CONTEXT COMMAND [args]\n or: runcon [ -c ] [-u USER] [-r ROLE] [-t TYPE] [-l RANGE] COMMAND [args]", + description: "Run COMMAND with specified SELinux security context.", + options: COMMAND_HELP_OPTIONS.runcon, + }, + rm: { + usage: "Usage: rm [OPTION]... [FILE]...", + description: "Remove (unlink) the FILE(s).", + options: COMMAND_HELP_OPTIONS.rm, + }, + sum: { + usage: "Usage: sum [OPTION]... [FILE]...", + description: "Print or check BSD (16-bit) checksums.\nLegacy interface to the cksum utility.\n\nWith no FILE, or when FILE is -, read standard input.", + options: [ + "-r\n use BSD sum algorithm (the default), use 1K blocks", + "-s, --sysv\n use System V sum algorithm, use 512 bytes blocks", + ], + }, + tac: { + usage: "Usage: tac [OPTION]... [FILE]...", + description: "Write each FILE to standard output, last line first.", + options: COMMAND_HELP_OPTIONS.tac, + }, + sync: { + usage: "Usage: sync [OPTION] [FILE]...", + description: "Synchronize cached writes to persistent storage\n\nIf one or more files are specified, sync only them,\nor their containing file systems.", + options: [ + "-d, --data sync only file data, no unneeded metadata", + "-f, --file-system sync the file systems that contain the files", + ], + }, + shuf: { + usage: "Usage: shuf [OPTION]... [FILE]\n or: shuf -e [OPTION]... [ARG]...\n or: shuf -i LO-HI [OPTION]...", + description: "Write a random permutation of the input lines to standard output.", + options: COMMAND_HELP_OPTIONS.shuf, + }, + shred: { + usage: "Usage: shred [OPTION]... FILE...", + description: "Overwrite the specified FILE(s) repeatedly, in order to make it harder\nfor even very expensive hardware probing to recover the data.", + options: COMMAND_HELP_OPTIONS.shred, + }, + sort: { + usage: "Usage: sort [OPTION]... [FILE]...\n or: sort [OPTION]... --files0-from=F", + description: "Write sorted concatenation of all FILE(s) to standard output.", + options: COMMAND_HELP_OPTIONS.sort, + }, + seq: { + usage: "Usage: seq [OPTION]... LAST\n or: seq [OPTION]... FIRST LAST\n or: seq [OPTION]... FIRST INCREMENT LAST", + description: "Print numbers from FIRST to LAST, in steps of INCREMENT.", + options: COMMAND_HELP_OPTIONS.seq, + }, + sha1sum: { + usage: "Usage: sha1sum [OPTION]... [FILE]...", + description: "Print or check SHA1 (160-bit) checksums.\nLegacy interface to the cksum utility.", + options: COMMAND_HELP_OPTIONS.sha1sum, + }, + sha224sum: { + usage: "Usage: sha224sum [OPTION]... [FILE]...", + description: "Print or check SHA224 (224-bit) checksums.\nLegacy interface to the cksum utility.", + options: COMMAND_HELP_OPTIONS.sha224sum, + }, + sha256sum: { + usage: "Usage: sha256sum [OPTION]... [FILE]...", + description: "Print or check SHA256 (256-bit) checksums.\nLegacy interface to the cksum utility.", + options: COMMAND_HELP_OPTIONS.sha256sum, + }, + sha384sum: { + usage: "Usage: sha384sum [OPTION]... [FILE]...", + description: "Print or check SHA384 (384-bit) checksums.\nLegacy interface to the cksum utility.", + options: COMMAND_HELP_OPTIONS.sha384sum, + }, + sha512sum: { + usage: "Usage: sha512sum [OPTION]... [FILE]...", + description: "Print or check SHA512 (512-bit) checksums.\nLegacy interface to the cksum utility.", + options: COMMAND_HELP_OPTIONS.sha512sum, + }, + sm3sum: { + usage: "Usage: sm3sum [OPTION]... [FILE]...", + description: "Print or check SM3 (256-bit) checksums.\nLegacy interface to the cksum utility.", + options: COMMAND_HELP_OPTIONS.sm3sum, + }, + split: { + usage: "Usage: split [OPTION]... [FILE [PREFIX]]", + description: "Output pieces of FILE to PREFIXaa, PREFIXab, ...;\ndefault size is 1000 lines, and default PREFIX is 'x'.", + options: COMMAND_HELP_OPTIONS.split, + }, + stat: { + usage: "Usage: stat [OPTION]... FILE...", + description: "Display file or file system status.", + options: COMMAND_HELP_OPTIONS.stat, + }, + tail: { + usage: "Usage: tail [OPTION]... [FILE]...", + description: "Print the last 10 lines of each FILE to standard output.\nWith more than one FILE, precede each with a header giving the file name.", + options: COMMAND_HELP_OPTIONS.tail, + }, + tee: { + usage: "Usage: tee [OPTION]... [FILE]...", + description: "Copy standard input to each FILE, and also to standard output.", + options: COMMAND_HELP_OPTIONS.tee, + }, + stty: { + usage: "Usage: stty [-F DEVICE | --file=DEVICE] [SETTING]...\n or: stty [-F DEVICE | --file=DEVICE] [-a|--all]\n or: stty [-F DEVICE | --file=DEVICE] [-g|--save]", + description: "Print or change terminal characteristics.", + options: COMMAND_HELP_OPTIONS.stty, + }, + stdbuf: { + usage: "Usage: stdbuf OPTION... COMMAND", + description: "Run COMMAND, with modified buffering operations for its standard streams.\n\nMandatory arguments to long options are mandatory for short options too.", + options: [ + "-i, --input=MODE adjust standard input stream buffering", + "-o, --output=MODE adjust standard output stream buffering", + "-e, --error=MODE adjust standard error stream buffering", + ], + after: "If MODE is 'L' the corresponding stream will be line buffered.\nThis option is invalid with standard input.\n\nIf MODE is '0' the corresponding stream will be unbuffered.\n\nOtherwise MODE is a number which may be followed by one of the following:\nKB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G,T,P,E,Z,Y,R,Q.\nBinary prefixes can be used, too: KiB=K, MiB=M, and so on.\nIn this case the corresponding stream will be fully buffered with the buffer\nsize set to MODE bytes.\n\nNOTE: If COMMAND adjusts the buffering of its standard streams ('tee' does\nfor example) then that will override corresponding changes by 'stdbuf'.\nAlso some filters (like 'dd' and 'cat' etc.) don't use streams for I/O,\nand are thus unaffected by 'stdbuf' settings.\n\nExit status:\n 125 if the stdbuf command itself fails\n 126 if COMMAND is found but cannot be invoked\n 127 if COMMAND cannot be found\n - the exit status of COMMAND otherwise", + }, + touch: { + usage: "Usage: touch [OPTION]... FILE...", + description: "Update the access and modification times of each FILE to the current time.", + options: COMMAND_HELP_OPTIONS.touch, + }, + timeout: { + usage: "Usage: timeout [OPTION]... DURATION COMMAND [ARG]...", + description: "Start COMMAND, and kill it if still running after DURATION.", + options: COMMAND_HELP_OPTIONS.timeout, + }, + tr: { + usage: "Usage: tr [OPTION]... STRING1 [STRING2]", + description: "Translate, squeeze, and/or delete characters from standard input,\nwriting to standard output.", + options: COMMAND_HELP_OPTIONS.tr, + }, + truncate: { + usage: "Usage: truncate OPTION... FILE...", + description: "Shrink or extend the size of each FILE to the specified size", + options: ["-c, --no-create", "-o, --io-blocks", "-r, --reference=RFILE", "-s, --size=SIZE"], + }, + tsort: { + usage: "Usage: tsort [OPTION] [FILE]", + description: "Write totally ordered list consistent with the partial ordering in FILE.\n\nWith no FILE, or when FILE is -, read standard input.", + }, + tty: { + usage: "Usage: tty [OPTION]...", + description: "Print the file name of the terminal connected to standard input.", + options: ["-s, --silent, --quiet\n print nothing, only return an exit status"], + }, + uname: { + usage: "Usage: uname [OPTION]...", + description: "Print certain system information. With no OPTION, same as -s.", + options: COMMAND_HELP_OPTIONS.uname, + }, + unexpand: { + usage: "Usage: unexpand [OPTION]... [FILE]...", + description: "Convert blanks in each FILE to tabs, writing to standard output.", + options: COMMAND_HELP_OPTIONS.unexpand, + }, + uniq: { + usage: "Usage: uniq [OPTION]... [INPUT [OUTPUT]]", + description: "Filter adjacent matching lines from INPUT (or standard input),\nwriting to OUTPUT (or standard output).", + options: COMMAND_HELP_OPTIONS.uniq, + }, + uptime: { + usage: "Usage: uptime [OPTION]... [FILE]", + description: "Print the current time, the length of time the system has been up,\nthe number of users on the system, and the average number of jobs\nin the run queue over the last 1, 5 and 15 minutes.\nIf FILE is not specified, use /var/run/utmp. /var/log/wtmp as FILE is common.", + }, + users: { + usage: "Usage: users [OPTION]... [FILE]", + description: "Output who is currently logged in according to FILE.\nIf FILE is not specified, use /var/run/utmp. /var/log/wtmp as FILE is common.", + }, + vdir: { + usage: "Usage: vdir [OPTION]... [FILE]...", + description: "List information about the FILEs (the current directory by default).\nSort entries alphabetically if none of -cftuvSUX nor --sort is specified.", + options: COMMAND_HELP_OPTIONS.vdir, + }, + whoami: { + usage: "Usage: whoami [OPTION]...", + description: "Print the user name associated with the current effective user ID.\nSame as id -un.", + }, + wc: { + usage: "Usage: wc [OPTION]... [FILE]...\n or: wc [OPTION]... --files0-from=F", + description: "Print newline, word, and byte counts for each FILE, and a total line if\nmore than one FILE is specified.", + options: COMMAND_HELP_OPTIONS.wc, + }, + who: { + usage: "Usage: who [OPTION]... [ FILE | ARG1 ARG2 ]", + description: "Print information about users who are currently logged in.", + options: COMMAND_HELP_OPTIONS.who, + }, +}; + +export function showGenericHelp(program) { + const helpProgram = program === "ginstall" ? "install" : program; + if (program === "[") { + showBracketHelp(); + return; + } + if (program === "link") { + showLinkHelp(); + return; + } + if (program === "unlink") { + showUnlinkHelp(); + return; + } + if (SPECIAL_HELP[program]) { + showSpecialHelp(program); + return; + } + if (program === "cksum") { + stdout("Usage: cksum [OPTION]... [FILE]...\n"); + stdout("Print or verify checksums.\nBy default use the 32 bit CRC algorithm.\n\n"); + stdout("Available algorithms:\n sysv\n bsd\n crc\n crc32b\n md5\n sha1\n sha2\n sha3\n blake2b\n sm3\n"); + stdout("\nOptions:\n"); + for (const option of COMMAND_HELP_OPTIONS.cksum) stdout(` ${option}\n`); + stdout(" --help\n"); + stdout(" --version\n"); + return; + } + stdout(`Usage: ${program} [OPTION]... [FILE]...\n`); + stdout(`Bun implementation of GNU ${program}.\n\n`); + stdout("Options:\n"); + for (const option of COMMAND_HELP_OPTIONS[helpProgram] ?? []) stdout(` ${option}\n`); + stdout(" --help\n"); + stdout(" --version\n"); +} + +export function showSpecialHelp(program) { + const help = SPECIAL_HELP[program]; + stdout(`${help.usage}\n`); + stdout(`${help.description}\n\n`); + for (const option of help.options ?? []) stdout(` ${option}\n`); + if (help.options?.length) stdout("\n"); + stdout(" --help display this help and exit\n"); + stdout(" --version output version information and exit\n"); + if (help.after) stdout(`\n${help.after}\n`); +} + +export function showBracketHelp() { + stdout("Usage: test EXPRESSION\n"); + stdout(" or: test\n"); + stdout(" or: [ EXPRESSION ]\n"); + stdout(" or: [ ]\n"); + stdout(" or: [ OPTION\n"); + stdout("Exit with the status determined by EXPRESSION.\n\n"); + stdout(" --help display this help and exit\n"); + stdout(" --version output version information and exit\n\n"); + stdout("An omitted EXPRESSION defaults to false. Otherwise,\n"); + stdout("EXPRESSION is true or false and sets exit status.\n"); +} + +export function showLinkHelp() { + stdout("Usage: link FILE1 FILE2\n"); + stdout(" or: link OPTION\n"); + stdout("Call the link function to create a link named FILE2 to an existing FILE1.\n\n"); + stdout(" --help display this help and exit\n"); + stdout(" --version output version information and exit\n"); +} + +export function showUnlinkHelp() { + stdout("Usage: unlink FILE\n"); + stdout(" or: unlink OPTION\n"); + stdout("Call the unlink function to remove the specified FILE.\n\n"); + stdout(" --help display this help and exit\n"); + stdout(" --version output version information and exit\n"); +} diff --git a/src/shared/install.js b/src/shared/install.js new file mode 100644 index 0000000..f2a1abc --- /dev/null +++ b/src/shared/install.js @@ -0,0 +1,282 @@ +import { mkdirSync } from "node:fs"; +import { chown, unlink as fsUnlink, lstat, mkdir, readFile, rm, stat } from "node:fs/promises"; +import { join, basename as pathBasename, dirname as pathDirname } from "node:path"; +import { localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, parseOptions, pathDisplayName, selinuxRuntimeEnabled, shellEscapeLsName, statAttachNanoseconds, touchStatDate } from "./common.js"; +import { backupDestination, cpCopyFileContents, cpDebugLine, validateBackupMode } from "./copy.js"; +import { InvocationError, UsageError, stderr, stdout } from "./diagnostics.js"; +import { errnoMessage, invalidModeDiagnosticValue, mkdirParents, parseModeSpec, resolveGroup, resolveUser, selinuxSecurityContext, setFileMode, setSelinuxSecurityContext, touchSetPathTimes, withSelinuxCreationContext } from "./filesystem.js"; + +export const INSTALL_LONG_OPTIONS = ["backup", "compare", "directory", "debug", "group", "mode", "owner", "preserve-timestamps", "strip", "strip-program", "suffix", "target-directory", "no-target-directory", "verbose", "preserve-context", "context", "help", "version"]; + +export async function installCmd(args, program = "install") { + args = normalizeInstallLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { c: false, C: false, D: false, d: false, g: "value", m: "value", o: "value", p: false, t: "value", T: false, v: false, b: false, S: "value", s: false, Z: false }, long: { compare: false, debug: false, directory: false, group: "value", mode: "value", "no-target-directory": false, owner: "value", "preserve-context": false, "preserve-timestamps": false, verbose: false, backup: "optional-value", suffix: "value", strip: false, "strip-program": "value", "target-directory": "value", context: "optional-value", help: false, version: false } }); + if (opts.p) opts["preserve-timestamps"] = true; + if (opts.b != null && opts.backup == null) opts.backup = opts.b; + if (opts.S != null && opts.suffix == null) opts.suffix = opts.S; + if (opts.b || opts.backup != null) validateBackupMode(program, opts.backup); + if ((opts.C || opts.compare) && (opts.s || opts.strip)) throw new UsageError("options --compare (-C) and --strip are mutually exclusive", true); + if (opts["strip-program"] != null && !(opts.s || opts.strip)) { + stderr(`${program}: WARNING: ignoring --strip-program option as -s option was not specified\n`); + } + const verbose = opts.v || opts.verbose || opts.debug; + const securityContext = installSecurityContextOptions(program, opts); + if (opts.d || opts.directory) { + if (!operands.length) throw new UsageError("missing file operand", true); + const mode = opts.m || opts.mode ? parseInstallMode(opts.m ?? opts.mode) : undefined; + const uid = await resolveUser(opts.o ?? opts.owner); + const gid = await resolveGroup(opts.g ?? opts.group); + let status = 0; + await withSelinuxCreationContext(program, securityContext, async (created, sameThread) => { + for (const dir of operands) { + try { + const existing = await lstat(dir).catch(() => null); + if (existing && !existing.isDirectory()) { + stderr(`${program}: cannot create directory ${installQuotedName(dir)}: File exists\n`); + status = 1; + continue; + } + await mkdirParents(dir, mode ?? 0o755, false, created, sameThread); + if (mode !== undefined) await setFileMode(dir, mode); + if (uid != null || gid != null) await chown(dir, uid ?? (await stat(dir)).uid, gid ?? (await stat(dir)).gid); + if (verbose) stdout(`${program}: creating directory ${installQuotedName(dir)}\n`); + } catch (error) { + if (error instanceof UsageError && error.message.startsWith("failed to set default file creation context")) throw error; + stderr(`${program}: cannot create directory ${installQuotedName(dir)}: ${errnoMessage(error)}\n`); + status = 1; + } + } + }); + return status; + } + const targetDirectory = opts.t ?? opts["target-directory"]; + const noTargetDirectory = opts.T || opts["no-target-directory"]; + if (targetDirectory != null && noTargetDirectory) throw new UsageError("cannot combine --target-directory and --no-target-directory"); + if (noTargetDirectory && operands.length > 2) throw new UsageError(`extra operand ${shellEscapeLsName(operands[2], true)}`, true); + if (targetDirectory == null && operands.length < 2) throw installMissingOperandError(operands); + if (targetDirectory != null && operands.length < 1) throw installMissingOperandError(operands); + const mode = opts.m || opts.mode ? parseInstallMode(opts.m ?? opts.mode) : undefined; + const uid = await resolveUser(opts.o ?? opts.owner); + const gid = await resolveGroup(opts.g ?? opts.group); + const dest = targetDirectory ?? operands.at(-1); + const sources = targetDirectory == null ? operands.slice(0, -1) : operands; + let destStat = await stat(dest).catch(() => null); + if (targetDirectory != null && !destStat) { + if (opts.D) { + try { + await withSelinuxCreationContext(program, securityContext, (created, sameThread) => makeInstallParents(dest, opts, program, created, sameThread)); + destStat = await stat(dest); + } catch (error) { + stderr(`${program}: cannot create directory ${installQuotedName(dest)}: ${errnoMessage(error)}\n`); + return 1; + } + } else { + stderr(`${program}: failed to access ${installQuotedName(dest)}: No such file or directory\n`); + return 1; + } + } + if (targetDirectory != null && destStat && !destStat.isDirectory()) { + stderr(`${program}: failed to access ${installQuotedName(dest)}: Not a directory\n`); + return 1; + } + if (sources.length > 1 && !destStat?.isDirectory() && !opts.D) throw new UsageError(`target '${dest}' is not a directory`); + if (noTargetDirectory && destStat?.isDirectory()) throw new UsageError(`cannot overwrite directory '${dest}' with non-directory '${sources[0]}'`); + for (const src of sources) { + const target = !noTargetDirectory && (targetDirectory != null || destStat?.isDirectory()) ? join(dest, pathBasename(src)) : dest; + const srcStat = await stat(src).catch(() => null); + if (!srcStat) { + stderr(`${program}: cannot stat ${installQuotedName(src)}: No such file or directory\n`); + return 1; + } + if (srcStat?.isDirectory()) { + stderr(`${program}: omitting directory ${installQuotedName(src)}\n`); + return 1; + } + const targetStat = await stat(target).catch(() => null); + if (srcStat && targetStat && srcStat.dev === targetStat.dev && srcStat.ino === targetStat.ino) { + const targetDisplay = targetDirectory != null && dest === "." ? `./${pathBasename(src)}` : target; + stderr(`${program}: ${installQuotedName(src)} and ${installQuotedName(targetDisplay)} are the same file\n`); + return 1; + } + if (opts.D || targetDirectory != null) { + await withSelinuxCreationContext(program, securityContext, (created, sameThread) => makeInstallParents(pathDirname(target), opts, program, created, sameThread)); + } + if ((opts.C || opts.compare) && await installTargetMatches(src, target, mode ?? 0o755, opts, securityContext, uid, gid)) continue; + if (opts.b || opts.backup != null) await backupDestination(target, opts.suffix, opts.backup); + if (await lstat(target).catch(() => null)) { + await fsUnlink(target); + if (opts.v || opts.verbose) stdout(`removed ${installQuotedName(target)}\n`); + } + const copyDebug = opts.debug ? { reflinkMode: "auto", sparseMode: "auto", debug: true } : {}; + await withSelinuxCreationContext(program, securityContext, async (created) => { + await cpCopyFileContents(src, target, await stat(src), copyDebug); + await created(target); + }); + await setFileMode(target, mode ?? 0o755); + if (uid != null || gid != null) { + const s = await stat(target); + await chown(target, uid ?? s.uid, gid ?? s.gid); + } + if (opts["preserve-timestamps"]) { + const s = statAttachNanoseconds(await stat(src), src, true); + await touchSetPathTimes(target, touchStatDate(s, "atime"), touchStatDate(s, "mtime"), false); + } + if ((opts.s || opts.strip) && !(await stripInstalledFile(target, opts["strip-program"], program))) return 1; + await applyInstallSecurityContext(src, target, securityContext); + if (verbose) stdout(`${installQuotedName(src)} -> ${installQuotedName(target)}\n`); + if (opts.debug) stdout(cpDebugLine(copyDebug)); + } + return 0; +} + +export function installSecurityContextOptions(program, opts) { + const enabled = selinuxRuntimeEnabled(); + if (typeof opts.context === "string" && !enabled) stderr(`${program}: warning: ignoring --context; it requires an SELinux-enabled kernel\n`); + if (opts["preserve-context"] && !enabled) stderr(`${program}: WARNING: ignoring --preserve-context; this kernel is not SELinux-enabled\n`); + const explicitContext = typeof opts.context === "string" && enabled ? opts.context : null; + const restoreContext = enabled && (opts.Z || opts.context === true); + const preserveContext = enabled && Boolean(opts["preserve-context"]); + if (preserveContext && (explicitContext != null || restoreContext)) { + throw new InvocationError("cannot set target context and preserve it", 1, false); + } + return { enabled, explicitContext, restoreContext, preserveContext }; +} + +export async function applyInstallSecurityContext(src, target, options) { + if (!options.preserveContext) return; + const context = selinuxSecurityContext(src); + if (context == null) throw new UsageError(`failed to get security context of '${src}'`); + if (!setSelinuxSecurityContext(target, context)) throw new UsageError(`failed to set the security context of '${target}'`); +} + +export function installMissingOperandError(operands) { + return operands.length + ? new UsageError(`missing destination file operand after '${operands.at(-1)}'`, true) + : new UsageError("missing file operand", true); +} + +export function parseInstallMode(spec) { + try { + return /^[0-7]+$/.test(spec) ? Number.parseInt(spec, 8) : parseModeSpec(spec, 0, true, { ignoreUmask: true }).mode; + } catch (error) { + if (error instanceof UsageError && invalidModeDiagnosticValue(error.message) != null) { + throw new UsageError(`invalid mode ${localeQuotedEscapedDiagnostic(spec)}`); + } + throw error; + } +} + +export async function makeInstallParents(dir, opts, program = "install", created = async () => {}, sameThread = false) { + if (!dir || dir === ".") return; + const parts = []; + let cursor = dir; + while (cursor && cursor !== "." && !(await stat(cursor).catch(() => null))) { + parts.push(cursor); + cursor = pathDirname(cursor); + } + for (const path of parts.reverse()) { + if (sameThread) mkdirSync(path); + else await mkdir(path); + await created(path); + if (opts.v || opts.verbose) stdout(`${program}: creating directory ${installQuotedName(path)}\n`); + } +} + +export function installQuotedName(path) { + return shellEscapeLsName(pathDisplayName(path), true); +} + +export async function installTargetMatches(src, target, mode, opts, securityContext = {}, uid = null, gid = null) { + const srcStat = await stat(src).then((s) => statAttachNanoseconds(s, src, true), () => null); + const targetLinkStat = await lstat(target).catch(() => null); + if (!srcStat || !targetLinkStat?.isFile()) return false; + const targetStat = await stat(target); + if ((mode & 0o7000) !== 0) return false; + if ((targetStat.mode & 0o7777) !== mode) return false; + const expectedUid = uid ?? process.getuid?.() ?? targetStat.uid; + const expectedGid = gid ?? process.getgid?.() ?? targetStat.gid; + if (targetStat.uid !== expectedUid || targetStat.gid !== expectedGid) return false; + const [srcBytes, targetBytes] = await Promise.all([readFile(src), readFile(target)]); + if (srcBytes.length !== targetBytes.length) return false; + for (let i = 0; i < srcBytes.length; i++) if (srcBytes[i] !== targetBytes[i]) return false; + if (securityContext.preserveContext && selinuxSecurityContext(src) !== selinuxSecurityContext(target)) return false; + if (opts["preserve-timestamps"]) await touchSetPathTimes(target, touchStatDate(srcStat, "atime"), touchStatDate(srcStat, "mtime"), false); + return true; +} + +export async function stripInstalledFile(target, stripProgramOption, program = "install") { + const stripProgram = stripProgramOption ?? "strip"; + const stripTarget = target.startsWith("-") ? `./${target}` : target; + try { + const proc = Bun.spawn([stripProgram, stripTarget], { stdin: "ignore", stdout: "inherit", stderr: "inherit" }); + const code = await proc.exited; + if (code === 0) return true; + } catch (error) { + stderr(`${program}: cannot run strip program ${installQuotedName(stripProgram)}: ${errnoMessage(error)}\n`); + } + await rm(target, { force: true }); + return false; +} + +export function installMetaOption(args) { + const normalized = normalizeInstallLongOptions(args); + const longValueOptions = new Set(["group", "mode", "owner", "strip-program", "suffix", "target-directory"]); + const longOptionalValueOptions = new Set(["backup", "context"]); + const longKnownOptions = new Set(INSTALL_LONG_OPTIONS); + const shortValueOptions = new Set(["g", "m", "o", "S", "t"]); + const shortKnownOptions = new Set(["b", "c", "C", "D", "d", "g", "m", "o", "p", "s", "S", "t", "T", "v", "Z"]); + let sawPreserveContext = false; + let sawContextValue = false; + for (let i = 0; i < normalized.length; i++) { + const arg = normalized[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const [name, inlineValue] = arg.slice(2).split(/=(.*)/s, 2); + if (!longKnownOptions.has(name)) return null; + if ((arg === "--help" || arg === "--version") && inlineValue == null) { + if (sawPreserveContext && !selinuxRuntimeEnabled()) stderr("install: WARNING: ignoring --preserve-context; this kernel is not SELinux-enabled\n"); + if (sawContextValue && !selinuxRuntimeEnabled()) stderr("install: warning: ignoring --context; it requires an SELinux-enabled kernel\n"); + return arg; + } + if (name === "preserve-context") sawPreserveContext = true; + if (name === "context" && inlineValue !== undefined) sawContextValue = true; + if (longValueOptions.has(name)) { + if (inlineValue == null) i++; + } else if (longOptionalValueOptions.has(name)) { + continue; + } else if (inlineValue != null) return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + if (arg.slice(j + 1) === "") i++; + break; + } + } + } + return null; +} + +export function normalizeInstallLongOptions(args) { + const out = []; + const valueOptions = new Set(["group", "mode", "owner", "strip-program", "suffix", "target-directory"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + out.push(arg, ...args.slice(i + 1)); + break; + } + if (!arg.startsWith("--")) { + out.push(arg); + continue; + } + const normalized = normalizeLongOptionByPrefix(arg, INSTALL_LONG_OPTIONS); + out.push(normalized); + const [name, inlineValue] = normalized.slice(2).split(/=(.*)/s, 2); + if (valueOptions.has(name) && inlineValue == null && i + 1 < args.length) out.push(args[++i]); + } + return out; +} diff --git a/src/shared/listing.js b/src/shared/listing.js new file mode 100644 index 0000000..d2ae60e --- /dev/null +++ b/src/shared/listing.js @@ -0,0 +1,1517 @@ +import { ptr, read, toArrayBuffer } from "bun:ffi"; +import { lstatSync, statSync } from "node:fs"; +import { lstat, readlink, stat } from "node:fs/promises"; +import { hostname as osHostname } from "node:os"; +import { isAbsolute, dirname as pathDirname, resolve } from "node:path"; +import { AT_FDCWD, AT_SYMLINK_NOFOLLOW, attachDateNanoseconds, attachStatNanoseconds, bufferPathJoin, compareSortBytes, compareSortLocaleText, cstr, cstrPath, displayWidth, globMatch, groupName, initializeSortLocaleCollation, invalidOptionMessage, isAsciiDigit, isBytePath, libc, libcErrno, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, lsEscapedName, lsUsesUtf8Locale, nativeStatNanoseconds, normalizeLongOptionByPrefix, parseOptions, pathDisplayName, pathLikeJoin, readdirPathEntries, shellEscapeLsName, shellQuoteLsName, systemErrorMessage, userNameForUid } from "./common.js"; +import { InvocationError, UsageError, stderr, stdout } from "./diagnostics.js"; +import { applyBlockSizeSpecialMode, blockSizeSpecialMode, defaultGNUBlockSize, fileTypeChar, gnuBlockSizeErrorMessage, humanSizeWithUnits, modeString, optionValues, parseGNUBlockSize, rawOperandPlan, rmLibcError, selinuxSecurityContext, validateBlockSizeMetaOption } from "./filesystem.js"; +import { lsTimeText, parseGNUBlockSizeEnv } from "./time.js"; + +export const LS_LONG_OPTIONS = ["all", "almost-all", "author", "directory", "classify", "file-type", "dereference", "dereference-command-line", "dereference-command-line-symlink-to-dir", "full", "full-time", "group-directories-first", "group", "indicator-style", "inode", "size", "human-readable", "si", "kibibytes", "recursive", "reverse", "color", "sort", "time-style", "time", "tabsize", "width", "block-size", "format", "ignore", "hide-control-chars", "hide", "hyperlink", "ignore-backups", "zero", "dired", "quote-name", "literal", "escape", "show-control-chars", "no-group", "numeric-uid-gid", "quoting", "quoting-style", "context", "help", "version"]; + +export function lsMetaOption(args) { + const longOptionalValueOptions = new Set(["classify", "color", "hyperlink"]); + const longValueOptions = new Set(["indicator-style", "sort", "time-style", "time", "tabsize", "width", "block-size", "format", "ignore", "hide", "quoting", "quoting-style"]); + const longFlagOptions = new Set(LS_LONG_OPTIONS.filter((option) => !longOptionalValueOptions.has(option) && !longValueOptions.has(option) && option !== "help" && option !== "version")); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeLsLongOption(arg); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + return normalized; + } + if (longFlagOptions.has(name)) { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + continue; + } + if (longOptionalValueOptions.has(name)) { + if (name === "color" && inlineValue !== undefined) lsColorEnabled({ color: inlineValue }); + if (name === "hyperlink" && inlineValue !== undefined) lsHyperlinkEnabled({ hyperlink: inlineValue }); + if (name === "classify" && inlineValue !== undefined) validateLsClassifyMetaOption(inlineValue); + continue; + } + if (longValueOptions.has(name)) { + const value = inlineValue ?? args[i + 1]; + if (name === "block-size" && value !== undefined) validateBlockSizeMetaOption("--block-size", value); + if (name === "width" && value !== undefined) parseLsWidth(value); + if (name === "tabsize" && value !== undefined) parseLsTabSize(value); + if (name === "time" && value !== undefined) validateLsTime(value); + if (name === "sort" && value !== undefined) validateLsSort(value); + if (name === "indicator-style" && value !== undefined) validateLsIndicatorStyle(value); + if (name === "format" && value !== undefined) lsLayoutForFormat(value); + if ((name === "quoting" || name === "quoting-style") && value !== undefined) validateLsQuotingStyle(value); + if (inlineValue === undefined) i++; + continue; + } + if (arg.startsWith("-") && !arg.startsWith("--")) { + i = scanLsShortMetaOption(args, i); + continue; + } + if (arg.startsWith("-")) throw new UsageError(invalidOptionMessage(arg), true); + } + return null; +} + +export function scanLsShortMetaOption(args, index) { + const arg = args[index]; + const flagOptions = new Set(["a", "A", "B", "D", "H", "L", "c", "u", "l", "d", "f", "g", "G", "o", "n", "i", "s", "h", "k", "1", "F", "R", "S", "t", "X", "U", "r", "b", "C", "x", "m", "q", "Q", "N", "v", "p", "Z", "0"]); + const valueOptions = new Set(["T", "w", "I"]); + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (valueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? args[index + 1] : inlineValue; + if (ch === "w" && value !== undefined) parseLsWidth(value); + if (ch === "T" && value !== undefined) parseLsTabSize(value); + return inlineValue === "" ? index + 1 : index; + } + if (flagOptions.has(ch)) { + continue; + } + throw new UsageError(`invalid option -- '${ch}'`, true); + } + return index; +} + +export function validateLsIndicatorStyle(value) { + if (lsIndicatorStyle({ "indicator-style": value }) === null) { + throw new InvocationError(lsInvalidEnumMessage(value, "--indicator-style", LS_INDICATOR_VALID_ARGUMENTS), 1, true); + } +} + +export function validateLsClassifyMetaOption(value) { + if (lsIndicatorStyle({ classify: value }) === null) { + throw new InvocationError(lsInvalidEnumMessage(value, "--classify", LS_CLASSIFY_VALID_ARGUMENTS), 1, true); + } +} + +export async function lsCmd(args) { + args = normalizeLsLongOptions(args); + const { opts, operands: parsedOperands } = parseOptions(args, { short: { a: false, A: false, B: false, D: false, H: false, L: false, c: false, u: false, l: false, d: false, f: false, g: false, G: false, o: false, n: false, i: false, s: false, h: false, k: false, 1: false, F: false, R: false, S: false, t: false, T: "value", X: false, U: false, r: false, b: false, C: false, x: false, m: false, q: false, Q: false, N: false, v: false, p: false, w: "value", I: "value-array", Z: false, 0: false }, long: { all: false, "almost-all": false, author: false, directory: false, classify: "optional-value", "file-type": false, dereference: false, "dereference-command-line": false, "dereference-command-line-symlink-to-dir": false, full: false, "full-time": false, "group-directories-first": false, group: false, "indicator-style": "value", inode: false, size: false, "human-readable": false, si: false, kibibytes: false, recursive: false, reverse: false, color: "optional-value", hyperlink: "optional-value", hyper: "optional-value", "sort": "value", "time-style": "value", time: "value", "tabsize": "value", width: "value", "block-size": "value", format: "value", ignore: "value-array", hide: "value-array", "ignore-backups": false, zero: false, dired: false, "quote-name": false, literal: false, escape: false, "hide-control-chars": false, "show-control-chars": false, "no-group": false, "numeric-uid-gid": false, quoting: "value", "quoting-style": "value", context: false, help: false, version: false } }); + const operands = rawOperandPlan("ls", args, parsedOperands, { + valueOptions: ["--indicator-style", "--sort", "--time-style", "--time", "--tabsize", "--width", "--block-size", "--format", "--ignore", "--hide", "--quoting", "--quoting-style"], + shortValueOptions: ["T", "w", "I"], + }) ?? parsedOperands; + opts.localeCollation = initializeSortLocaleCollation(); + if (opts.B) opts["ignore-backups"] = true; + if (opts.D) opts.dired = true; + if ((opts[0] || opts.zero) && opts.dired) throw new UsageError("--dired and --zero are incompatible", true); + applyLsFormatOptions(args, opts); + opts.allMode = lsAllMode(args, opts); + opts.ignorePatterns = optionValues(opts.I).concat(optionValues(opts.ignore)); + if (opts.allMode === "visible") opts.ignorePatterns.push(...optionValues(opts.hide)); + if (opts["ignore-backups"]) opts.ignorePatterns.push("*~"); + opts.zero = opts[0] || opts.zero; + if (opts.literal) opts.N = true; + if (opts.b || opts.escape) opts.quoting = "escape"; + if (opts["hide-control-chars"]) opts.q = true; + if (opts["show-control-chars"]) opts.N = true; + if (opts["no-group"]) opts.G = true; + if (opts.kibibytes) opts.k = true; + if (opts["numeric-uid-gid"]) opts.n = true; + if (opts.n) opts.l = true; + applyLsBlockSizeSpecialMode(opts); + opts.width = parseLsWidth(opts.w ?? opts.width); + opts.tabSize = parseLsTabSize(opts.T ?? opts.tabsize); + opts.blockSize = lsBlockSize(opts); + opts.sizeBlockSize = lsSizeBlockSize(opts); + if (opts.full || opts["full-time"]) { + opts.l = true; + if (opts["time-style"] == null) opts["time-style"] = "full-iso"; + } + if (opts["time-style"] == null && process.env.TIME_STYLE) opts["time-style"] = process.env.TIME_STYLE; + if (lsUsesLongListing(opts)) validateLsTimeStyle(opts["time-style"]); + validateLsSort(opts.sort); + validateLsTime(opts.time); + validateLsQuotingStyle(opts.quoting ?? opts["quoting-style"]); + opts.indicatorStyle = lsIndicatorStyle(opts); + opts.colorEnabled = lsColorEnabled(opts); + opts.hyperlinkEnabled = lsHyperlinkEnabled(opts); + if (opts.indicatorStyle === null) { + const option = opts.classify != null ? "classify" : "indicator-style"; + throw new InvocationError(lsInvalidEnumMessage(opts.classify ?? opts["indicator-style"], `--${option}`, option === "classify" ? LS_CLASSIFY_VALID_ARGUMENTS : LS_INDICATOR_VALID_ARGUMENTS), 1, true); + } + const targets = operands.length ? operands : ["."]; + let out = await renderLsTargets(targets, opts, targets.length > 1); + if (opts.dired) out = formatLsDiredOutput(out, lsResolvedQuotingStyle(opts)); + stdout(out); + if (opts.majorProblem) return 2; + return opts.minorProblem ? 1 : 0; +} + +export function applyLsFormatOptions(args, opts) { + let layout = null; + let diredSeen = Boolean(opts.dired); + let diredActive = Boolean(opts.dired); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") break; + if (arg === "-" || !arg.startsWith("-")) continue; + if (arg.startsWith("--")) { + const [name, inlineValue] = arg.slice(2).split(/=(.*)/s, 2); + if (name === "dired") { + diredSeen = true; + diredActive = true; + layout = "l"; + } + if (name === "format") { + const value = inlineValue ?? args[++i]; + layout = lsLayoutForFormat(value); + if (diredSeen) diredActive = layout === "l"; + } + continue; + } + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (ch === "D") { + diredSeen = true; + diredActive = true; + layout = "l"; + } else if ("Cxm".includes(ch)) { + layout = ch; + if (diredSeen) diredActive = false; + } else if (ch === "1") { + if (!diredActive) layout = ch; + } else if ("lgno".includes(ch)) { + layout = ch; + if (diredSeen) diredActive = true; + } + if (["T", "w", "I"].includes(ch)) break; + } + } + opts.dired = diredActive; + if (layout == null) return; + opts[1] = opts.C = opts.x = opts.m = false; + if (["1", "C", "x", "m"].includes(layout)) opts.l = opts.g = opts.n = opts.o = false; + if (layout === "1") opts[1] = true; + else if (layout === "C") opts.C = true; + else if (layout === "x") opts.x = true; + else if (layout === "m") opts.m = true; + else if (layout === "g") opts.g = true; + else if (layout === "n") opts.n = true; + else if (layout === "o") opts.o = true; + else opts.l = true; +} + +export function lsLayoutForFormat(value) { + const text = String(value); + const layouts = { + "single-column": "1", + vertical: "C", + long: "l", + verbose: "l", + commas: "m", + horizontal: "x", + across: "x", + }; + const layout = layouts[text]; + if (layout) return layout; + throw new InvocationError(lsInvalidEnumMessage(text, "--format", LS_FORMAT_VALID_ARGUMENTS), 1, true); +} + +export function normalizeLsLongOptions(args) { + const out = []; + let end = false; + for (const arg of args) { + if (end || arg === "--" || !arg.startsWith("--")) { + out.push(arg); + if (arg === "--") end = true; + continue; + } + out.push(normalizeLsLongOption(arg)); + } + return out; +} + +export function normalizeLsLongOption(arg) { + const [rawName, inlineValue] = arg.slice(2).split(/=(.*)/s, 2); + if (rawName.startsWith("hy") && "hyperlink".startsWith(rawName)) { + return inlineValue === undefined ? "--hyperlink" : `--hyperlink=${inlineValue}`; + } + const normalized = normalizeLongOptionByPrefix(arg, LS_LONG_OPTIONS); + if (normalized === "--hyper" || normalized.startsWith("--hyper=")) return `--hyperlink${normalized.slice("--hyper".length)}`; + return normalized; +} + +export const LS_FORMAT_VALID_ARGUMENTS = [ + ["verbose", "long"], + ["commas"], + ["horizontal", "across"], + ["vertical"], + ["single-column"], +]; + +export const LS_INDICATOR_VALID_ARGUMENTS = [ + ["none"], + ["slash"], + ["file-type"], + ["classify"], +]; + +export const LS_CLASSIFY_VALID_ARGUMENTS = [ + ["always", "yes", "force"], + ["never", "no", "none"], + ["auto", "tty", "if-tty"], +]; + +export const LS_TRISTATE_VALID_ARGUMENTS = LS_CLASSIFY_VALID_ARGUMENTS; + +export const LS_SORT_VALID_ARGUMENTS = [ + ["none"], + ["size"], + ["time"], + ["version"], + ["extension"], + ["name"], + ["width"], +]; + +export const LS_TIME_VALID_ARGUMENTS = [ + ["atime", "access", "use"], + ["ctime", "status"], + ["mtime", "modification"], + ["birth", "creation"], +]; + +export const LS_QUOTING_STYLE_VALID_ARGUMENTS = [ + ["literal"], + ["shell"], + ["shell-always"], + ["shell-escape"], + ["shell-escape-always"], + ["c"], + ["c-maybe"], + ["escape"], + ["locale"], + ["clocale"], +]; + +export function lsInvalidEnumMessage(value, option, groups) { + const kind = value === "" ? "ambiguous" : "invalid"; + return `${kind} argument ${localeQuotedEscapedDiagnostic(value)} for ${localeQuotedDiagnostic(option)}\nValid arguments are:\n${groups.map((group) => ` - ${group.map(localeQuotedDiagnostic).join(", ")}`).join("\n")}`; +} + +export function lsUsesLongListing(opts) { + return Boolean(opts.l || opts.g || opts.o || opts.n); +} + +export function lsBlockSize(opts) { + if (opts["block-size"] !== undefined) { + try { + return parseGNUBlockSize(opts["block-size"]); + } catch { + throw new UsageError(gnuBlockSizeErrorMessage("--block-size", opts["block-size"])); + } + } + if (opts.k) return 1024; + return parseGNUBlockSizeEnv(lsBlockSizeEnvValue(), lsDefaultBlockSize()); +} + +export function lsSizeBlockSize(opts) { + if (opts["block-size"] !== undefined) return parseGNUBlockSize(opts["block-size"]); + const envValue = lsSizeBlockSizeEnvValue(); + if (envValue != null) return parseGNUBlockSizeEnv(envValue, lsDefaultBlockSize()); + return 1; +} + +export function lsBlockSizeValue(opts) { + return opts["block-size"] ?? lsBlockSizeEnvValue(); +} + +export function lsSizeBlockSizeValue(opts) { + return opts["block-size"] ?? lsSizeBlockSizeEnvValue(); +} + +export function lsBlockSizeEnvValue() { + return process.env.LS_BLOCK_SIZE ?? process.env.BLOCK_SIZE ?? process.env.BLOCKSIZE; +} + +export function lsSizeBlockSizeEnvValue() { + return process.env.LS_BLOCK_SIZE ?? process.env.BLOCK_SIZE; +} + +export function lsDefaultBlockSize() { + return defaultGNUBlockSize(); +} + +export function parseLsWidth(value) { + if (value == null) return null; + const text = String(value); + const width = parseLsUnsignedNumber(text); + if (width == null) throw new UsageError(`invalid line width: ${localeQuotedEscapedDiagnostic(text)}`); + if (width === 0) return Number.POSITIVE_INFINITY; + return width; +} + +export function parseLsTabSize(value) { + if (value == null) return 8; + const text = String(value); + const tabSize = parseLsUnsignedNumber(text); + if (tabSize == null) throw new UsageError(`invalid tab size: ${localeQuotedEscapedDiagnostic(text)}`); + return tabSize; +} + +export function parseLsUnsignedNumber(text) { + if (/^-/.test(text)) return null; + let digits = text; + let base = 10; + if (/^0[xX]/.test(text)) { + digits = text.slice(2); + base = 16; + if (!/^[0-9a-fA-F]+$/.test(digits)) return null; + } else if (text.startsWith("0") && text !== "0") { + digits = text.slice(1); + base = 8; + if (!/^[0-7]+$/.test(digits)) return null; + } else if (!/^\d+$/.test(text)) { + return null; + } + const value = Number.parseInt(digits || "0", base); + return Number.isFinite(value) ? value : Number.MAX_SAFE_INTEGER; +} + +export function validateLsTimeStyle(style) { + if (style == null || (typeof style === "string" && style.startsWith("+"))) return; + const allowed = ["full-iso", "long-iso", "iso", "locale"]; + const normalized = String(style).replace(/^posix-/, ""); + if (allowed.includes(normalized)) return; + throw new UsageError(`invalid argument ${localeQuotedEscapedDiagnostic(style)} for ${localeQuotedDiagnostic("time style")} +Valid arguments are: + - [posix-]full-iso + - [posix-]long-iso + - [posix-]iso + - [posix-]locale + - +FORMAT (e.g., +%H:%M) for a 'date'-style format`, true); +} + +export function validateLsSort(sort) { + if (sort == null) return; + const allowed = ["none", "size", "time", "version", "extension", "name", "width"]; + if (allowed.includes(String(sort))) return; + throw new InvocationError(lsInvalidEnumMessage(sort, "--sort", LS_SORT_VALID_ARGUMENTS), 1, true); +} + +export function validateLsTime(time) { + if (time == null) return; + const allowed = LS_TIME_VALID_ARGUMENTS.flat(); + if (allowed.includes(String(time))) return; + throw new InvocationError(lsInvalidEnumMessage(time, "--time", LS_TIME_VALID_ARGUMENTS), 1, true); +} + +export function validateLsQuotingStyle(style) { + if (style == null) return; + const resolved = resolveLsQuotingStyle(style); + if (resolved) return; + throw new InvocationError(lsInvalidEnumMessage(style, "--quoting-style", LS_QUOTING_STYLE_VALID_ARGUMENTS), 1, true); +} + +export function resolveLsQuotingStyle(style) { + if (style == null) return null; + const text = String(style); + const allowed = LS_QUOTING_STYLE_VALID_ARGUMENTS.flat(); + if (allowed.includes(text)) return text; + const matches = allowed.filter((candidate) => candidate.startsWith(text)); + if (matches.length === 1) return matches[0]; + if (matches.length > 1) throw new InvocationError(`ambiguous argument ${localeQuotedDiagnostic(text)} for ${localeQuotedDiagnostic("--quoting-style")}\nValid arguments are:\n${LS_QUOTING_STYLE_VALID_ARGUMENTS.map((group) => ` - ${group.map(localeQuotedDiagnostic).join(", ")}`).join("\n")}`, 1, true); + return null; +} + +export function lsIndicatorStyle(opts) { + if (opts.p) return "slash"; + if (opts["file-type"]) return "file-type"; + const style = opts["indicator-style"]; + if (style != null) { + if (["none", "classify", "file-type", "slash"].includes(style)) return style; + return null; + } + if (opts.F) return "classify"; + if (opts.classify != null) { + const mode = opts.classify === true ? "always" : opts.classify; + if (mode === "always" || mode === "yes" || mode === "force") return "classify"; + if (mode === "auto" || mode === "tty" || mode === "if-tty" || mode === "never" || mode === "no" || mode === "none") return "none"; + return null; + } + return "none"; +} + +export function lsColorEnabled(opts) { + if (opts.color == null || opts.color === "never") return false; + if (opts.color === true || opts.color === "always" || opts.color === "yes" || opts.color === "force") return terminalSupportsColor(); + if (opts.color === "auto" || opts.color === "tty" || opts.color === "if-tty") return terminalSupportsColor() && Boolean(process.stdout.isTTY); + throw new InvocationError(lsInvalidEnumMessage(opts.color, "--color", LS_TRISTATE_VALID_ARGUMENTS), 1, true); +} + +export function lsHyperlinkEnabled(opts) { + const value = opts.hyperlink ?? opts.hyper; + if (opts.dired) return false; + if (value == null || value === "never" || value === "no" || value === "none") return false; + if (value === true || value === "always" || value === "yes" || value === "force") return true; + if (value === "auto" || value === "tty" || value === "if-tty") return Boolean(process.stdout.isTTY); + throw new InvocationError(lsInvalidEnumMessage(value, "--hyperlink", LS_TRISTATE_VALID_ARGUMENTS), 1, true); +} + +export function terminalSupportsColor() { + if (process.env.COLORTERM) return true; + if (process.env.TERM === "") return false; + return process.env.TERM !== "dumb"; +} + +export function lsAllMode(args, opts) { + let mode = "visible"; + let sawMode = false; + for (const arg of args) { + if (arg === "--") break; + if (arg === "--all") { + mode = "all"; + sawMode = true; + } else if (arg === "--almost-all") { + mode = "almost"; + sawMode = true; + } else if (/^-[^-]/.test(arg)) { + for (const ch of arg.slice(1)) { + if (ch === "I") break; + if (ch === "a" || ch === "f") { + mode = "all"; + sawMode = true; + } else if (ch === "A") { + mode = "almost"; + sawMode = true; + } + } + } + } + if (sawMode) return mode; + if (opts.a || opts.f || opts.all) return "all"; + if (opts.A || opts["almost-all"]) return "almost"; + return "visible"; +} + +export async function renderLsTargets(targets, opts, labelDirs) { + const recursiveDirSeen = new Set(); + const targetStats = await Promise.all(targets.map(async (target) => { + try { + return { target, ...(await lsEntryStats(target, opts, true)) }; + } catch (error) { + opts.majorProblem = true; + stderr(`ls: cannot access '${target}': ${systemErrorMessage(error)}\n`); + return null; + } + })); + const validTargets = targetStats.filter(Boolean); + if (!validTargets.length) return ""; + if (validTargets.every(({ stat }) => opts.d || opts.directory || !stat.isDirectory())) { + const entries = validTargets.map(({ target, stat, linkStat, danglingDereference, targetDirectory, targetMissing, hasCapability }) => ({ name: target, path: target, stat, linkStat, danglingDereference, targetDirectory, targetMissing, hasCapability })); + sortLsEntries(entries, opts); + return await renderLsEntries(entries, opts); + } + let out = ""; + const fileEntries = validTargets + .filter(({ stat: s }) => opts.d || opts.directory || !s.isDirectory()) + .map(({ target, stat, linkStat, danglingDereference, targetDirectory, targetMissing, hasCapability }) => ({ name: target, path: target, stat, linkStat, danglingDereference, targetDirectory, targetMissing, hasCapability })); + if (fileEntries.length) { + sortLsEntries(fileEntries, opts); + out += await renderLsEntries(fileEntries, opts); + } + let renderedDir = false; + for (const { target, stat: s } of validTargets) { + if (!s.isDirectory() || opts.d || opts.directory) continue; + out += await renderLsDirectory(target, opts, labelDirs || opts.R || opts.recursive, fileEntries.length > 0 || renderedDir, recursiveDirSeen, s); + renderedDir = true; + } + return out; +} + +export async function renderLsDirectory(target, opts, label, leadingBlank = false, recursiveDirSeen = new Set(), directoryStat = null) { + let out = ""; + const currentStat = directoryStat ?? await stat(target).catch(() => null); + if (currentStat) recursiveDirSeen.add(lsDirectoryIdentity(currentStat)); + if (leadingBlank) out += "\n"; + if (label) out += `${formatLsHyperlink(quoteLsDirectoryLabelName(target, opts), await lsDirectoryLabelPath(target), opts)}:\n`; + const rawDisplayNames = Boolean((await lstat(target).catch(() => null))?.isSymbolicLink()); + let names = await lsReadDirectoryEntries(target); + if (opts.allMode === "all") names = [{ name: Buffer.from("."), type: 4 }, { name: Buffer.from(".."), type: 4 }, ...names]; + else if (opts.allMode !== "almost") names = names.filter(({ name }) => !lsDirectoryEntryDisplayName(name, rawDisplayNames).startsWith(".")); + names = names.filter(({ name }) => !lsIgnored(lsDirectoryEntryDisplayName(name, rawDisplayNames), opts)); + const entries = []; + for (const { name, type, inode } of names) { + const displayName = lsDirectoryEntryDisplayName(name, rawDisplayNames); + const path = displayName === "." ? target : displayName === ".." ? pathLikeJoin(target, Buffer.from("..")) : pathLikeJoin(target, name); + try { + entries.push({ name: displayName, path, direntInode: inode, ...(await lsEntryStats(path, opts, false)) }); + } catch (error) { + // readdir has already established that this name exists. Preserve its + // d_type when a following metadata lookup fails, as GNU ls does for + // inaccessible and concurrently removed directory entries. + opts.minorProblem = true; + stderr(`ls: cannot access '${pathDisplayName(path)}': ${systemErrorMessage(error)}\n`); + entries.push({ + name: displayName, + path, + direntInode: inode, + direntTypeChar: lsDirentTypeChar(type), + statFailed: true, + stat: lsUnknownStat(type), + }); + } + } + sortLsEntries(entries, opts); + if (opts.l || opts.g || opts.o || opts.s || opts.size || opts.dired) out += `total ${lsTotalBlocks(entries)}\n`; + out += await renderLsEntries(entries, opts); + if (opts.R || opts.recursive) { + for (const entry of entries) { + if (entry.name === "." || entry.name === ".." || !entry.stat.isDirectory() || entry.stat.isSymbolicLink()) continue; + const childTarget = lsRecursiveChildTarget(target, entry); + const identity = lsDirectoryIdentity(entry.stat); + if (recursiveDirSeen.has(identity)) { + opts.majorProblem = true; + stderr(`ls: ${childTarget}: not listing already-listed directory\n`); + continue; + } + out += `\n${await renderLsDirectory(childTarget, opts, true, false, recursiveDirSeen, entry.stat)}`; + } + } + return out; +} + +export function lsReadDirectoryEntries(path) { + if (process.platform !== "linux") { + return readdirPathEntries(path).then((names) => names.map((name) => ({ name, type: 0 }))); + } + const directory = libc.symbols.opendir(cstrPath(path)); + if (!directory) throw rmLibcError(libcErrno()); + const entries = []; + try { + while (true) { + const errnoPointer = libc.symbols.__errno_location(); + if (errnoPointer) libc.symbols.memset(errnoPointer, 0, 4); + const address = libc.symbols.readdir(directory); + if (!address) { + const errno = libcErrno(); + if (errno) throw rmLibcError(errno); + break; + } + const recordLength = read.u16(address, 16); + const maximumNameLength = Math.max(0, recordLength - 19); + let nameLength = 0; + while (nameLength < maximumNameLength && read.u8(address + 19, nameLength) !== 0) nameLength++; + const name = Buffer.from(new Uint8Array(toArrayBuffer(address + 19, 0, nameLength))); + if (name.equals(Buffer.from(".")) || name.equals(Buffer.from(".."))) continue; + entries.push({ name, type: read.u8(address, 18), inode: read.u64(address, 0) }); + } + } finally { + libc.symbols.closedir(directory); + } + return entries; +} + +export function lsDirentTypeChar(type) { + return ({ 1: "p", 2: "c", 4: "d", 6: "b", 8: "-", 10: "l", 12: "s" })[type] ?? "?"; +} + +export function lsUnknownStat(type) { + const isType = (value) => type === value; + return { + mode: 0, + size: 0, + blocks: 0, + ino: 0, + nlink: 0, + uid: 0, + gid: 0, + isFile: () => isType(8), + isDirectory: () => isType(4), + isBlockDevice: () => isType(6), + isCharacterDevice: () => isType(2), + isSymbolicLink: () => isType(10), + isFIFO: () => isType(1), + isSocket: () => isType(12), + }; +} + +export function lsDirectoryEntryDisplayName(name, rawDisplayNames = false) { + if (!isBytePath(name)) return String(name); + return pathDisplayName(name); +} + +export function lsDirectoryIdentity(statInfo) { + return `${statInfo.dev}:${statInfo.ino}`; +} + +export async function lsEntryStats(path, opts, commandLine) { + const needsNanoseconds = lsNeedsStatNanoseconds(opts); + const linkStat = lsAttachStatNanoseconds(await lstat(path), path, false, needsNanoseconds); + const hasCapability = linkStat.isFile() && opts.colorEnabled && lsCapabilityColorEnabled() + ? lsFileHasCapability(path) + : false; + // -F needs the mode bits of regular files to distinguish executable + // entries. Perform that required query on the calling thread, while + // retaining d_type/lstat knowledge for symlinks and directories so those + // do not acquire an unnecessary target stat. + if (linkStat.isFile() && opts.indicatorStyle === "classify") lsNativeModeProbe(path); + if (linkStat.isSymbolicLink() && (opts.L || opts.dereference || (commandLine && (opts.H || opts["dereference-command-line"])))) { + try { + return { stat: lsAttachStatNanoseconds(await stat(path), path, true, needsNanoseconds), linkStat, hasCapability }; + } catch (error) { + if (commandLine) throw error; + const needsInfo = opts.i || opts.inode || opts.s || opts.size || opts.l || opts.g || opts.o; + if ((opts.L || opts.dereference) && (needsInfo || error?.code === "ENOENT" || error?.code === "ENOTDIR")) { + opts.minorProblem = true; + stderr(`ls: cannot access '${path}': ${systemErrorMessage(error)}\n`); + } + if (!needsInfo) return { stat: linkStat, linkStat, targetMissing: true }; + return { stat: linkStat, linkStat, danglingDereference: needsInfo, targetMissing: true }; + } + } + if (commandLine && linkStat.isSymbolicLink() && !(opts.d || opts.directory) && (opts.H || opts["dereference-command-line"] || opts.L || opts.dereference || !(opts.F || opts.p || opts.classify != null || opts["file-type"] || opts["indicator-style"]))) { + try { + const targetStat = lsAttachStatNanoseconds(await stat(path), path, true, needsNanoseconds); + if (targetStat.isDirectory()) return { stat: targetStat, linkStat, targetDirectory: true }; + } catch {} + } + let targetDirectory = false; + let targetMissing = false; + if (linkStat.isSymbolicLink()) { + try { + targetDirectory = (await stat(path)).isDirectory(); + } catch { + targetMissing = true; + } + } + return { stat: linkStat, linkStat, targetDirectory, targetMissing, hasCapability }; +} + +export let lsCapabilityProbeDone = false; + +export function lsCapabilityColorEnabled() { + const code = lsColorCode("ca"); + return code != null && code !== "" && code !== "00"; +} + +export function lsFileHasCapability(path) { + // Match GNU ls/libcap's process capability probe, while reading the file + // capability directly from Linux's canonical security.capability xattr. + if (!lsCapabilityProbeDone) { + const header = Buffer.alloc(8); + const data = Buffer.alloc(24); + header.writeUInt32LE(0x20080522, 0); + libc.symbols.capget(ptr(header), ptr(data)); + lsCapabilityProbeDone = true; + } + return libc.symbols.getxattr(cstrPath(path), cstr("security.capability"), 0, 0n) > 0n; +} + +export function lsNativeModeProbe(path) { + const buffer = Buffer.alloc(256); + libc.symbols.statx(AT_FDCWD, cstrPath(path), AT_SYMLINK_NOFOLLOW, 0x0002, buffer); +} + +export function lsNeedsStatNanoseconds(opts) { + const style = opts["time-style"]; + if (opts.t || opts.c || opts.u || opts.time != null || opts.sort === "time") return true; + if (style === "full-iso" || style === "posix-full-iso") return true; + return typeof style === "string" && style.startsWith("+") && /%[-_0^#+]*\d*N/.test(style); +} + +export function lsAttachStatNanoseconds(statInfo, path, dereference, enabled = true) { + if (!enabled) return statInfo; + try { + const precise = nativeStatNanoseconds(path, dereference) ?? (dereference ? statSync(path, { bigint: true }) : lstatSync(path, { bigint: true })); + attachStatNanoseconds(statInfo, precise); + } catch {} + return statInfo; +} + +export function lsRecursiveChildTarget(parent, entry) { + if (isBytePath(parent)) return entry.path; + if (parent === "." || parent.startsWith("./")) return `${parent.replace(/\/+$/, "")}/${entry.name}`; + return entry.path; +} + +export function lsIgnored(name, opts) { + return (opts.ignorePatterns ?? []).some((pattern) => globMatch(String(pattern), name)); +} + +export function lsTotalBlocks(entries) { + return entries.reduce((sum, entry) => entry.danglingDereference || entry.statFailed ? sum : sum + Number(lsAllocatedBlockCount(entry.stat, 1024n)), 0); +} + +export function sortLsEntries(entries, opts) { + if (((opts.U || opts.f) && opts.sort == null) || opts.sort === "none") return; + entries.sort((a, b) => { + if (opts["group-directories-first"] || opts.group) { + const dirCompare = Number(lsGroupDirectory(b)) - Number(lsGroupDirectory(a)); + if (dirCompare) return dirCompare; + } + let result; + if (opts.sort === "name") result = compareLsNames(a.name, b.name, opts); + else if (opts.S || opts.sort === "size") result = b.stat.size - a.stat.size || compareLsNames(a.name, b.name, opts); + else if (opts.t || opts.c || opts.u || opts.time != null || opts.sort === "time") result = compareBigInt(lsEntryTimeNs(b, opts), lsEntryTimeNs(a, opts)) || compareLsNames(a.name, b.name, opts); + else if (opts.sort === "width") result = [...pathDisplayName(a.name)].length - [...pathDisplayName(b.name)].length || compareLsNames(a.name, b.name, opts); + else if (opts.v || opts.sort === "version") result = lsVersionCompare(a.name, b.name); + else if (opts.X || opts.sort === "extension") result = extensionCompare(a.name, b.name); + else result = compareLsNames(a.name, b.name, opts); + return opts.r || opts.reverse ? -result : result; + }); +} + +export function compareLsNames(a, b, opts) { + const aa = pathDisplayName(a); + const bb = pathDisplayName(b); + return opts.localeCollation ? compareSortLocaleText(aa, bb) : compareSortBytes(aa, bb); +} + +export function lsGroupDirectory(entry) { + return entry.stat.isDirectory() || entry.targetDirectory; +} + +export function lsVersionCompare(a, b) { + const aa = String(a); + const bb = String(b); + let ai = 0; + let bi = 0; + while (ai < aa.length || bi < bb.length) { + if (aa[ai] === "~" || bb[bi] === "~") { + if (aa[ai] !== "~") return 1; + if (bb[bi] !== "~") return -1; + ai++; + bi++; + continue; + } + if (ai >= aa.length) return -1; + if (bi >= bb.length) return 1; + if (isAsciiDigit(aa[ai]) && isAsciiDigit(bb[bi])) { + const aStart = ai; + const bStart = bi; + while (isAsciiDigit(aa[ai])) ai++; + while (isAsciiDigit(bb[bi])) bi++; + const aDigits = aa.slice(aStart, ai); + const bDigits = bb.slice(bStart, bi); + const na = BigInt(aDigits.replace(/^0+/, "") || "0"); + const nb = BigInt(bDigits.replace(/^0+/, "") || "0"); + if (na !== nb) return na < nb ? -1 : 1; + if (aDigits.length !== bDigits.length) return aDigits.length - bDigits.length; + continue; + } + if (aa[ai] !== bb[bi]) return aa[ai] < bb[bi] ? -1 : 1; + ai++; + bi++; + } + return 0; +} + +export function lsEntryTimeNs(entry, opts) { + const mode = lsEntryTimeMode(opts); + const value = entry.stat[`${mode}Ns`]; + if (value != null) return BigInt(value); + return BigInt(lsEntryTime(entry, opts).getTime()) * 1_000_000n; +} + +export function compareBigInt(a, b) { + return a < b ? -1 : a > b ? 1 : 0; +} + +export function extensionCompare(a, b) { + const ax = extensionSortKey(a); + const bx = extensionSortKey(b); + if (ax.ext !== bx.ext) return ax.ext < bx.ext ? -1 : 1; + return a < b ? -1 : a > b ? 1 : 0; +} + +export function extensionSortKey(name) { + const index = name.lastIndexOf("."); + if (index <= 0 || index === name.length - 1) return { ext: "" }; + return { ext: name.slice(index + 1) }; +} + +export async function renderLsEntries(entries, opts) { + if (!entries.length) return ""; + if (opts.l || opts.g || opts.o) { + const lines = renderLsLongEntries(await Promise.all(entries.map((entry) => lsLongEntryFields(entry, opts))), opts); + return opts.zero ? lines.join("\0") + "\0" : lines.join("\n") + "\n"; + } + if (opts.zero) return entries.map((entry) => formatLsEntry(entry, opts)).join("\0") + "\0"; + if (opts.m) return renderLsCommaEntries(entries, opts); + const wideQuotedColumns = opts.width == null && (opts.quoting ?? opts["quoting-style"]) === "shell-escape"; + const separator = opts.C || opts.x ? (wideQuotedColumns ? " " : " ") : "\n"; + const widths = lsShortEntryWidths(entries, opts); + const fields = []; + let emittedInitialColorReset = Boolean(opts.colorResetEmitted); + for (const entry of entries) { + let field = formatLsEntry(entry, opts, widths); + if (emittedInitialColorReset) field = stripLsLeadingReset(field); + else if (field.startsWith("\x1b[0m")) { + emittedInitialColorReset = true; + opts.colorResetEmitted = true; + } + fields.push(field); + } + return (opts.C || opts.x) && !wideQuotedColumns ? renderLsColumnFields(fields, opts) : fields.join(separator) + "\n"; +} + +export function renderLsColumnFields(fields, opts) { + const outputWidth = opts.width ?? lsOutputWidth(opts); + const widths = fields.map((field) => lsDisplayWidth(stripAnsi(field))); + const columnCount = lsColumnCount(widths, outputWidth, opts); + const rowCount = Math.ceil(fields.length / columnCount); + const columnWidths = Array(columnCount).fill(0); + for (let row = 0; row < rowCount; row++) { + for (let column = 0; column < columnCount; column++) { + const index = lsColumnFieldIndex(row, column, rowCount, columnCount, opts); + if (index < fields.length) columnWidths[column] = Math.max(columnWidths[column], widths[index]); + } + } + const starts = []; + let start = 0; + for (const width of columnWidths) { + starts.push(start); + start += width + 2; + } + const lines = []; + for (let row = 0; row < rowCount; row++) { + let line = ""; + let displayColumn = 0; + for (let column = 0; column < columnCount; column++) { + const index = lsColumnFieldIndex(row, column, rowCount, columnCount, opts); + if (index >= fields.length) continue; + if (line) { + const padding = lsColumnPadding(displayColumn, starts[column], opts.tabSize); + line += padding.text; + displayColumn = padding.column; + } + line += fields[index]; + displayColumn += widths[index]; + } + lines.push(line); + } + return lines.join("\n") + "\n"; +} + +export function lsColumnCount(widths, outputWidth, opts) { + const count = widths.length; + if (count <= 1) return count; + const available = Math.max(1, outputWidth); + for (let columns = count; columns > 1; columns--) { + const rows = Math.ceil(count / columns); + const columnWidths = Array(columns).fill(0); + for (let row = 0; row < rows; row++) { + for (let column = 0; column < columns; column++) { + const index = lsColumnFieldIndex(row, column, rows, columns, opts); + if (index < count) columnWidths[column] = Math.max(columnWidths[column], widths[index]); + } + } + const total = columnWidths.reduce((sum, width) => sum + width, 0) + ((columns - 1) * 2); + if (total <= available) return columns; + } + return 1; +} + +export function lsColumnFieldIndex(row, column, rowCount, columnCount, opts) { + return opts.x ? (row * columnCount) + column : (column * rowCount) + row; +} + +export function lsColumnPadding(column, target, tabSize) { + let text = ""; + while (column < target) { + const nextTab = tabSize > 0 ? column + tabSize - (column % tabSize) : Number.POSITIVE_INFINITY; + if (nextTab <= target && nextTab - column > 1) { + text += "\t"; + column = nextTab; + } else { + text += " "; + column++; + } + } + return { text, column }; +} + +export function stripLsLeadingReset(text) { + return text.startsWith("\x1b[0m") ? text.slice(4) : text; +} + +export function renderLsCommaEntries(entries, opts) { + const fields = []; + let emittedInitialColorReset = Boolean(opts.colorResetEmitted); + for (const entry of entries) { + let field = formatLsEntry(entry, opts); + if (emittedInitialColorReset) field = stripLsLeadingReset(field); + else if (field.startsWith("\x1b[0m")) { + emittedInitialColorReset = true; + opts.colorResetEmitted = true; + } + fields.push(field); + } + const width = opts.width ?? Number.POSITIVE_INFINITY; + const lines = []; + let line = ""; + for (let i = 0; i < fields.length; i++) { + const text = `${fields[i]}${i === fields.length - 1 ? "" : ","}`; + const candidate = line ? `${line} ${text}` : text; + if (line && width > 0 && candidate.length > width) { + lines.push(line); + line = text; + } else { + line = candidate; + } + } + if (line) lines.push(line); + return lines.join("\n") + "\n"; +} + +export function formatLsDiredOutput(output, quotingStyle = "literal") { + const fileMarks = []; + const subdirMarks = []; + let out = ""; + let offset = 0; + for (const rawLine of output.split(/(?<=\n)/)) { + if (rawLine === "") continue; + const hasNewline = rawLine.endsWith("\n"); + const line = hasNewline ? rawLine.slice(0, -1) : rawLine; + if (line === "") { + out += rawLine; + offset += Buffer.byteLength(rawLine); + continue; + } + const prefixed = ` ${line}`; + const lineStart = offset; + const subdir = line.match(/^(.+):$/); + if (subdir) { + subdirMarks.push(lineStart + 2, lineStart + 2 + Buffer.byteLength(subdir[1])); + } else if (!/^total(?:\s|$)/.test(line)) { + const range = lsDiredFilenameRange(line); + if (range) fileMarks.push(lineStart + 2 + range.start, lineStart + 2 + range.end); + } + const rendered = `${prefixed}${hasNewline ? "\n" : ""}`; + out += rendered; + offset += Buffer.byteLength(rendered); + } + if (fileMarks.length) out += `//DIRED// ${fileMarks.join(" ")}\n`; + if (subdirMarks.length) out += `//SUBDIRED// ${subdirMarks.join(" ")}\n`; + out += `//DIRED-OPTIONS// --quoting-style=${quotingStyle}\n`; + return out; +} + +export function lsDiredFilenameRange(line) { + let text = line; + if (/^[bcdlps?-][rwx?-]{9}/.test(line) && line.slice(1, 10).includes("?")) { + const match = line.match(/^(\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+)(.*)$/); + if (!match) return null; + const prefixBytes = Buffer.byteLength(match[1]); + return { start: prefixBytes, end: prefixBytes + Buffer.byteLength(match[2]) }; + } + if (/^[bcdlps-][rwx-]{9}/.test(line)) { + const hasContextColumn = /^[bcdlps-][rwx-]{9}\s+\S+\s+\S+\s+\S+\s+\?\s+/.test(line); + const prefixPattern = hasContextColumn + ? /^(\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+)(.*)$/ + : /^(\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+)(.*)$/; + const match = line.match(prefixPattern); + if (!match) return null; + const prefixBytes = Buffer.byteLength(match[1]); + text = match[2]; + const symlinkIndex = text.indexOf(" -> "); + const name = symlinkIndex === -1 ? text : text.slice(0, symlinkIndex); + return { start: prefixBytes, end: prefixBytes + Buffer.byteLength(name) }; + } + if (/^\s*$/.test(text)) return null; + return { start: 0, end: Buffer.byteLength(text) }; +} + +export function renderLsLongEntries(rows, opts) { + const widths = { + inode: maxLsFieldWidth(rows, "inode", 0), + block: maxLsFieldWidth(rows, "block", 0), + nlink: maxLsFieldWidth(rows, "nlink", 0), + user: maxLsFieldWidth(rows, "user", 0), + author: maxLsFieldWidth(rows, "author", 0), + group: maxLsFieldWidth(rows, "group", 0), + context: maxLsFieldWidth(rows, "context", 0), + size: maxLsFieldWidth(rows, "size", 0), + }; + return rows.map((row, index) => { + const fields = [row.mode, row.nlink.padStart(widths.nlink)]; + if (opts.i || opts.inode) fields.unshift(row.inode.padStart(widths.inode)); + if (opts.s || opts.size) fields.unshift(row.block.padStart(widths.block)); + if (!opts.g) fields.push(row.user.padStart(widths.user)); + if (!(opts.G || opts.o)) fields.push(row.group.padStart(widths.group)); + if (opts.author) fields.push(row.author.padStart(widths.author)); + if (opts.Z || opts.context) fields.push(row.context.padStart(widths.context)); + fields.push(row.size.padStart(widths.size), row.time, row.name); + let line = opts.colorEnabled && lsNormalColorCode() != null ? formatLsNormalLongRow(fields, index === 0) : fields.join(" "); + if (row.clearToEol && lsDisplayWidth(stripAnsi(line)) >= lsOutputWidth(opts)) line += "\x1b[K"; + return line; + }); +} + +export function formatLsNormalLongRow(fields, resetFirst = false) { + const normal = lsNormalColorCode(); + if (normal == null) return fields.join(" "); + const coloredPrefix = lsNormalColoredPrefix(); + const normalPrefix = `\x1b[${normal}m`; + const resetNormalPrefix = `\x1b[0m${normalPrefix}`; + let name = fields.at(-1); + if (name.startsWith(resetNormalPrefix) && name.endsWith("\x1b[0m")) name = name.slice(resetNormalPrefix.length, -4); + else if (name.startsWith(coloredPrefix)) name = `\x1b[m${name.slice(coloredPrefix.length)}`; + else if (name.startsWith("\x1b[0m")) name = `\x1b[m${name.slice(4)}`; + else { + if (name.startsWith(normalPrefix) && name.endsWith("\x1b[0m")) name = name.slice(normalPrefix.length, -4); + else return `\x1b[${normal}m${fields.join(" ")}\x1b[0m`; + } + if (name.startsWith("\x1b[m") && name.endsWith("\x1b[0m")) name = name.slice(0, -4); + return `${resetFirst ? "\x1b[0m" : ""}\x1b[${normal}m${[...fields.slice(0, -1), name].join(" ")}\x1b[0m`; +} + +export function stripAnsi(text) { + return String(text).replace(/\x1b\[[0-9;]*[A-Za-z]/g, ""); +} + +export function lsDisplayWidth(text) { + let width = 0; + for (const ch of String(text)) width += displayWidth(ch); + return width; +} + +export function lsOutputWidth(opts) { + const value = opts.width ?? process.env.COLUMNS ?? 80; + const width = Number(value); + return Number.isFinite(width) && width > 0 ? width : 80; +} + +export function maxLsFieldWidth(rows, key, minimum) { + return Math.max(minimum, ...rows.map((row) => row[key]?.length ?? 0)); +} + +export async function lsLongEntryFields(entry, opts) { + let name = formatLsName(entry, opts); + if (lsNeedsShellEscapeColorPadding(entry, name, opts)) name = ` ${name}`; + if (entry.statFailed) { + return { + inode: entry.direntInode == null ? "?" : String(entry.direntInode), + block: "?", + mode: `${entry.direntTypeChar ?? "?"}?????????`, + nlink: "?", + user: "?", + author: "?", + group: "?", + context: "?", + size: "?", + time: " ?", + name, + }; + } + const uidText = String(entry.stat.uid); + const gidText = String(entry.stat.gid); + const numericOwner = opts.n || opts["numeric-uid-gid"]; + const userText = numericOwner ? uidText : await userNameForUid(entry.stat.uid) ?? uidText; + const groupText = numericOwner ? gidText : await groupName(entry.stat.gid); + if (entry.danglingDereference) { + return { + inode: "?", + block: "?", + mode: `${fileTypeChar(entry.linkStat ?? entry.stat)}?????????`, + nlink: "?", + user: "?", + author: "?", + group: "?", + context: "?", + size: "?", + time: " ?", + name, + }; + } + if (entry.linkStat?.isSymbolicLink() && entry.stat === entry.linkStat) { + const target = await readlink(entry.path); + const quotedTarget = quoteLsName(target, opts); + name += ` -> ${opts.colorEnabled ? colorizeLsTarget(quotedTarget) : quotedTarget}`; + } else if (opts.colorEnabled && lsNormalColorCode() == null && name.endsWith("\x1b[0m")) { + return { + inode: lsEntryInodeText(entry), + block: entry.danglingDereference ? "?" : lsBlockText(entry.stat, opts), + mode: `${modeString(entry.stat)}${lsAclIndicator(entry)}`, + nlink: String(entry.stat.nlink), + user: userText, + author: userText, + group: groupText, + context: lsSecurityContext(entry, opts), + size: String(lsDisplaySize(entry.stat, opts)), + time: lsTimeText(lsEntryTime(entry, opts), opts), + name, + clearToEol: true, + }; + } + return { + inode: lsEntryInodeText(entry), + block: entry.danglingDereference ? "?" : lsBlockText(entry.stat, opts), + mode: `${modeString(entry.stat)}${lsAclIndicator(entry)}`, + nlink: String(entry.stat.nlink), + user: userText, + author: userText, + group: groupText, + context: lsSecurityContext(entry, opts), + size: String(lsDisplaySize(entry.stat, opts)), + time: lsTimeText(lsEntryTime(entry, opts), opts), + name, + }; +} + +export function lsAclIndicator(entry) { + // POSIX ACLs are represented by a trailing '+' in GNU ls's mode field. + // Node/Bun do not expose ACLs through stat, so use getfacl when present. + // A symlink itself cannot carry a POSIX access ACL; only query its target + // when this ls invocation dereferenced it. + if (process.platform !== "linux") return ""; + const noDereference = entry.linkStat?.isSymbolicLink() && entry.stat === entry.linkStat; + if (!noDereference) { + try { + const result = Bun.spawnSync(["getfacl", "--omit-header", "--absolute-names", "--", String(entry.path)], { stdout: "pipe", stderr: "ignore" }); + if (result.exitCode === 0 && lsAclIndicatorFromText(new TextDecoder().decode(result.stdout)) === "+") return "+"; + } catch { + // A missing ACL helper does not prevent checking the SELinux label below. + } + } + // GNU ls uses '.' when a security context exists but no extended ACL does. + return selinuxSecurityContext(entry.path, noDereference) == null ? "" : "."; +} + +export function lsAclIndicatorFromText(text) { + for (const line of String(text).split(/\r?\n/)) { + if (line.startsWith("default:") || line.startsWith("mask:") || /^user:[^:]+:/.test(line) || /^group:[^:]+:/.test(line)) return "+"; + } + return ""; +} + +export function lsSecurityContext(entry, _opts) { + const noDereference = entry.linkStat?.isSymbolicLink() && entry.stat === entry.linkStat; + return selinuxSecurityContext(entry.path, noDereference) ?? "?"; +} + +export function lsNeedsShellEscapeColorPadding(entry, renderedName, opts) { + const quoting = opts.quoting ?? opts["quoting-style"]; + if (quoting !== "shell-escape" || !opts.colorEnabled || !renderedName.startsWith("\x1b[0m")) return false; + const quoted = quoteLsName(entry.name, opts); + return !quoted.startsWith("'"); +} + +export function lsShortEntryWidths(entries, opts) { + return { + inode: opts.i || opts.inode ? Math.max(...entries.map((entry) => lsEntryInodeText(entry).length)) : 0, + block: opts.s || opts.size ? Math.max(...entries.map((entry) => entry.danglingDereference || entry.statFailed ? 1 : lsBlockText(entry.stat, opts).length)) : 0, + }; +} + +export function formatLsEntry(entry, opts, widths = null) { + const name = formatLsName(entry, opts); + const fields = []; + if (opts.i || opts.inode) fields.push(lsEntryInodeText(entry).padStart(widths?.inode ?? 0)); + if (opts.s || opts.size) fields.push((entry.danglingDereference || entry.statFailed ? "?" : lsBlockText(entry.stat, opts)).padStart(widths?.block ?? 0)); + if (opts.Z || opts.context) fields.push(entry.statFailed ? "?" : lsSecurityContext(entry, opts)); + fields.push(name); + return fields.join(" "); +} + +export function lsEntryInodeText(entry) { + if (entry.danglingDereference || (entry.statFailed && entry.direntInode == null)) return "?"; + // d_ino can identify the covered directory entry rather than the mounted + // object at a mount point. Since this implementation already gathered + // metadata, prefer st_ino; this also selects the target inode under -L. + if (!entry.statFailed && entry.stat?.ino != null) return String(entry.stat.ino); + return String(entry.direntInode); +} + +export function lsBlockText(statInfo, opts) { + const allocatedBytes = Number(lsAllocatedBytes(statInfo)); + const human = opts.h || opts["human-readable"] || opts.si || opts.blockHumanReadable || opts.blockSi; + return human ? lsHumanSize(allocatedBytes, opts.si || opts.blockSi ? 1000 : 1024) : String(lsAllocatedBlockCount(statInfo, BigInt(opts.blockSize || 1024))); +} + +export function lsDisplaySize(statInfo, opts) { + if (opts.h || opts["human-readable"] || opts.si) return lsHumanSize(statInfo.size, opts.si ? 1000 : 1024); + return Math.ceil(statInfo.size / (opts.sizeBlockSize || 1)); +} + +export function lsAllocatedBytes(statInfo) { + return BigInt(statInfo.blocks ?? Math.ceil(statInfo.size / 512)) * 512n; +} + +export function lsAllocatedBlockCount(statInfo, blockSize) { + const bytes = lsAllocatedBytes(statInfo); + return (bytes + blockSize - 1n) / blockSize; +} + +export function lsHumanSize(bytes, base = 1024) { + if (bytes < base) return String(bytes); + const units = base === 1000 ? ["B", "k", "M", "G", "T"] : ["B", "K", "M", "G", "T"]; + return humanSizeWithUnits(bytes, base, units); +} + +export function lsEntryTime(entry, opts) { + const mode = lsEntryTimeMode(opts); + return lsStatTime(entry.stat, mode); +} + +export function lsEntryTimeMode(opts) { + const mode = opts.time ?? (opts.u ? "access" : opts.c ? "ctime" : "mtime"); + if (["access", "atime", "use"].includes(mode)) return "atime"; + if (["ctime", "status"].includes(mode)) return "ctime"; + if (["birth", "creation"].includes(mode)) return "birthtime"; + return "mtime"; +} + +export function lsStatTime(statInfo, mode) { + const date = statInfo[mode]; + const value = statInfo[`${mode}Ns`]; + if (value == null) return date; + const fraction = Number(((BigInt(value) % 1_000_000_000n) + 1_000_000_000n) % 1_000_000_000n); + return attachDateNanoseconds(new Date(date.getTime()), fraction); +} + +export function formatLsName(entry, opts) { + const name = quoteLsName(entry.name, opts); + const suffix = classifySuffix(entry.stat, opts); + const linkedName = opts.hyperlinkEnabled ? formatLsHyperlink(name, lsHyperlinkPath(entry), opts) : name; + return `${opts.colorEnabled ? colorizeLs(linkedName, entry) : linkedName}${suffix}`; +} + +export function lsHyperlinkPath(entry) { + if (entry.linkStat?.isSymbolicLink() && entry.targetDirectory && !isBytePath(entry.path)) return entry.path.replace(/\/+$/, ""); + return entry.path; +} + +export function formatLsHyperlink(name, path, opts) { + if (!opts.hyperlinkEnabled || opts.zero) return name; + return `\x1b]8;;${fileUriForLs(path)}\x1b\\${name}\x1b]8;;\x1b\\`; +} + +export function fileUriForLs(path) { + const host = osHostname(); + if (isBytePath(path)) return `file://${host}${encodeLsFileUriBytes(absoluteLsPathBytes(path))}`; + return `file://${host}${encodeLsFileUriPath(resolve(pathDisplayName(path)))}`; +} + +export async function lsDirectoryLabelPath(path) { + const s = await lstat(path).catch(() => null); + if (!s?.isSymbolicLink()) return path; + const target = await readlink(path).catch(() => null); + if (target == null) return path; + return isAbsolute(target) ? target : resolve(pathDirname(pathDisplayName(path)), target); +} + +export function absoluteLsPathBytes(path) { + if (path[0] === 0x2f) return Buffer.from(path); + return bufferPathJoin(Buffer.from(resolve(".")), path); +} + +export function encodeLsFileUriBytes(path) { + return [...Buffer.from(path)].map((byte) => { + const ch = String.fromCharCode(byte); + return /[A-Za-z0-9/._~-]/.test(ch) ? ch : `%${byte.toString(16).padStart(2, "0")}`; + }).join(""); +} + +export function encodeLsFileUriPath(path) { + return String(path).replace(/[^A-Za-z0-9/._~-]/g, (ch) => { + const bytes = new TextEncoder().encode(ch); + return [...bytes].map((byte) => `%${byte.toString(16).padStart(2, "0")}`).join(""); + }); +} + +export function quoteLsName(name, opts) { + const displayName = pathDisplayName(name); + if (opts.zero) return displayName; + const style = lsResolvedQuotingStyle(opts); + const text = opts.q && !["c", "escape", "shell-escape", "clocale", "locale"].includes(style) ? lsQuestionName(displayName) : displayName; + if (style === "literal") return text; + if (style === "shell") return shellQuoteLsName(displayName, false, text); + if (style === "question") return lsQuestionName(displayName); + if (style === "escape") return lsEscapedName(displayName, { escapeSpaces: true }); + if (style === "c") return `"${lsEscapedName(displayName)}"`; + if (style === "clocale") return lsUsesUtf8Locale() ? `‘${lsEscapedName(displayName, { escapeDouble: false })}’` : `"${lsEscapedName(displayName)}"`; + if (style === "locale") return lsUsesUtf8Locale() ? `‘${lsEscapedName(displayName, { escapeDouble: false })}’` : `'${lsEscapedName(displayName)}'`; + if (style === "shell-always" || style === "shell-al") return shellQuoteLsName(displayName, true, text); + if (style === "shell-escape") return shellEscapeLsName(displayName); + if (style === "shell-escape-always" || style === "shell-escape-al") return shellEscapeLsName(displayName, true); + return text; +} + +export function quoteLsDirectoryLabelName(name, opts) { + const quoted = quoteLsName(name, opts); + const style = lsResolvedQuotingStyle(opts); + const displayName = pathDisplayName(name); + if ((style === "shell" || style === "shell-escape") && displayName.includes(":") && quoted === displayName) return shellQuoteLsName(displayName, true); + return quoted; +} + +export function lsResolvedQuotingStyle(opts) { + if (opts.N) return "literal"; + if (opts.Q || opts["quote-name"]) return "c"; + const style = opts.quoting ?? opts["quoting-style"]; + return resolveLsQuotingStyle(style) ?? (opts.q ? "question" : process.stdout.isTTY ? "shell-escape" : "literal"); +} + +export function lsQuestionName(name) { + return [...String(name)].map((ch) => { + const code = ch.codePointAt(0); + return code < 32 || code === 127 ? "?" : ch; + }).join(""); +} + +export function classifySuffix(s, opts) { + if (opts.indicatorStyle === "none") return ""; + if (s.isDirectory()) return "/"; + if (opts.indicatorStyle === "slash") return ""; + if (s.isSymbolicLink()) return "@"; + if (s.isFIFO()) return "|"; + if (s.isSocket()) return "="; + if (opts.indicatorStyle === "file-type") return ""; + if (s.mode & 0o111) return "*"; + return ""; +} + +export function colorizeLs(name, entry) { + const code = lsColorCodeForEntry(entry); + if (code != null) return lsColorWrap(name, code); + const custom = lsColorForName(entry.name); + if (custom != null) return lsColorWrap(name, custom); + const fileCode = lsColorCode("fi"); + if (fileCode != null) return lsColorWrap(name, fileCode); + const normal = lsNormalColorCode(); + return normal == null ? name : `\x1b[0m\x1b[${normal}m${name}\x1b[0m`; +} + +export function colorizeLsTarget(name) { + const code = lsColorCode("mi"); + return code == null ? name : lsColorWrap(name, code, false); +} + +export function lsColorWrap(name, code, resetFirst = true) { + return `${resetFirst ? lsColorResetPrefix() : ""}\x1b[${code}m${name}${lsColorResetSuffix()}`; +} + +export function lsColorResetPrefix() { + const normal = lsNormalColorCode(); + return normal == null ? lsColorResetSuffix() : lsNormalColoredPrefix(); +} + +export function lsNormalColoredPrefix() { + const normal = lsNormalColorCode(); + return normal == null ? lsColorResetSuffix() : `${lsColorResetSuffix()}\x1b[${normal}m\x1b[m`; +} + +export function lsNormalColorCode() { + return lsColorCode("no"); +} + +export function lsColorResetSuffix() { + const reset = lsColorCode("rs"); + return `\x1b[${reset ?? "0"}m`; +} + +export function lsColorCodeForEntry(entry) { + const s = entry.stat; + if (entry.linkStat?.isSymbolicLink() && s === entry.linkStat) { + const linkCode = lsColorCode("ln"); + if (entry.targetMissing) { + const orphanCode = lsColorCode("or"); + if (orphanCode != null && orphanCode !== "") return orphanCode; + if (orphanCode === "") return linkCode && linkCode !== "target" ? linkCode : ""; + return linkCode === "target" ? null : linkCode ?? "40;31;01"; + } + if (linkCode === "target") { + if (entry.targetDirectory) return lsColorCode("di") ?? "01;34"; + return null; + } + return linkCode ?? "01;36"; + } + if (s.isDirectory()) { + if ((s.mode & 0o1000) && (s.mode & 0o002)) return lsSpecialDirectoryColor("tw", "30;42"); + if (s.mode & 0o1000) return lsSpecialDirectoryColor("st", "37;44"); + if (s.mode & 0o002) return lsSpecialDirectoryColor("ow", "34;42"); + return lsColorCode("di") || "01;34"; + } + if (entry.hasCapability) return lsColorCode("ca"); + if (s.mode & 0o4000) return lsColorCode("su") ?? null; + if (s.mode & 0o2000) return lsColorCode("sg") ?? null; + if (s.mode & 0o111) return lsColorCode("ex") ?? "01;32"; + if (s.nlink > 1) { + const hardLinkCode = lsColorCode("mh"); + if (hardLinkCode != null && hardLinkCode !== "" && hardLinkCode !== "00") return hardLinkCode; + } + return null; +} + +export function lsSpecialDirectoryColor(key, defaultCode) { + const code = lsColorCode(key); + if (code == null) return defaultCode; + if (code === "") return lsColorCode("di") || "01;34"; + return code; +} + +export function lsColorCode(key) { + for (const part of lsColorParts()) { + if (part.key === key) return part.value; + } + return null; +} + +export function lsColorForName(name) { + const wildcardRules = []; + for (const { key, value } of lsColorParts()) { + if (!key) continue; + if (key.startsWith("*")) wildcardRules.push({ key, suffix: key.slice(1), value, extension: key.startsWith("*.") }); + } + const text = String(name); + let matched = null; + for (let i = 0; i < wildcardRules.length; i++) { + const rule = wildcardRules[i]; + const suffix = rule.suffix; + if (text.endsWith(suffix)) matched = rule; + else if (rule.extension && text.toLowerCase().endsWith(suffix.toLowerCase()) && lsExtensionRuleIsCaseInsensitive(wildcardRules, i)) matched = rule; + } + if (matched) return matched.value; + return null; +} + +export function lsColorParts() { + const colors = process.env.LS_COLORS ?? ""; + const parts = []; + let current = ""; + let escaped = false; + for (const ch of colors) { + if (escaped) { + current += ch; + escaped = false; + } else if (ch === "\\") { + escaped = true; + } else if (ch === ":") { + parts.push(current); + current = ""; + } else { + current += ch; + } + } + if (escaped) current += "\\"; + parts.push(current); + return parts.flatMap((part) => { + const index = part.indexOf("="); + return index < 0 ? [] : [{ key: part.slice(0, index), value: part.slice(index + 1) }]; + }); +} + +export function lsExtensionRuleIsCaseInsensitive(rules, index) { + const rule = rules[index]; + const effective = new Map(); + for (const other of rules) { + if (other.suffix.toLowerCase() === rule.suffix.toLowerCase()) effective.set(other.suffix, other.value); + } + const values = [...effective.values()]; + return values.length === 1 || values.every((value) => value === rule.value); +} + +export function applyLsBlockSizeSpecialMode(opts) { + applyBlockSizeSpecialMode(opts, lsSizeBlockSizeValue(opts)); + const mode = blockSizeSpecialMode(lsBlockSizeValue(opts)); + if (mode === "human") opts.blockHumanReadable = true; + if (mode === "si") opts.blockSi = true; +} diff --git a/src/shared/ownership.js b/src/shared/ownership.js new file mode 100644 index 0000000..68acc38 --- /dev/null +++ b/src/shared/ownership.js @@ -0,0 +1,194 @@ +import { chown, lchown, lstat, stat } from "node:fs/promises"; +import { groupName, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, pathDisplayName, pathLikeJoin, readdirPathEntries, shellEscapeLsName, userNameForUid } from "./common.js"; +import { UsageError, stderr, stdout } from "./diagnostics.js"; +import { errnoMessage, invalidClusterOptionMessage, isAccessError, isRecursiveRootTarget, preserveRootError, preserveRootMessage, resolveGroup, resolveUser } from "./filesystem.js"; + +export const CHOWN_LONG_OPTIONS = ["changes", "recursive", "silent", "quiet", "dereference", "no-dereference", "from", "reference", "preserve-root", "no-preserve-root", "verbose", "help", "version"]; + +export function chownMetaOption(args) { + const longValueOptions = new Set(["from", "reference"]); + const longFlagOptions = new Set(["changes", "recursive", "silent", "quiet", "dereference", "no-dereference", "preserve-root", "no-preserve-root", "verbose"]); + const shortFlagOptions = new Set(["R", "c", "f", "h", "H", "L", "P", "v"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeLongOptionByPrefix(arg, CHOWN_LONG_OPTIONS); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + return normalized; + } + if (longFlagOptions.has(name)) { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + continue; + } + if (name === "from") return null; + if (longValueOptions.has(name)) { + if (inlineValue === undefined) i++; + continue; + } + if (arg.startsWith("-") && arg !== "-") { + if ([...arg.slice(1)].every((ch) => shortFlagOptions.has(ch))) continue; + throw new UsageError(invalidClusterOptionMessage(arg, shortFlagOptions), true); + } + return null; + } + return null; +} + +export function normalizeChownArgs(args) { + const out = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + out.push(...args.slice(i)); + break; + } + if (arg.startsWith("--")) { + const normalized = normalizeLongOptionByPrefix(arg, CHOWN_LONG_OPTIONS); + if ((normalized === "--from" || normalized === "--reference") && i + 1 < args.length) { + out.push(`${normalized}=${args[++i]}`); + } else { + out.push(normalized); + } + continue; + } + out.push(arg); + } + return out; +} + +export function parseChownSpec(spec) { + if (spec === "") return { ownerPart: "", groupPart: null, separator: null }; + const colon = spec.indexOf(":"); + const dot = spec.indexOf("."); + let index = colon; + let separator = ":"; + if (index === -1 || (dot !== -1 && dot < index)) index = dot; + if (index === dot) separator = "."; + if (index === -1) return { ownerPart: spec, groupPart: null, separator: null }; + const ownerPart = spec.slice(0, index); + const groupPart = spec.slice(index + 1); + if (/^\d+$/.test(ownerPart) && groupPart === "" && separator === ":") throw new UsageError(`invalid spec: ${localeQuotedEscapedDiagnostic(spec)}`); + return { ownerPart, groupPart, separator }; +} + +export async function resolveChownOwnerGroupSpec(spec, options = {}) { + const { ownerPart, groupPart, separator } = parseChownSpec(spec); + if (separator === ".") { + try { + if (groupPart === "" && /^\d+$/.test(ownerPart)) throw new UsageError(`invalid user: ${localeQuotedEscapedDiagnostic(spec)}`); + const uid = await resolveUser(ownerPart); + const gid = groupPart == null ? null : await resolveGroup(groupPart); + if (options.warnDot) stderr(`${options.warningCommand ?? "chown"}: warning: '.' should be ':': '${spec}'\n`); + return { uid, gid }; + } catch { + throw new UsageError(`invalid user: ${localeQuotedEscapedDiagnostic(spec)}`); + } + } + let uid; + try { + uid = await resolveUser(ownerPart); + } catch { + if (groupPart === "") throw new UsageError(`invalid spec: ${localeQuotedEscapedDiagnostic(spec)}`); + throw new UsageError(`invalid user: ${localeQuotedEscapedDiagnostic(spec)}`); + } + try { + return { uid, gid: groupPart == null ? null : await resolveGroup(groupPart) }; + } catch { + throw new UsageError(`invalid group: ${localeQuotedEscapedDiagnostic(spec)}`); + } +} + +export async function chownPath(path, uid, gid, recursive, options = { dereference: true }, isRoot = false, display = path) { + let ok = true; + const linkInfo = await lstat(path); + let operationInfo = linkInfo; + let traversalInfo = linkInfo; + const isLink = linkInfo.isSymbolicLink(); + const followForTraversal = isLink && recursive && (options.traversal === "L" || (isRoot && options.traversal === "H")); + // Recursive -P changes a symlink itself. -H still dereferences symlinks for + // the ownership operation, but only descends through a command-line one; + // -L both dereferences and descends through every symlink. + const shouldDereference = isLink && options.dereference && (!recursive || options.traversal !== "P"); + if (isLink && (shouldDereference || followForTraversal)) { + try { + const followedInfo = await stat(path); + if (shouldDereference) operationInfo = followedInfo; + if (followForTraversal) traversalInfo = followedInfo; + } catch (error) { + error.cannotDereference = true; + throw error; + } + } + if (recursive && options.preserveRoot && traversalInfo.isDirectory() && await isRecursiveRootTarget(path)) throw preserveRootError(options.command, path); + const nextUid = uid ?? operationInfo.uid; + const nextGid = gid ?? operationInfo.gid; + const fromMatches = (options.fromUid == null || operationInfo.uid === options.fromUid) && (options.fromGid == null || operationInfo.gid === options.fromGid); + if (recursive && traversalInfo.isDirectory() && (!isLink || followForTraversal)) { + for (const entry of await readdirPathEntries(path)) { + const childDisplay = `${String(display).replace(/\/+$/, "")}/${pathDisplayName(entry)}`; + try { + if (!(await chownPath(pathLikeJoin(path, entry), uid, gid, true, options, false, childDisplay))) ok = false; + } catch (error) { + ok = false; + if (options.verbose) stdout(chownFailureVerboseLine(childDisplay, options)); + if (!options.silent) stderr(options.command === "chgrp" ? chgrpErrorLine(childDisplay, error) : chownErrorLine(childDisplay, error)); + } + } + } + if (fromMatches) { + if (isLink && !shouldDereference) await lchown(path, nextUid, nextGid); + else await chown(path, nextUid, nextGid); + const changed = operationInfo.uid !== nextUid || operationInfo.gid !== nextGid; + if ((options.changes || options.verbose) && changed) stdout(await chownVerboseLine(options.command, display, operationInfo.uid, operationInfo.gid, nextUid, nextGid, options, true)); + else if (options.verbose) stdout(await chownVerboseLine(options.command, display, operationInfo.uid, operationInfo.gid, nextUid, nextGid, options, false)); + } else if (options.verbose) { + stdout(await chownVerboseLine(options.command, display, operationInfo.uid, operationInfo.gid, operationInfo.uid, operationInfo.gid, options, false)); + } + return ok; +} + +export function chownFailureVerboseLine(path, options) { + const kind = options.failureKind === "group" ? "group" : "ownership"; + const suffix = options.failureSpec == null || options.failureSpec === "" ? "" : ` to ${options.failureSpec}`; + return `failed to change ${kind} of ${chownQuotedName(path)}${suffix}\n`; +} + +export async function chownVerboseLine(command, path, beforeUid, beforeGid, afterUid, afterGid, options, changed) { + if (command === "chgrp") { + const before = await groupName(beforeGid); + const after = await groupName(afterGid); + return changed ? `changed group of ${chownQuotedName(path)} from ${before} to ${after}\n` : `group of ${chownQuotedName(path)} retained as ${after}\n`; + } + const before = await chownOwnershipDisplay(beforeUid, beforeGid, options); + const after = await chownOwnershipDisplay(afterUid, afterGid, options); + if (changed) return `changed ownership of ${chownQuotedName(path)}${before ? ` from ${before}` : ""}${after ? ` to ${after}` : ""}\n`; + return `ownership of ${chownQuotedName(path)} retained${after ? ` as ${after}` : ""}\n`; +} + +export async function chownOwnershipDisplay(uid, gid, options) { + const parts = []; + if (options.reportUid) parts.push(await userNameForUid(uid) ?? String(uid)); + if (options.reportGid) parts.push(await groupName(gid)); + return parts.join(":"); +} + +export function chownErrorLine(path, error) { + if (error?.preserveRoot) return preserveRootMessage("chown", error.path, error.rootPath); + if (error?.cannotDereference) return `chown: cannot dereference ${chownQuotedName(path)}: ${errnoMessage(error)}\n`; + if (isAccessError(error)) return `chown: cannot access ${chownQuotedName(path)}: ${errnoMessage(error)}\n`; + return `chown: changing ownership of ${chownQuotedName(path)}: ${errnoMessage(error)}\n`; +} + +export function chgrpErrorLine(path, error) { + if (error?.preserveRoot) return preserveRootMessage("chgrp", error.path, error.rootPath); + if (error?.cannotDereference) return `chgrp: cannot dereference ${chownQuotedName(path)}: ${errnoMessage(error)}\n`; + if (isAccessError(error)) return `chgrp: cannot access ${chownQuotedName(path)}: ${errnoMessage(error)}\n`; + return `chgrp: changing group of ${chownQuotedName(path)}: ${errnoMessage(error)}\n`; +} + +export function chownQuotedName(path) { + return shellEscapeLsName(pathDisplayName(path), true); +} diff --git a/src/shared/paths.js b/src/shared/paths.js new file mode 100644 index 0000000..9fdce56 --- /dev/null +++ b/src/shared/paths.js @@ -0,0 +1,92 @@ +import { lstat, readlink, realpath, stat } from "node:fs/promises"; +import { isAbsolute, join, normalize, basename as pathBasename, dirname as pathDirname, resolve } from "node:path"; +import { pathDisplayName, shellEscapeLsName } from "./common.js"; + +export function readlinkQuotedName(path) { + const name = pathDisplayName(path); + return name === "" ? "''" : shellEscapeLsName(name); +} + +export async function canonicalPath(path, opts = {}) { + if (opts.s || opts.strip || opts["no-symlinks"]) return lexicalAbsolute(path); + if (opts.L || opts.logical) return logicalCanonicalPath(path, opts); + if (opts.m || opts["canonicalize-missing"]) return canonicalizeMissing(path); + return physicalCanonicalPath(path, opts); +} + +export async function logicalCanonicalPath(path, opts = {}) { + const lexical = lexicalAbsolute(path); + if (opts.m || opts["canonicalize-missing"] || opts.E) return lexical; + if (opts.e || opts["canonicalize-existing"]) return realpath(lexical); + try { + return await realpath(lexical); + } catch (error) { + if (error.code !== "ENOENT") throw error; + return join(await realpath(pathDirname(lexical)), pathBasename(lexical)); + } +} + +export async function physicalCanonicalPath(path, opts = {}) { + const mustExist = opts.e || opts["canonicalize-existing"]; + const allowMissingFinal = !mustExist; + const allowMissingAny = opts.m || opts["canonicalize-missing"]; + const trailingSlash = /\/$/.test(path) && path !== "/"; + const absolute = isAbsolute(path) ? path : `${process.cwd().replace(/\/+$/, "")}/${path}`; + const parts = absolute.split("/").filter(Boolean); + let current = "/"; + let symlinkDepth = 0; + for (let i = 0; i < parts.length; i++) { + const part = parts[i]; + if (part === ".") continue; + if (part === "..") { + current = pathDirname(current.replace(/\/+$/, "") || "/"); + continue; + } + const candidate = join(current, part); + let s; + try { + s = await lstat(candidate); + } catch (error) { + if (allowMissingAny) return normalize(join(candidate, ...parts.slice(i + 1))); + if (allowMissingFinal && error.code === "ENOENT" && i === parts.length - 1) return candidate; + throw error; + } + if (s.isSymbolicLink()) { + if (++symlinkDepth > 40) { + const error = new Error("Too many levels of symbolic links"); + error.code = "ELOOP"; + throw error; + } + const target = await readlink(candidate); + const targetParts = target.split("/").filter(Boolean); + const rest = parts.slice(i + 1); + parts.splice(i, parts.length - i, ...targetParts, ...rest); + if (isAbsolute(target)) current = "/"; + i--; + } else { + current = candidate; + } + } + if (!allowMissingAny && trailingSlash && !(await stat(current).catch(() => null))?.isDirectory()) { + const error = new Error("Not a directory"); + error.code = "ENOTDIR"; + throw error; + } + return current; +} + +export async function canonicalizeMissing(path) { + return physicalCanonicalPath(path, { m: true }); +} + +export function readlinkErrorMessage(error) { + if (error?.code === "EINVAL") return "Invalid argument"; + if (error?.code === "ENOENT") return "No such file or directory"; + if (error?.code === "ENOTDIR") return "Not a directory"; + if (error?.code === "ELOOP") return "Too many levels of symbolic links"; + return error?.message || String(error); +} + +export function lexicalAbsolute(path) { + return normalize(isAbsolute(path) ? path : resolve(process.cwd(), path)); +} diff --git a/src/shared/process.js b/src/shared/process.js new file mode 100644 index 0000000..bc3e805 --- /dev/null +++ b/src/shared/process.js @@ -0,0 +1,123 @@ +import { ptr, read } from "bun:ffi"; +import { stat } from "node:fs/promises"; +import { cstr, libc, localeQuotedEscapedDiagnostic } from "./common.js"; +import { InvocationError } from "./diagnostics.js"; + +export const SIG_IGN = 1; + +export function normalizeInvocationLongOptionByPrefix(arg, longOptions) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (longOptions.includes(name)) return arg; + const matches = longOptions.filter((option) => option.startsWith(name)); + if (matches.length === 1) return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; + if (matches.length > 1) throw new InvocationError(`option '${arg}' is ambiguous; possibilities: ${matches.map((option) => `'--${option}'`).join(" ")}`); + return arg; +} + +export const SIGNAL_NAMES = ["EXIT", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "BUS", "FPE", "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM", "STKFLT", "CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", "URG", "XCPU", "XFSZ", "VTALRM", "PROF", "WINCH", "IO", "PWR", "SYS"]; + +export function signalNumberFromOperand(signal) { + const text = String(signal).replace(/^SIG/i, "").toUpperCase(); + const rtmin = text.match(/^RTMIN(?:\+(\d+))?$/); + if (rtmin) { + const signum = 34 + Number(rtmin[1] ?? 0); + return signum <= 64 ? signum : -1; + } + const rtmax = text.match(/^RTMAX(?:-(\d+))?$/); + if (rtmax) { + const signum = 64 - Number(rtmax[1] ?? 0); + return signum >= 34 ? signum : -1; + } + if (/^\d+$/.test(text)) { + const status = Number(text); + const signum = status >= 128 ? status & 127 : status; + // Linux leaves 32 and 33 reserved by its threading implementation. GNU + // kill accepts them while enumerating a numeric range, but has no name to + // print for either one. + return signum >= 0 && signum <= 64 ? signum : -1; + } + const index = SIGNAL_NAMES.indexOf(text); + return index; +} + +export function signalDisplayName(signum) { + if (signum === 34) return "RTMIN"; + if (signum === 64) return "RTMAX"; + if (signum > 34 && signum <= 49) return `RTMIN+${signum - 34}`; + if (signum >= 50 && signum < 64) return `RTMAX-${64 - signum}`; + return SIGNAL_NAMES[signum]; +} + +export async function isKnownUnexecutableCommand(command) { + if (!(command === "." || command === ".." || command.includes("/"))) return false; + try { + const s = await stat(command); + return s.isDirectory(); + } catch { + return false; + } +} + +export function parseDuration(value) { + const text = String(value); + const match = text.match(/^(\+?(?:(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?|0[xX](?:[0-9a-fA-F]+(?:\.[0-9a-fA-F]*)?|\.[0-9a-fA-F]+)(?:[pP][+-]?\d+)?|[iI][nN][fF](?:[iI][nN][iI][tT][yY])?))([smhd]?)$/); + if (!match) throw new InvocationError(`invalid time interval ${localeQuotedEscapedDiagnostic(value)}`, 125, true); + let amount = parseDurationNumber(match[1]); + if (Number.isNaN(amount) || amount < 0) throw new InvocationError(`invalid time interval ${localeQuotedEscapedDiagnostic(value)}`, 125, true); + if (amount === 0 && /[1-9]/.test(match[1]) && !/^0+(?:\.0*)?(?:[eE][+-]?\d+)?$/.test(match[1])) amount = Number.MIN_VALUE; + return amount * { "": 1000, s: 1000, m: 60000, h: 3600000, d: 86400000 }[match[2]]; +} + +export function parseDurationNumber(value) { + const text = String(value).replace(/^\+/, ""); + if (/^inf(?:inity)?$/i.test(text)) return Infinity; + if (/^0x/i.test(text)) return parseHexFloat(text); + return Number(value); +} + +export function parseHexFloat(value) { + const [, mantissa, exponentText] = value.match(/^0[xX]([0-9a-fA-F]*(?:\.[0-9a-fA-F]*)?|\.[0-9a-fA-F]+)(?:[pP]([+-]?\d+))?$/) ?? []; + if (mantissa == null) return NaN; + const [whole, fraction = ""] = mantissa.split("."); + let amount = Number.parseInt(whole || "0", 16); + for (let i = 0; i < fraction.length; i++) amount += Number.parseInt(fraction[i], 16) / 16 ** (i + 1); + return amount * 2 ** Number(exponentText ?? 0); +} + +export function commandSpawnErrorMessage(error) { + if (error?.code === "ENOENT" || /not found/i.test(error?.message || "")) return "No such file or directory"; + if (error?.code === "EACCES" || /permission denied/i.test(error?.message || "")) return "Permission denied"; + if (error?.code === "EISDIR" || /is a directory/i.test(error?.message || "")) return "Is a directory"; + return error?.message || String(error); +} + +export function execCommand(command, options = {}) { + const file = cstr(options.executable ?? command[0]); + const strings = [options.argv0 ?? command[0], ...command.slice(1)].map(cstr); + const argv = cStringVector(strings); + if (options.env) { + const environmentStrings = Object.entries(options.env).map(([name, value]) => cstr(`${name}=${value}`)); + const envp = cStringVector(environmentStrings); + libc.symbols.execve(file, ptr(argv), ptr(envp)); + } else { + libc.symbols.execvp(file, ptr(argv)); + } + const errnoPointer = libc.symbols.__errno_location(); + const errno = errnoPointer ? read.i32(errnoPointer, 0) : 0; + return { + errno, + message: errno === 2 ? "No such file or directory" : errno === 13 ? "Permission denied" : `execution failed (errno ${errno})`, + }; +} + +export function isDirectCommandInvocation() { + return globalThis[Symbol.for("bnu.cli")] === true || import.meta.main; +} + +export function cStringVector(strings) { + const vector = Buffer.alloc((strings.length + 1) * 8); + strings.forEach((value, index) => vector.writeBigUInt64LE(BigInt(ptr(value)), index * 8)); + return vector; +} diff --git a/src/shared/runtime.js b/src/shared/runtime.js new file mode 100644 index 0000000..4f894a0 --- /dev/null +++ b/src/shared/runtime.js @@ -0,0 +1,83 @@ +import { commandNames, loadCommand } from "./catalog.js"; +import { InvocationError, UsageError, VERSION, commandUsageHint, emittedDiagnosticHints, fail, gnulyCorrectDiagnostics, stderr, stdout } from "./diagnostics.js"; +import { showGenericHelp } from "./help.js"; + +export const USAGE_STATUS_2 = new Set(["dir", "ls", "printenv", "sort", "tty", "vdir"]); + +export const OUTPUT_ERROR_STATUS = new Map([ + ["[", 2], + ["chroot", 125], + ["dir", 2], + ["env", 125], + ["expr", 3], + ["ls", 2], + ["nice", 125], + ["nohup", 125], + ["printenv", 2], + ["runcon", 125], + ["sort", 2], + ["stdbuf", 125], + ["timeout", 125], + ["tty", 3], + ["vdir", 2], +]); + +export async function main(argv) { + emittedDiagnosticHints.clear(); + const [program, ...args] = argv; + if (!program || program === "--help") { + stdout(`Usage: bnu COMMAND [ARG]...\nImplemented commands: ${commandNames.join(", ")}\n`); + return 0; + } + if (program === "--version") { + stdout(`${VERSION}\n`); + return 0; + } + const command = await loadCommand(program); + if (!command) return fail("bnu", `unknown command '${program}'`); + return command(args); +} + +export async function executeCommand(program, command, metaOptionForCommand, args) { + emittedDiagnosticHints.clear(); + try { + const metaOption = metaOptionForCommand?.(args) ?? null; + if (metaOption === "--help") { + showGenericHelp(program); + return 0; + } + if (metaOption === "--version") { + stdout(`${VERSION}\n`); + return 0; + } + return await command(args); + } catch (error) { + if (error instanceof InvocationError) { + if (error.message) stderr(`${program}: ${error.message}\n`, true); + else if (!gnulyCorrectDiagnostics()) stderr(`Hint: ${commandUsageHint(program, "Add the required command or argument")}\n`); + if (error.showHelp) stderr(`Try '${program} --help' for more information.\n`); + return error.code; + } + if (error instanceof UsageError) { + stderr(`${program}: ${error.message}\n`, true); + if (error.showHelp) stderr(`Try '${program} --help' for more information.\n`); + return USAGE_STATUS_2.has(program) ? 2 : 1; + } + if (isOutputWriteError(error)) { + const status = OUTPUT_ERROR_STATUS.get(program) ?? (USAGE_STATUS_2.has(program) ? 2 : 1); + return fail(program, `write error: ${outputWriteErrorMessage(error)}`, status); + } + return fail(program, error.message || String(error), USAGE_STATUS_2.has(program) ? 2 : 1); + } +} + +export function isOutputWriteError(error) { + return error?.code === "ENOSPC" || error?.code === "EIO"; +} + +export function outputWriteErrorMessage(error) { + if (error?.code === "EPIPE") return "Broken pipe"; + if (error?.code === "ENOSPC") return "No space left on device"; + if (error?.code === "EIO") return "Input/output error"; + return error?.message || String(error); +} diff --git a/src/shared/system.js b/src/shared/system.js new file mode 100644 index 0000000..046bf25 --- /dev/null +++ b/src/shared/system.js @@ -0,0 +1,65 @@ +import { readFile } from "node:fs/promises"; +import { statSyncNoThrow } from "./common.js"; + +export async function readUtmpRecords(file) { + let bytes; + try { + bytes = await readFile(file); + } catch { + return []; + } + return parseUtmpRecords(bytes); +} + +export function nulTerminatedBufferString(bytes) { + const end = bytes.indexOf(0); + return bytes.subarray(0, end === -1 ? bytes.length : end).toString(); +} + +export function parseUtmpRecords(bytes) { + const recordSize = 384; + const records = []; + for (let offset = 0; offset + recordSize <= bytes.length; offset += recordSize) { + records.push({ + type: bytes.readInt16LE(offset), + pid: bytes.readInt32LE(offset + 4), + line: nulTerminatedBufferString(bytes.subarray(offset + 8, offset + 40)), + id: nulTerminatedBufferString(bytes.subarray(offset + 40, offset + 44)), + user: nulTerminatedBufferString(bytes.subarray(offset + 44, offset + 76)), + host: nulTerminatedBufferString(bytes.subarray(offset + 76, offset + 332)), + exitTermination: bytes.readInt16LE(offset + 332), + exitStatus: bytes.readInt16LE(offset + 334), + time: bytes.readInt32LE(offset + 340), + }); + } + return records; +} + +export async function filterWhoUsers(records, requireLiveLine = false) { + const users = records.filter((record) => record.type === 7 && record.user); + if (!requireLiveLine) return users; + const live = []; + for (const record of users) { + if (record.line && statSyncNoThrow(`/dev/${record.line}`)) live.push(record); + } + return live; +} + +export function whoBootRecord(records, allowFallback = false) { + const boots = records.filter((record) => record.time && record.type === 2); + if (boots.length || !allowFallback) return boots.at(-1) ?? null; + return records.filter((record) => record.time).sort((a, b) => a.time - b.time).at(-1) ?? null; +} + +export function formatWhoDate(seconds) { + const date = new Date(seconds * 1000); + const pad = (value) => String(value).padStart(2, "0"); + if (!whoUsesPosixDateFormat()) return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`; + const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; + return `${months[date.getMonth()]} ${String(date.getDate()).padStart(2)} ${pad(date.getHours())}:${pad(date.getMinutes())}`; +} + +export function whoUsesPosixDateFormat() { + const locale = process.env.LC_ALL || process.env.LC_TIME || process.env.LANG || ""; + return locale === "C" || locale === "POSIX"; +} diff --git a/src/shared/tabs.js b/src/shared/tabs.js new file mode 100644 index 0000000..bcf0479 --- /dev/null +++ b/src/shared/tabs.js @@ -0,0 +1,194 @@ +import { localeQuotedDiagnostic, localeQuotedEscapedDiagnostic } from "./common.js"; +import { UsageError } from "./diagnostics.js"; + +export const EXPAND_LONG_OPTIONS = ["tabs", "initial", "help", "version"]; + +export const UNEXPAND_LONG_OPTIONS = ["tabs", "all", "first-only", "help", "version"]; + +export function tabCommandMetaOption(program, args) { + const longOptions = program === "expand" ? EXPAND_LONG_OPTIONS : UNEXPAND_LONG_OPTIONS; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeTabCommandLongOption(arg, longOptions); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!longOptions.includes(name)) return null; + if (inlineValue != null) { + if (name !== "tabs") return null; + validateTabMetaOptionValue(program, inlineValue); + } + if (option === "--help" || option === "--version") return option; + if (name === "tabs" && inlineValue == null) { + validateTabMetaOptionValue(program, args[i + 1]); + i++; + } + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + if (/^-\d[\d,\s]*$/.test(arg)) continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (program === "expand" && ch === "i") continue; + if (program === "unexpand" && ch === "a") continue; + if (ch !== "t") return null; + const value = arg.slice(j + 1) || args[i + 1]; + validateTabMetaOptionValue(program, value); + if (!arg.slice(j + 1)) i++; + break; + } + } + return null; +} + +export function validateTabMetaOptionValue(program, value) { + try { + parseTabStops(value, program); + } catch (error) { + if (error instanceof UsageError && ["tab size cannot be 0", "tab sizes must be ascending"].includes(error.message)) return; + throw error; + } +} + +export function normalizeTabCommandLongOptions(args, longOptions) { + const out = []; + let end = false; + for (const arg of args) { + if (end || arg === "--" || !arg.startsWith("--")) { + out.push(arg); + if (arg === "--") end = true; + continue; + } + out.push(normalizeTabCommandLongOption(arg, longOptions)); + } + return out; +} + +export function normalizeTabCommandLongOption(arg, longOptions) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const optionNames = name === "tab" || "tabs".startsWith(name) ? ["tabs"] : longOptions.filter((option) => option.startsWith(name)); + if (optionNames.length !== 1) return arg; + return eq === -1 ? `--${optionNames[0]}` : `--${optionNames[0]}=${body.slice(eq + 1)}`; +} + +export function parseTabStops(value = "8", program = null) { + const text = String(value).replace(/^["']|["']$/g, "").trim(); + if (text === "" || text === "," || text === "/" || text === "+") return [8]; + const parts = text.split(/[,\t ]+/).filter(Boolean); + const diagnostics = []; + let interval = null; + let intervalMode = null; + let zeroInterval = false; + const numbers = []; + let previous = 0; + for (let index = 0; index < parts.length; index++) { + let part = parts[index]; + if (part === "/" || part === "+") continue; + if (part.includes("/") && !part.startsWith("/") && !part.startsWith("+/")) throw new UsageError("'/' specifier not at start of number: '/'"); + const matchedInterval = /^[/+]+(?=\/?\d)/.test(part); + if (matchedInterval && part.startsWith("/") && index !== parts.length - 1) throw new UsageError("'/' specifier only allowed with the last value"); + part = part.replace(/^[/+]+(?=\/?\d)/, (prefix) => { + intervalMode = prefix.includes("+") ? "+" : "/"; + return ""; + }).replace(/^\//, ""); + if (!/^\d+$/.test(part)) { + const leadingDigits = part.match(/^\d+/)?.[0] ?? ""; + if (leadingDigits !== "" && Number(leadingDigits) > Number.MAX_SAFE_INTEGER) diagnostics.push(`tab stop is too large ${localeQuotedDiagnostic(leadingDigits)}`); + const invalid = invalidTabSizeCharacters(part); + if (diagnostics.length) { + diagnostics.push(`tab size contains invalid character(s): ${localeQuotedEscapedDiagnostic(invalid)}`); + continue; + } + throw new UsageError(`tab size contains invalid character(s): ${localeQuotedEscapedDiagnostic(invalid)}`); + } + const n = Number(part); + if (n === 0) { + if (matchedInterval) { + zeroInterval = true; + continue; + } + throw new UsageError("tab size cannot be 0"); + } + if (n > Number.MAX_SAFE_INTEGER) { + diagnostics.push(`tab stop is too large ${localeQuotedDiagnostic(part)}`); + continue; + } + if (matchedInterval) interval = n; + else { + if (n <= previous) throw new UsageError("tab sizes must be ascending"); + numbers.push(n); + previous = n; + } + } + if (diagnostics.length) throw new UsageError(formatTabDiagnostics(diagnostics, program)); + const stops = numbers.length ? numbers : interval ? [interval] : zeroInterval ? [8] : []; + if (interval) { + stops.interval = interval; + stops.intervalMode = intervalMode; + } + if (!stops.length || stops.some((n) => !Number.isInteger(n) || n <= 0)) throw new UsageError(`invalid tab size: ${value}`); + return stops; +} + +export function formatTabDiagnostics(diagnostics, program = null) { + if (diagnostics.length <= 1 || !program) return diagnostics.join("\n"); + return diagnostics.map((message, index) => index === 0 ? message : `${program}: ${message}`).join("\n"); +} + +export function invalidTabSizeCharacters(part) { + const stripped = String(part).replace(/^[+/]?\d*/, "").replace(/^[+/]/, ""); + return stripped || String(part); +} + +export function normalizeTabArgs(args) { + const out = []; + const obsolete = []; + const shortTabs = []; + let scanning = true; + const requireValue = (index, option) => { + if (index + 1 < args.length) return args[index + 1]; + throw new UsageError(`option requires an argument -- '${option.slice(1)}'`, true); + }; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (scanning && /^-\d[\d,\s]*$/.test(arg)) obsolete.push(arg.slice(1)); + else if (arg === "-t") { + shortTabs.push(requireValue(i, arg)); + i++; + scanning = false; + } else if (/^-t.+/.test(arg)) { + shortTabs.push(arg.slice(2)); + scanning = false; + } + else if (arg === "-a" || arg === "--all" || arg === "--first-only" || arg === "-i" || arg === "--initial") { + out.push(arg); + } + else { + scanning = false; + out.push(arg); + } + } + const tabSpecs = [...obsolete, ...shortTabs]; + return tabSpecs.length ? ["-t", tabSpecs.join(","), ...out] : out; +} + +export function combinedTabSpec(longSpec, shortSpec) { + if (longSpec != null && shortSpec != null) return `${longSpec},${shortSpec}`; + return shortSpec ?? longSpec; +} + +export function nextTabColumn(column, stops) { + if (stops.length === 1 && !stops.interval) return column + (stops[0] - (column % stops[0])); + for (const stop of stops) if (stop > column) return stop; + if (stops.interval) { + if (stops.intervalMode === "+" && stops.length) { + const base = stops.at(-1); + return base + Math.ceil((column + 1 - base) / stops.interval) * stops.interval; + } + return column + (stops.interval - (column % stops.interval)); + } + return column + 1; +} diff --git a/src/shared/test-expression.js b/src/shared/test-expression.js new file mode 100644 index 0000000..36192db --- /dev/null +++ b/src/shared/test-expression.js @@ -0,0 +1,206 @@ +import { constants as fsConstants } from "node:fs"; +import { access, lstat, stat } from "node:fs/promises"; +import { fdStat, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic } from "./common.js"; +import { fail } from "./diagnostics.js"; + +export async function testExpressionStatus(args, program = "test") { + if (args[0] === "[") args = args.slice(1); + if (args.at(-1) === "]") args = args.slice(0, -1); + if (args.length === 0) return 1; + try { + const arity = await testByArity(args); + if (arity != null) return arity ? 0 : 1; + const parsed = await parseTestOr(args, 0); + if (parsed.index !== args.length) throw new TestExpressionError(`extra argument ${localeQuotedDiagnostic(args[parsed.index])}`); + return parsed.value ? 0 : 1; + } catch (error) { + if (error instanceof TestExpressionError) return fail(program, error.message, 2); + throw error; + } +} + +export class TestExpressionError extends Error {} + +export async function parseTestOr(args, index) { + let left = await parseTestAnd(args, index); + while (args[left.index] === "-o") { + const right = await parseTestAnd(args, left.index + 1); + left = { value: left.value || right.value, index: right.index }; + } + return left; +} + +export async function parseTestAnd(args, index) { + let left = await parseTestNot(args, index); + while (args[left.index] === "-a") { + const right = await parseTestNot(args, left.index + 1); + left = { value: left.value && right.value, index: right.index }; + } + return left; +} + +export async function parseTestNot(args, index) { + if (args[index] === "!") { + const value = await parseTestOr(args, index + 1); + return { value: !value.value, index: value.index }; + } + return parseTestPrimary(args, index); +} + +export async function testByArity(args) { + if (args.length === 1) return args[0].length > 0; + if (args.length === 2) { + if (args[0] === "!") return args[1].length === 0; + if (args[0] === "-o") throw new TestExpressionError(`${localeQuotedDiagnostic("-o")}: unary operator expected`); + if (isTestUnaryOperator(args[0])) return testUnaryExpression(args[0], args[1]); + return null; + } + if (args.length === 3) { + if (isTestBinaryOperator(args[1])) return testBinaryExpression(args[0], args[1], args[2]); + if (args[0] === "!") { + const value = await testByArity(args.slice(1)); + if (value != null) return !value; + } + if (args[0] === "(" && args[2] === ")") return args[1].length > 0; + return null; + } + if (args.length === 4) { + if (args[0] === "!") { + const value = await testByArity(args.slice(1)); + if (value != null) return !value; + } + if (args[0] === "(" && args[3] === ")") { + const value = await testByArity(args.slice(1, 3)); + if (value != null) return value; + } + } + return null; +} + +export async function parseTestPrimary(args, index) { + if (index >= args.length) throw new TestExpressionError("missing argument after operator"); + if (args[index] === "(") { + const close = args.at(-1) === ")" ? args.length - 1 : findTestClosingParen(args, index); + if (close !== -1) { + const arity = await testByArity(args.slice(index + 1, close)); + if (arity != null) return { value: arity, index: close + 1 }; + } + const value = await parseTestOr(args, index + 1); + if (args[value.index] !== ")") throw new TestExpressionError(`missing ${localeQuotedDiagnostic(")")}`); + return { value: value.value, index: value.index + 1 }; + } + if (args[index + 1] && isTestBinaryOperator(args[index + 1])) { + return { value: await testBinaryExpression(args[index], args[index + 1], args[index + 2]), index: index + 3 }; + } + if (isTestUnaryOperator(args[index])) { + if (args[index + 1] == null) return { value: String(args[index]).length > 0, index: index + 1 }; + return { value: await testUnaryExpression(args[index], args[index + 1]), index: index + 2 }; + } + if (args[index] === ")") throw new TestExpressionError(`unexpected ${localeQuotedDiagnostic(")")}`); + return { value: String(args[index]).length > 0, index: index + 1 }; +} + +export function findTestClosingParen(args, index) { + let depth = 0; + for (let i = index; i < args.length; i++) { + if (args[i] === "(") depth++; + else if (args[i] === ")") { + depth--; + if (depth === 0) return i; + } + } + return -1; +} + +export function isTestUnaryOperator(op) { + return ["-a", "-b", "-c", "-d", "-e", "-f", "-g", "-G", "-h", "-k", "-L", "-N", "-n", "-O", "-p", "-r", "-S", "-s", "-t", "-u", "-w", "-x", "-z"].includes(op); +} + +export function isTestBinaryOperator(op) { + return ["=", "==", "!=", "<", ">", "-eq", "-ne", "-lt", "-le", "-gt", "-ge", "-ef", "-nt", "-ot"].includes(op); +} + +export async function testUnaryExpression(op, value) { + if (op === "-a" || op === "-e") return await access(value).then(() => true, () => false); + if (op === "-n") return value.length > 0; + if (op === "-z") return value.length === 0; + if (op === "-t") return testFdIsTerminal(value); + return await testUnaryFile(op, value) === 0; +} + +export async function testBinaryExpression(a, op, b) { + if (b == null) throw new TestExpressionError(`missing argument after ${localeQuotedDiagnostic(op)}`); + if (op === "=" || op === "==") return a === b; + if (op === "!=") return a !== b; + if (op === "<") return a < b; + if (op === ">") return a > b; + if (["-eq", "-ne", "-lt", "-le", "-gt", "-ge"].includes(op)) { + const left = parseTestInteger(a); + const right = parseTestInteger(b); + return ({ "-eq": left === right, "-ne": left !== right, "-lt": left < right, "-le": left <= right, "-gt": left > right, "-ge": left >= right })[op]; + } + if (["-ef", "-nt", "-ot"].includes(op)) return await testBinaryFile(a, op, b) === 0; + throw new TestExpressionError(`${localeQuotedDiagnostic(op)}: binary operator expected`); +} + +export function parseTestInteger(value) { + const text = String(value); + if (!/^\s*[+-]?\d+\s*$/.test(text)) throw new TestExpressionError(`invalid integer ${localeQuotedEscapedDiagnostic(text)}`); + return BigInt(text.trim()); +} + +export function testFdIsTerminal(value) { + const fd = Number(value); + if (!Number.isInteger(fd) || fd < 0) return false; + const s = fdStat(fd); + return Boolean(s?.isCharacterDevice()); +} + +export async function testUnaryFile(op, path) { + try { + if (op === "-r") return await access(path, fsConstants.R_OK).then(() => 0, () => 1); + if (op === "-w") return await access(path, fsConstants.W_OK).then(() => 0, () => 1); + if (op === "-x") return await access(path, fsConstants.X_OK).then(() => 0, () => 1); + if (op === "-N") { + const s = await stat(path, { bigint: true }); + return testStatTimeNs(s, "mtime") > testStatTimeNs(s, "atime") ? 0 : 1; + } + const s = op === "-h" || op === "-L" ? await lstat(path) : await stat(path); + const mode = s.mode; + if (op === "-b") return s.isBlockDevice() ? 0 : 1; + if (op === "-c") return s.isCharacterDevice() ? 0 : 1; + if (op === "-d") return s.isDirectory() ? 0 : 1; + if (op === "-f") return s.isFile() ? 0 : 1; + if (op === "-g") return (mode & 0o2000) ? 0 : 1; + if (op === "-G") return process.getgid && s.gid === process.getgid() ? 0 : 1; + if (op === "-h" || op === "-L") return s.isSymbolicLink() ? 0 : 1; + if (op === "-k") return (mode & 0o1000) ? 0 : 1; + if (op === "-O") return process.getuid && s.uid === process.getuid() ? 0 : 1; + if (op === "-p") return s.isFIFO() ? 0 : 1; + if (op === "-S") return s.isSocket() ? 0 : 1; + if (op === "-s") return s.size > 0 ? 0 : 1; + if (op === "-u") return (mode & 0o4000) ? 0 : 1; + return 1; + } catch { + return 1; + } +} + +export async function testBinaryFile(left, op, right) { + const [a, b] = await Promise.all([stat(left, { bigint: true }).catch(() => null), stat(right, { bigint: true }).catch(() => null)]); + if (!a || !b) { + if (op === "-nt") return a && !b ? 0 : 1; + if (op === "-ot") return !a && b ? 0 : 1; + return 1; + } + if (op === "-ef") return a.dev === b.dev && a.ino === b.ino ? 0 : 1; + if (op === "-nt") return testStatTimeNs(a, "mtime") > testStatTimeNs(b, "mtime") ? 0 : 1; + if (op === "-ot") return testStatTimeNs(a, "mtime") < testStatTimeNs(b, "mtime") ? 0 : 1; + return 1; +} + +export function testStatTimeNs(statInfo, name) { + const value = statInfo[`${name}Ns`]; + if (value != null) return BigInt(value); + return BigInt(statInfo[`${name}Ms`]) * 1_000_000n; +} diff --git a/src/shared/text.js b/src/shared/text.js new file mode 100644 index 0000000..9c62bad --- /dev/null +++ b/src/shared/text.js @@ -0,0 +1,165 @@ +import { fstatSync, readSync, statSync } from "node:fs"; +import { mkdtemp, open, rm, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { SEEK_CUR, displayWidth, libc, pathDisplayName, shellEscapeLsName } from "./common.js"; +import { stdout } from "./diagnostics.js"; + +export const POLLOUT = 0x004; + +export const POLLERR = 0x008; + +export const POLLHUP = 0x010; + +export const POLLNVAL = 0x020; + +export async function spoolInputToTemporaryFile(input, prefix, initialChunks = []) { + const base = process.env.TMPDIR || "/tmp"; + const dir = await mkdtemp(join(base, `${prefix}-`)); + const path = join(dir, "data"); + const source = input === "-" ? null : await open(input, "r"); + let target; + try { + target = await open(path, "wx", 0o600); + const buffer = Buffer.allocUnsafe(64 * 1024); + const writeChunk = async (chunk) => { + let offset = 0; + while (offset < chunk.length) { + const { bytesWritten } = await target.write(chunk, offset, chunk.length - offset, null); + if (bytesWritten === 0) throw new Error("short write while spooling input"); + offset += bytesWritten; + } + }; + for (const chunk of initialChunks) await writeChunk(chunk); + while (true) { + const n = readSync(source?.fd ?? 0, buffer, 0, buffer.length, null); + if (n === 0) break; + await writeChunk(buffer.subarray(0, n)); + } + await target.close(); + target = null; + return { path, cleanup: () => rm(dir, { recursive: true, force: true }) }; + } catch (error) { + await target?.close().catch(() => {}); + await rm(dir, { recursive: true, force: true }).catch(() => {}); + throw error; + } finally { + await source?.close().catch(() => {}); + } +} + +export function readStdinChunks(accept) { + const buffer = Buffer.allocUnsafe(64 * 1024); + while (true) { + const n = readSync(0, buffer, 0, buffer.length, null); + if (n === 0) break; + accept(Buffer.from(buffer.subarray(0, n))); + } +} + +export async function writeAll(path, data, append = false) { + if (path === "-") return stdout(data); + await writeFile(path, data, append ? { flag: "a" } : undefined); +} + +export function isSingleByteLocale() { + return /^(C|POSIX)$/.test(process.env.LC_ALL ?? process.env.LC_CTYPE ?? process.env.LANG ?? ""); +} + +export function concatOutputParts(parts) { + const chunks = parts.map((part) => part instanceof Uint8Array ? Buffer.from(part) : Buffer.from(String(part))); + return chunks.length ? Buffer.concat(chunks) : ""; +} + +export function tabFoldDiagnosticName(file) { + return shellEscapeLsName(pathDisplayName(file)); +} + +export function nextUtf8Token(bytes, index) { + const b0 = bytes[index]; + if (b0 < 0x80) return { bytes: bytes.slice(index, index + 1), char: String.fromCharCode(b0), width: b0 === 0x09 ? 0 : 1, next: index + 1 }; + const len = b0 >= 0xc2 && b0 <= 0xdf ? 2 : b0 >= 0xe0 && b0 <= 0xef ? 3 : b0 >= 0xf0 && b0 <= 0xf4 ? 4 : 1; + if (len === 1 || index + len > bytes.length) return { bytes: bytes.slice(index, index + 1), char: null, width: 1, next: index + 1 }; + for (let i = 1; i < len; i++) { + if ((bytes[index + i] & 0xc0) !== 0x80) return { bytes: bytes.slice(index, index + 1), char: null, width: 1, next: index + 1 }; + } + const slice = bytes.slice(index, index + len); + const char = new TextDecoder("utf-8", { ignoreBOM: true }).decode(slice); + return { bytes: slice, char, width: displayWidth(char), next: index + len }; +} + +export function tacRegexPattern(sep) { + let out = ""; + let lastAtomKind = null; + for (let i = 0; i < sep.length; i++) { + const ch = sep[i]; + if (ch === "\\") { + const next = sep[++i]; + if (next == null) out += ch; + else if ("()|".includes(next)) out += next; + else if ("+?{}".includes(next)) out += `\\${next}`; + else out += ch + next; + lastAtomKind = "literal"; + } else if (ch === "[") { + const end = sep.indexOf("]", i + 1); + if (end === -1) { + out += ch; + lastAtomKind = null; + } else { + out += sep.slice(i, end + 1); + i = end; + lastAtomKind = "bracket"; + } + } else if (ch === "+" || ch === "?") { + if (!tacRegexHasPreviousAtom(out)) { + out += `\\${ch}`; + lastAtomKind = "literal"; + } else if (lastAtomKind !== "literal" && lastAtomKind !== "bracket") { + out += ch; + } + } else if ("()|{}+?".includes(ch)) { + out += `\\${ch}`; + lastAtomKind = "literal"; + } else { + out += ch; + lastAtomKind = ch === "." || ch === "_" ? "repeatable" : "literal"; + } + } + return out; +} + +export function tacRegexHasPreviousAtom(pattern) { + if (pattern === "" || pattern.endsWith("|") || pattern.endsWith("(")) return false; + return true; +} + +export function splitSeparator(value) { + return value === "\\0" ? "\0" : value; +} + +export function decodeValidUtf8(bytes) { + try { + return new TextDecoder("utf-8", { fatal: true }).decode(bytes); + } catch { + return null; + } +} + +export function prInputIsNonRegular(file) { + try { + return !(file === "-" ? fstatSync(0) : statSync(file)).isFile(); + } catch { + return false; + } +} + +export function fdIsSeekable(fd) { + return libc.symbols.lseek(fd, 0n, SEEK_CUR) >= 0; +} + +export function pollFd(fd, events, timeout = 0) { + const pollfd = Buffer.alloc(8); + pollfd.writeInt32LE(fd, 0); + pollfd.writeInt16LE(events, 4); + if (libc.symbols.poll(pollfd, 1n, timeout) <= 0) return 0; + return pollfd.readInt16LE(6); +} diff --git a/src/shared/time.js b/src/shared/time.js new file mode 100644 index 0000000..193fdd0 --- /dev/null +++ b/src/shared/time.js @@ -0,0 +1,779 @@ +import { attachDateNanoseconds, floorDivBigInt } from "./common.js"; +import { parseGNUBlockSizeEnvInfo } from "./filesystem.js"; + +export function dateParts(date, utc = false) { + const pad2 = (n) => String(n).padStart(2, "0"); + const tz = utc ? { offsetSeconds: 0, name: "UTC" } : parsePosixTZ(date); + if (tz) { + const shifted = new Date(date.getTime() + tz.offsetSeconds * 1000); + const year = shifted.getUTCFullYear(); + const monthIndex = shifted.getUTCMonth(); + const dayOfMonth = shifted.getUTCDate(); + const weekDay = shifted.getUTCDay(); + const hours = shifted.getUTCHours(); + const minutes = shifted.getUTCMinutes(); + const seconds = shifted.getUTCSeconds(); + const milliseconds = shifted.getUTCMilliseconds(); + const day = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][weekDay]; + const month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][monthIndex]; + const zoneText = formatZoneOffset(tz.offsetSeconds); + const ydayStart = Date.UTC(year, 0, 1); + const ydayNow = Date.UTC(year, monthIndex, dayOfMonth); + return { date, utc, pad2, year, monthIndex, dayOfMonth, weekDay, hours, minutes, seconds, milliseconds, nanoseconds: date.__bnuNanoseconds, day, shortDay: day.slice(0, 3), month, shortMonth: month.slice(0, 3), zoneText, zoneName: tz.name, yday: Math.floor((ydayNow - ydayStart) / 86400000) + 1 }; + } + const get = (local, universal) => utc ? date[universal]() : date[local](); + const year = get("getFullYear", "getUTCFullYear"); + const monthIndex = get("getMonth", "getUTCMonth"); + const dayOfMonth = get("getDate", "getUTCDate"); + const weekDay = get("getDay", "getUTCDay"); + const hours = get("getHours", "getUTCHours"); + const minutes = get("getMinutes", "getUTCMinutes"); + const seconds = get("getSeconds", "getUTCSeconds"); + const milliseconds = get("getMilliseconds", "getUTCMilliseconds"); + const day = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][weekDay]; + const shortDay = day.slice(0, 3); + const month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][monthIndex]; + const shortMonth = month.slice(0, 3); + const zone = (utc ? 0 : -date.getTimezoneOffset()) * 60; + const zoneText = formatZoneOffset(zone); + const ydayStart = utc ? Date.UTC(year, 0, 1) : new Date(year, 0, 1).getTime(); + const ydayNow = utc ? Date.UTC(year, monthIndex, dayOfMonth) : new Date(year, monthIndex, dayOfMonth).getTime(); + return { date, utc, pad2, year, monthIndex, dayOfMonth, weekDay, hours, minutes, seconds, milliseconds, nanoseconds: date.__bnuNanoseconds, day, shortDay, month, shortMonth, zoneText, zoneName: utc ? "UTC" : localZoneName(date), yday: Math.floor((ydayNow - ydayStart) / 86400000) + 1 }; +} + +export function dateLocaleCalendar(parts) { + const locale = process.env.LC_ALL || process.env.LC_TIME || process.env.LANG || ""; + if (/^am_ET(?:[.@_]|$)/i.test(locale)) { + const afterNewYear = parts.monthIndex > 8 || (parts.monthIndex === 8 && parts.dayOfMonth >= 11); + const year = parts.year - (afterNewYear ? 7 : 8); + return { year }; + } + if (/^fa_IR(?:[.@_]|$)/i.test(locale)) { + const afterNewYear = parts.monthIndex > 2 || (parts.monthIndex === 2 && parts.dayOfMonth >= 21); + const year = parts.year - (afterNewYear ? 621 : 622); + return { year }; + } + if (/^th_TH(?:[.@_]|$)/i.test(locale)) { + const months = ["มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม"]; + const month = months[parts.monthIndex]; + return { year: parts.year + 543, month, shortMonth: month }; + } + return null; +} + +export function localZoneName(date) { + const tz = process.env.TZ || ""; + if (/^UTC/i.test(tz)) return "UTC"; + if (/^PST8PDT/.test(tz)) return date.getMonth() >= 2 && date.getMonth() <= 10 ? "PDT" : "PST"; + if (/^PST8/.test(tz)) return "PST"; + const resolved = Intl.DateTimeFormat().resolvedOptions().timeZone || ""; + if (tz.includes("/") || resolved.includes("/")) { + const abbreviation = localRuntimeZoneAbbreviation(date); + if (abbreviation) return abbreviation; + } + const match = tz.match(/^([A-Za-z]{3,})/); + return match?.[1] || resolved || ""; +} + +export function localRuntimeZoneAbbreviation(date) { + const tz = process.env.TZ || Intl.DateTimeFormat().resolvedOptions().timeZone || ""; + const locales = []; + if (/^Australia\//.test(tz)) locales.push("en-AU", "en-CA"); + if (/^Europe\//.test(tz)) locales.push("en-GB"); + if (/^Pacific\//.test(tz)) locales.push("en-NZ", "en-AU"); + if (/^America\//.test(tz)) locales.push("en-US", "en-CA"); + locales.push("en-US", "en-GB", "en-AU", "en-NZ", "en-CA"); + for (const locale of [...new Set(locales)]) { + const value = new Intl.DateTimeFormat(locale, { timeZoneName: "short" }) + .formatToParts(date) + .find((part) => part.type === "timeZoneName")?.value; + if (value && !/^(?:GMT|UTC)[+-]\d/.test(value)) return value; + } + const match = String(date).match(/\(([^)]+)\)$/); + if (!match) return null; + const words = match[1].split(/\s+/).filter(Boolean); + if (/European Standard Time$/i.test(match[1])) { + return words.filter((word) => !/^Standard$/i.test(word)).map((word) => word[0]).join("").toUpperCase() || null; + } + return words.map((word) => word[0]).join("").toUpperCase() || null; +} + +export function parsePosixTZ(date) { + return parsePosixTZText(process.env.TZ || "", date); +} + +export function parsePosixTZText(tz, date = new Date()) { + const bareZones = { + UTC: { offsetSeconds: 0, name: "UTC" }, + GMT: { offsetSeconds: 0, name: "GMT" }, + Zulu: { offsetSeconds: 0, name: "Zulu" }, + CET: { offsetSeconds: 3600, name: "CET" }, + EST: { offsetSeconds: -5 * 3600, name: "EST" }, + }; + if (bareZones[tz]) return bareZones[tz]; + if (/^UTC0$/i.test(tz)) return { offsetSeconds: 0, name: "UTC" }; + if (/^PST8$/.test(tz)) return { offsetSeconds: -8 * 3600, name: "PST" }; + if (/^PST8PDT/.test(tz)) return { offsetSeconds: -7 * 3600, name: "PDT" }; + const match = tz.match(/^([A-Za-z]{3,})([+-])?(\d{1,2})(?::(\d{2}))?(?::(\d{2}))?$/); + if (!match) return null; + const [, name, sign = "+", hours, minutes = "0", seconds = "0"] = match; + const raw = Number(hours) * 3600 + Number(minutes) * 60 + Number(seconds); + return { offsetSeconds: sign === "-" ? raw : -raw, name }; +} + +export function formatZoneOffset(offsetSeconds) { + const pad2 = (n) => String(n).padStart(2, "0"); + const sign = offsetSeconds >= 0 ? "+" : "-"; + const abs = Math.abs(offsetSeconds); + const hh = Math.trunc(abs / 3600); + const mm = Math.trunc(abs / 60) % 60; + const ss = abs % 60; + return `${sign}${pad2(hh)}${pad2(mm)}${pad2(ss)}`; +} + +export function isoWeek(date, utc = false) { + const d = new Date(date.getTime()); + const getDay = utc ? "getUTCDay" : "getDay"; + const getFullYear = utc ? "getUTCFullYear" : "getFullYear"; + const getMonth = utc ? "getUTCMonth" : "getMonth"; + const getDate = utc ? "getUTCDate" : "getDate"; + const setDate = utc ? "setUTCDate" : "setDate"; + const weekDay = d[getDay]() || 7; + d[setDate](d[getDate]() + 4 - weekDay); + const weekYear = d[getFullYear](); + const yearStart = utc ? new Date(Date.UTC(weekYear, 0, 1)) : new Date(weekYear, 0, 1); + const week = Math.ceil((((d - yearStart) / 86400000) + 1) / 7); + return { week, year: weekYear }; +} + +export function formatZoneColon(zoneText, precision) { + const sign = zoneText[0]; + const hh = zoneText.slice(1, 3); + const mm = zoneText.slice(3, 5); + const ss = zoneText.slice(5, 7) || "00"; + if (precision === 1) return `${sign}${hh}:${mm}`; + if (precision === 2) return `${sign}${hh}:${mm}:${ss}`; + if (ss !== "00") return `${sign}${hh}:${mm}:${ss}`; + return mm === "00" ? `${sign}${hh}` : `${sign}${hh}:${mm}`; +} + +export function strftime(date, format, utc = false, options = {}) { + const p = dateParts(date, utc); + const calendar = options.gregorian ? null : dateLocaleCalendar(p); + const displayYear = calendar?.year ?? p.year; + const displayMonth = calendar?.month ?? p.month; + const displayShortMonth = calendar?.shortMonth ?? p.shortMonth; + const iso = isoWeek(date, utc); + const expand = (spec) => ({ + "%": "%", + ":z": formatZoneColon(p.zoneText, 1), + "::z": formatZoneColon(p.zoneText, 2), + ":::z": formatZoneColon(p.zoneText, 3), + a: p.shortDay, + A: p.day, + b: displayShortMonth, + B: displayMonth, + C: String(Math.trunc(displayYear / 100)).padStart(2, "0"), + c: `${p.shortDay} ${displayShortMonth} ${String(p.dayOfMonth).padStart(2, " ")} ${p.pad2(p.hours)}:${p.pad2(p.minutes)}:${p.pad2(p.seconds)} ${displayYear}`, + d: p.pad2(p.dayOfMonth), + D: `${p.pad2(p.monthIndex + 1)}/${p.pad2(p.dayOfMonth)}/${p.pad2(displayYear % 100)}`, + e: String(p.dayOfMonth).padStart(2, " "), + F: `${displayYear}-${p.pad2(p.monthIndex + 1)}-${p.pad2(p.dayOfMonth)}`, + g: p.pad2(iso.year % 100), + G: String(iso.year), + h: displayShortMonth, + H: p.pad2(p.hours), + I: p.pad2((p.hours % 12) || 12), + j: String(p.yday).padStart(3, "0"), + k: String(p.hours).padStart(2, " "), + l: String((p.hours % 12) || 12).padStart(2, " "), + m: p.pad2(p.monthIndex + 1), + M: p.pad2(p.minutes), + n: "\n", + N: String(p.nanoseconds ?? p.milliseconds * 1_000_000).padStart(9, "0"), + p: p.hours < 12 ? "AM" : "PM", + P: p.hours < 12 ? "am" : "pm", + q: String(Math.floor(p.monthIndex / 3) + 1), + r: `${p.pad2((p.hours % 12) || 12)}:${p.pad2(p.minutes)}:${p.pad2(p.seconds)} ${p.hours < 12 ? "AM" : "PM"}`, + R: `${p.pad2(p.hours)}:${p.pad2(p.minutes)}`, + s: String(Math.floor(date.getTime() / 1000)), + S: p.pad2(p.seconds), + t: "\t", + T: `${p.pad2(p.hours)}:${p.pad2(p.minutes)}:${p.pad2(p.seconds)}`, + u: String(p.weekDay || 7), + U: weekNumber(date, utc, 0), + V: p.pad2(iso.week), + w: String(p.weekDay), + W: weekNumber(date, utc, 1), + x: `${p.pad2(p.monthIndex + 1)}/${p.pad2(p.dayOfMonth)}/${p.pad2(p.year % 100)}`, + X: `${p.pad2(p.hours)}:${p.pad2(p.minutes)}:${p.pad2(p.seconds)}`, + y: p.pad2(displayYear % 100), + Y: String(displayYear), + z: p.zoneText.slice(0, 5), + Z: p.zoneName, + })[spec]; + return format.replace(/%([-_0^#+]*)(\d+)?([EO](?::){1,3}z|[EO].|(?::){1,3}z|.)/gs, (match, flags, widthText, rawSpec) => { + const spec = /^[EO]./s.test(rawSpec) ? rawSpec.slice(1) : rawSpec; + if (rawSpec === "%" && (flags || widthText != null)) return formatDateUnknownDirective(match, flags, widthText == null ? null : Number(widthText)); + if (spec === "N") return formatDateNanoseconds(p, flags, widthText == null ? null : Number(widthText)); + const value = expand(spec); + if (value == null) return formatDateUnknownDirective(match, flags, widthText == null ? null : Number(widthText)); + return applyDateFormatFlags(value, flags, widthText == null ? null : Number(widthText), spec); + }); +} + +export function formatDateUnknownDirective(token, flags = "", width = null) { + if (flags.includes("-")) return token; + const targetWidth = token.endsWith("%") ? width + 1 : width; + if (width == null || !Number.isFinite(width) || token.length >= targetWidth) return token; + const pad = flags.includes("0") && !flags.includes("-") ? "0" : " "; + return token.padStart(targetWidth, pad); +} + +export function formatDateNanoseconds(parts, flags = "", width = null) { + const value = String(parts.nanoseconds ?? parts.milliseconds * 1_000_000).padStart(9, "0"); + if (width == null || !Number.isFinite(width)) return value; + if (width <= value.length) return value.slice(0, Math.max(0, width)); + if (flags.includes("-")) return value; + const pad = flags.includes("_") && !flags.includes("0") ? " " : "0"; + return value.padEnd(width, pad); +} + +export function weekNumber(date, utc, firstDay) { + const p = dateParts(date, utc); + const yearStart = utc ? new Date(Date.UTC(p.year, 0, 1)) : new Date(p.year, 0, 1); + const startWeekday = utc ? yearStart.getUTCDay() : yearStart.getDay(); + const firstWeekStart = (7 + firstDay - startWeekday) % 7; + const yday = p.yday - 1; + const week = yday < firstWeekStart ? 0 : Math.floor((yday - firstWeekStart) / 7) + 1; + return String(week).padStart(2, "0"); +} + +export function applyDateFormatFlags(value, flags = "", width = null, spec = "") { + let out = String(value); + const defaultWidth = out.length; + if (flags.includes("^") && spec !== "P") out = out.toUpperCase(); + if (flags.includes("#")) { + if ("aAbBh".includes(spec)) out = out.toUpperCase(); + else if (spec === "p" || spec === "P") out = out.toLowerCase(); + } + if (flags.includes("-") && !flags.includes("_") && !flags.includes("0")) return stripDateNumericPadding(out); + if (flags.includes("+") && "yCY".includes(spec) && width != null && width > defaultWidth && /^[+-]?\d+$/.test(out) && !out.startsWith("-") && !out.startsWith("+")) out = `+${out}`; + if (flags.includes("_") && !flags.includes("0")) out = stripDateNumericPadding(out); + if (flags.includes("0")) out = out.replace(/^ +(?=\d)/, (spaces) => "0".repeat(spaces.length)); + const targetWidth = width ?? defaultWidth; + if (!Number.isFinite(targetWidth) || targetWidth <= out.length) return out; + const pad = flags.includes("_") && !flags.includes("0") || !dateFieldDefaultsToZeroPadding(spec, out) ? " " : "0"; + return padDateField(out, targetWidth, pad, pad === "0" && datePadAfterSign(spec, out)); +} + +export function dateFieldDefaultsToZeroPadding(spec, value) { + return /^[+-]?\d+$/.test(value) || spec.endsWith("z"); +} + +export function stripDateNumericPadding(value) { + if (/^-?0+\d+$/.test(value)) return value.replace(/^(-?)0+(\d.*)$/, "$1$2"); + if (/^ +\d+$/.test(value)) return value.trimStart(); + return value; +} + +export function datePadAfterSign(spec, value) { + return /^[+-]/.test(value) && (spec === "s" || spec === "y" || spec === "Y" || spec === "C" || spec.endsWith("z")); +} + +export function padDateField(value, width, pad, afterSign = false) { + if (!afterSign || value.length >= width || !/^[+-]/.test(value)) return value.padStart(width, pad); + return `${value[0]}${value.slice(1).padStart(width - 1, pad)}`; +} + +export function parseDateInput(input, utc = false, allowRelative = true) { + input = normalizeDateInput(stripDateComments(String(input))).trim(); + input = input.replace(/\s+UTC$/i, "Z"); + if (/^TZ=(?!")\S+(?:\s|$)/.test(input)) return new Date(Number.NaN); + if (input === "") return todayAt(0, 0, 0, 0, utc); + const european = parseEuropeanDateInput(input, utc); + if (european) return european; + const monthName = parseMonthNameDateInput(input, utc); + if (monthName) return monthName; + const compact = parseCompactDateInput(input, utc); + if (compact) return compact; + const hourOnly = parseHourOnlyDateInput(input, utc); + if (hourOnly) return hourOnly; + const explicitTimezone = parseExplicitTimezoneDateInput(input); + if (explicitTimezone) return explicitTimezone; + const namedZoneTime = parseNamedZoneTimeInput(input); + if (namedZoneTime) return namedZoneTime; + const militaryTime = parseMilitaryTimeZoneInput(input); + if (militaryTime) return militaryTime; + const militaryZoneDate = parseMilitaryZoneDateInput(input); + if (militaryZoneDate) return militaryZoneDate; + const slashMilitaryZoneDate = parseSlashMilitaryZoneDateInput(input); + if (slashMilitaryZoneDate) return slashMilitaryZoneDate; + const localZoneAbbrev = parseLocalZoneAbbreviationDateInput(input, utc); + if (localZoneAbbrev) return localZoneAbbrev; + const absolute = parseAbsoluteDateInput(input, utc); + if (absolute) return absolute; + const epoch = input.match(/^@(-?\d+)(?:\.(\d+))?$/); + if (epoch) { + const [, secondsText, fractionText = ""] = epoch; + const nanoseconds = dateFractionNanoseconds(fractionText); + return attachDateNanoseconds(new Date(Number(`${secondsText}.${fractionText || "0"}`) * 1000), nanoseconds); + } + if (String(input).trim().toLowerCase() === "now") return new Date(); + if (String(input).trim().toLowerCase() === "yesterday") return new Date(Date.now() - 86400 * 1000); + if (String(input).trim().toLowerCase() === "tomorrow") return new Date(Date.now() + 86400 * 1000); + const weekday = allowRelative ? parseWeekdayDateSpec(input, utc) : null; + if (weekday) return weekday; + const timeTodayRelative = allowRelative ? parseTimeTodayRelativeDateSpec(input) : null; + if (timeTodayRelative) return timeTodayRelative; + const absoluteRelative = allowRelative ? parseAbsoluteRelativeDateSpec(input, utc) : null; + if (absoluteRelative) return absoluteRelative; + const relative = allowRelative ? parseRelativeDateSpec(input, new Date(), utc) : null; + if (relative) return relative; + const timeZoneOnly = String(input).match(/^(\d{1,2}):(\d{2})(?::(\d{2}))?\s+([+-])(\d{2})(\d{2})$/); + if (timeZoneOnly) { + const [, hhText, mmText, ssText, sign, zoneHoursText, zoneMinutesText] = timeZoneOnly; + const now = new Date(); + const hours = Number(hhText); + const minutes = Number(mmText); + const seconds = Number(ssText ?? "0"); + const offsetMinutes = (Number(zoneHoursText) * 60 + Number(zoneMinutesText)) * (sign === "+" ? 1 : -1); + if (hours > 23 || minutes > 59 || seconds > 60) return new Date(Number.NaN); + return new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), hours, minutes, Math.min(seconds, 59)) - offsetMinutes * 60000 + (seconds === 60 ? 1000 : 0)); + } + return parseFractionalDateInput(input); +} + +export function parseWeekdayDateSpec(spec, utc = false) { + const match = String(spec).trim().match(/^(?:(this|next|last)\s+)?(sun(?:day)?|mon(?:day)?|tue(?:sday)?|wed(?:nesday)?|thu(?:rsday)?|fri(?:day)?|sat(?:urday)?)$/i); + if (!match) return null; + const [, direction = "", dayText] = match; + const target = weekdayIndex(dayText); + const nowParts = dateParts(new Date(), utc); + const base = zonedDate(nowParts.year, nowParts.monthIndex + 1, nowParts.dayOfMonth, 0, 0, 0, 0, utc); + const forward = (target - nowParts.weekDay + 7) % 7; + if (direction.toLowerCase() === "next") return applyDateRelative(base, forward || 7, "day", String(forward || 7), 1, utc); + if (direction.toLowerCase() === "last") { + const backward = (nowParts.weekDay - target + 7) % 7 || 7; + return applyDateRelative(base, -backward, "day", String(backward), -1, utc); + } + return applyDateRelative(base, forward, "day", String(forward), 1, utc); +} + +export function weekdayIndex(dayText) { + return ["sun", "mon", "tue", "wed", "thu", "fri", "sat"].indexOf(String(dayText).slice(0, 3).toLowerCase()); +} + +export function parseAbsoluteDateInput(input, utc = false, timezone = null) { + const match = String(input).match(/^(\d{4})-(\d{1,2})-(\d{1,2})(?:[ T](\d{1,2}):(\d{2})(?::(\d{2})([.,](\d+))?)?)?((?:Z|[+-]\d{2}(?::?\d{2})?)?)$/); + if (!match) return null; + const [, yearText, monthText, dayText, hourText = "0", minuteText = "0", secondText = "0", , fractionText = "", zone = ""] = match; + if (zone) return parseFractionalDateInput(input); + const tz = timezone ?? (utc ? { offsetSeconds: 0 } : parsePosixTZ(new Date())); + if (!tz) return dateFromParts({ yearText, monthText, dayText, hourText, minuteText, secondText, fractionText, utc: false }); + const [year, month, day, hour, minute, second] = [yearText, monthText, dayText, hourText, minuteText, secondText].map(Number); + if (month < 1 || month > 12 || day < 1 || hour > 23 || minute > 59 || second > 60) return new Date(Number.NaN); + const milliseconds = Number(fractionText.padEnd(3, "0").slice(0, 3) || "0"); + const time = Date.UTC(year, month - 1, day, hour, minute, Math.min(second, 59), milliseconds) - tz.offsetSeconds * 1000 + (second === 60 ? 1000 : 0); + const date = new Date(time); + const nanoseconds = fractionText ? dateFractionNanoseconds(fractionText) : milliseconds * 1_000_000; + return attachDateNanoseconds(date, nanoseconds); +} + +export function parseEuropeanDateInput(input, utc = false) { + const datePattern = "(\\d{1,2})\\.(\\d{1,2})\\.(?:(\\d{4}))?"; + const timePattern = "(\\d{1,2})(?::(\\d{1,2})(?::(\\d{1,2})([.,](\\d+))?)?)?"; + const dateFirst = String(input).match(new RegExp(`^${datePattern}(?:\\s+${timePattern})?$`)); + const timeFirst = String(input).match(new RegExp(`^${timePattern}\\s+${datePattern}$`)); + if (dateFirst) { + const [, dayText, monthText, yearText, hourText = "0", minuteText = "0", secondText = "0", , fractionText = ""] = dateFirst; + return dateFromParts({ yearText, monthText, dayText, hourText, minuteText, secondText, fractionText, utc }); + } + if (timeFirst) { + const [, hourText = "0", minuteText = "0", secondText = "0", , fractionText = "", dayText, monthText, yearText] = timeFirst; + return dateFromParts({ yearText, monthText, dayText, hourText, minuteText, secondText, fractionText, utc }); + } + return null; +} + +export function parseMonthNameDateInput(input, utc = false) { + const match = String(input).match(/^([A-Za-z]+)\s+(\d{1,2})(?:\s+(\d{1,2}):(\d{2})(?::(\d{2})([.,](\d+))?)?)?\s+(\d{4})(?:\s*([A-IK-Z]))?$/i); + if (!match) return null; + const [, monthText, dayText, hourText = "0", minuteText = "0", secondText = "0", , fractionText = "", yearText, zoneText] = match; + const month = monthNumber(monthText); + if (month == null) return null; + if (zoneText) { + const offsetHours = militaryZoneOffsetHours(zoneText); + if (offsetHours == null) return null; + return parseAbsoluteDateInput(`${yearText}-${month}-${dayText} ${hourText}:${minuteText.padStart(2, "0")}:${secondText.padStart(2, "0")}${fractionText ? `.${fractionText}` : ""}`, false, { offsetSeconds: offsetHours * 3600 }); + } + return dateFromParts({ yearText, monthText: String(month), dayText, hourText, minuteText, secondText, fractionText, utc }); +} + +export function monthNumber(monthText) { + const index = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"].indexOf(String(monthText).slice(0, 3).toLowerCase()); + return index < 0 ? null : index + 1; +} + +export function parseCompactDateInput(input, utc = false) { + const match = String(input).match(/^(?:(\d{4})|(\d{2}))(\d{2})(\d{2})$/); + if (!match) return null; + const [, fullYearText, shortYearText, monthText, dayText] = match; + const year = fullYearText == null ? Number(shortYearText) + (Number(shortYearText) >= 69 ? 1900 : 2000) : Number(fullYearText); + return dateFromParts({ yearText: String(year), monthText, dayText, utc }); +} + +export function parseHourOnlyDateInput(input, utc = false) { + const match = String(input).match(/^(\d{1,2})$/); + if (!match) return null; + const hour = Number(match[1]); + if (hour > 23) return null; + return todayAt(hour, 0, 0, 0, utc); +} + +export function parseExplicitTimezoneDateInput(input) { + const match = String(input).match(/^TZ="([^"]*)"\s+(.+)$/); + if (!match) return null; + const timezoneText = match[1]; + const timezone = parsePosixTZText(timezoneText); + if (!timezone && timezoneText.includes("/")) { + const absolute = parseIanaTimezoneDateInput(match[2], timezoneText); + if (absolute) return absolute; + return parseExplicitTimezoneRelativeDateInput(match[2], timezoneText, null); + } + if (!timezone) return null; + const absolute = parseAbsoluteDateInput(match[2], false, timezone); + if (absolute) return absolute; + const relative = parseExplicitTimezoneRelativeDateInput(match[2], null, timezone); + if (relative) return relative; + return null; +} + +export function parseExplicitTimezoneRelativeDateInput(input, timeZone, timezone) { + const match = String(input).match(/^(?:[A-Za-z]+,\s*)?(\d{2}|\d{4})-(\d{1,2})-(\d{1,2})(?:[ T](\d{1,2}):(\d{2})(?::(\d{2})([.,](\d+))?)?)?\s+(.+)$/); + if (!match) return null; + const [, rawYearText, monthText, dayText, hourText = "0", minuteText = "00", secondText = "00", , fractionText = "", relativeText] = match; + const yearText = rawYearText.length === 2 ? String(Number(rawYearText) + (Number(rawYearText) >= 69 ? 1900 : 2000)) : rawYearText; + const baseText = `${yearText}-${monthText}-${dayText} ${hourText}:${minuteText}:${secondText}${fractionText ? `.${fractionText}` : ""}`; + let date = timeZone ? parseIanaTimezoneDateInput(baseText, timeZone) : parseAbsoluteDateInput(baseText, false, timezone); + if (!date || Number.isNaN(date.getTime())) return date; + const parts = parseDateRelativeTerms(relativeText); + if (!parts) return null; + for (const part of parts) date = applyDateRelative(date, part.amount, part.unit, part.amountText, 1, true); + return date; +} + +export function parseDateRelativeTerms(text) { + const parts = []; + let offset = 0; + const pattern = /\s*([+-]?\s*\d+(?:\.\d+)?)\s+(secs?|seconds?|mins?|minutes?|hours?|days?|weeks?|months?|years?)\b/ig; + let match; + while ((match = pattern.exec(String(text))) !== null) { + if (String(text).slice(offset, match.index).trim() !== "") return null; + const amountText = match[1].replace(/\s+/g, ""); + parts.push({ amount: Number(amountText), amountText, unit: normalizeDateRelativeUnit(match[2]) }); + offset = pattern.lastIndex; + } + if (!parts.length || String(text).slice(offset).trim() !== "") return null; + return parts; +} + +export function parseIanaTimezoneDateInput(input, timeZone) { + const match = String(input).match(/^(\d{4})-(\d{1,2})-(\d{1,2})(?:[ T](\d{1,2}):(\d{2})(?::(\d{2})([.,](\d+))?)?)?$/); + if (!match) return null; + const [, yearText, monthText, dayText, hourText = "0", minuteText = "0", secondText = "0", , fractionText = ""] = match; + const [year, month, day, hour, minute, second] = [yearText, monthText, dayText, hourText, minuteText, secondText].map(Number); + if (month < 1 || month > 12 || day < 1 || hour > 23 || minute > 59 || second > 60) return new Date(Number.NaN); + const milliseconds = Number(fractionText.padEnd(3, "0").slice(0, 3) || "0"); + const target = { year, month, day, hour, minute, second: Math.min(second, 59) }; + const baseUtc = Date.UTC(year, month - 1, day, hour, minute, Math.min(second, 59), milliseconds); + let candidate = baseUtc; + let formatter; + try { + formatter = ianaDateFormatter(timeZone); + } catch { + return null; + } + for (let i = 0; i < 4; i++) { + const seen = ianaDateParts(formatter, new Date(candidate)); + const offset = Date.UTC(seen.year, seen.month - 1, seen.day, seen.hour, seen.minute, seen.second, milliseconds) - candidate; + const next = baseUtc - offset; + if (next === candidate) break; + candidate = next; + } + const seen = ianaDateParts(formatter, new Date(candidate)); + if (!sameIanaWallTime(seen, target)) return new Date(Number.NaN); + const date = new Date(candidate + (second === 60 ? 1000 : 0)); + return attachDateNanoseconds(date, fractionText ? dateFractionNanoseconds(fractionText) : milliseconds * 1_000_000); +} + +export function ianaDateFormatter(timeZone) { + return new Intl.DateTimeFormat("en-US", { + timeZone, + hourCycle: "h23", + year: "numeric", + month: "2-digit", + day: "2-digit", + hour: "2-digit", + minute: "2-digit", + second: "2-digit", + }); +} + +export function ianaDateParts(formatter, date) { + const values = {}; + for (const part of formatter.formatToParts(date)) { + if (part.type !== "literal") values[part.type] = Number(part.value); + } + return { year: values.year, month: values.month, day: values.day, hour: values.hour, minute: values.minute, second: values.second }; +} + +export function sameIanaWallTime(actual, expected) { + return actual.year === expected.year && actual.month === expected.month && actual.day === expected.day && actual.hour === expected.hour && actual.minute === expected.minute && actual.second === expected.second; +} + +export function parseNamedZoneTimeInput(input) { + const match = String(input).match(/^(\d{1,2}):(\d{2})(?::(\d{2}))?\s+UTC([+-])(\d{1,2})(?::?(\d{2}))?$/i); + if (!match) return null; + const [, hourText, minuteText, secondText = "0", sign, zoneHourText, zoneMinuteText = "0"] = match; + const hours = Number(hourText); + const minutes = Number(minuteText); + const seconds = Number(secondText); + if (hours > 23 || minutes > 59 || seconds > 60) return new Date(Number.NaN); + const offsetSeconds = (Number(zoneHourText) * 3600 + Number(zoneMinuteText) * 60) * (sign === "+" ? 1 : -1); + const now = new Date(); + const time = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), hours, minutes, Math.min(seconds, 59)) - offsetSeconds * 1000 + (seconds === 60 ? 1000 : 0); + return new Date(time); +} + +export function parseMilitaryTimeZoneInput(input) { + const match = String(input).match(/^(\d{1,2}):(\d{2})(?::(\d{2}))?([A-IK-Z])$/i); + if (!match) return null; + const [, hourText, minuteText, secondText = "0", zoneText] = match; + const hours = Number(hourText); + const minutes = Number(minuteText); + const seconds = Number(secondText); + if (hours > 23 || minutes > 59 || seconds > 60) return new Date(Number.NaN); + const offsetHours = militaryZoneOffsetHours(zoneText); + if (offsetHours == null) return null; + const now = new Date(); + const time = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), hours, minutes, Math.min(seconds, 59)) - offsetHours * 3600 * 1000 + (seconds === 60 ? 1000 : 0); + return new Date(time); +} + +export function parseMilitaryZoneDateInput(input) { + const match = String(input).match(/^(\d{4}-\d{1,2}-\d{1,2})(?:[ T](\d{1,2}):(\d{2})(?::(\d{2})([.,](\d+))?)?)?\s*([A-IK-Z])$/i); + if (!match) return null; + const [, dateText, hourText = "0", minuteText = "00", secondText = "00", , fractionText = "", zoneText] = match; + const offsetHours = militaryZoneOffsetHours(zoneText); + if (offsetHours == null) return null; + return parseAbsoluteDateInput(`${dateText} ${hourText}:${minuteText}:${secondText}${fractionText ? `.${fractionText}` : ""}`, false, { offsetSeconds: offsetHours * 3600 }); +} + +export function parseSlashMilitaryZoneDateInput(input) { + const match = String(input).match(/^(\d{1,4})\/(\d{1,2})\/(\d{1,4})(?:[ T](\d{1,2}):(\d{2})(?::(\d{2})([.,](\d+))?)?)?\s*([A-IK-Z])$/i); + if (!match) return null; + const [, firstText, secondText, thirdText, hourText = "0", minuteText = "00", secondTimeText = "00", , fractionText = "", zoneText] = match; + const yearText = firstText.length === 4 ? firstText : thirdText; + const monthText = firstText.length === 4 ? secondText : firstText; + const dayText = firstText.length === 4 ? thirdText : secondText; + if (yearText.length !== 4) return null; + const offsetHours = militaryZoneOffsetHours(zoneText); + if (offsetHours == null) return null; + return parseAbsoluteDateInput(`${yearText}-${monthText}-${dayText} ${hourText}:${minuteText}:${secondTimeText}${fractionText ? `.${fractionText}` : ""}`, false, { offsetSeconds: offsetHours * 3600 }); +} + +export function militaryZoneOffsetHours(zoneText) { + const zone = String(zoneText).toUpperCase(); + const positive = "ABCDEFGHIKLM".indexOf(zone); + const negative = "NOPQRSTUVWXY".indexOf(zone); + return zone === "Z" ? 0 : positive >= 0 ? positive + 1 : negative >= 0 ? -(negative + 1) : null; +} + +export function parseLocalZoneAbbreviationDateInput(input, utc = false) { + if (utc) return null; + const match = String(input).match(/^(\d{4}-\d{1,2}-\d{1,2}(?:[ T]\d{1,2}:\d{2}(?::\d{2}(?:[.,]\d+)?)?)?)\s+([A-Za-z]{2,6})$/); + if (!match) return null; + const date = parseAbsoluteDateInput(match[1], false); + if (!date || Number.isNaN(date.getTime())) return date; + if (localZoneName(date).toLowerCase() !== match[2].toLowerCase()) return null; + date.__bnuLocalZoneAbbrev = match[2]; + date.__bnuLocalOffsetSeconds = localOffsetSeconds(date); + return date; +} + +export function localOffsetSeconds(date) { + return -date.getTimezoneOffset() * 60; +} + +export function dateFromParts({ yearText, monthText, dayText, hourText = "0", minuteText = "0", secondText = "0", fractionText = "", utc = false }) { + const now = new Date(); + const year = yearText == null || yearText === "" ? (utc ? now.getUTCFullYear() : now.getFullYear()) : Number(yearText); + const month = Number(monthText); + const day = Number(dayText); + const hour = Number(hourText); + const minute = Number(minuteText); + const second = Number(secondText); + if (month < 1 || month > 12 || day < 1 || hour > 23 || minute > 59 || second > 60) return new Date(Number.NaN); + const milliseconds = Number(String(fractionText).replace(/^[.,]/, "").padEnd(3, "0").slice(0, 3) || "0"); + const date = zonedDate(year, month, day, hour, minute, Math.min(second, 59), milliseconds, utc); + if (second === 60) date.setTime(date.getTime() + 1000); + const fraction = String(fractionText).replace(/^[.,]/, ""); + return attachDateNanoseconds(date, fraction ? dateFractionNanoseconds(fraction) : milliseconds * 1_000_000); +} + +export function todayAt(hour, minute, second, millisecond, utc = false) { + const now = new Date(); + return zonedDate(utc ? now.getUTCFullYear() : now.getFullYear(), (utc ? now.getUTCMonth() : now.getMonth()) + 1, utc ? now.getUTCDate() : now.getDate(), hour, minute, second, millisecond, utc); +} + +export function zonedDate(year, month, day, hour, minute, second, millisecond, utc = false) { + if (utc) return new Date(Date.UTC(year, month - 1, day, hour, minute, second, millisecond)); + const tz = parsePosixTZ(new Date()); + if (tz) return new Date(Date.UTC(year, month - 1, day, hour, minute, second, millisecond) - tz.offsetSeconds * 1000); + return new Date(year, month - 1, day, hour, minute, second, millisecond); +} + +export function parseFractionalDateInput(input) { + const match = String(input).match(/^(.+\d{1,2}:\d{2}:\d{2})([.,](\d+))((?:Z|[+-]\d{2}(?::?\d{2})?)?)$/); + if (isYearFirstMonthNameDateInput(input)) return new Date(Number.NaN); + if (!match) return new Date(input); + const [, prefix, separator, fraction, zone] = match; + const milliseconds = fraction.padEnd(3, "0").slice(0, 3); + const date = new Date(`${prefix}${separator}${milliseconds}${zone}`); + return attachDateNanoseconds(date, dateFractionNanoseconds(fraction)); +} + +export function isYearFirstMonthNameDateInput(input) { + const match = String(input).match(/^(\d{4})\s+([A-Za-z]+)\s+\d{1,2}\b/); + return Boolean(match && monthNumber(match[2]) != null); +} + +export function dateFractionNanoseconds(fraction) { + return Number(String(fraction || "").padEnd(9, "0").slice(0, 9)); +} + +export function normalizeDateInput(input) { + return input.replace(/(T\d{1,2}:\d{2}:\d{2})([+-])(\d{1,2})$/, (_, time, sign, hour) => `${time}${sign}${String(hour).padStart(2, "0")}:00`); +} + +export function stripDateComments(input) { + let depth = 0; + let output = ""; + for (const ch of String(input)) { + if (ch === "(") { + depth++; + continue; + } + if (ch === ")" && depth > 0) { + depth--; + continue; + } + if (depth === 0) output += ch; + } + return output; +} + +export function lsTimeText(date, opts) { + const style = opts["time-style"]; + if (typeof style === "string" && style.startsWith("+")) return strftime(date, style.slice(1)); + if (style === "full-iso" || style === "posix-full-iso") return lsFullIsoTime(date); + if (style === "iso" || style === "posix-iso") return `${strftime(date, "%F")} `; + if (style === "long-iso" || style === "posix-long-iso") return strftime(date, "%F %R"); + if (style == null || style === "locale" || style === "posix-locale") { + const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; + const now = Date.now(); + const sixMonthsMs = 365.2425 * 24 * 60 * 60 * 1000 / 2; + const recent = date.getTime() <= now && date.getTime() >= now - sixMonthsMs; + return `${months[date.getMonth()]} ${String(date.getDate()).padStart(2)} ${recent ? strftime(date, "%H:%M") : ` ${date.getFullYear()}`}`; + } + return strftime(date, "%F %R"); +} + +export function lsFullIsoTime(date) { + return strftime(date, "%F %T.%N %z"); +} + +export function parseGNUBlockSizeEnv(value, defaultSize) { + return parseGNUBlockSizeEnvInfo(value, defaultSize).size; +} + +export function parseRelativeDateSpec(spec, base, utc = false) { + const match = String(spec).trim().match(/^([+-]?\s*\d+(?:\.\d+)?)\s+(secs?|seconds?|mins?|minutes?|hours?|days?|weeks?|months?|years?)(?:\s+(ago))?$/i); + if (!match) return null; + const amountText = match[1].replace(/\s+/g, ""); + const amount = Number(amountText) * (match[3] ? -1 : 1); + const unit = normalizeDateRelativeUnit(match[2]); + return applyDateRelative(base, amount, unit, amountText, match[3] ? -1 : 1, utc); +} + +export function normalizeDateRelativeUnit(unitText) { + const unit = unitText.toLowerCase().replace(/s$/, ""); + if (unit === "sec") return "second"; + if (unit === "min") return "minute"; + return unit; +} + +export function applyDateRelative(base, amount, unit, amountText = String(amount), direction = 1, utc = false) { + if (unit === "month" || unit === "year") { + const next = new Date(base); + if (unit === "month") utc ? next.setUTCMonth(next.getUTCMonth() + amount) : next.setMonth(next.getMonth() + amount); + else utc ? next.setUTCFullYear(next.getUTCFullYear() + amount) : next.setFullYear(next.getFullYear() + amount); + if (!utc && base.__bnuLocalZoneAbbrev && base.__bnuLocalOffsetSeconds != null) { + const offsetDelta = localOffsetSeconds(next) - base.__bnuLocalOffsetSeconds; + if (offsetDelta) next.setTime(next.getTime() + offsetDelta * 1000); + } + return next; + } + const seconds = { second: 1, minute: 60, hour: 3600, day: 86400, week: 604800 }[unit]; + const deltaNanoseconds = decimalSecondsToNanoseconds(amountText, seconds) * BigInt(direction); + const baseNanoseconds = BigInt(base.getTime()) * 1_000_000n + BigInt((base.__bnuNanoseconds ?? base.getMilliseconds() * 1_000_000) - base.getMilliseconds() * 1_000_000); + const totalNanoseconds = baseNanoseconds + deltaNanoseconds; + const milliseconds = floorDivBigInt(totalNanoseconds, 1_000_000n); + const nanoseconds = Number(((totalNanoseconds % 1_000_000_000n) + 1_000_000_000n) % 1_000_000_000n); + return attachDateNanoseconds(new Date(Number(milliseconds)), nanoseconds); +} + +export function decimalSecondsToNanoseconds(amountText, unitSeconds) { + const text = String(amountText); + const sign = text.startsWith("-") ? -1n : 1n; + const unsigned = text.replace(/^[+-]/, ""); + const [wholeText, fractionText = ""] = unsigned.split("."); + const whole = BigInt(wholeText || "0") * BigInt(unitSeconds) * 1_000_000_000n; + const fraction = BigInt(fractionText.padEnd(9, "0").slice(0, 9) || "0") * BigInt(unitSeconds); + return sign * (whole + fraction); +} + +export function parseAbsoluteRelativeDateSpec(spec, utc = false) { + const text = String(spec).trim(); + const keyword = text.match(/^(.+?)\s+(now|yesterday|tomorrow|this\s+(?:secs?|seconds?|mins?|minutes?|hours?|days?|weeks?|months?|years?)|next\s+(?:secs?|seconds?|mins?|minutes?|hours?|days?|weeks?|months?|years?))$/i); + if (keyword) { + const base = parseDateInput(keyword[1], utc, false); + if (Number.isNaN(base.getTime())) return null; + const suffix = keyword[2].toLowerCase(); + if (suffix === "now") return base; + if (suffix === "yesterday") return applyDateRelative(base, -1, "day", "1", -1, utc); + if (suffix === "tomorrow") return applyDateRelative(base, 1, "day", "1", 1, utc); + const [, direction, unitText] = suffix.match(/^(this|next)\s+(.+)$/); + return direction === "this" ? base : applyDateRelative(base, 1, normalizeDateRelativeUnit(unitText), "1", 1, utc); + } + const match = text.match(/^(.+?)\s+([+-]?\s*\d+(?:\.\d+)?\s+(?:secs?|seconds?|mins?|minutes?|hours?|days?|weeks?|months?|years?)(?:\s+ago)?)$/i); + if (!match) return null; + const base = parseDateInput(match[1], utc, false); + if (Number.isNaN(base.getTime())) return null; + return parseRelativeDateSpec(match[2], base, utc); +} + +export function parseTimeTodayRelativeDateSpec(spec) { + const match = String(spec).trim().match(/^(\d{1,2}):(\d{2})(?::(\d{2}))?\s+today\s+([+-]\d+)\s+(seconds?|minutes?|hours?|days?|weeks?|months?|years?)$/i); + if (!match) return null; + const [, hoursText, minutesText, secondsText, amountText, unitText] = match; + const base = new Date(); + const hours = Number(hoursText); + const minutes = Number(minutesText); + const seconds = Number(secondsText ?? 0); + if (hours > 23 || minutes > 59 || seconds > 60) return new Date(Number.NaN); + base.setHours(hours, minutes, Math.min(seconds, 59), 0); + if (seconds === 60) base.setSeconds(base.getSeconds() + 1); + return parseRelativeDateSpec(`${amountText} ${unitText}`, base); +} -- cgit v1.3