@indigoai-us/hq-cli 5.345.215 → 5.345.217
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +141 -0
- package/README.md +45 -0
- package/assets/scaffold/core/scripts/lib/epochrealtime.sh +30 -0
- package/assets/scaffold/core/scripts/lib/work-mesh-enqueue.sh +12 -5
- package/dist/command-catalog.generated.d.ts +59 -4
- package/dist/command-catalog.generated.js +74 -4
- package/dist/command-help.js +3 -1
- package/dist/command-registration-plan.d.ts +6 -0
- package/dist/command-registration-plan.js +1 -0
- package/dist/commands/auth.d.ts +1 -1
- package/dist/commands/auth.js +11 -52
- package/dist/commands/bot.js +11 -4
- package/dist/commands/cloud-demote.d.ts +19 -4
- package/dist/commands/cloud-demote.js +85 -12
- package/dist/commands/cloud-provision.d.ts +5 -1
- package/dist/commands/cloud-provision.js +6 -2
- package/dist/commands/cloud.d.ts +36 -0
- package/dist/commands/cloud.js +109 -26
- package/dist/commands/core.js +1 -0
- package/dist/commands/daemon.js +4 -1
- package/dist/commands/dm.js +7 -0
- package/dist/commands/doctor-usage.d.ts +8 -0
- package/dist/commands/doctor-usage.js +20 -0
- package/dist/commands/doctor.js +7 -1
- package/dist/commands/env.d.ts +18 -0
- package/dist/commands/env.js +82 -0
- package/dist/commands/install-global.js +2 -2
- package/dist/commands/lanes.js +100 -24
- package/dist/commands/logout.d.ts +7 -0
- package/dist/commands/logout.js +41 -34
- package/dist/commands/sources.d.ts +8 -2
- package/dist/commands/sources.js +65 -9
- package/dist/commands/whoami.js +26 -1
- package/dist/data/command-states.json +15 -0
- package/dist/index.js +52 -4
- package/dist/lazy-commands.js +2 -1
- package/dist/lib/anywhere-person-setting.d.ts +2 -0
- package/dist/lib/anywhere-person-setting.js +6 -3
- package/dist/lib/billing/usage-card.d.ts +25 -4
- package/dist/lib/billing/usage-card.js +76 -9
- package/dist/lib/billing/usage-cost.d.ts +3 -3
- package/dist/lib/billing/usage-cost.js +35 -13
- package/dist/lib/billing/usage-only.js +4 -2
- package/dist/lib/billing/usage-rate-card.d.ts +2 -2
- package/dist/lib/billing/usage-rate-card.js +3 -0
- package/dist/lib/bot/fleet-cron.js +93 -6
- package/dist/lib/bot/inflight.d.ts +9 -0
- package/dist/lib/bot/inflight.js +10 -0
- package/dist/lib/bot/jobs/hot-signal-sync.js +12 -1
- package/dist/lib/bot/runtime/grok-acp.d.ts +1 -0
- package/dist/lib/bot/runtime/grok-acp.js +26 -11
- package/dist/lib/bot/runtime/grok.js +1 -1
- package/dist/lib/daemon/flag-monitor.d.ts +8 -0
- package/dist/lib/daemon/flag-monitor.js +17 -6
- package/dist/lib/daemon/flag.d.ts +65 -1
- package/dist/lib/daemon/flag.js +190 -40
- package/dist/lib/daemon/ops/policy.js +62 -5
- package/dist/lib/daemon/paths.d.ts +72 -2
- package/dist/lib/daemon/paths.js +154 -3
- package/dist/lib/daemon/run.js +25 -20
- package/dist/lib/daemon/selfcheck.d.ts +22 -0
- package/dist/lib/daemon/selfcheck.js +40 -1
- package/dist/lib/daemon/server.d.ts +3 -1
- package/dist/lib/daemon/server.js +7 -22
- package/dist/lib/daemon/state-file-switch.d.ts +57 -0
- package/dist/lib/daemon/state-file-switch.js +88 -0
- package/dist/lib/daemon/state-file.d.ts +198 -0
- package/dist/lib/daemon/state-file.js +419 -0
- package/dist/lib/daemon/timed-flag-monitors.d.ts +47 -0
- package/dist/lib/daemon/timed-flag-monitors.js +75 -0
- package/dist/lib/doctor/checks/host-core.d.ts +50 -0
- package/dist/lib/doctor/checks/host-core.js +269 -0
- package/dist/lib/doctor/checks/replay-recorder.d.ts +39 -0
- package/dist/lib/doctor/checks/replay-recorder.js +261 -0
- package/dist/lib/doctor/registry.js +4 -0
- package/dist/lib/environment/profile.d.ts +239 -0
- package/dist/lib/environment/profile.js +251 -0
- package/dist/lib/hook-engine/reconcile.js +39 -13
- package/dist/lib/hook-engine/registration.d.ts +9 -0
- package/dist/lib/hook-engine/registration.js +19 -0
- package/dist/lib/hot-channels/adapters/slack.d.ts +21 -5
- package/dist/lib/hot-channels/adapters/slack.js +64 -21
- package/dist/lib/hot-channels/alert.d.ts +2 -0
- package/dist/lib/hot-channels/alert.js +8 -1
- package/dist/lib/hot-channels/contract.d.ts +12 -0
- package/dist/lib/hot-channels/contract.js +7 -0
- package/dist/lib/hot-channels/loop.js +1 -0
- package/dist/lib/hot-channels/run.d.ts +6 -0
- package/dist/lib/hot-channels/run.js +4 -0
- package/dist/lib/hot-channels/signals-pass.d.ts +64 -1
- package/dist/lib/hot-channels/signals-pass.js +21 -49
- package/dist/lib/install/claude-targets.d.ts +12 -1
- package/dist/lib/install/claude-targets.js +57 -5
- package/dist/lib/install/codex-targets.d.ts +2 -2
- package/dist/lib/install/codex-targets.js +36 -6
- package/dist/lib/install/hook-target.d.ts +3 -0
- package/dist/lib/install/hook-target.js +11 -0
- package/dist/lib/lanes/canonical-host.d.ts +35 -0
- package/dist/lib/lanes/canonical-host.js +65 -0
- package/dist/lib/lanes/host-capacity.d.ts +30 -0
- package/dist/lib/lanes/host-capacity.js +29 -3
- package/dist/lib/mesh/live/daemon/run.d.ts +2 -0
- package/dist/lib/mesh/live/daemon/run.js +5 -2
- package/dist/lib/mesh/live/flush.d.ts +5 -1
- package/dist/lib/mesh/live/flush.js +12 -6
- package/dist/lib/mesh/live/reporting-privacy.d.ts +32 -0
- package/dist/lib/mesh/live/reporting-privacy.js +56 -1
- package/dist/lib/mesh/live/spool.d.ts +7 -0
- package/dist/lib/mesh/live/spool.js +25 -0
- package/dist/lib/person-setting-cache.d.ts +38 -0
- package/dist/lib/person-setting-cache.js +98 -0
- package/dist/lib/safe-store.d.ts +245 -0
- package/dist/lib/safe-store.js +777 -0
- package/dist/lib/signals/engine.d.ts +29 -2
- package/dist/lib/signals/engine.js +103 -4
- package/dist/lib/signals/reply.js +73 -18
- package/dist/lib/signals/steps/adapters.js +7 -2
- package/dist/lib/signals/steps/extract.d.ts +13 -0
- package/dist/lib/signals/steps/extract.js +84 -4
- package/dist/main.js +46 -3
- package/dist/schemas/hq-environment-profile.v1.schema.json +438 -0
- package/dist/utils/auth-expiry.d.ts +8 -0
- package/dist/utils/auth-expiry.js +53 -0
- package/dist/utils/cli-telemetry.d.ts +21 -0
- package/dist/utils/cli-telemetry.js +34 -0
- package/dist/utils/command-usage.d.ts +275 -0
- package/dist/utils/command-usage.js +797 -0
- package/dist/utils/hook-trust.d.ts +7 -0
- package/dist/utils/hook-trust.js +31 -4
- package/dist/utils/host-registration.d.ts +126 -0
- package/dist/utils/host-registration.js +378 -0
- package/dist/utils/resident-package-preload.js +2 -0
- package/package.json +10 -8
- package/scripts/bench-core.mjs +1096 -0
- package/scripts/check-function-manifest.mjs +702 -0
|
@@ -0,0 +1,777 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One private store folder, held open, with file operations bound to it.
|
|
3
|
+
*
|
|
4
|
+
* Every store hq writes under the user's home goes through this module so the
|
|
5
|
+
* file-safety rules live in one place. The rules (store rule v2):
|
|
6
|
+
*
|
|
7
|
+
* - Nothing left on disk beforehand can redirect a read or a write: a symlink
|
|
8
|
+
* or hardlink at a file, a symlink or junction at the folder, a FIFO, device
|
|
9
|
+
* or folder in a file's place, or a folder or file owned by another user or
|
|
10
|
+
* writable by group or others is refused.
|
|
11
|
+
* - A same-user process that swaps the folder while an operation runs is
|
|
12
|
+
* caught as far as Node allows (see "Binding to the folder" below).
|
|
13
|
+
*
|
|
14
|
+
* Every refusal is a typed result ({ ok: false, code }), never a thrown
|
|
15
|
+
* error, so a caller can carry on with nothing counted and nothing written.
|
|
16
|
+
*
|
|
17
|
+
* Parent folders. At open, every folder above the store folder (real path) is
|
|
18
|
+
* checked up to the user's home, inclusive, when the store is inside it, and
|
|
19
|
+
* otherwise up to the filesystem root. Each must be owned by this user or by
|
|
20
|
+
* root and not writable by group or others; a root-owned folder with the
|
|
21
|
+
* sticky bit (such as /tmp) is allowed, because others cannot rename or
|
|
22
|
+
* remove entries they do not own there. Parents are checked at open only. A
|
|
23
|
+
* symlink in a parent path that this user made is followed: same-user
|
|
24
|
+
* changes above the store are outside what this module defends against.
|
|
25
|
+
*
|
|
26
|
+
* Binding to the folder. Node has no openat, renameat or unlinkat, so:
|
|
27
|
+
*
|
|
28
|
+
* 1. The folder is opened once with O_DIRECTORY|O_NOFOLLOW and the descriptor
|
|
29
|
+
* is held until close(). Its owner and mode are checked by fstat on that
|
|
30
|
+
* descriptor.
|
|
31
|
+
* 2. Before each operation, before each rename retry, and after a listing,
|
|
32
|
+
* the folder path's lstat device and inode are compared with the held
|
|
33
|
+
* descriptor's fstat, and the held folder's owner and mode are checked
|
|
34
|
+
* again; on a mismatch the operation is refused. writeAtomic and rename
|
|
35
|
+
* compare again after the rename and report a mismatch as
|
|
36
|
+
* "folder-changed-after".
|
|
37
|
+
* 3. Files are opened with O_NOFOLLOW (and O_NONBLOCK, so a FIFO never hangs
|
|
38
|
+
* the process), then checked on the opened descriptor: regular file, one
|
|
39
|
+
* link, owned by this user, not writable by group or others. After the
|
|
40
|
+
* open, the name's lstat device and inode must match the opened descriptor
|
|
41
|
+
* and the folder comparison runs again, so a file opened through a folder
|
|
42
|
+
* swapped in and back again is not used.
|
|
43
|
+
* 4. Temporary and exclusive files are opened with O_CREAT|O_EXCL|O_NOFOLLOW,
|
|
44
|
+
* mode 0600; temporary names are random.
|
|
45
|
+
* 5. On Linux, when /proc is mounted, file paths are built as
|
|
46
|
+
* /proc/self/fd/<n>/<name>, which resolve through the held descriptor and
|
|
47
|
+
* not the folder's name.
|
|
48
|
+
*
|
|
49
|
+
* Remaining window (everywhere except Linux with /proc): the checks are
|
|
50
|
+
* comparisons by path, so a same-user process that replaces the folder after
|
|
51
|
+
* the last comparison and before an operation's own path lookup, and puts it
|
|
52
|
+
* back before the next comparison, microseconds apart, is not always seen:
|
|
53
|
+
* - a rename or remove can act on the replacement; a rename is reported when
|
|
54
|
+
* the folder is still replaced at the comparison after it;
|
|
55
|
+
* - an opened file is refused unless the attacker also moves that same file
|
|
56
|
+
* into the store folder before the post-open check, which is a direct write
|
|
57
|
+
* to the store that a same-user process can do anyway;
|
|
58
|
+
* - a refused atomic write can leave an owner-only temporary file under a
|
|
59
|
+
* random name in the held folder.
|
|
60
|
+
*
|
|
61
|
+
* Windows: Node cannot open a folder there, and O_NOFOLLOW does not exist. The
|
|
62
|
+
* folder is checked by lstat (a real folder, not a symlink or junction) and its
|
|
63
|
+
* device and inode are compared by lstat at the same points. Files are checked
|
|
64
|
+
* by lstat for a link before they are opened. Owner and mode, of the folder,
|
|
65
|
+
* its parents and its files, are not checked on Windows, because Node reports
|
|
66
|
+
* no owner and no ACL there.
|
|
67
|
+
*/
|
|
68
|
+
import { randomBytes } from "node:crypto";
|
|
69
|
+
import * as fs from "node:fs";
|
|
70
|
+
import * as os from "node:os";
|
|
71
|
+
import * as path from "node:path";
|
|
72
|
+
const DEFAULT_READ_MAX_BYTES = 1024 * 1024;
|
|
73
|
+
function refusal(code, message, errno) {
|
|
74
|
+
return errno === undefined ? { ok: false, code, message } : { ok: false, code, message, errno };
|
|
75
|
+
}
|
|
76
|
+
function errnoOf(error) {
|
|
77
|
+
const code = error?.code;
|
|
78
|
+
return typeof code === "string" ? code : undefined;
|
|
79
|
+
}
|
|
80
|
+
function ioError(what, error) {
|
|
81
|
+
const errno = errnoOf(error);
|
|
82
|
+
return refusal("io-error", `${what} failed${errno ? ` (${errno})` : ""}`, errno);
|
|
83
|
+
}
|
|
84
|
+
function sleepSync(ms) {
|
|
85
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
86
|
+
}
|
|
87
|
+
/** A plain file name: no separator, no NUL, not "." or "..", at most 255 bytes. */
|
|
88
|
+
export function isPlainName(name) {
|
|
89
|
+
return (typeof name === "string" &&
|
|
90
|
+
name.length > 0 &&
|
|
91
|
+
name !== "." &&
|
|
92
|
+
name !== ".." &&
|
|
93
|
+
!/[/\\\0]/.test(name) &&
|
|
94
|
+
Buffer.byteLength(name) <= 255);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Whether a folder or file is owned by `uid` and closed to group and other
|
|
98
|
+
* writes. `uid` undefined (Windows) skips both checks.
|
|
99
|
+
*/
|
|
100
|
+
export function ownerModeRefusal(stat, uid, kind) {
|
|
101
|
+
if (uid === undefined)
|
|
102
|
+
return null;
|
|
103
|
+
if (Number(stat.uid) !== uid) {
|
|
104
|
+
return refusal(kind === "folder" ? "folder-foreign-owner" : "file-foreign-owner", `the ${kind} is owned by another user`);
|
|
105
|
+
}
|
|
106
|
+
if ((Number(stat.mode) & 0o022) !== 0) {
|
|
107
|
+
return refusal(kind === "folder" ? "folder-writable" : "file-writable", `group or others can write the ${kind}`);
|
|
108
|
+
}
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Whether a folder above the store is safe: owned by `uid` or root, and not
|
|
113
|
+
* writable by group or others unless it is a root-owned sticky folder.
|
|
114
|
+
*/
|
|
115
|
+
export function parentStatRefusal(stat, uid) {
|
|
116
|
+
const owner = Number(stat.uid);
|
|
117
|
+
const mode = Number(stat.mode);
|
|
118
|
+
if (owner !== uid && owner !== 0)
|
|
119
|
+
return refusal("parent-unsafe", "a folder above the store is owned by another user");
|
|
120
|
+
if ((mode & 0o022) !== 0 && !(owner === 0 && (mode & 0o1000) !== 0)) {
|
|
121
|
+
return refusal("parent-unsafe", "group or others can write a folder above the store");
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
/** Check every existing folder above `directory`, up to `home` when inside it, else up to the filesystem root. */
|
|
126
|
+
function parentsRefusal(directory, uid, home) {
|
|
127
|
+
// The nearest existing parent, resolved; missing parents are created 0700 by create.
|
|
128
|
+
let start = path.dirname(directory);
|
|
129
|
+
let real;
|
|
130
|
+
for (;;) {
|
|
131
|
+
try {
|
|
132
|
+
real = fs.realpathSync(start);
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
const next = path.dirname(start);
|
|
137
|
+
if (errnoOf(error) !== "ENOENT" || next === start)
|
|
138
|
+
return ioError("checking the folders above the store", error);
|
|
139
|
+
start = next;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
let realHome;
|
|
143
|
+
try {
|
|
144
|
+
realHome = fs.realpathSync(home);
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
realHome = undefined;
|
|
148
|
+
}
|
|
149
|
+
const insideHome = realHome !== undefined && (real === realHome || real.startsWith(realHome + path.sep));
|
|
150
|
+
for (let current = real;; current = path.dirname(current)) {
|
|
151
|
+
let stat;
|
|
152
|
+
try {
|
|
153
|
+
stat = fs.lstatSync(current);
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
return ioError("checking the folders above the store", error);
|
|
157
|
+
}
|
|
158
|
+
if (!stat.isDirectory())
|
|
159
|
+
return refusal("parent-unsafe", "a path above the store is not a folder");
|
|
160
|
+
const checked = parentStatRefusal(stat, uid);
|
|
161
|
+
if (checked)
|
|
162
|
+
return checked;
|
|
163
|
+
if (insideHome && current === realHome)
|
|
164
|
+
return null;
|
|
165
|
+
if (path.dirname(current) === current)
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/** The checks every file this module reads or appends to must pass, on the opened descriptor. */
|
|
170
|
+
export function fileStatRefusal(stat, uid) {
|
|
171
|
+
if (stat.isSymbolicLink())
|
|
172
|
+
return refusal("file-link", "the file is a symlink");
|
|
173
|
+
if (!stat.isFile())
|
|
174
|
+
return refusal("not-regular-file", "the file is not a regular file");
|
|
175
|
+
if (Number(stat.nlink) !== 1)
|
|
176
|
+
return refusal("file-hardlinked", "the file has more than one link");
|
|
177
|
+
return ownerModeRefusal(stat, uid, "file");
|
|
178
|
+
}
|
|
179
|
+
function sameNode(a, b) {
|
|
180
|
+
return a.dev === b.dev && a.ino === b.ino;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Open `dir` as a store folder. With `create`, a missing folder is made 0700.
|
|
184
|
+
* The folder must be a real folder (not a symlink or junction); on POSIX it
|
|
185
|
+
* must be owned by this user and not writable by group or others, and so must
|
|
186
|
+
* the folders above it (see "Parent folders" above).
|
|
187
|
+
*/
|
|
188
|
+
export function openStoreDir(dir, options = {}) {
|
|
189
|
+
const directory = path.resolve(dir);
|
|
190
|
+
const platform = options.platform ?? process.platform;
|
|
191
|
+
const { O_RDONLY, O_DIRECTORY, O_NOFOLLOW } = fs.constants;
|
|
192
|
+
const posix = platform !== "win32" && O_DIRECTORY !== undefined && O_NOFOLLOW !== undefined;
|
|
193
|
+
const uid = posix && typeof process.getuid === "function" ? process.getuid() : undefined;
|
|
194
|
+
const home = options.home ?? os.homedir();
|
|
195
|
+
// Parents first: nothing is created under an unsafe folder.
|
|
196
|
+
if (uid !== undefined) {
|
|
197
|
+
const parents = parentsRefusal(directory, uid, home);
|
|
198
|
+
if (parents)
|
|
199
|
+
return parents;
|
|
200
|
+
}
|
|
201
|
+
if (options.create) {
|
|
202
|
+
try {
|
|
203
|
+
fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
const errno = errnoOf(error);
|
|
207
|
+
if (errno === "EEXIST" || errno === "ENOTDIR")
|
|
208
|
+
return refusal("not-a-folder", "the store path is not a folder", errno);
|
|
209
|
+
return ioError("creating the store folder", error);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (!posix) {
|
|
213
|
+
// Windows: no folder descriptor. A symlink or junction reports as a link to lstat.
|
|
214
|
+
let held;
|
|
215
|
+
try {
|
|
216
|
+
held = fs.lstatSync(directory, { bigint: true });
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
if (errnoOf(error) === "ENOENT")
|
|
220
|
+
return refusal("folder-missing", "the store folder does not exist", "ENOENT");
|
|
221
|
+
return ioError("checking the store folder", error);
|
|
222
|
+
}
|
|
223
|
+
if (held.isSymbolicLink())
|
|
224
|
+
return refusal("folder-link", "the store folder is a symlink or junction");
|
|
225
|
+
if (!held.isDirectory())
|
|
226
|
+
return refusal("not-a-folder", "the store path is not a folder");
|
|
227
|
+
return { ok: true, value: new HeldStore(directory, undefined, held, undefined, platform, options.at) };
|
|
228
|
+
}
|
|
229
|
+
let folderFd;
|
|
230
|
+
try {
|
|
231
|
+
folderFd = fs.openSync(directory, O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
const errno = errnoOf(error);
|
|
235
|
+
if (errno === "ENOENT")
|
|
236
|
+
return refusal("folder-missing", "the store folder does not exist", errno);
|
|
237
|
+
if (errno === "ELOOP")
|
|
238
|
+
return refusal("folder-link", "the store folder is a symlink", errno);
|
|
239
|
+
if (errno === "ENOTDIR") {
|
|
240
|
+
// macOS reports a symlink under O_DIRECTORY|O_NOFOLLOW as ENOTDIR.
|
|
241
|
+
try {
|
|
242
|
+
if (fs.lstatSync(directory).isSymbolicLink())
|
|
243
|
+
return refusal("folder-link", "the store folder is a symlink", errno);
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
// fall through to not-a-folder
|
|
247
|
+
}
|
|
248
|
+
return refusal("not-a-folder", "the store path is not a folder", errno);
|
|
249
|
+
}
|
|
250
|
+
return ioError("opening the store folder", error);
|
|
251
|
+
}
|
|
252
|
+
const fail = (result) => {
|
|
253
|
+
try {
|
|
254
|
+
fs.closeSync(folderFd);
|
|
255
|
+
}
|
|
256
|
+
catch {
|
|
257
|
+
// already closed
|
|
258
|
+
}
|
|
259
|
+
return result;
|
|
260
|
+
};
|
|
261
|
+
try {
|
|
262
|
+
let held = fs.fstatSync(folderFd, { bigint: true });
|
|
263
|
+
if (!held.isDirectory())
|
|
264
|
+
return fail(refusal("not-a-folder", "the store path is not a folder"));
|
|
265
|
+
const ownerCheck = ownerModeRefusal(held, uid, "folder");
|
|
266
|
+
if (ownerCheck)
|
|
267
|
+
return fail(ownerCheck);
|
|
268
|
+
if (options.create && (Number(held.mode) & 0o077) !== 0) {
|
|
269
|
+
fs.fchmodSync(folderFd, 0o700);
|
|
270
|
+
held = fs.fstatSync(folderFd, { bigint: true });
|
|
271
|
+
}
|
|
272
|
+
// The path must still lead to the folder just opened.
|
|
273
|
+
let atPath;
|
|
274
|
+
try {
|
|
275
|
+
atPath = fs.lstatSync(directory, { bigint: true });
|
|
276
|
+
}
|
|
277
|
+
catch (error) {
|
|
278
|
+
return fail(refusal("folder-changed", "the store folder was replaced while it was opened", errnoOf(error)));
|
|
279
|
+
}
|
|
280
|
+
if (!sameNode(atPath, held))
|
|
281
|
+
return fail(refusal("folder-changed", "the store folder was replaced while it was opened"));
|
|
282
|
+
// Parents again, now that any missing ones exist.
|
|
283
|
+
if (uid !== undefined) {
|
|
284
|
+
const parents = parentsRefusal(directory, uid, home);
|
|
285
|
+
if (parents)
|
|
286
|
+
return fail(parents);
|
|
287
|
+
}
|
|
288
|
+
const bound = platform === "linux" && fs.existsSync(`/proc/self/fd/${folderFd}`) ? `/proc/self/fd/${folderFd}` : undefined;
|
|
289
|
+
return { ok: true, value: new HeldStore(directory, folderFd, held, uid, platform, options.at, bound) };
|
|
290
|
+
}
|
|
291
|
+
catch (error) {
|
|
292
|
+
return fail(ioError("checking the store folder", error));
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
class HeldStore {
|
|
296
|
+
path;
|
|
297
|
+
folderFd;
|
|
298
|
+
held;
|
|
299
|
+
uid;
|
|
300
|
+
at;
|
|
301
|
+
/** Prefix for file paths: /proc/self/fd/<n> on Linux, else the folder path. */
|
|
302
|
+
base;
|
|
303
|
+
noFollow;
|
|
304
|
+
closed = false;
|
|
305
|
+
constructor(directory, folderFd, held, uid, platform, at, bound) {
|
|
306
|
+
this.path = directory;
|
|
307
|
+
this.folderFd = folderFd;
|
|
308
|
+
this.held = held;
|
|
309
|
+
this.uid = uid;
|
|
310
|
+
this.at = at ?? (() => { });
|
|
311
|
+
this.base = bound ?? directory;
|
|
312
|
+
this.noFollow = platform === "win32" ? 0 : (fs.constants.O_NOFOLLOW ?? 0);
|
|
313
|
+
}
|
|
314
|
+
file(name) {
|
|
315
|
+
return path.join(this.base, name);
|
|
316
|
+
}
|
|
317
|
+
/** Null when the folder path still leads to the held folder and the folder is still private. */
|
|
318
|
+
check() {
|
|
319
|
+
if (this.closed)
|
|
320
|
+
return refusal("closed", "the store was closed");
|
|
321
|
+
let atPath;
|
|
322
|
+
try {
|
|
323
|
+
atPath = fs.lstatSync(this.path, { bigint: true });
|
|
324
|
+
}
|
|
325
|
+
catch (error) {
|
|
326
|
+
return refusal("folder-changed", "the store folder was replaced; nothing was done", errnoOf(error));
|
|
327
|
+
}
|
|
328
|
+
if (!sameNode(atPath, this.held))
|
|
329
|
+
return refusal("folder-changed", "the store folder was replaced; nothing was done");
|
|
330
|
+
if (this.folderFd !== undefined) {
|
|
331
|
+
try {
|
|
332
|
+
const now = fs.fstatSync(this.folderFd, { bigint: true });
|
|
333
|
+
const ownerCheck = ownerModeRefusal(now, this.uid, "folder");
|
|
334
|
+
if (ownerCheck)
|
|
335
|
+
return ownerCheck;
|
|
336
|
+
}
|
|
337
|
+
catch (error) {
|
|
338
|
+
return ioError("checking the store folder", error);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* After a file was opened: the name must still lead to the opened file, and
|
|
345
|
+
* the folder must still be the held one. Catches an open that went through a
|
|
346
|
+
* folder swapped in and back again.
|
|
347
|
+
*/
|
|
348
|
+
checkOpened(file, opened) {
|
|
349
|
+
try {
|
|
350
|
+
if (!sameNode(fs.lstatSync(file, { bigint: true }), opened)) {
|
|
351
|
+
return refusal("file-changed", "the name no longer leads to the file that was opened");
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
catch (error) {
|
|
355
|
+
return refusal("file-changed", "the name no longer leads to the file that was opened", errnoOf(error));
|
|
356
|
+
}
|
|
357
|
+
return this.check();
|
|
358
|
+
}
|
|
359
|
+
start(operation, ...names) {
|
|
360
|
+
if (this.closed)
|
|
361
|
+
return refusal("closed", "the store was closed");
|
|
362
|
+
for (const name of names) {
|
|
363
|
+
if (!isPlainName(name))
|
|
364
|
+
return refusal("bad-name", "a store file name must be a plain name");
|
|
365
|
+
}
|
|
366
|
+
this.at("before-check", operation);
|
|
367
|
+
return this.check();
|
|
368
|
+
}
|
|
369
|
+
/** Windows has no O_NOFOLLOW: refuse a link at the name before opening it. */
|
|
370
|
+
linkBeforeOpen(file) {
|
|
371
|
+
if (this.noFollow !== 0)
|
|
372
|
+
return null;
|
|
373
|
+
try {
|
|
374
|
+
if (fs.lstatSync(file).isSymbolicLink())
|
|
375
|
+
return refusal("file-link", "the file is a symlink");
|
|
376
|
+
}
|
|
377
|
+
catch {
|
|
378
|
+
// missing: the open reports it
|
|
379
|
+
}
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
/** Remove `file` only when it is still the node this call created. */
|
|
383
|
+
unlinkIfSame(file, created) {
|
|
384
|
+
try {
|
|
385
|
+
if (sameNode(fs.lstatSync(file, { bigint: true }), created))
|
|
386
|
+
fs.unlinkSync(file);
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
// already gone
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
openRefusal(error, what) {
|
|
393
|
+
const errno = errnoOf(error);
|
|
394
|
+
if (errno === "ENOENT")
|
|
395
|
+
return refusal("missing", "the file does not exist", errno);
|
|
396
|
+
if (errno === "ELOOP" || errno === "EMLINK")
|
|
397
|
+
return refusal("file-link", "the file is a symlink", errno);
|
|
398
|
+
// ENXIO: a FIFO with no reader, opened non-blocking for writing.
|
|
399
|
+
if (errno === "EISDIR" || errno === "ENXIO" || errno === "ENODEV")
|
|
400
|
+
return refusal("not-regular-file", "the file is not a regular file", errno);
|
|
401
|
+
return ioError(what, error);
|
|
402
|
+
}
|
|
403
|
+
/** Rename with Windows retries; the folder is compared again before every retry. Throws what the rename throws. */
|
|
404
|
+
renameWithRetry(from, to, options) {
|
|
405
|
+
const delays = options.retryDelaysMs ?? [];
|
|
406
|
+
const renameFile = options.renameFile ?? fs.renameSync;
|
|
407
|
+
for (let attempt = 0;; attempt += 1) {
|
|
408
|
+
try {
|
|
409
|
+
renameFile(from, to);
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
catch (error) {
|
|
413
|
+
const errno = errnoOf(error);
|
|
414
|
+
const delay = delays[attempt];
|
|
415
|
+
if (delay === undefined || (errno !== "EPERM" && errno !== "EACCES" && errno !== "EBUSY"))
|
|
416
|
+
throw error;
|
|
417
|
+
(options.sleep ?? sleepSync)(delay);
|
|
418
|
+
const again = this.check();
|
|
419
|
+
if (again)
|
|
420
|
+
return again;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
syncFolder() {
|
|
425
|
+
if (this.folderFd === undefined)
|
|
426
|
+
return;
|
|
427
|
+
try {
|
|
428
|
+
fs.fsyncSync(this.folderFd);
|
|
429
|
+
}
|
|
430
|
+
catch {
|
|
431
|
+
// not supported for folders on every filesystem
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
writeAll(fd, data) {
|
|
435
|
+
const bytes = typeof data === "string" ? Buffer.from(data, "utf8") : data;
|
|
436
|
+
let written = 0;
|
|
437
|
+
while (written < bytes.length)
|
|
438
|
+
written += fs.writeSync(fd, bytes, written, bytes.length - written);
|
|
439
|
+
}
|
|
440
|
+
/** Open an existing file read-only and run every check on it; the caller closes the descriptor. */
|
|
441
|
+
openChecked(name, operation) {
|
|
442
|
+
const file = this.file(name);
|
|
443
|
+
this.at("before-open", operation);
|
|
444
|
+
const linked = this.linkBeforeOpen(file);
|
|
445
|
+
if (linked)
|
|
446
|
+
return linked;
|
|
447
|
+
let fd;
|
|
448
|
+
try {
|
|
449
|
+
fd = fs.openSync(file, fs.constants.O_RDONLY | this.noFollow | (fs.constants.O_NONBLOCK ?? 0));
|
|
450
|
+
}
|
|
451
|
+
catch (error) {
|
|
452
|
+
return this.openRefusal(error, "opening the file");
|
|
453
|
+
}
|
|
454
|
+
try {
|
|
455
|
+
const stat = fs.fstatSync(fd, { bigint: true });
|
|
456
|
+
const statCheck = fileStatRefusal(stat, this.uid);
|
|
457
|
+
if (statCheck) {
|
|
458
|
+
fs.closeSync(fd);
|
|
459
|
+
return statCheck;
|
|
460
|
+
}
|
|
461
|
+
this.at("after-open", operation);
|
|
462
|
+
const opened = this.checkOpened(file, stat);
|
|
463
|
+
if (opened) {
|
|
464
|
+
fs.closeSync(fd);
|
|
465
|
+
return opened;
|
|
466
|
+
}
|
|
467
|
+
return { ok: true, value: { fd, stat } };
|
|
468
|
+
}
|
|
469
|
+
catch (error) {
|
|
470
|
+
fs.closeSync(fd);
|
|
471
|
+
return ioError("checking the file", error);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
appendLine(name, line, options = {}) {
|
|
475
|
+
const refused = this.start("appendLine", name);
|
|
476
|
+
if (refused)
|
|
477
|
+
return refused;
|
|
478
|
+
const file = this.file(name);
|
|
479
|
+
const text = line.endsWith("\n") ? line : `${line}\n`;
|
|
480
|
+
const { O_WRONLY, O_APPEND, O_CREAT, O_EXCL } = fs.constants;
|
|
481
|
+
const flags = O_WRONLY | O_APPEND | this.noFollow | (fs.constants.O_NONBLOCK ?? 0);
|
|
482
|
+
this.at("before-open", "appendLine");
|
|
483
|
+
const linked = this.linkBeforeOpen(file);
|
|
484
|
+
if (linked)
|
|
485
|
+
return linked;
|
|
486
|
+
let fd;
|
|
487
|
+
let created;
|
|
488
|
+
try {
|
|
489
|
+
try {
|
|
490
|
+
fd = fs.openSync(file, flags);
|
|
491
|
+
}
|
|
492
|
+
catch (error) {
|
|
493
|
+
if (errnoOf(error) !== "ENOENT")
|
|
494
|
+
throw error;
|
|
495
|
+
// O_EXCL never follows a link and fails on any name already there.
|
|
496
|
+
fd = fs.openSync(file, flags | O_CREAT | O_EXCL, 0o600);
|
|
497
|
+
created = fs.fstatSync(fd, { bigint: true });
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
catch (error) {
|
|
501
|
+
if (fd !== undefined)
|
|
502
|
+
fs.closeSync(fd);
|
|
503
|
+
// A missing name after O_CREAT means the folder itself went away.
|
|
504
|
+
const result = this.openRefusal(error, "opening the file");
|
|
505
|
+
return result.code === "missing" ? this.check() ?? result : result;
|
|
506
|
+
}
|
|
507
|
+
try {
|
|
508
|
+
const stat = fs.fstatSync(fd, { bigint: true });
|
|
509
|
+
const statCheck = fileStatRefusal(stat, this.uid);
|
|
510
|
+
if (statCheck)
|
|
511
|
+
return statCheck;
|
|
512
|
+
// The open may have gone through a folder swapped in after the check.
|
|
513
|
+
this.at("after-open", "appendLine");
|
|
514
|
+
const afterOpen = this.checkOpened(file, stat);
|
|
515
|
+
if (afterOpen) {
|
|
516
|
+
if (created)
|
|
517
|
+
this.unlinkIfSame(file, created);
|
|
518
|
+
return afterOpen;
|
|
519
|
+
}
|
|
520
|
+
this.at("before-write", "appendLine");
|
|
521
|
+
const beforeWrite = this.check();
|
|
522
|
+
if (beforeWrite) {
|
|
523
|
+
if (created)
|
|
524
|
+
this.unlinkIfSame(file, created);
|
|
525
|
+
return beforeWrite;
|
|
526
|
+
}
|
|
527
|
+
if (options.maxBytes !== undefined && Number(stat.size) >= options.maxBytes) {
|
|
528
|
+
return refusal("too-large", `the file is already ${stat.size} bytes`);
|
|
529
|
+
}
|
|
530
|
+
// One write: whole lines, in some order, when several processes append at once.
|
|
531
|
+
this.writeAll(fd, text);
|
|
532
|
+
const after = fs.fstatSync(fd);
|
|
533
|
+
return { ok: true, value: { size: after.size, unlinked: after.nlink === 0 } };
|
|
534
|
+
}
|
|
535
|
+
catch (error) {
|
|
536
|
+
return ioError("appending to the file", error);
|
|
537
|
+
}
|
|
538
|
+
finally {
|
|
539
|
+
fs.closeSync(fd);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
writeAtomic(name, data, options = {}) {
|
|
543
|
+
const refused = this.start("writeAtomic", name);
|
|
544
|
+
if (refused)
|
|
545
|
+
return refused;
|
|
546
|
+
const { O_WRONLY, O_CREAT, O_EXCL } = fs.constants;
|
|
547
|
+
const temporary = this.file(`.${name.slice(0, 64)}.${process.pid}.${randomBytes(16).toString("hex")}.tmp`);
|
|
548
|
+
const destination = this.file(name);
|
|
549
|
+
this.at("before-temp-open", "writeAtomic");
|
|
550
|
+
let fd;
|
|
551
|
+
let created;
|
|
552
|
+
try {
|
|
553
|
+
fd = fs.openSync(temporary, O_WRONLY | O_CREAT | O_EXCL | this.noFollow, 0o600);
|
|
554
|
+
created = fs.fstatSync(fd, { bigint: true });
|
|
555
|
+
// The open may have gone through a folder swapped in after the check.
|
|
556
|
+
this.at("after-open", "writeAtomic");
|
|
557
|
+
const afterOpen = this.checkOpened(temporary, created);
|
|
558
|
+
if (afterOpen)
|
|
559
|
+
return afterOpen;
|
|
560
|
+
this.writeAll(fd, data);
|
|
561
|
+
fs.fsyncSync(fd);
|
|
562
|
+
fs.closeSync(fd);
|
|
563
|
+
fd = undefined;
|
|
564
|
+
this.at("before-rename", "writeAtomic");
|
|
565
|
+
const beforeRename = this.check();
|
|
566
|
+
if (beforeRename)
|
|
567
|
+
return beforeRename;
|
|
568
|
+
const retried = this.renameWithRetry(temporary, destination, options);
|
|
569
|
+
if (retried)
|
|
570
|
+
return retried;
|
|
571
|
+
created = undefined;
|
|
572
|
+
}
|
|
573
|
+
catch (error) {
|
|
574
|
+
return ioError("writing the file", error);
|
|
575
|
+
}
|
|
576
|
+
finally {
|
|
577
|
+
if (fd !== undefined)
|
|
578
|
+
fs.closeSync(fd);
|
|
579
|
+
// Removes the temporary file this call created, and only that node.
|
|
580
|
+
if (created)
|
|
581
|
+
this.unlinkIfSame(temporary, created);
|
|
582
|
+
}
|
|
583
|
+
this.syncFolder();
|
|
584
|
+
this.at("after-rename", "writeAtomic");
|
|
585
|
+
const afterRename = this.check();
|
|
586
|
+
if (afterRename) {
|
|
587
|
+
return refusal("folder-changed-after", "the store folder was replaced during the rename; the file may have been written into the replacement", afterRename.errno);
|
|
588
|
+
}
|
|
589
|
+
return { ok: true, value: undefined };
|
|
590
|
+
}
|
|
591
|
+
read(name, options = {}) {
|
|
592
|
+
const read = this.readWithFacts(name, options);
|
|
593
|
+
return read.ok ? { ok: true, value: read.value.data } : read;
|
|
594
|
+
}
|
|
595
|
+
readWithFacts(name, options = {}) {
|
|
596
|
+
const refused = this.start("read", name);
|
|
597
|
+
if (refused)
|
|
598
|
+
return refused;
|
|
599
|
+
const maxBytes = options.maxBytes ?? DEFAULT_READ_MAX_BYTES;
|
|
600
|
+
const opened = this.openChecked(name, "read");
|
|
601
|
+
if (!opened.ok)
|
|
602
|
+
return opened;
|
|
603
|
+
const { fd, stat } = opened.value;
|
|
604
|
+
try {
|
|
605
|
+
if (stat.size > BigInt(maxBytes))
|
|
606
|
+
return refusal("too-large", `the file is larger than ${maxBytes} bytes`);
|
|
607
|
+
const buffer = Buffer.alloc(maxBytes + 1);
|
|
608
|
+
let length = 0;
|
|
609
|
+
for (;;) {
|
|
610
|
+
const count = fs.readSync(fd, buffer, length, buffer.length - length, null);
|
|
611
|
+
if (count === 0)
|
|
612
|
+
break;
|
|
613
|
+
length += count;
|
|
614
|
+
if (length > maxBytes)
|
|
615
|
+
return refusal("too-large", `the file is larger than ${maxBytes} bytes`);
|
|
616
|
+
}
|
|
617
|
+
// Bigint stats round mtimeMs down to a whole millisecond; mtimeNs keeps the fraction.
|
|
618
|
+
const facts = { size: length, mtimeMs: Number(stat.mtimeNs) / 1e6, mtimeNs: stat.mtimeNs };
|
|
619
|
+
return { ok: true, value: { data: Buffer.from(buffer.subarray(0, length)), facts } };
|
|
620
|
+
}
|
|
621
|
+
catch (error) {
|
|
622
|
+
return ioError("reading the file", error);
|
|
623
|
+
}
|
|
624
|
+
finally {
|
|
625
|
+
fs.closeSync(fd);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
createExclusive(name, data) {
|
|
629
|
+
const refused = this.start("createExclusive", name);
|
|
630
|
+
if (refused)
|
|
631
|
+
return refused;
|
|
632
|
+
const file = this.file(name);
|
|
633
|
+
const { O_WRONLY, O_CREAT, O_EXCL } = fs.constants;
|
|
634
|
+
this.at("before-open", "createExclusive");
|
|
635
|
+
let fd;
|
|
636
|
+
let created;
|
|
637
|
+
try {
|
|
638
|
+
try {
|
|
639
|
+
// O_EXCL fails on any entry at the name, a dangling link included, and never follows one.
|
|
640
|
+
fd = fs.openSync(file, O_WRONLY | O_CREAT | O_EXCL | this.noFollow | (fs.constants.O_NONBLOCK ?? 0), 0o600);
|
|
641
|
+
}
|
|
642
|
+
catch (error) {
|
|
643
|
+
if (errnoOf(error) === "EEXIST")
|
|
644
|
+
return this.check() ?? { ok: true, value: false };
|
|
645
|
+
// A missing folder in the path means the folder itself went away.
|
|
646
|
+
if (errnoOf(error) === "ENOENT")
|
|
647
|
+
return this.check() ?? this.openRefusal(error, "creating the file");
|
|
648
|
+
return this.openRefusal(error, "creating the file");
|
|
649
|
+
}
|
|
650
|
+
created = fs.fstatSync(fd, { bigint: true });
|
|
651
|
+
const statCheck = fileStatRefusal(created, this.uid);
|
|
652
|
+
if (statCheck)
|
|
653
|
+
return statCheck;
|
|
654
|
+
this.at("after-open", "createExclusive");
|
|
655
|
+
const afterOpen = this.checkOpened(file, created);
|
|
656
|
+
if (afterOpen)
|
|
657
|
+
return afterOpen;
|
|
658
|
+
if (data !== undefined) {
|
|
659
|
+
this.writeAll(fd, data);
|
|
660
|
+
fs.fsyncSync(fd);
|
|
661
|
+
}
|
|
662
|
+
created = undefined;
|
|
663
|
+
return { ok: true, value: true };
|
|
664
|
+
}
|
|
665
|
+
catch (error) {
|
|
666
|
+
return ioError("creating the file", error);
|
|
667
|
+
}
|
|
668
|
+
finally {
|
|
669
|
+
if (fd !== undefined)
|
|
670
|
+
fs.closeSync(fd);
|
|
671
|
+
// Refused or failed after the create: remove that node, and only that node.
|
|
672
|
+
if (created)
|
|
673
|
+
this.unlinkIfSame(file, created);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
stat(name) {
|
|
677
|
+
const refused = this.start("stat", name);
|
|
678
|
+
if (refused)
|
|
679
|
+
return refused;
|
|
680
|
+
const opened = this.openChecked(name, "stat");
|
|
681
|
+
if (!opened.ok)
|
|
682
|
+
return opened;
|
|
683
|
+
fs.closeSync(opened.value.fd);
|
|
684
|
+
const { stat } = opened.value;
|
|
685
|
+
// Bigint stats round mtimeMs down to a whole millisecond; mtimeNs keeps the fraction.
|
|
686
|
+
return { ok: true, value: { size: Number(stat.size), mtimeMs: Number(stat.mtimeNs) / 1e6 } };
|
|
687
|
+
}
|
|
688
|
+
rename(from, to, options = {}) {
|
|
689
|
+
const refused = this.start("rename", from, to);
|
|
690
|
+
if (refused)
|
|
691
|
+
return refused;
|
|
692
|
+
const source = this.file(from);
|
|
693
|
+
try {
|
|
694
|
+
const stat = fs.lstatSync(source, { bigint: true });
|
|
695
|
+
const statCheck = fileStatRefusal(stat, this.uid);
|
|
696
|
+
if (statCheck)
|
|
697
|
+
return statCheck;
|
|
698
|
+
}
|
|
699
|
+
catch (error) {
|
|
700
|
+
return this.openRefusal(error, "checking the file");
|
|
701
|
+
}
|
|
702
|
+
this.at("before-rename", "rename");
|
|
703
|
+
const beforeRename = this.check();
|
|
704
|
+
if (beforeRename)
|
|
705
|
+
return beforeRename;
|
|
706
|
+
try {
|
|
707
|
+
const retried = this.renameWithRetry(source, this.file(to), options);
|
|
708
|
+
if (retried)
|
|
709
|
+
return retried;
|
|
710
|
+
}
|
|
711
|
+
catch (error) {
|
|
712
|
+
return this.openRefusal(error, "renaming the file");
|
|
713
|
+
}
|
|
714
|
+
this.syncFolder();
|
|
715
|
+
this.at("after-rename", "rename");
|
|
716
|
+
const afterRename = this.check();
|
|
717
|
+
if (afterRename) {
|
|
718
|
+
return refusal("folder-changed-after", "the store folder was replaced during the rename; the file may have been renamed in the replacement", afterRename.errno);
|
|
719
|
+
}
|
|
720
|
+
return { ok: true, value: undefined };
|
|
721
|
+
}
|
|
722
|
+
remove(name) {
|
|
723
|
+
const refused = this.start("remove", name);
|
|
724
|
+
if (refused)
|
|
725
|
+
return refused;
|
|
726
|
+
this.at("before-remove", "remove");
|
|
727
|
+
const beforeRemove = this.check();
|
|
728
|
+
if (beforeRemove)
|
|
729
|
+
return beforeRemove;
|
|
730
|
+
try {
|
|
731
|
+
// unlink removes a link itself and never its target; it refuses a folder.
|
|
732
|
+
fs.unlinkSync(this.file(name));
|
|
733
|
+
return { ok: true, value: true };
|
|
734
|
+
}
|
|
735
|
+
catch (error) {
|
|
736
|
+
const errno = errnoOf(error);
|
|
737
|
+
if (errno === "ENOENT")
|
|
738
|
+
return { ok: true, value: false };
|
|
739
|
+
if (errno === "EISDIR" || errno === "EPERM")
|
|
740
|
+
return refusal("not-regular-file", "the name is a folder", errno);
|
|
741
|
+
return ioError("removing the file", error);
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
list() {
|
|
745
|
+
const refused = this.start("list");
|
|
746
|
+
if (refused)
|
|
747
|
+
return refused;
|
|
748
|
+
let names;
|
|
749
|
+
try {
|
|
750
|
+
names = fs.readdirSync(this.base);
|
|
751
|
+
}
|
|
752
|
+
catch (error) {
|
|
753
|
+
return ioError("listing the folder", error);
|
|
754
|
+
}
|
|
755
|
+
// The listing may have read a folder swapped in after the check.
|
|
756
|
+
this.at("after-list", "list");
|
|
757
|
+
const afterList = this.check();
|
|
758
|
+
if (afterList)
|
|
759
|
+
return afterList;
|
|
760
|
+
return { ok: true, value: names };
|
|
761
|
+
}
|
|
762
|
+
close() {
|
|
763
|
+
if (this.closed)
|
|
764
|
+
return;
|
|
765
|
+
this.closed = true;
|
|
766
|
+
if (this.folderFd !== undefined) {
|
|
767
|
+
try {
|
|
768
|
+
fs.closeSync(this.folderFd);
|
|
769
|
+
}
|
|
770
|
+
catch {
|
|
771
|
+
// already closed
|
|
772
|
+
}
|
|
773
|
+
this.folderFd = undefined;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
//# sourceMappingURL=safe-store.js.map
|