@fnndsc/fond 0.6.2 → 0.7.0
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/README.md +1 -3
- package/dist/errorStack.d.ts +0 -6
- package/dist/errorStack.js +0 -9
- package/dist/errorStack.js.map +1 -1
- package/dist/heldWriter.d.ts +130 -0
- package/dist/heldWriter.js +539 -0
- package/dist/heldWriter.js.map +1 -0
- package/dist/homePath.d.ts +60 -0
- package/dist/homePath.js +117 -0
- package/dist/homePath.js.map +1 -0
- package/dist/hostPlace.d.ts +275 -0
- package/dist/hostPlace.js +777 -0
- package/dist/hostPlace.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -1
- package/dist/placedPath.d.ts +250 -0
- package/dist/placedPath.js +757 -0
- package/dist/placedPath.js.map +1 -0
- package/dist/publication.d.ts +43 -0
- package/dist/publication.js +55 -0
- package/dist/publication.js.map +1 -0
- package/dist/recordFile.d.ts +61 -0
- package/dist/recordFile.js +258 -0
- package/dist/recordFile.js.map +1 -0
- package/dist/vfs/contract.js +19 -5
- package/dist/vfs/contract.js.map +1 -1
- package/dist/vfs/dispatcher.d.ts +222 -13
- package/dist/vfs/dispatcher.js +483 -73
- package/dist/vfs/dispatcher.js.map +1 -1
- package/dist/vfs/memory.d.ts +107 -3
- package/dist/vfs/memory.js +198 -24
- package/dist/vfs/memory.js.map +1 -1
- package/dist/vfs/naming.d.ts +97 -0
- package/dist/vfs/naming.js +122 -0
- package/dist/vfs/naming.js.map +1 -0
- package/dist/vfs/outcome.d.ts +72 -2
- package/dist/vfs/outcome.js +84 -3
- package/dist/vfs/outcome.js.map +1 -1
- package/dist/vfs/provider.d.ts +191 -6
- package/dist/vfs/render.d.ts +7 -0
- package/dist/vfs/render.js +16 -13
- package/dist/vfs/render.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file The writer's Linux backend: folders held as descriptors, every name opened, made and renamed relative to
|
|
4
|
+
* the folder it is in (through `/proc/self/fd/<fd>/<name>`), so a folder swapped for a link while a file is
|
|
5
|
+
* written never takes a byte outside the operator's root, and a held folder moved out of it is never published
|
|
6
|
+
* into: a move can carry the unfinished temporary file along with it while the stream runs, and the check before
|
|
7
|
+
* the commit then refuses the write and removes that file through the held folder.
|
|
8
|
+
*
|
|
9
|
+
* How it holds:
|
|
10
|
+
* - The root is opened `O_PATH|O_DIRECTORY|O_NOFOLLOW` and must have the identity its token recorded; each part
|
|
11
|
+
* below it is opened the same way relative to the folder before it, so no link is ever followed.
|
|
12
|
+
* - A held descriptor pins a folder, not its place: before the commit and at confirmation the held folder's
|
|
13
|
+
* kernel path must still be its expected place under the root's real spelling.
|
|
14
|
+
* - The temporary file is pinned by an `O_PATH` reopen of its own write descriptor (no name, so no race and no
|
|
15
|
+
* FIFO), held until the publication is proved, so its inode cannot be freed and reused meanwhile.
|
|
16
|
+
*
|
|
17
|
+
* Chosen once per process ({@link heldFolders_available}): `/proc` must be procfs and must name a held `/` as
|
|
18
|
+
* `/`; elsewhere the pathname writer is used.
|
|
19
|
+
*
|
|
20
|
+
* Residuals: an ancestor moved between the location check and the rename; inode reuse across NFS clients.
|
|
21
|
+
*
|
|
22
|
+
* Key components:
|
|
23
|
+
* - heldFolders_available: whether this process holds folders (probed once).
|
|
24
|
+
* - heldFolder_make, heldFile_write: the folder maker and the writer, used by placedPath.
|
|
25
|
+
*
|
|
26
|
+
* Typical usage:
|
|
27
|
+
* ```ts
|
|
28
|
+
* if (heldFolders_available()) await heldFile_write(root, parts, 0o600, shared);
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* Dependencies: Node's fs; the shared checks of placedPath, handed in.
|
|
32
|
+
*
|
|
33
|
+
* @module
|
|
34
|
+
*/
|
|
35
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
36
|
+
if (k2 === undefined) k2 = k;
|
|
37
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
38
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
39
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
40
|
+
}
|
|
41
|
+
Object.defineProperty(o, k2, desc);
|
|
42
|
+
}) : (function(o, m, k, k2) {
|
|
43
|
+
if (k2 === undefined) k2 = k;
|
|
44
|
+
o[k2] = m[k];
|
|
45
|
+
}));
|
|
46
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
47
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
48
|
+
}) : function(o, v) {
|
|
49
|
+
o["default"] = v;
|
|
50
|
+
});
|
|
51
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
52
|
+
var ownKeys = function(o) {
|
|
53
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
54
|
+
var ar = [];
|
|
55
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
56
|
+
return ar;
|
|
57
|
+
};
|
|
58
|
+
return ownKeys(o);
|
|
59
|
+
};
|
|
60
|
+
return function (mod) {
|
|
61
|
+
if (mod && mod.__esModule) return mod;
|
|
62
|
+
var result = {};
|
|
63
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
64
|
+
__setModuleDefault(result, mod);
|
|
65
|
+
return result;
|
|
66
|
+
};
|
|
67
|
+
})();
|
|
68
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
69
|
+
exports.heldFolders_available = heldFolders_available;
|
|
70
|
+
exports.heldProbe_judge = heldProbe_judge;
|
|
71
|
+
exports.heldFolder_make = heldFolder_make;
|
|
72
|
+
exports.heldFile_write = heldFile_write;
|
|
73
|
+
const node_fs_1 = require("node:fs");
|
|
74
|
+
const promises_1 = require("node:fs/promises");
|
|
75
|
+
const path = __importStar(require("node:path"));
|
|
76
|
+
/** Linux's O_PATH (Node does not export it): a descriptor that names, needing only search permission. */
|
|
77
|
+
const O_PATH = 0o10000000;
|
|
78
|
+
/** procfs's magic number (statfs f_type). */
|
|
79
|
+
const PROC_SUPER_MAGIC = 0x9fa0;
|
|
80
|
+
/** What a held folder is opened with. */
|
|
81
|
+
const HOLD_FLAGS = O_PATH | node_fs_1.constants.O_DIRECTORY | node_fs_1.constants.O_NOFOLLOW;
|
|
82
|
+
/** Whether this process holds folders as descriptors: probed once. */
|
|
83
|
+
let available = null;
|
|
84
|
+
/**
|
|
85
|
+
* Whether folders can be held here: Linux, with `/proc` a real procfs that names a held `/` as `/`.
|
|
86
|
+
*
|
|
87
|
+
* @returns True when the held writer is used.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* if (heldFolders_available()) placedFile_write(placed, stream, { mode: 0o600 });
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
function heldFolders_available() {
|
|
95
|
+
if (available !== null)
|
|
96
|
+
return available;
|
|
97
|
+
available = false;
|
|
98
|
+
if (process.platform !== 'linux')
|
|
99
|
+
return available;
|
|
100
|
+
let fd = null;
|
|
101
|
+
try {
|
|
102
|
+
fd = (0, node_fs_1.openSync)('/', HOLD_FLAGS);
|
|
103
|
+
available = heldProbe_judge((0, node_fs_1.statfsSync)('/proc/self/fd').type, (0, node_fs_1.readlinkSync)(fdPath_say(fd)), (0, node_fs_1.fstatSync)(fd, { bigint: true }), (0, node_fs_1.statSync)(fdPath_say(fd), { bigint: true }));
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
available = false;
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
if (fd !== null)
|
|
110
|
+
fd_release(fd);
|
|
111
|
+
}
|
|
112
|
+
return available;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Judges the probe: `/proc/self/fd` is procfs, and a held `/` reads back as `/` with the same identity through it.
|
|
116
|
+
*
|
|
117
|
+
* @param type - The filesystem type of `/proc/self/fd`.
|
|
118
|
+
* @param link - What the held `/` reads as through it.
|
|
119
|
+
* @param held - The held `/` itself.
|
|
120
|
+
* @param through - The held `/` as reached through `/proc/self/fd/<fd>`.
|
|
121
|
+
* @returns True when folders can be held.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* heldProbe_judge(0x9fa0, '/', held, through); // true when the identities match
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
function heldProbe_judge(type, link, held, through) {
|
|
129
|
+
return Number(type) === PROC_SUPER_MAGIC && link === '/' && held.dev === through.dev && held.ino === through.ino;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The path that reaches a held folder, or a name inside it.
|
|
133
|
+
*
|
|
134
|
+
* @param fd - The held descriptor.
|
|
135
|
+
* @param name - A name inside it.
|
|
136
|
+
* @returns `/proc/self/fd/<fd>[/<name>]`.
|
|
137
|
+
*/
|
|
138
|
+
function fdPath_say(fd, name) {
|
|
139
|
+
return name === undefined ? `/proc/self/fd/${String(fd)}` : `/proc/self/fd/${String(fd)}/${name}`;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Releases one of this module's own read-only descriptors (an `O_PATH` folder or pin): never retried, and a close
|
|
143
|
+
* error never changes an outcome, since such a descriptor carries no deferred write.
|
|
144
|
+
*
|
|
145
|
+
* @param fd - The descriptor.
|
|
146
|
+
*/
|
|
147
|
+
function fd_release(fd) {
|
|
148
|
+
try {
|
|
149
|
+
(0, node_fs_1.closeSync)(fd);
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
// Released either way: the number is never closed twice.
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Proves a held folder is still at its place: its kernel path is the expected one.
|
|
157
|
+
*
|
|
158
|
+
* @param fd - The held folder.
|
|
159
|
+
* @param expected - Its place under the root's real spelling.
|
|
160
|
+
* @throws {Error} When it has moved (or was removed).
|
|
161
|
+
*/
|
|
162
|
+
function location_prove(fd, expected) {
|
|
163
|
+
const now = (0, node_fs_1.readlinkSync)(fdPath_say(fd));
|
|
164
|
+
if (now !== expected)
|
|
165
|
+
throw new Error(`${JSON.stringify(expected)} is no longer where it was (it is now ${JSON.stringify(now)})`);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Opens the root held, and proves it is the folder its token recorded.
|
|
169
|
+
*
|
|
170
|
+
* @param root - The root.
|
|
171
|
+
* @returns Its held descriptor (the caller releases it).
|
|
172
|
+
* @throws {Error} When it is not the folder it was.
|
|
173
|
+
*/
|
|
174
|
+
function root_open(root) {
|
|
175
|
+
const fd = (0, node_fs_1.openSync)(root.real, HOLD_FLAGS);
|
|
176
|
+
try {
|
|
177
|
+
const stood = (0, node_fs_1.fstatSync)(fd, { bigint: true });
|
|
178
|
+
if (stood.dev !== root.dev || stood.ino !== root.ino)
|
|
179
|
+
throw new Error(`refused: the destination ${JSON.stringify(root.real)} is no longer the folder it was; nothing was written`);
|
|
180
|
+
return fd;
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
fd_release(fd);
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Holds the folder at a token's parts: the root by identity, each part below opened relative to the one before,
|
|
189
|
+
* never through a link.
|
|
190
|
+
*
|
|
191
|
+
* @param root - The root.
|
|
192
|
+
* @param parts - The parts below it.
|
|
193
|
+
* @returns The held descriptor of the last part (the caller releases it).
|
|
194
|
+
* @throws {Error} When a part is not a folder of its own, cannot be held, or the root is not the folder it was.
|
|
195
|
+
*/
|
|
196
|
+
function folder_hold(root, parts) {
|
|
197
|
+
let fd = root_open(root);
|
|
198
|
+
try {
|
|
199
|
+
let place = root.real;
|
|
200
|
+
for (const part of parts) {
|
|
201
|
+
place = path.join(place, part);
|
|
202
|
+
// The child is owned before the parent is let go: one descriptor is held at every step, closed once.
|
|
203
|
+
const parent = fd;
|
|
204
|
+
fd = part_open(parent, part, place);
|
|
205
|
+
fd_release(parent);
|
|
206
|
+
}
|
|
207
|
+
return fd;
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
fd_release(fd);
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Makes one part owner-only inside its held parent, the parent's place checked first (EEXIST is fine).
|
|
216
|
+
*
|
|
217
|
+
* @param parent - The held parent.
|
|
218
|
+
* @param part - The part's name.
|
|
219
|
+
* @param place - Its place under the root's real spelling.
|
|
220
|
+
* @returns True when it was made here; false when it stood already.
|
|
221
|
+
* @throws {Error} When the parent has moved, or the part cannot be made.
|
|
222
|
+
*/
|
|
223
|
+
function part_make(parent, part, place) {
|
|
224
|
+
try {
|
|
225
|
+
location_prove(parent, path.dirname(place));
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
throw new Error(`refused: ${error instanceof Error ? error.message : String(error)}; nothing was made`);
|
|
229
|
+
}
|
|
230
|
+
try {
|
|
231
|
+
(0, node_fs_1.mkdirSync)(fdPath_say(parent, part), { mode: 0o700 });
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
const code = error.code ?? 'EIO';
|
|
236
|
+
if (code !== 'EEXIST')
|
|
237
|
+
throw new Error(`refused: making ${JSON.stringify(place)} could not be confirmed (${code})`);
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Holds one part inside its held parent, never through a link.
|
|
243
|
+
*
|
|
244
|
+
* @param parent - The held parent.
|
|
245
|
+
* @param part - The part's name.
|
|
246
|
+
* @param place - Its place, for the words.
|
|
247
|
+
* @returns The part's held descriptor.
|
|
248
|
+
* @throws {Error} When it is missing, or not a folder of its own.
|
|
249
|
+
*/
|
|
250
|
+
function part_open(parent, part, place) {
|
|
251
|
+
try {
|
|
252
|
+
return (0, node_fs_1.openSync)(fdPath_say(parent, part), HOLD_FLAGS);
|
|
253
|
+
}
|
|
254
|
+
catch (error) {
|
|
255
|
+
const code = error.code ?? 'EIO';
|
|
256
|
+
throw new Error(code === 'ENOENT' ? `refused: ${JSON.stringify(place)} could not be looked at (ENOENT); nothing was written` : `refused: ${JSON.stringify(place)} is not a folder of its own inside the destination (${code}); nothing was written`);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Makes a placed folder through held folders, and proves it stands at its place.
|
|
261
|
+
*
|
|
262
|
+
* @param root - The root.
|
|
263
|
+
* @param parts - The folder's parts.
|
|
264
|
+
* @param shared - The shared checks.
|
|
265
|
+
* @throws {Error} When a part cannot be made or held, or the folder is not at its place.
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```ts
|
|
269
|
+
* heldFolder_make(root, ['anat', 'run1'], { root_prove });
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
function heldFolder_make(root, parts, shared) {
|
|
273
|
+
shared.root_prove();
|
|
274
|
+
// The folders this making made, by place: never removed again (their ownership cannot be proved by a name), and
|
|
275
|
+
// said when the making fails, so nothing is left unsaid.
|
|
276
|
+
const made = [];
|
|
277
|
+
let fd = root_open(root);
|
|
278
|
+
try {
|
|
279
|
+
let place = root.real;
|
|
280
|
+
for (const part of parts) {
|
|
281
|
+
place = path.join(place, part);
|
|
282
|
+
if (part_make(fd, part, place))
|
|
283
|
+
made.push(place);
|
|
284
|
+
// One folder held at a time: the child is owned before the parent is let go.
|
|
285
|
+
const parent = fd;
|
|
286
|
+
fd = part_open(parent, part, place);
|
|
287
|
+
fd_release(parent);
|
|
288
|
+
}
|
|
289
|
+
try {
|
|
290
|
+
location_prove(fd, place);
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
throw new Error(`refused: ${error instanceof Error ? error.message : String(error)}; the folder was not confirmed`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
catch (error) {
|
|
297
|
+
const said = error instanceof Error ? error.message : String(error);
|
|
298
|
+
// When folders were made, the refusal does not also say nothing was.
|
|
299
|
+
const message = made.length === 0 ? said : said.replace(/; nothing was (made|written)$/, '');
|
|
300
|
+
throw new Error(made.length === 0 ? message : `${message}; the folder(s) it made were left as they are: ${made.map((one) => JSON.stringify(one)).join(', ')} (named where they were made; a folder moved meanwhile took them with it)`);
|
|
301
|
+
}
|
|
302
|
+
finally {
|
|
303
|
+
fd_release(fd);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Writes a placed file through its held folder: a temporary file made there, pinned, written, closed, checked
|
|
308
|
+
* where it stands, and renamed over the name within the same folder (the commit point), then proved.
|
|
309
|
+
*
|
|
310
|
+
* @param root - The root.
|
|
311
|
+
* @param parts - The file's parts (its folder's, then its name).
|
|
312
|
+
* @param mode - The final mode, already checked.
|
|
313
|
+
* @param shared - The shared checks and the stream.
|
|
314
|
+
* @returns The bytes written.
|
|
315
|
+
* @throws {Error} When the write is refused or fails before the commit; an `UnconfirmedPublication` after it.
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* ```ts
|
|
319
|
+
* const written: number = await heldFile_write(root, ['anat', 't1.nii'], 0o600, shared);
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
322
|
+
async function heldFile_write(root, parts, mode, shared) {
|
|
323
|
+
const name = parts[parts.length - 1] ?? '';
|
|
324
|
+
const folderAt = path.join(root.real, ...parts.slice(0, -1));
|
|
325
|
+
shared.root_prove();
|
|
326
|
+
const dir = folder_hold(root, parts.slice(0, -1));
|
|
327
|
+
try {
|
|
328
|
+
const at = {
|
|
329
|
+
dir,
|
|
330
|
+
shown: path.join(folderAt, name),
|
|
331
|
+
target: fdPath_say(dir, name),
|
|
332
|
+
temporary: fdPath_say(dir, shared.temporary),
|
|
333
|
+
root_check: () => { shared.root_prove(); },
|
|
334
|
+
folderAt,
|
|
335
|
+
place_check: () => {
|
|
336
|
+
shared.root_prove();
|
|
337
|
+
try {
|
|
338
|
+
location_prove(dir, folderAt);
|
|
339
|
+
}
|
|
340
|
+
catch (error) {
|
|
341
|
+
throw new Error(`refused: ${error instanceof Error ? error.message : String(error)}; nothing was published`);
|
|
342
|
+
}
|
|
343
|
+
},
|
|
344
|
+
};
|
|
345
|
+
at.place_check();
|
|
346
|
+
shared.standing_judge(at.target, at.shown);
|
|
347
|
+
return await temporary_publish(at, mode, shared);
|
|
348
|
+
}
|
|
349
|
+
finally {
|
|
350
|
+
fd_release(dir);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Makes, pins, writes and publishes the temporary file; before the commit a failure removes it (when its name
|
|
355
|
+
* still holds this write's inode), after it a failed proof is an unconfirmed publication.
|
|
356
|
+
*
|
|
357
|
+
* @param at - Where.
|
|
358
|
+
* @param mode - The final mode.
|
|
359
|
+
* @param shared - The shared checks and the stream.
|
|
360
|
+
* @returns The bytes written.
|
|
361
|
+
* @throws {Error} When refused or failed.
|
|
362
|
+
*/
|
|
363
|
+
async function temporary_publish(at, mode, shared) {
|
|
364
|
+
const handle = await (0, promises_1.open)(at.temporary, node_fs_1.constants.O_WRONLY | node_fs_1.constants.O_CREAT | node_fs_1.constants.O_EXCL | node_fs_1.constants.O_NOFOLLOW | node_fs_1.constants.O_NONBLOCK, 0o600);
|
|
365
|
+
const state = { open: true, pin: null, published: false, held: null };
|
|
366
|
+
try {
|
|
367
|
+
state.held = await handle.stat({ bigint: true });
|
|
368
|
+
state.pin = temporary_pin(handle, state.held);
|
|
369
|
+
const written = await shared.stream_write(handle);
|
|
370
|
+
await handle.sync();
|
|
371
|
+
await handle.chmod(mode);
|
|
372
|
+
state.open = false;
|
|
373
|
+
await handle.close();
|
|
374
|
+
const committed = temporary_commit(at, state.pin.pinned, shared);
|
|
375
|
+
// From here the file may be published: never removed, never written again.
|
|
376
|
+
state.published = true;
|
|
377
|
+
if (committed !== 'renamed')
|
|
378
|
+
throw shared.unconfirmed_make(`publication unconfirmed: ${committed}; it was left as it is, and so was the unfinished file ${JSON.stringify(path.join(path.dirname(at.shown), shared.temporary))} if it is still there`);
|
|
379
|
+
publication_confirm(at, state.pin.pinned, shared);
|
|
380
|
+
temporary_unlinkIfLeft(at, state.pin.pinned);
|
|
381
|
+
return written;
|
|
382
|
+
}
|
|
383
|
+
catch (error) {
|
|
384
|
+
if (state.published)
|
|
385
|
+
throw error;
|
|
386
|
+
// Removed while the writer or the pin still holds its inode, so the name is matched to this write's file.
|
|
387
|
+
const left = state.held === null ? 'its unfinished file could not be looked at, so it was left' : temporary_remove(at.temporary, state.held);
|
|
388
|
+
if (state.open)
|
|
389
|
+
await handle.close().catch(() => {
|
|
390
|
+
// The write failed already; a failed close of its temporary adds nothing to say.
|
|
391
|
+
});
|
|
392
|
+
if (left === null)
|
|
393
|
+
throw error;
|
|
394
|
+
throw new Error(`${error instanceof Error ? error.message : String(error)}; and ${left}`);
|
|
395
|
+
}
|
|
396
|
+
finally {
|
|
397
|
+
// The outcome stands as it is: a confirmed or unconfirmed publication is never turned into a retryable error.
|
|
398
|
+
if (state.pin !== null)
|
|
399
|
+
fd_release(state.pin.fd);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Pins the temporary file by reopening its own write descriptor `O_PATH`: the same inode, no name, no read
|
|
404
|
+
* permission needed.
|
|
405
|
+
*
|
|
406
|
+
* @param handle - The writer.
|
|
407
|
+
* @param held - What the writer holds.
|
|
408
|
+
* @returns The pin.
|
|
409
|
+
* @throws {Error} When the pin is not the writer's file.
|
|
410
|
+
*/
|
|
411
|
+
function temporary_pin(handle, held) {
|
|
412
|
+
const fd = (0, node_fs_1.openSync)(fdPath_say(handle.fd), O_PATH);
|
|
413
|
+
try {
|
|
414
|
+
const pinned = (0, node_fs_1.fstatSync)(fd, { bigint: true });
|
|
415
|
+
if (pinned.dev !== held.dev || pinned.ino !== held.ino || !pinned.isFile())
|
|
416
|
+
throw new Error('refused: the file being written could not be pinned; nothing was published');
|
|
417
|
+
return { fd, pinned };
|
|
418
|
+
}
|
|
419
|
+
catch (error) {
|
|
420
|
+
fd_release(fd);
|
|
421
|
+
throw error;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* The last checks and the commit: not cancelled, the temporary name still holds the pinned file, what stands at
|
|
426
|
+
* the name may be replaced, the folder is still at its place; then the rename within the held folder.
|
|
427
|
+
*
|
|
428
|
+
* @param at - Where.
|
|
429
|
+
* @param pinned - The pinned file.
|
|
430
|
+
* @param shared - The shared checks.
|
|
431
|
+
* @returns `renamed`, or the words for a rename whose outcome cannot be told.
|
|
432
|
+
* @throws {Error} When a check fails, or the rename was not done (nothing published).
|
|
433
|
+
*/
|
|
434
|
+
function temporary_commit(at, pinned, shared) {
|
|
435
|
+
if (shared.cancel_read())
|
|
436
|
+
throw new Error('cancelled: the write was stopped before it was published');
|
|
437
|
+
const standing = (0, node_fs_1.lstatSync)(at.temporary, { bigint: true });
|
|
438
|
+
if (standing.dev !== pinned.dev || standing.ino !== pinned.ino)
|
|
439
|
+
throw new Error('refused: the file being written is not where it was made; nothing was published');
|
|
440
|
+
shared.standing_judge(at.target, at.shown);
|
|
441
|
+
at.place_check();
|
|
442
|
+
try {
|
|
443
|
+
(0, node_fs_1.renameSync)(at.temporary, at.target);
|
|
444
|
+
return 'renamed';
|
|
445
|
+
}
|
|
446
|
+
catch (error) {
|
|
447
|
+
return renameError_settle(at, pinned, error);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
/** Rename errors that say the rename was not done: anything else (EIO, ESTALE, a timeout) leaves it unknown. */
|
|
451
|
+
const RENAME_REFUSED = new Set(['EXDEV', 'EACCES', 'EPERM', 'EROFS', 'EISDIR', 'ENOTDIR', 'ENOTEMPTY', 'EBUSY', 'EINVAL', 'ENAMETOOLONG', 'ELOOP', 'ENOSPC', 'EDQUOT', 'EMLINK']);
|
|
452
|
+
/**
|
|
453
|
+
* Settles a rename that reported an error, by the pin (a server may rename and still report an error, as NFS
|
|
454
|
+
* does): the name proved to hold the pinned file was published; an error that says the rename was not done
|
|
455
|
+
* published nothing; anything else is not known, and is reported as unconfirmed, never retried, however the
|
|
456
|
+
* temporary name looks (a lookup after such an error may be a stale one).
|
|
457
|
+
*
|
|
458
|
+
* @param at - Where.
|
|
459
|
+
* @param pinned - The pinned file.
|
|
460
|
+
* @param error - What the rename reported.
|
|
461
|
+
* @returns `renamed`, or the unconfirmed outcome's words.
|
|
462
|
+
* @throws {Error} A refusal when the rename was not done.
|
|
463
|
+
*/
|
|
464
|
+
function renameError_settle(at, pinned, error) {
|
|
465
|
+
const code = error.code ?? 'EIO';
|
|
466
|
+
let looked;
|
|
467
|
+
try {
|
|
468
|
+
const now = (0, node_fs_1.lstatSync)(at.target, { bigint: true });
|
|
469
|
+
if (now.dev === pinned.dev && now.ino === pinned.ino)
|
|
470
|
+
return 'renamed';
|
|
471
|
+
looked = 'it holds another file';
|
|
472
|
+
}
|
|
473
|
+
catch (lookError) {
|
|
474
|
+
looked = `it could not be looked at (${lookError.code ?? 'EIO'})`;
|
|
475
|
+
}
|
|
476
|
+
if (RENAME_REFUSED.has(code))
|
|
477
|
+
throw new Error(`refused: ${JSON.stringify(at.shown)} could not be published (${code}); nothing was published`);
|
|
478
|
+
return `the rename reported ${code}, and ${JSON.stringify(at.shown)} could not be confirmed: ${looked}`;
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Removes the temporary name if it still holds the published file after the rename (a hard link made to the name
|
|
482
|
+
* first turns the rename into a no-op): the pin is still open, so the match is certain.
|
|
483
|
+
*
|
|
484
|
+
* @param at - Where.
|
|
485
|
+
* @param pinned - The published file.
|
|
486
|
+
*/
|
|
487
|
+
function temporary_unlinkIfLeft(at, pinned) {
|
|
488
|
+
try {
|
|
489
|
+
const now = (0, node_fs_1.lstatSync)(at.temporary, { bigint: true });
|
|
490
|
+
if (now.dev === pinned.dev && now.ino === pinned.ino)
|
|
491
|
+
(0, node_fs_1.unlinkSync)(at.temporary);
|
|
492
|
+
}
|
|
493
|
+
catch {
|
|
494
|
+
// Gone, as it should be after a rename; or not ours, and so left.
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Proves a publication: the folder still at its place, the name holding the pinned file.
|
|
499
|
+
*
|
|
500
|
+
* @param at - Where.
|
|
501
|
+
* @param pinned - The pinned file.
|
|
502
|
+
* @param shared - The shared checks.
|
|
503
|
+
* @throws {Error} An unconfirmed publication when it cannot be proved.
|
|
504
|
+
*/
|
|
505
|
+
function publication_confirm(at, pinned, shared) {
|
|
506
|
+
try {
|
|
507
|
+
at.root_check();
|
|
508
|
+
location_prove(at.dir, at.folderAt);
|
|
509
|
+
const now = (0, node_fs_1.lstatSync)(at.target, { bigint: true });
|
|
510
|
+
if (now.dev !== pinned.dev || now.ino !== pinned.ino)
|
|
511
|
+
throw new Error('the name holds another file');
|
|
512
|
+
}
|
|
513
|
+
catch (error) {
|
|
514
|
+
throw shared.unconfirmed_make(`publication unconfirmed: ${JSON.stringify(at.shown)}: ${error instanceof Error ? error.message : String(error)}; it was left as it is`);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Removes the temporary file through its held folder, only when its name still holds this write's file (its inode
|
|
519
|
+
* held open meanwhile, by the writer or the pin, so it cannot have been reused).
|
|
520
|
+
*
|
|
521
|
+
* @param temporary - Its path through the held folder.
|
|
522
|
+
* @param held - The file's identity as the writer opened it.
|
|
523
|
+
* @returns Null when removed (or already gone); else what was left, said.
|
|
524
|
+
*/
|
|
525
|
+
function temporary_remove(temporary, held) {
|
|
526
|
+
try {
|
|
527
|
+
const now = (0, node_fs_1.lstatSync)(temporary, { bigint: true });
|
|
528
|
+
if (now.dev !== held.dev || now.ino !== held.ino)
|
|
529
|
+
return 'its unfinished file could not be confirmed as this write\'s, so it was left';
|
|
530
|
+
(0, node_fs_1.unlinkSync)(temporary);
|
|
531
|
+
return null;
|
|
532
|
+
}
|
|
533
|
+
catch (error) {
|
|
534
|
+
if (error.code === 'ENOENT')
|
|
535
|
+
return null;
|
|
536
|
+
return `its unfinished file could not be removed with certainty (${error instanceof Error ? error.message : String(error)})`;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
//# sourceMappingURL=heldWriter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"heldWriter.js","sourceRoot":"","sources":["../src/heldWriter.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoEH,sDAcC;AAgBD,0CAEC;AA6ID,0CA6BC;AAwDD,wCA4BC;AAhWD,qCAAuL;AACvL,+CAAqE;AACrE,gDAAkC;AAElC,yGAAyG;AACzG,MAAM,MAAM,GAAW,UAAU,CAAC;AAElC,6CAA6C;AAC7C,MAAM,gBAAgB,GAAW,MAAM,CAAC;AAExC,yCAAyC;AACzC,MAAM,UAAU,GAAW,MAAM,GAAG,mBAAW,CAAC,WAAW,GAAG,mBAAW,CAAC,UAAU,CAAC;AA0CrF,sEAAsE;AACtE,IAAI,SAAS,GAAmB,IAAI,CAAC;AAErC;;;;;;;;;GASG;AACH,SAAgB,qBAAqB;IACnC,IAAI,SAAS,KAAK,IAAI;QAAE,OAAO,SAAS,CAAC;IACzC,SAAS,GAAG,KAAK,CAAC;IAClB,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO;QAAE,OAAO,SAAS,CAAC;IACnD,IAAI,EAAE,GAAkB,IAAI,CAAC;IAC7B,IAAI,CAAC;QACH,EAAE,GAAG,IAAA,kBAAQ,EAAC,GAAG,EAAE,UAAU,CAAC,CAAC;QAC/B,SAAS,GAAG,eAAe,CAAC,IAAA,oBAAU,EAAC,eAAe,CAAC,CAAC,IA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C;IAAC,OAAO,KAAc,EAAE,CAAC;QACxB,IAAK,KAA+B,CAAC,IAAI,KAAK,QAAQ;YAAE,OAAO,IAAI,CAAC;QACpE,OAAO,4DAA4D,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC;IAC/H,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The home folder's two readings every package shares: a typed `~`,
|
|
3
|
+
* and the XDG base folders.
|
|
4
|
+
*
|
|
5
|
+
* Key components:
|
|
6
|
+
* - home_expand: `~` and `~/…` made the home folder; anything else as given.
|
|
7
|
+
* - xdgDir_resolve: `$XDG_<KIND>_HOME` when it names an absolute folder,
|
|
8
|
+
* else the home's default for that kind.
|
|
9
|
+
*
|
|
10
|
+
* Typical usage:
|
|
11
|
+
* ```ts
|
|
12
|
+
* const jobs: string = path.join(xdgDir_resolve('state', env), 'brasa', 'jobs');
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* Dependencies: node:os, node:path.
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* A path with a leading `~` made the home folder, as a shell would: `~` alone
|
|
21
|
+
* and `~/` followed by a path. Anything else is returned as given: a
|
|
22
|
+
* relative path stays relative (the caller decides what it is relative to),
|
|
23
|
+
* and `~name` is another account's home, which this does not look up.
|
|
24
|
+
*
|
|
25
|
+
* @param typed - The path as typed.
|
|
26
|
+
* @param home - Reads the home folder, asked only for a path that starts with `~` (a test's seam).
|
|
27
|
+
* @returns The path, its `~` expanded.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* home_expand('~/.ssh/id_ed25519.pub'); // '/home/chris/.ssh/id_ed25519.pub'
|
|
32
|
+
* home_expand('keys/id.pub'); // 'keys/id.pub'
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare function home_expand(typed: string, home?: () => string): string;
|
|
36
|
+
/** The XDG base folders a program keeps its own files under. */
|
|
37
|
+
export type XdgKind = 'config' | 'state' | 'cache' | 'data';
|
|
38
|
+
/**
|
|
39
|
+
* An XDG base folder, as the specification reads it: the variable when it
|
|
40
|
+
* names an absolute path, else the kind's default under the home folder. An
|
|
41
|
+
* empty variable is unset, and a relative one is passed over (the
|
|
42
|
+
* specification calls it invalid), so neither puts a program's files in
|
|
43
|
+
* whatever folder it was started in.
|
|
44
|
+
*
|
|
45
|
+
* The result is spelled, not normalised: a `..` in the variable or the home
|
|
46
|
+
* reaches the caller's folder check rather than being folded away first,
|
|
47
|
+
* and a trailing `/` is trimmed from the home.
|
|
48
|
+
*
|
|
49
|
+
* @param kind - Which base folder.
|
|
50
|
+
* @param env - The environment holding the variable; `process.env` in production.
|
|
51
|
+
* @param home - Reads the home folder, asked only when the variable does not answer (a test's seam).
|
|
52
|
+
* @returns The base folder, absolute when the home is.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* xdgDir_resolve('cache', { XDG_CACHE_HOME: '' }, () => '/home/chris'); // '/home/chris/.cache'
|
|
57
|
+
* xdgDir_resolve('state', { XDG_STATE_HOME: '/s' }); // '/s'
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function xdgDir_resolve(kind: XdgKind, env?: Readonly<Record<string, string | undefined>>, home?: () => string): string;
|