1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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);
|