@openclaw/fs-safe 0.18.0 → 0.18.2
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 +18 -0
- package/dist/advanced.d.ts +4 -2
- package/dist/advanced.js +1 -1
- package/dist/archive-durability.d.ts +1 -1
- package/dist/archive-merge.js +0 -1
- package/dist/archive-parser.wasm +0 -0
- package/dist/archive-plan.d.ts +64 -3
- package/dist/archive-plan.js +55 -1
- package/dist/archive-staging.d.ts +1 -2
- package/dist/archive-staging.js +0 -2
- package/dist/archive-tar-wasm.d.ts +1 -1
- package/dist/archive-zip-directory.d.ts +1 -1
- package/dist/archive-zip-entry.d.ts +1 -1
- package/dist/archive-zip-entry.js +1 -1
- package/dist/archive-zip-preflight.d.ts +0 -2
- package/dist/archive-zip-preflight.js +0 -1
- package/dist/archive.d.ts +14 -8
- package/dist/archive.js +127 -12
- package/dist/atomic.d.ts +3 -1
- package/dist/copy-publication.d.ts +1 -1
- package/dist/create.d.ts +16 -2
- package/dist/create.js +388 -2
- package/dist/creation-boundary.d.ts +60 -0
- package/dist/creation-boundary.js +371 -0
- package/dist/directory-guard.d.ts +0 -1
- package/dist/directory-guard.js +0 -3
- package/dist/directory-mode-node.d.ts +20 -2
- package/dist/directory-mode-node.js +89 -3
- package/dist/durability.d.ts +2 -1
- package/dist/file-lock-sync-acquisition.js +3 -3
- package/dist/file-lock-sync-root-acquire.js +2 -6
- package/dist/file-lock-sync-root-options.d.ts +2 -10
- package/dist/file-lock-sync-root.d.ts +2 -1
- package/dist/file-lock-sync-stale-admission.js +3 -10
- package/dist/file-lock-sync.d.ts +4 -21
- package/dist/file-store-boundary.d.ts +7 -0
- package/dist/file-store-boundary.js +58 -1
- package/dist/file-store-sync-write.js +2 -3
- package/dist/file-store.js +1 -6
- package/dist/guarded-mkdir.d.ts +1 -1
- package/dist/index.d.ts +9 -6
- package/dist/json-durable-queue-ownership.d.ts +1 -5
- package/dist/json-durable-queue-read.js +7 -3
- package/dist/json-durable-queue.d.ts +0 -1
- package/dist/json-durable-queue.js +1 -3
- package/dist/json-store.js +1 -2
- package/dist/json.js +1 -2
- package/dist/move-path.js +1 -2
- package/dist/native-operations.js +0 -1
- package/dist/native-pinned-write-windows.d.ts +1 -1
- package/dist/native-pinned-write-windows.js +1 -1
- package/dist/native-pinned-write.d.ts +1 -1
- package/dist/native-pinned-write.js +3 -6
- package/dist/native-policy-parent-windows.d.ts +1 -1
- package/dist/native-staged-file.d.ts +2 -2
- package/dist/native-staged-file.js +1 -2
- package/dist/permission-exec.d.ts +6 -0
- package/dist/permissions-public.d.ts +2 -1
- package/dist/permissions-windows.d.ts +3 -6
- package/dist/permissions.d.ts +3 -10
- package/dist/permissions.js +0 -1
- package/dist/pinned-mutation-admission.d.ts +1 -1
- package/dist/pinned-open.d.ts +1 -0
- package/dist/pinned-open.js +7 -2
- package/dist/pinned-operation.d.ts +1 -1
- package/dist/pinned-operation.js +3 -16
- package/dist/pinned-write-mode.js +1 -1
- package/dist/pinned-write-staged.js +1 -2
- package/dist/pinned-write.d.ts +0 -1
- package/dist/pinned-write.js +3 -5
- package/dist/private-producer-handoff-sync.d.ts +2 -12
- package/dist/private-producer-handoff-sync.js +1 -1
- package/dist/private-producer-handoff.d.ts +3 -3
- package/dist/private-temp-workspace.js +1 -2
- package/dist/publish-copy-stage.d.ts +1 -1
- package/dist/publish-file.d.ts +0 -1
- package/dist/publish-file.js +1 -2
- package/dist/replace-directory.js +6 -19
- package/dist/replace-file-copy-fallback.d.ts +1 -8
- package/dist/replace-file-descriptor.d.ts +3 -11
- package/dist/replace-file-descriptor.js +1 -1
- package/dist/replace-file-rename-policy.d.ts +1 -2
- package/dist/replace-file.d.ts +3 -3
- package/dist/replace-file.js +1 -1
- package/dist/retained-directory-replacement.d.ts +1 -0
- package/dist/retained-directory-replacement.js +1 -1
- package/dist/root-context.js +3 -7
- package/dist/root-create-input.d.ts +1 -1
- package/dist/root-directory-creation.d.ts +1 -1
- package/dist/root-directory-creation.js +1 -1
- package/dist/root-directory-list.js +3 -7
- package/dist/root-entries.js +2 -2
- package/dist/root-errors.d.ts +4 -0
- package/dist/root-errors.js +5 -3
- package/dist/root-file-final-admission.d.ts +1 -1
- package/dist/root-file-final-admission.js +1 -6
- package/dist/root-file.js +2 -5
- package/dist/root-impl.d.ts +0 -5
- package/dist/root-impl.js +22 -21
- package/dist/root-move-noreplace.js +4 -10
- package/dist/root-observed-path.js +2 -4
- package/dist/root-options.d.ts +1 -1
- package/dist/root-path-existing.js +2 -3
- package/dist/root-path-stat.js +2 -4
- package/dist/root-public-types.d.ts +11 -0
- package/dist/root-remove-identity.js +2 -4
- package/dist/root-remove.js +2 -4
- package/dist/root-write-admission.d.ts +1 -1
- package/dist/root-write-admission.js +3 -7
- package/dist/root-write-complete-parent.js +2 -3
- package/dist/root-write-verification.d.ts +1 -1
- package/dist/root.d.ts +2 -6
- package/dist/secret-file.js +11 -17
- package/dist/secret-read-policy.js +1 -1
- package/dist/secure-file-windows.js +4 -9
- package/dist/secure-temp-dir.js +2 -7
- package/dist/secure-temp-repair.js +5 -1
- package/dist/sidecar-lock-acquire.js +8 -11
- package/dist/sidecar-lock-policy.d.ts +1 -0
- package/dist/sidecar-lock-policy.js +7 -0
- package/dist/sidecar-lock-reclaim.js +5 -6
- package/dist/sidecar-lock-stale-admission.js +2 -6
- package/dist/sidecar-lock-types.d.ts +22 -16
- package/dist/stat-observation.js +3 -10
- package/dist/strict-file-identity.d.ts +2 -0
- package/dist/strict-file-identity.js +6 -6
- package/dist/temp-target.js +3 -8
- package/dist/temp-workspace-admission.js +7 -15
- package/dist/temp-workspace-child-admission.d.ts +16 -2
- package/dist/temp-workspace-child-admission.js +65 -3
- package/dist/temp-workspace-descriptor.d.ts +1 -1
- package/dist/temp-workspace-descriptor.js +2 -4
- package/dist/text-atomic.js +1 -1
- package/dist/windows-owner.d.ts +3 -6
- package/dist/windows-owner.js +1 -1
- package/dist/windows-path-alias.d.ts +1 -0
- package/dist/windows-path-alias.js +5 -0
- package/dist/windows-security-bridge.cs +0 -2
- package/docs/advanced.md +3 -1
- package/docs/secret-file.md +13 -0
- package/docs/sidecar-lock.md +6 -0
- package/docs/store.md +6 -0
- package/docs/temp.md +8 -1
- package/docs/writing.md +5 -0
- package/package.json +8 -8
- package/dist/archive-native.d.ts +0 -10
- package/dist/archive-native.js +0 -73
- package/dist/archive-options.d.ts +0 -30
- package/dist/archive-policy.d.ts +0 -21
- package/dist/archive-policy.js +0 -38
- package/dist/archive-tar-inspect.d.ts +0 -6
- package/dist/archive-tar-inspect.js +0 -62
- package/dist/archive-tar.d.ts +0 -18
- package/dist/archive-tar.js +0 -21
- package/dist/create-directory.d.ts +0 -18
- package/dist/create-directory.js +0 -111
- package/dist/create-file-async.d.ts +0 -6
- package/dist/create-file-async.js +0 -121
- package/dist/create-file.d.ts +0 -7
- package/dist/create-file.js +0 -179
- package/dist/creation-darwin.d.ts +0 -5
- package/dist/creation-darwin.js +0 -70
- package/dist/creation-file-state.d.ts +0 -18
- package/dist/creation-file-state.js +0 -118
- package/dist/creation-path.d.ts +0 -22
- package/dist/creation-path.js +0 -83
- package/dist/creation-permissions.d.ts +0 -18
- package/dist/creation-permissions.js +0 -115
- package/dist/directory-mode-owner.d.ts +0 -20
- package/dist/directory-mode-owner.js +0 -78
- package/dist/file-store-copy-source.d.ts +0 -4
- package/dist/file-store-copy-source.js +0 -31
- package/dist/file-store-limit.d.ts +0 -1
- package/dist/file-store-limit.js +0 -7
- package/dist/file-store-path.d.ts +0 -2
- package/dist/file-store-path.js +0 -30
- package/dist/standalone-publication-path.d.ts +0 -1
- package/dist/standalone-publication-path.js +0 -6
- package/dist/temp-workspace-identity.d.ts +0 -14
- package/dist/temp-workspace-identity.js +0 -41
- package/dist/temp-workspace-permissions.d.ts +0 -3
- package/dist/temp-workspace-permissions.js +0 -32
- /package/dist/{archive-options.js → root-public-types.js} +0 -0
package/dist/create.js
CHANGED
|
@@ -1,2 +1,388 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import fs, {} from "node:fs";
|
|
3
|
+
import fsAsync, {} from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { ownFileDescriptorSync } from "./create-owned-file.js";
|
|
6
|
+
import { assertDirectoryIdentitySync, inspectDirectoryIdentitySync } from "./directory-guard.js";
|
|
7
|
+
import { FsSafeError } from "./errors.js";
|
|
8
|
+
import { assertSynchronousCallbackResult } from "./mutation-authority.js";
|
|
9
|
+
import { assertPrivateFileCreationAvailable, inspectCreationDirectorySync, protectCreatedFileSync, resolveCreationPermissions, verifyCreatedFileSync, assertPrivateDirectory, assertPrivateDirectorySync, inspectCreationDirectory, protectCreatedFile, verifyCreatedFile, assertBeforeCreation, creationCollision, prepareCreationPath, removeCreationDirectory, removeCreationDirectoryAsync, assertCreationFile, assertPrivateCreationFile, creationPublicationAfterFailure, privateFileSettlementFailure, rethrowPrivateStageCreationFailure, removeRecordedCreationFileSync, removeRecordedCreationFile, } from "./creation-boundary.js";
|
|
10
|
+
import { handoffCreatedFileSync } from "./private-producer-handoff-sync.js";
|
|
11
|
+
import { resolveReadOpenFlags } from "./read-open-flags.js";
|
|
12
|
+
import { getNativeBinding } from "./native.js";
|
|
13
|
+
import { getFsSafeNativeConfig } from "./native-config.js";
|
|
14
|
+
import { createPrivateWindowsDirectoryCommand, createPrivateWindowsDirectoryCommandSync, hasUnsettledWindowsSecurityCommand, } from "./windows-security-command.js";
|
|
15
|
+
import { warnNativeFallback } from "./native-fallback-warning.js";
|
|
16
|
+
import { handoffCreatedFile } from "./private-producer-handoff.js";
|
|
17
|
+
function createPrivateWindowsFile(selected, options, mode) {
|
|
18
|
+
const assertion = options.assertBeforeMutation;
|
|
19
|
+
if (fs.lstatSync(selected.target, { throwIfNoEntry: false })) {
|
|
20
|
+
throw new FsSafeError("already-exists", "creation target already exists");
|
|
21
|
+
}
|
|
22
|
+
const parentIdentity = inspectCreationDirectorySync(selected.parent.dir, false);
|
|
23
|
+
selected.assertParent();
|
|
24
|
+
const stageDirectory = path.join(selected.parent.dir, `.fs-safe-create-${randomUUID()}`);
|
|
25
|
+
let stage;
|
|
26
|
+
try {
|
|
27
|
+
stage = createDirectoryWithReceiptSync(stageDirectory, { private: true, assertBeforeMutation: assertion }, {
|
|
28
|
+
expectedParentIdentity: selected.parent.stat,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
rethrowPrivateStageCreationFailure(error, selected.target, stageDirectory);
|
|
33
|
+
}
|
|
34
|
+
const stageIdentity = stage.windowsIdentity;
|
|
35
|
+
const stagePath = path.join(stageDirectory, "file");
|
|
36
|
+
const assertStage = () => {
|
|
37
|
+
selected.assertParent();
|
|
38
|
+
assertDirectoryIdentitySync(stageDirectory, stage.stat);
|
|
39
|
+
};
|
|
40
|
+
let file;
|
|
41
|
+
let identity;
|
|
42
|
+
let windowsIdentity;
|
|
43
|
+
let publication = "not-published";
|
|
44
|
+
try {
|
|
45
|
+
if (inspectCreationDirectorySync(stageDirectory, true) !== stageIdentity) {
|
|
46
|
+
throw new FsSafeError("path-mismatch", "private staging directory changed before file creation");
|
|
47
|
+
}
|
|
48
|
+
assertSynchronousCallbackResult(assertion?.(), "assertBeforeMutation");
|
|
49
|
+
assertStage();
|
|
50
|
+
file = ownFileDescriptorSync(fs.openSync(stagePath, fs.constants.O_RDWR | fs.constants.O_CREAT |
|
|
51
|
+
fs.constants.O_EXCL | resolveReadOpenFlags(), 0o600));
|
|
52
|
+
identity = assertCreationFile(file.fd, stagePath);
|
|
53
|
+
// The inherited ACL must already be restrictive. This freezes that policy;
|
|
54
|
+
// the bridge refuses to repair broad access on an exposed file.
|
|
55
|
+
assertSynchronousCallbackResult(assertion?.(), "assertBeforeMutation");
|
|
56
|
+
assertStage();
|
|
57
|
+
assertCreationFile(file.fd, stagePath, identity);
|
|
58
|
+
windowsIdentity = protectCreatedFileSync(file.fd, stagePath, stageIdentity);
|
|
59
|
+
assertStage();
|
|
60
|
+
file = handoffCreatedFileSync({
|
|
61
|
+
source: file, sourcePath: stagePath, targetPath: selected.target, identity,
|
|
62
|
+
assertSourceParent: assertStage, assertTargetParent: selected.assertParent,
|
|
63
|
+
assertBeforeMutation: assertion,
|
|
64
|
+
verifyDescriptor: (fd, pathname, links) => verifyCreatedFileSync(fd, pathname, windowsIdentity, pathname === stagePath ? stageIdentity : parentIdentity, links),
|
|
65
|
+
onPublished: () => { publication = "published"; },
|
|
66
|
+
});
|
|
67
|
+
// Windows may clear the read-only attribute while retiring the stage name.
|
|
68
|
+
// Finalize it only after the published-name descriptor owns the file.
|
|
69
|
+
fs.fchmodSync(file.fd, mode);
|
|
70
|
+
selected.assertParent();
|
|
71
|
+
identity = assertCreationFile(file.fd, selected.target, identity);
|
|
72
|
+
verifyCreatedFileSync(file.fd, selected.target, windowsIdentity, parentIdentity);
|
|
73
|
+
removeCreationDirectory(stageDirectory, stage.stat, selected.assertParent);
|
|
74
|
+
return file;
|
|
75
|
+
}
|
|
76
|
+
catch (primary) {
|
|
77
|
+
publication = creationPublicationAfterFailure(primary, publication);
|
|
78
|
+
const cleanup = [];
|
|
79
|
+
if (file) {
|
|
80
|
+
try {
|
|
81
|
+
file.close();
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
cleanup.push(error);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (publication !== "indeterminate") {
|
|
88
|
+
if (identity) {
|
|
89
|
+
try {
|
|
90
|
+
removeRecordedCreationFileSync(stagePath, identity, assertStage);
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
cleanup.push(error);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
removeCreationDirectory(stageDirectory, stage.stat, selected.assertParent);
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
cleanup.push(error);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (publication === "not-published" && cleanup.length === 0)
|
|
104
|
+
throw creationCollision(primary);
|
|
105
|
+
throw privateFileSettlementFailure({ primary, cleanup, publication, path: selected.target, stageDirectory });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function createFileCore(targetPath, options, admission) {
|
|
109
|
+
const permissions = resolveCreationPermissions(options, false);
|
|
110
|
+
const assertion = options.assertBeforeMutation;
|
|
111
|
+
const selected = prepareCreationPath(targetPath, admission.expectedParentIdentity);
|
|
112
|
+
if (permissions.private && process.platform === "win32") {
|
|
113
|
+
assertPrivateFileCreationAvailable();
|
|
114
|
+
return createPrivateWindowsFile(selected, { ...permissions, assertBeforeMutation: assertion }, permissions.mode);
|
|
115
|
+
}
|
|
116
|
+
assertBeforeCreation(selected, permissions, assertion, "file");
|
|
117
|
+
let file;
|
|
118
|
+
try {
|
|
119
|
+
file = ownFileDescriptorSync(fs.openSync(selected.target, fs.constants.O_RDWR | fs.constants.O_CREAT |
|
|
120
|
+
fs.constants.O_EXCL | resolveReadOpenFlags(), permissions.mode ?? 0o666));
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
throw creationCollision(error);
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
const identity = assertCreationFile(file.fd, selected.target);
|
|
127
|
+
selected.assertParent();
|
|
128
|
+
if (permissions.private)
|
|
129
|
+
assertPrivateCreationFile(identity, file.fd);
|
|
130
|
+
return file;
|
|
131
|
+
}
|
|
132
|
+
catch (primary) {
|
|
133
|
+
let failure = primary;
|
|
134
|
+
try {
|
|
135
|
+
file.close();
|
|
136
|
+
}
|
|
137
|
+
catch (closeError) {
|
|
138
|
+
failure = new AggregateError([primary, closeError], "created file admission and close failed");
|
|
139
|
+
}
|
|
140
|
+
throw new FsSafeError("helper-failed", "created file admission failed", {
|
|
141
|
+
cause: failure, details: { publication: { status: "published" }, path: selected.target, cleanup: "preserved" },
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
export function createFileSync(targetPath, options = {}) {
|
|
146
|
+
return createFileCore(targetPath, options, {});
|
|
147
|
+
}
|
|
148
|
+
/** Internal async writer; Windows preparation uses the nonblocking stage owner. */
|
|
149
|
+
export async function createFileHandle(targetPath, options = {}, admission = {}) {
|
|
150
|
+
const permissions = resolveCreationPermissions(options, false);
|
|
151
|
+
const assertion = options.assertBeforeMutation;
|
|
152
|
+
if (process.platform !== "win32" || !permissions.private) {
|
|
153
|
+
const selected = prepareCreationPath(targetPath, admission.expectedParentIdentity);
|
|
154
|
+
assertBeforeCreation(selected, permissions, assertion, "file");
|
|
155
|
+
const handle = await fsAsync.open(selected.target, fs.constants.O_RDWR | fs.constants.O_CREAT |
|
|
156
|
+
fs.constants.O_EXCL | resolveReadOpenFlags(), permissions.mode ?? 0o666)
|
|
157
|
+
.catch(error => { throw creationCollision(error); });
|
|
158
|
+
try {
|
|
159
|
+
const identity = assertCreationFile(handle.fd, selected.target);
|
|
160
|
+
selected.assertParent();
|
|
161
|
+
if (permissions.private)
|
|
162
|
+
assertPrivateCreationFile(identity, handle.fd);
|
|
163
|
+
return handle;
|
|
164
|
+
}
|
|
165
|
+
catch (primary) {
|
|
166
|
+
let failure = primary;
|
|
167
|
+
try {
|
|
168
|
+
await handle.close();
|
|
169
|
+
}
|
|
170
|
+
catch (closeError) {
|
|
171
|
+
failure = new AggregateError([primary, closeError], "created file admission and close failed");
|
|
172
|
+
}
|
|
173
|
+
throw new FsSafeError("helper-failed", "created file handle admission failed", {
|
|
174
|
+
cause: failure, details: { publication: { status: "published" }, path: selected.target, cleanup: "preserved" },
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
assertPrivateFileCreationAvailable();
|
|
179
|
+
return await createPrivateWindowsFileHandle(prepareCreationPath(targetPath, admission.expectedParentIdentity), { mode: permissions.mode, assertBeforeMutation: assertion });
|
|
180
|
+
}
|
|
181
|
+
function directoryAdoptionFailure(error, target, windowsIdentity) {
|
|
182
|
+
return new FsSafeError("helper-failed", "created directory admission failed", {
|
|
183
|
+
cause: error,
|
|
184
|
+
details: {
|
|
185
|
+
publication: { status: "published" }, path: target, cleanup: "preserved",
|
|
186
|
+
...(windowsIdentity === undefined ? {} : { windowsIdentity }),
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
function privateDirectoryBackend() {
|
|
191
|
+
const binding = getNativeBinding();
|
|
192
|
+
if (typeof binding?.createPrivateDirectoryWithParentIdentity === "function") {
|
|
193
|
+
return binding.createPrivateDirectoryWithParentIdentity.bind(binding);
|
|
194
|
+
}
|
|
195
|
+
if (getFsSafeNativeConfig().mode === "require") {
|
|
196
|
+
throw new FsSafeError("helper-unavailable", "private directory creation requires an up-to-date native parent identity capability");
|
|
197
|
+
}
|
|
198
|
+
warnNativeFallback("windows-private-directory", "Private Windows directory creation uses a slower built-in system command.");
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
export async function createDirectory(targetPath, options = {}) {
|
|
202
|
+
await createDirectoryWithAdmission(targetPath, options, {});
|
|
203
|
+
}
|
|
204
|
+
export async function createDirectoryWithAdmission(targetPath, options = {}, admission = {}) {
|
|
205
|
+
await createDirectoryWithReceipt(targetPath, options, admission);
|
|
206
|
+
}
|
|
207
|
+
async function createDirectoryWithReceipt(targetPath, options = {}, admission = {}) {
|
|
208
|
+
const permissions = resolveCreationPermissions(options, true);
|
|
209
|
+
const assertion = options.assertBeforeMutation;
|
|
210
|
+
const selected = prepareCreationPath(targetPath, admission.expectedParentIdentity);
|
|
211
|
+
const backend = permissions.private && process.platform === "win32" ? privateDirectoryBackend() : undefined;
|
|
212
|
+
const parentIdentity = permissions.private && process.platform === "win32"
|
|
213
|
+
? await inspectCreationDirectory(selected.parent.dir, false) : undefined;
|
|
214
|
+
assertBeforeCreation(selected, permissions, assertion, "directory");
|
|
215
|
+
let created = false;
|
|
216
|
+
let windowsIdentity;
|
|
217
|
+
try {
|
|
218
|
+
if (parentIdentity !== undefined) {
|
|
219
|
+
const receipt = backend
|
|
220
|
+
? backend(selected.target, parentIdentity)
|
|
221
|
+
: await createPrivateWindowsDirectoryCommand(selected.target, parentIdentity);
|
|
222
|
+
created = true;
|
|
223
|
+
windowsIdentity = receipt.identity;
|
|
224
|
+
if ((await inspectCreationDirectory(selected.target, true)) !== receipt.identity) {
|
|
225
|
+
throw new FsSafeError("path-mismatch", "created private directory changed before adoption");
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
await fsAsync.mkdir(selected.target, { mode: permissions.mode });
|
|
230
|
+
created = true;
|
|
231
|
+
if (permissions.private)
|
|
232
|
+
await assertPrivateDirectory(selected.target);
|
|
233
|
+
}
|
|
234
|
+
selected.assertParent();
|
|
235
|
+
return { stat: inspectDirectoryIdentitySync(selected.target), windowsIdentity };
|
|
236
|
+
}
|
|
237
|
+
catch (error) {
|
|
238
|
+
if (created)
|
|
239
|
+
throw directoryAdoptionFailure(error, selected.target, windowsIdentity);
|
|
240
|
+
throw creationCollision(error);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
export function createDirectorySync(targetPath, options = {}) {
|
|
244
|
+
createDirectoryWithReceiptSync(targetPath, options, {});
|
|
245
|
+
}
|
|
246
|
+
function createDirectoryWithReceiptSync(targetPath, options = {}, admission = {}) {
|
|
247
|
+
const permissions = resolveCreationPermissions(options, true);
|
|
248
|
+
const assertion = options.assertBeforeMutation;
|
|
249
|
+
const selected = prepareCreationPath(targetPath, admission.expectedParentIdentity);
|
|
250
|
+
const backend = permissions.private && process.platform === "win32" ? privateDirectoryBackend() : undefined;
|
|
251
|
+
const parentIdentity = permissions.private && process.platform === "win32"
|
|
252
|
+
? inspectCreationDirectorySync(selected.parent.dir, false) : undefined;
|
|
253
|
+
assertBeforeCreation(selected, permissions, assertion, "directory");
|
|
254
|
+
let created = false;
|
|
255
|
+
let windowsIdentity;
|
|
256
|
+
try {
|
|
257
|
+
if (parentIdentity !== undefined) {
|
|
258
|
+
const receipt = backend
|
|
259
|
+
? backend(selected.target, parentIdentity)
|
|
260
|
+
: createPrivateWindowsDirectoryCommandSync(selected.target, parentIdentity);
|
|
261
|
+
created = true;
|
|
262
|
+
windowsIdentity = receipt.identity;
|
|
263
|
+
if (inspectCreationDirectorySync(selected.target, true) !== receipt.identity) {
|
|
264
|
+
throw new FsSafeError("path-mismatch", "created private directory changed before adoption");
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
fs.mkdirSync(selected.target, { mode: permissions.mode });
|
|
269
|
+
created = true;
|
|
270
|
+
if (permissions.private)
|
|
271
|
+
assertPrivateDirectorySync(selected.target);
|
|
272
|
+
}
|
|
273
|
+
selected.assertParent();
|
|
274
|
+
return { stat: inspectDirectoryIdentitySync(selected.target), windowsIdentity };
|
|
275
|
+
}
|
|
276
|
+
catch (error) {
|
|
277
|
+
if (created)
|
|
278
|
+
throw directoryAdoptionFailure(error, selected.target, windowsIdentity);
|
|
279
|
+
throw creationCollision(error);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
async function createPrivateWindowsFileHandle(selected, options) {
|
|
283
|
+
const assertion = options.assertBeforeMutation;
|
|
284
|
+
const existing = await fsAsync.lstat(selected.target).catch((error) => {
|
|
285
|
+
if (error.code !== "ENOENT")
|
|
286
|
+
throw error;
|
|
287
|
+
return undefined;
|
|
288
|
+
});
|
|
289
|
+
selected.assertParent();
|
|
290
|
+
if (existing)
|
|
291
|
+
throw new FsSafeError("already-exists", "creation target already exists");
|
|
292
|
+
const parentIdentity = await inspectCreationDirectory(selected.parent.dir, false);
|
|
293
|
+
selected.assertParent();
|
|
294
|
+
const stageDirectory = path.join(selected.parent.dir, `.fs-safe-create-${randomUUID()}`);
|
|
295
|
+
let stage;
|
|
296
|
+
try {
|
|
297
|
+
stage = await createDirectoryWithReceipt(stageDirectory, { private: true, assertBeforeMutation: assertion }, {
|
|
298
|
+
expectedParentIdentity: selected.parent.stat,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
catch (error) {
|
|
302
|
+
rethrowPrivateStageCreationFailure(error, selected.target, stageDirectory);
|
|
303
|
+
}
|
|
304
|
+
const stageIdentity = stage.windowsIdentity;
|
|
305
|
+
const stagePath = path.join(stageDirectory, "file");
|
|
306
|
+
const assertStage = () => {
|
|
307
|
+
selected.assertParent();
|
|
308
|
+
assertDirectoryIdentitySync(stageDirectory, stage.stat);
|
|
309
|
+
};
|
|
310
|
+
let file;
|
|
311
|
+
let identity;
|
|
312
|
+
let publication = "not-published";
|
|
313
|
+
try {
|
|
314
|
+
if (await inspectCreationDirectory(stageDirectory, true) !== stageIdentity) {
|
|
315
|
+
throw new FsSafeError("path-mismatch", "private staging directory changed before file creation");
|
|
316
|
+
}
|
|
317
|
+
assertSynchronousCallbackResult(assertion?.(), "assertBeforeMutation");
|
|
318
|
+
assertStage();
|
|
319
|
+
file = await fsAsync.open(stagePath, fs.constants.O_RDWR | fs.constants.O_CREAT |
|
|
320
|
+
fs.constants.O_EXCL | resolveReadOpenFlags(), 0o600);
|
|
321
|
+
assertStage();
|
|
322
|
+
identity = assertCreationFile(file.fd, stagePath);
|
|
323
|
+
assertSynchronousCallbackResult(assertion?.(), "assertBeforeMutation");
|
|
324
|
+
assertStage();
|
|
325
|
+
assertCreationFile(file.fd, stagePath, identity);
|
|
326
|
+
const windowsIdentity = await protectCreatedFile(file.fd, stagePath, stageIdentity);
|
|
327
|
+
assertStage();
|
|
328
|
+
assertCreationFile(file.fd, stagePath, identity);
|
|
329
|
+
const source = file;
|
|
330
|
+
file = undefined;
|
|
331
|
+
// The handoff consumes the source pin on both success and failure.
|
|
332
|
+
file = await handoffCreatedFile({
|
|
333
|
+
source, sourcePath: stagePath, targetPath: selected.target, identity,
|
|
334
|
+
assertSourceParent: assertStage, assertTargetParent: selected.assertParent,
|
|
335
|
+
assertBeforeMutation: assertion,
|
|
336
|
+
verifyDescriptor: (fd, pathname, links) => verifyCreatedFile(fd, pathname, windowsIdentity, pathname === stagePath ? stageIdentity : parentIdentity, links),
|
|
337
|
+
onPublished: () => { publication = "published"; },
|
|
338
|
+
});
|
|
339
|
+
assertSynchronousCallbackResult(assertion?.(), "assertBeforeMutation");
|
|
340
|
+
selected.assertParent();
|
|
341
|
+
assertCreationFile(file.fd, selected.target, identity);
|
|
342
|
+
await verifyCreatedFile(file.fd, selected.target, windowsIdentity, parentIdentity);
|
|
343
|
+
selected.assertParent();
|
|
344
|
+
assertCreationFile(file.fd, selected.target, identity);
|
|
345
|
+
await file.chmod(options.mode);
|
|
346
|
+
await verifyCreatedFile(file.fd, selected.target, windowsIdentity, parentIdentity);
|
|
347
|
+
selected.assertParent();
|
|
348
|
+
assertCreationFile(file.fd, selected.target, identity);
|
|
349
|
+
await removeCreationDirectoryAsync(stageDirectory, stage.stat, selected.assertParent);
|
|
350
|
+
selected.assertParent();
|
|
351
|
+
assertCreationFile(file.fd, selected.target, identity);
|
|
352
|
+
return file;
|
|
353
|
+
}
|
|
354
|
+
catch (primary) {
|
|
355
|
+
publication = creationPublicationAfterFailure(primary, publication);
|
|
356
|
+
const preserved = publication === "indeterminate" || hasUnsettledWindowsSecurityCommand(primary);
|
|
357
|
+
const cleanup = [];
|
|
358
|
+
if (file) {
|
|
359
|
+
const owned = file;
|
|
360
|
+
file = undefined;
|
|
361
|
+
try {
|
|
362
|
+
await owned.close();
|
|
363
|
+
}
|
|
364
|
+
catch (error) {
|
|
365
|
+
cleanup.push(error);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
if (!preserved) {
|
|
369
|
+
if (identity) {
|
|
370
|
+
try {
|
|
371
|
+
await removeRecordedCreationFile(stagePath, identity, assertStage);
|
|
372
|
+
}
|
|
373
|
+
catch (error) {
|
|
374
|
+
cleanup.push(error);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
try {
|
|
378
|
+
await removeCreationDirectoryAsync(stageDirectory, stage.stat, selected.assertParent);
|
|
379
|
+
}
|
|
380
|
+
catch (error) {
|
|
381
|
+
cleanup.push(error);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (publication === "not-published" && cleanup.length === 0 && !preserved)
|
|
385
|
+
throw creationCollision(primary);
|
|
386
|
+
throw privateFileSettlementFailure({ primary, cleanup, publication, path: selected.target, stageDirectory, preserved });
|
|
387
|
+
}
|
|
388
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type BigIntStats } from "node:fs";
|
|
2
|
+
import { type AsyncDirectoryGuard, type AnyAsyncDirectoryGuard } from "./directory-guard.js";
|
|
3
|
+
import { FsSafeError } from "./errors.js";
|
|
4
|
+
export type CreationPermissions = {
|
|
5
|
+
private?: boolean;
|
|
6
|
+
mode?: number;
|
|
7
|
+
};
|
|
8
|
+
export declare function resolveCreationPermissions(options: CreationPermissions, directory: boolean): {
|
|
9
|
+
private: boolean;
|
|
10
|
+
mode: number | undefined;
|
|
11
|
+
};
|
|
12
|
+
export declare function assertPrivateFileCreationAvailable(): void;
|
|
13
|
+
/** Full Windows identity, with security validation when requested; no repairs. */
|
|
14
|
+
export declare function inspectCreationDirectorySync(targetPath: string, privatePath: boolean): string;
|
|
15
|
+
export declare function inspectCreationDirectory(targetPath: string, privatePath: boolean): Promise<string>;
|
|
16
|
+
export declare function assertPrivateDirectorySync(targetPath: string): void;
|
|
17
|
+
export declare function assertPrivateDirectory(targetPath: string): Promise<void>;
|
|
18
|
+
export declare function protectCreatedFileSync(fd: number, targetPath: string, parentIdentity: string): string;
|
|
19
|
+
export declare function protectCreatedFile(fd: number, targetPath: string, parentIdentity: string): Promise<string>;
|
|
20
|
+
export declare function verifyCreatedFileSync(fd: number, targetPath: string, identity: string, parentIdentity: string, links?: number): void;
|
|
21
|
+
export declare function verifyCreatedFile(fd: number, targetPath: string, identity: string, parentIdentity: string, links?: number): Promise<void>;
|
|
22
|
+
export declare function assertDarwinPrivateCreationAvailable(): void;
|
|
23
|
+
export declare function assertDarwinPrivateDirectoryMode(mode: number): void;
|
|
24
|
+
export declare function assertDarwinCreationAcl(fd: number, inheritanceTarget?: "file" | "directory"): void;
|
|
25
|
+
export declare function assertDarwinCreationDirectoryAcl(pathname: string, expected: BigIntStats, inheritanceTarget?: "file" | "directory"): void;
|
|
26
|
+
export type CreationPublicationStatus = "not-published" | "published" | "indeterminate";
|
|
27
|
+
export declare function assertCreationFile(fd: number, pathname: string, expected?: BigIntStats): BigIntStats;
|
|
28
|
+
export declare function assertPrivateCreationFile(stat: BigIntStats, fd: number): void;
|
|
29
|
+
export declare function removeRecordedCreationFileSync(pathname: string, identity: BigIntStats, assertParent: () => void): void;
|
|
30
|
+
export declare function removeRecordedCreationFile(pathname: string, identity: BigIntStats, assertParent: () => void): Promise<void>;
|
|
31
|
+
export declare function creationPublicationAfterFailure(error: unknown, recorded: CreationPublicationStatus): CreationPublicationStatus;
|
|
32
|
+
export declare function hasPreservedCreationArtifacts(error: unknown): boolean;
|
|
33
|
+
export declare function rethrowPrivateStageCreationFailure(error: unknown, path: string, stageDirectory: string): never;
|
|
34
|
+
export declare function privateFileSettlementFailure(params: {
|
|
35
|
+
primary: unknown;
|
|
36
|
+
cleanup: readonly unknown[];
|
|
37
|
+
publication: CreationPublicationStatus;
|
|
38
|
+
path: string;
|
|
39
|
+
stageDirectory: string;
|
|
40
|
+
preserved?: boolean;
|
|
41
|
+
}): FsSafeError;
|
|
42
|
+
export type CreationParentIdentity = Readonly<{
|
|
43
|
+
dev: bigint;
|
|
44
|
+
ino: bigint;
|
|
45
|
+
realPath?: string;
|
|
46
|
+
}>;
|
|
47
|
+
export type CreationPath = {
|
|
48
|
+
target: string;
|
|
49
|
+
parent: AsyncDirectoryGuard<BigIntStats>;
|
|
50
|
+
assertParent(): void;
|
|
51
|
+
};
|
|
52
|
+
export declare function assertBeforeCreation(selected: CreationPath, permissions: CreationPermissions, assertion: (() => void) | undefined, kind: "file" | "directory"): void;
|
|
53
|
+
export declare function creationAdmissionFromParent(parent: AnyAsyncDirectoryGuard): {
|
|
54
|
+
expectedParentIdentity: CreationParentIdentity;
|
|
55
|
+
};
|
|
56
|
+
export declare function prepareCreationPath(input: string, expectedParent?: CreationParentIdentity): CreationPath;
|
|
57
|
+
export declare function creationCollision(error: unknown): unknown;
|
|
58
|
+
/** Remove only the recorded empty stage; never traverse a replaced directory. */
|
|
59
|
+
export declare function removeCreationDirectory(directory: string, identity: BigIntStats, assertParent: () => void): void;
|
|
60
|
+
export declare function removeCreationDirectoryAsync(directory: string, identity: BigIntStats, assertParent: () => void): Promise<void>;
|