aboutsummaryrefslogtreecommitdiff
path: root/src/commands/sync.js
diff options
context:
space:
mode:
authorSoccera1 <[email protected]>2026-07-18 09:27:17 +1000
committerSoccera1 <[email protected]>2026-07-18 09:27:17 +1000
commit6f5241afb5bd1d6a1a8724bf06cc6f205c279402 (patch)
tree2aa852e968f6b89f2c8458333ce3e7c38b3c2450 /src/commands/sync.js
parenta753f669c9bedfccb9f7f400da018bb487dca5d0 (diff)
downloadbnu-6f5241afb5bd1d6a1a8724bf06cc6f205c279402.tar.gz
bnu-6f5241afb5bd1d6a1a8724bf06cc6f205c279402.zip
Refactor to single call
Diffstat (limited to 'src/commands/sync.js')
-rw-r--r--src/commands/sync.js118
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);