diff options
| author | Lily <[email protected]> | 2026-07-28 16:12:21 +1000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-07-28 16:12:21 +1000 |
| commit | c11e17573334dfb487a8e1cc25fa84fa9e138f5c (patch) | |
| tree | e91dbd19b524f9432da6bcb86b1fc193e0d02e26 /src/commands/sync.js | |
| parent | 6a2f098e72e71437154ec00506e8e736aa8e1026 (diff) | |
| download | bnu-c11e17573334dfb487a8e1cc25fa84fa9e138f5c.tar.gz bnu-c11e17573334dfb487a8e1cc25fa84fa9e138f5c.zip | |
Refactor coreutils into single-call command modules (#1)
* Refactor to single call
Diffstat (limited to 'src/commands/sync.js')
| -rw-r--r-- | src/commands/sync.js | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/commands/sync.js b/src/commands/sync.js new file mode 100644 index 0000000..d0f876f --- /dev/null +++ b/src/commands/sync.js @@ -0,0 +1,118 @@ +#!/usr/bin/env bun + +import { lstat, open } from "node:fs/promises"; +import { libc, pathDisplayName, shellEscapeLsName, systemErrorMessage } from "../shared/common.js"; +import { UsageError, fail, stderr } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const SYNC_LONG_OPTIONS = ["data", "file-system", "help", "version"]; + +export function syncMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (!arg.startsWith("--")) continue; + const option = normalizeSyncLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!SYNC_LONG_OPTIONS.includes(name) || option.includes("=")) return null; + if (option === "--help" || option === "--version") return option; + } + return null; +} + +export async function syncCmd(args) { + const { opts, operands } = parseSyncArgs(args); + const data = opts.d || opts.data; + const fileSystem = opts.f || opts["file-system"]; + if (data && fileSystem) return fail("sync", "cannot specify both --data and --file-system", 1); + if (data && !operands.length) return fail("sync", "--data needs at least one argument", 1); + if (operands.length) { + let status = 0; + for (const file of operands) { + const ok = await syncOneFile(file, data, fileSystem); + if (!ok) status = 1; + } + return status; + } + libc.symbols.sync(); + return 0; +} + +export function parseSyncArgs(args) { + const opts = {}; + const operands = []; + let end = false; + for (const arg of args) { + if (end || arg === "-" || !arg.startsWith("-")) { + operands.push(arg); + if (process.env.POSIXLY_CORRECT) end = true; + continue; + } + if (arg === "--") { + end = true; + continue; + } + if (arg.startsWith("--")) { + const normalized = normalizeSyncLongOption(arg); + const [name, inlineValue] = normalized.slice(2).split(/=(.*)/s, 2); + if (!SYNC_LONG_OPTIONS.includes(name)) throw new UsageError(`unrecognized option '${arg}'`, true); + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + opts[name] = true; + continue; + } + for (const ch of arg.slice(1)) { + if (ch === "d" || ch === "f") opts[ch] = true; + else throw new UsageError(`invalid option -- '${ch}'`, true); + } + } + return { opts, operands }; +} + +export function normalizeSyncLongOption(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 = SYNC_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export async function syncOneFile(file, data = false, fileSystem = false) { + const name = shellEscapeLsName(pathDisplayName(file), true); + const s = await lstat(file).catch((error) => error); + if (s instanceof Error) { + stderr(`sync: error opening ${name}: ${systemErrorMessage(s)}\n`); + return false; + } + if (s.isFIFO()) return true; + let handle; + try { + handle = await open(file, "r"); + } catch (readError) { + if (s.isSymbolicLink() && readError?.code === "ENOENT") { + stderr(`sync: error opening ${name}: ${systemErrorMessage(readError)}\n`); + return false; + } + try { + handle = await open(file, "a"); + } catch { + stderr(`sync: error opening ${name}: ${systemErrorMessage(readError)}\n`); + return false; + } + } + try { + if (data && !fileSystem) await handle.datasync(); + else await handle.sync(); + return true; + } catch (error) { + stderr(`sync: error syncing ${name}: ${systemErrorMessage(error)}\n`); + return false; + } finally { + await handle.close(); + } +} + +const singleCall = defineCommand("sync", syncCmd, syncMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); |
