@openclaw/fs-safe 0.14.0 → 0.16.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/CHANGELOG.md +88 -0
- package/README.md +38 -10
- package/dist/advanced.d.ts +2 -1
- package/dist/advanced.d.ts.map +1 -1
- package/dist/advanced.js +2 -1
- package/dist/archive-kind.d.ts +0 -1
- package/dist/archive-kind.d.ts.map +1 -1
- package/dist/archive-kind.js +5 -17
- package/dist/archive-merge.d.ts.map +1 -1
- package/dist/archive-merge.js +113 -46
- package/dist/archive-parser.wasm +0 -0
- package/dist/archive-read.d.ts.map +1 -1
- package/dist/archive-read.js +10 -11
- package/dist/archive-tar-stream.d.ts +3 -0
- package/dist/archive-tar-stream.d.ts.map +1 -1
- package/dist/archive-tar-stream.js +56 -37
- package/dist/archive-tar-wasm.d.ts +16 -4
- package/dist/archive-tar-wasm.d.ts.map +1 -1
- package/dist/archive-tar-wasm.js +134 -34
- package/dist/archive-zip-directory.d.ts +4 -0
- package/dist/archive-zip-directory.d.ts.map +1 -1
- package/dist/archive-zip-directory.js +2 -0
- package/dist/archive-zip-entry.d.ts +6 -2
- package/dist/archive-zip-entry.d.ts.map +1 -1
- package/dist/archive-zip-entry.js +23 -8
- package/dist/archive-zip-integrity.d.ts.map +1 -1
- package/dist/archive-zip-integrity.js +3 -4
- package/dist/archive-zip-loader.d.ts.map +1 -1
- package/dist/archive-zip-loader.js +107 -31
- package/dist/archive-zip-names.d.ts +1 -0
- package/dist/archive-zip-names.d.ts.map +1 -1
- package/dist/archive-zip-names.js +6 -0
- package/dist/archive.d.ts.map +1 -1
- package/dist/archive.js +11 -11
- package/dist/bounded-read-stream.d.ts +0 -1
- package/dist/bounded-read-stream.d.ts.map +1 -1
- package/dist/bounded-read-stream.js +0 -6
- package/dist/clone-metadata.d.ts +1 -0
- package/dist/clone-metadata.d.ts.map +1 -1
- package/dist/clone-metadata.js +6 -2
- package/dist/copy-publication.d.ts +6 -0
- package/dist/copy-publication.d.ts.map +1 -1
- package/dist/copy-publication.js +3 -0
- package/dist/copy-tree-portable.d.ts.map +1 -1
- package/dist/copy-tree-portable.js +44 -24
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +29 -11
- package/dist/create-directory.d.ts +20 -0
- package/dist/create-directory.d.ts.map +1 -0
- package/dist/create-directory.js +130 -0
- package/dist/create-file-async.d.ts +7 -0
- package/dist/create-file-async.d.ts.map +1 -0
- package/dist/create-file-async.js +121 -0
- package/dist/create-file.d.ts +8 -0
- package/dist/create-file.d.ts.map +1 -0
- package/dist/create-file.js +190 -0
- package/dist/create-owned-file.d.ts +8 -0
- package/dist/create-owned-file.d.ts.map +1 -0
- package/dist/create-owned-file.js +16 -0
- package/dist/create.d.ts +4 -0
- package/dist/create.d.ts.map +1 -0
- package/dist/create.js +2 -0
- package/dist/creation-darwin.d.ts +7 -0
- package/dist/creation-darwin.d.ts.map +1 -0
- package/dist/creation-darwin.js +79 -0
- package/dist/creation-file-state.d.ts +19 -0
- package/dist/creation-file-state.d.ts.map +1 -0
- package/dist/creation-file-state.js +118 -0
- package/dist/creation-path.d.ts +21 -0
- package/dist/creation-path.d.ts.map +1 -0
- package/dist/creation-path.js +71 -0
- package/dist/creation-permissions.d.ts +19 -0
- package/dist/creation-permissions.d.ts.map +1 -0
- package/dist/creation-permissions.js +125 -0
- package/dist/directory-durability.d.ts +1 -1
- package/dist/directory-durability.d.ts.map +1 -1
- package/dist/directory-durability.js +22 -80
- package/dist/directory-guard.d.ts +3 -0
- package/dist/directory-guard.d.ts.map +1 -1
- package/dist/directory-mode-node.d.ts +2 -0
- package/dist/directory-mode-node.d.ts.map +1 -1
- package/dist/directory-mode-node.js +8 -0
- package/dist/directory-mode-owner.js +5 -5
- package/dist/directory-receipt.d.ts +24 -0
- package/dist/directory-receipt.d.ts.map +1 -0
- package/dist/directory-receipt.js +127 -0
- package/dist/file-cleanup.d.ts +19 -0
- package/dist/file-cleanup.d.ts.map +1 -0
- package/dist/file-cleanup.js +78 -0
- package/dist/file-handle-transfer.d.ts +2 -0
- package/dist/file-handle-transfer.d.ts.map +1 -1
- package/dist/file-handle-transfer.js +57 -2
- package/dist/file-identity.d.ts.map +1 -1
- package/dist/file-identity.js +18 -4
- package/dist/file-lock-sync-admission.d.ts +19 -0
- package/dist/file-lock-sync-admission.d.ts.map +1 -0
- package/dist/file-lock-sync-admission.js +93 -0
- package/dist/file-lock-sync-root-acquire.d.ts +4 -0
- package/dist/file-lock-sync-root-acquire.d.ts.map +1 -0
- package/dist/file-lock-sync-root-acquire.js +370 -0
- package/dist/file-lock-sync-root-arbitration.d.ts +18 -0
- package/dist/file-lock-sync-root-arbitration.d.ts.map +1 -0
- package/dist/file-lock-sync-root-arbitration.js +66 -0
- package/dist/file-lock-sync-root-held.d.ts +34 -0
- package/dist/file-lock-sync-root-held.d.ts.map +1 -0
- package/dist/file-lock-sync-root-held.js +393 -0
- package/dist/file-lock-sync-root-io.d.ts +44 -0
- package/dist/file-lock-sync-root-io.d.ts.map +1 -0
- package/dist/file-lock-sync-root-io.js +209 -0
- package/dist/file-lock-sync-root-mutation.d.ts +17 -0
- package/dist/file-lock-sync-root-mutation.d.ts.map +1 -0
- package/dist/file-lock-sync-root-mutation.js +277 -0
- package/dist/file-lock-sync-root-options.d.ts +20 -0
- package/dist/file-lock-sync-root-options.d.ts.map +1 -0
- package/dist/file-lock-sync-root-options.js +58 -0
- package/dist/file-lock-sync-root-registration.d.ts +2 -0
- package/dist/file-lock-sync-root-registration.d.ts.map +1 -0
- package/dist/file-lock-sync-root-registration.js +90 -0
- package/dist/file-lock-sync-root.d.ts +36 -0
- package/dist/file-lock-sync-root.d.ts.map +1 -0
- package/dist/file-lock-sync-root.js +361 -0
- package/dist/file-lock-sync-stale-admission.d.ts +24 -0
- package/dist/file-lock-sync-stale-admission.d.ts.map +1 -0
- package/dist/file-lock-sync-stale-admission.js +205 -0
- package/dist/file-lock-sync.d.ts.map +1 -1
- package/dist/file-lock-sync.js +245 -205
- package/dist/file-observation.d.ts +1 -1
- package/dist/file-observation.d.ts.map +1 -1
- package/dist/file-store-boundary.d.ts +2 -6
- package/dist/file-store-boundary.d.ts.map +1 -1
- package/dist/file-store-boundary.js +3 -9
- package/dist/file-store-prune.d.ts.map +1 -1
- package/dist/file-store-prune.js +5 -1
- package/dist/file-store-sync-write.d.ts.map +1 -1
- package/dist/file-store-sync-write.js +5 -8
- package/dist/file-store.d.ts.map +1 -1
- package/dist/file-store.js +47 -12
- package/dist/guarded-mkdir.d.ts +1 -0
- package/dist/guarded-mkdir.d.ts.map +1 -1
- package/dist/guarded-mkdir.js +36 -7
- package/dist/json-document-store.d.ts.map +1 -1
- package/dist/json-document-store.js +22 -15
- package/dist/json-durable-queue-ownership.d.ts +0 -1
- package/dist/json-durable-queue-ownership.d.ts.map +1 -1
- package/dist/json-durable-queue-ownership.js +0 -6
- package/dist/move-path.js +1 -1
- package/dist/native-binding.d.ts +13 -1
- package/dist/native-binding.d.ts.map +1 -1
- package/dist/native-fallback-warning.d.ts +4 -0
- package/dist/native-fallback-warning.d.ts.map +1 -0
- package/dist/native-fallback-warning.js +11 -0
- package/dist/native-operations.d.ts +0 -2
- package/dist/native-operations.d.ts.map +1 -1
- package/dist/native-operations.js +0 -24
- package/dist/native-parent-admission.d.ts +5 -2
- package/dist/native-parent-admission.d.ts.map +1 -1
- package/dist/native-parent-admission.js +27 -7
- package/dist/native-pinned-write-windows.d.ts +1 -1
- package/dist/native-pinned-write-windows.d.ts.map +1 -1
- package/dist/native-pinned-write-windows.js +174 -29
- package/dist/native-pinned-write.d.ts.map +1 -1
- package/dist/native-pinned-write.js +26 -14
- package/dist/native-policy-parent-windows.d.ts +14 -0
- package/dist/native-policy-parent-windows.d.ts.map +1 -0
- package/dist/native-policy-parent-windows.js +209 -0
- package/dist/native-rename-outcome.d.ts +4 -0
- package/dist/native-rename-outcome.d.ts.map +1 -0
- package/dist/native-rename-outcome.js +8 -0
- package/dist/native-staged-file.d.ts +3 -2
- package/dist/native-staged-file.d.ts.map +1 -1
- package/dist/native-staged-file.js +121 -72
- package/dist/output.d.ts.map +1 -1
- package/dist/output.js +12 -8
- package/dist/owner-dacl.d.ts.map +1 -1
- package/dist/owner-dacl.js +10 -4
- package/dist/path-prefix.d.ts.map +1 -1
- package/dist/path-prefix.js +30 -8
- package/dist/path-suffix-aliases.d.ts +2 -0
- package/dist/path-suffix-aliases.d.ts.map +1 -1
- package/dist/path-suffix-aliases.js +25 -17
- package/dist/permission-exec.d.ts +2 -0
- package/dist/permission-exec.d.ts.map +1 -1
- package/dist/permission-exec.js +150 -21
- package/dist/permissions-windows.js +1 -1
- package/dist/pinned-mutation-admission.d.ts.map +1 -1
- package/dist/pinned-mutation-admission.js +10 -5
- package/dist/pinned-mutation-observation.d.ts +0 -1
- package/dist/pinned-mutation-observation.d.ts.map +1 -1
- package/dist/pinned-mutation-observation.js +0 -19
- package/dist/pinned-mutation-shared-route.d.ts +1 -0
- package/dist/pinned-mutation-shared-route.d.ts.map +1 -1
- package/dist/pinned-mutation-shared-route.js +1 -1
- package/dist/pinned-write-input.d.ts +4 -0
- package/dist/pinned-write-input.d.ts.map +1 -0
- package/dist/pinned-write-input.js +25 -0
- package/dist/pinned-write-mode.d.ts +5 -0
- package/dist/pinned-write-mode.d.ts.map +1 -0
- package/dist/pinned-write-mode.js +24 -0
- package/dist/pinned-write-staged.d.ts +6 -0
- package/dist/pinned-write-staged.d.ts.map +1 -0
- package/dist/pinned-write-staged.js +187 -0
- package/dist/pinned-write-types.d.ts +5 -0
- package/dist/pinned-write-types.d.ts.map +1 -1
- package/dist/pinned-write.d.ts.map +1 -1
- package/dist/pinned-write.js +35 -145
- package/dist/private-directory.d.ts.map +1 -1
- package/dist/private-directory.js +18 -4
- package/dist/private-producer-handoff-sync.d.ts +14 -0
- package/dist/private-producer-handoff-sync.d.ts.map +1 -0
- package/dist/private-producer-handoff-sync.js +114 -0
- package/dist/private-producer-handoff.d.ts +22 -4
- package/dist/private-producer-handoff.d.ts.map +1 -1
- package/dist/private-producer-handoff.js +140 -77
- package/dist/private-temp-workspace.d.ts.map +1 -1
- package/dist/private-temp-workspace.js +75 -121
- package/dist/publish-copy-stage.d.ts +2 -1
- package/dist/publish-copy-stage.d.ts.map +1 -1
- package/dist/publish-copy-stage.js +16 -7
- package/dist/publish-file.d.ts.map +1 -1
- package/dist/publish-file.js +2 -2
- package/dist/regular-file.d.ts.map +1 -1
- package/dist/regular-file.js +1 -15
- package/dist/replace-directory.d.ts.map +1 -1
- package/dist/replace-directory.js +256 -18
- package/dist/replace-file-copy-fallback.d.ts.map +1 -1
- package/dist/replace-file-copy-fallback.js +62 -70
- package/dist/replace-file-copy-source.d.ts.map +1 -1
- package/dist/replace-file-copy-source.js +10 -12
- package/dist/replace-file-temp-owner.d.ts +5 -9
- package/dist/replace-file-temp-owner.d.ts.map +1 -1
- package/dist/replace-file-temp-owner.js +56 -72
- package/dist/replace-file.js +6 -6
- package/dist/retained-directory-replacement.d.ts +26 -0
- package/dist/retained-directory-replacement.d.ts.map +1 -0
- package/dist/retained-directory-replacement.js +193 -0
- package/dist/root-boundary.d.ts +1 -0
- package/dist/root-boundary.d.ts.map +1 -1
- package/dist/root-boundary.js +4 -0
- package/dist/root-context.d.ts +0 -8
- package/dist/root-context.d.ts.map +1 -1
- package/dist/root-context.js +0 -3
- package/dist/root-create-input.d.ts +8 -1
- package/dist/root-create-input.d.ts.map +1 -1
- package/dist/root-create-input.js +17 -4
- package/dist/root-directory-creation.d.ts +3 -3
- package/dist/root-directory-creation.d.ts.map +1 -1
- package/dist/root-directory-creation.js +15 -3
- package/dist/root-directory-list.d.ts +1 -0
- package/dist/root-directory-list.d.ts.map +1 -1
- package/dist/root-directory-list.js +1 -0
- package/dist/root-impl.d.ts.map +1 -1
- package/dist/root-impl.js +46 -17
- package/dist/root-move-noreplace.d.ts.map +1 -1
- package/dist/root-move-noreplace.js +24 -15
- package/dist/root-options.d.ts +12 -4
- package/dist/root-options.d.ts.map +1 -1
- package/dist/root-path-errors.d.ts +1 -0
- package/dist/root-path-errors.d.ts.map +1 -1
- package/dist/root-path-errors.js +11 -2
- package/dist/root-path-existing.d.ts.map +1 -1
- package/dist/root-path-existing.js +11 -35
- package/dist/root-path-stat.d.ts.map +1 -1
- package/dist/root-path-stat.js +59 -7
- package/dist/root-path.js +1 -13
- package/dist/root-remove.d.ts +1 -0
- package/dist/root-remove.d.ts.map +1 -1
- package/dist/root-remove.js +4 -0
- package/dist/root-walk.d.ts +1 -1
- package/dist/root-walk.d.ts.map +1 -1
- package/dist/root-walk.js +17 -2
- package/dist/root-write-admission.d.ts +0 -2
- package/dist/root-write-admission.d.ts.map +1 -1
- package/dist/root-write-admission.js +1 -15
- package/dist/root-write-complete-parent.d.ts.map +1 -1
- package/dist/root-write-complete-parent.js +7 -23
- package/dist/root-write-publication.js +1 -1
- package/dist/root-write-verification.d.ts.map +1 -1
- package/dist/root-write-verification.js +29 -42
- package/dist/secret-file.d.ts.map +1 -1
- package/dist/secret-file.js +3 -24
- package/dist/secret-read-async.d.ts.map +1 -1
- package/dist/secret-read-async.js +3 -24
- package/dist/secret-read-policy.d.ts +6 -2
- package/dist/secret-read-policy.d.ts.map +1 -1
- package/dist/secret-read-policy.js +26 -2
- package/dist/secure-file-windows.d.ts +6 -0
- package/dist/secure-file-windows.d.ts.map +1 -1
- package/dist/secure-file-windows.js +34 -117
- package/dist/secure-file.js +2 -2
- package/dist/sibling-temp.d.ts.map +1 -1
- package/dist/sibling-temp.js +23 -12
- package/dist/sidecar-lock-acquire.d.ts +2 -28
- package/dist/sidecar-lock-acquire.d.ts.map +1 -1
- package/dist/sidecar-lock-acquire.js +288 -199
- package/dist/sidecar-lock-admission-context.d.ts +19 -0
- package/dist/sidecar-lock-admission-context.d.ts.map +1 -0
- package/dist/sidecar-lock-admission-context.js +60 -0
- package/dist/sidecar-lock-admission-parser.d.ts +43 -0
- package/dist/sidecar-lock-admission-parser.d.ts.map +1 -0
- package/dist/sidecar-lock-admission-parser.js +113 -0
- package/dist/sidecar-lock-admission.d.ts +35 -0
- package/dist/sidecar-lock-admission.d.ts.map +1 -0
- package/dist/sidecar-lock-admission.js +7 -0
- package/dist/sidecar-lock-reclaim.d.ts +9 -4
- package/dist/sidecar-lock-reclaim.d.ts.map +1 -1
- package/dist/sidecar-lock-reclaim.js +80 -25
- package/dist/sidecar-lock-root.d.ts.map +1 -1
- package/dist/sidecar-lock-root.js +2 -1
- package/dist/sidecar-lock-stale-admission.d.ts +39 -0
- package/dist/sidecar-lock-stale-admission.d.ts.map +1 -0
- package/dist/sidecar-lock-stale-admission.js +232 -0
- package/dist/sidecar-lock-target.d.ts +8 -0
- package/dist/sidecar-lock-target.d.ts.map +1 -0
- package/dist/sidecar-lock-target.js +55 -0
- package/dist/sidecar-lock.d.ts.map +1 -1
- package/dist/sidecar-lock.js +100 -16
- package/dist/staged-directory.d.ts.map +1 -1
- package/dist/staged-directory.js +6 -6
- package/dist/staged-file-settlement.d.ts +17 -0
- package/dist/staged-file-settlement.d.ts.map +1 -0
- package/dist/staged-file-settlement.js +57 -0
- package/dist/temp-workspace-descriptor.d.ts.map +1 -1
- package/dist/temp-workspace-descriptor.js +9 -27
- package/dist/temp-workspace-owner.d.ts.map +1 -1
- package/dist/temp-workspace-owner.js +8 -8
- package/dist/walk.d.ts +5 -1
- package/dist/walk.d.ts.map +1 -1
- package/dist/walk.js +19 -6
- package/dist/windows-owner.d.ts.map +1 -1
- package/dist/windows-owner.js +4 -3
- package/dist/windows-security-bridge.cs +336 -0
- package/dist/windows-security-bridge.ps1 +15 -0
- package/dist/windows-security-command.d.ts +26 -0
- package/dist/windows-security-command.d.ts.map +1 -0
- package/dist/windows-security-command.js +363 -0
- package/dist/windows-security-facts.d.ts +6 -0
- package/dist/windows-security-facts.d.ts.map +1 -0
- package/dist/windows-security-facts.js +108 -0
- package/docs/advanced.md +4 -2
- package/docs/archive.md +97 -46
- package/docs/atomic.md +85 -8
- package/docs/config.md +6 -2
- package/docs/contributing.md +44 -4
- package/docs/copy.md +37 -0
- package/docs/creation.md +128 -0
- package/docs/durability.md +24 -0
- package/docs/file-store.md +19 -0
- package/docs/install.md +31 -7
- package/docs/json-store.md +5 -0
- package/docs/migrating-to-0.5.md +15 -6
- package/docs/migrating-to-0.6.md +9 -4
- package/docs/native-helper.md +32 -12
- package/docs/native.md +38 -7
- package/docs/output.md +6 -0
- package/docs/path-prefix.md +10 -0
- package/docs/path-suffix-aliases.md +51 -6
- package/docs/permissions.md +50 -14
- package/docs/public-api.md +3 -2
- package/docs/root.md +56 -3
- package/docs/secret-file.md +11 -2
- package/docs/secure-file.md +9 -4
- package/docs/sidecar-lock.md +114 -8
- package/docs/staged-file.md +12 -3
- package/docs/temp.md +20 -3
- package/docs/walk.md +67 -1
- package/docs/writing.md +76 -6
- package/package.json +19 -16
- package/dist/darwin-acl.d.ts +0 -4
- package/dist/darwin-acl.d.ts.map +0 -1
- package/dist/darwin-acl.js +0 -24
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import fsSync, {} from "node:fs";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import { FsSafeError } from "./errors.js";
|
|
4
|
+
import { getNativeBinding } from "./native.js";
|
|
5
|
+
import { getFsSafeNativeConfig } from "./native-config.js";
|
|
6
|
+
import { assertSynchronousCallbackResult } from "./mutation-authority.js";
|
|
7
|
+
import { assertPrivateDirectory, assertPrivateDirectorySync, inspectCreationDirectory, inspectCreationDirectorySync, resolveCreationPermissions, } from "./creation-permissions.js";
|
|
8
|
+
import { creationCollision, prepareCreationPath } from "./creation-path.js";
|
|
9
|
+
import { createPrivateWindowsDirectoryCommand, createPrivateWindowsDirectoryCommandSync, } from "./windows-security-command.js";
|
|
10
|
+
import { warnNativeFallback } from "./native-fallback-warning.js";
|
|
11
|
+
import { inspectDirectoryIdentitySync } from "./directory-guard.js";
|
|
12
|
+
import { assertDarwinCreationDirectoryAcl, assertDarwinPrivateDirectoryMode } from "./creation-darwin.js";
|
|
13
|
+
function directoryAdoptionFailure(error, target, windowsIdentity) {
|
|
14
|
+
return new FsSafeError("helper-failed", "created directory admission failed", {
|
|
15
|
+
cause: error,
|
|
16
|
+
details: {
|
|
17
|
+
publication: { status: "published" }, path: target, cleanup: "preserved",
|
|
18
|
+
...(windowsIdentity === undefined ? {} : { windowsIdentity }),
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
function privateDirectoryBackend() {
|
|
23
|
+
const binding = getNativeBinding();
|
|
24
|
+
if (typeof binding?.createPrivateDirectoryWithParentIdentity === "function") {
|
|
25
|
+
return binding.createPrivateDirectoryWithParentIdentity.bind(binding);
|
|
26
|
+
}
|
|
27
|
+
if (getFsSafeNativeConfig().mode === "require") {
|
|
28
|
+
throw new FsSafeError("helper-unavailable", "private directory creation requires an up-to-date native parent identity capability");
|
|
29
|
+
}
|
|
30
|
+
warnNativeFallback("windows-private-directory", "Private Windows directory creation uses a slower built-in system command.");
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
export async function createDirectory(targetPath, options = {}) {
|
|
34
|
+
await createDirectoryWithAdmission(targetPath, options, {});
|
|
35
|
+
}
|
|
36
|
+
export async function createDirectoryWithAdmission(targetPath, options = {}, admission = {}) {
|
|
37
|
+
await createDirectoryWithReceipt(targetPath, options, admission);
|
|
38
|
+
}
|
|
39
|
+
export async function createDirectoryWithReceipt(targetPath, options = {}, admission = {}) {
|
|
40
|
+
const permissions = resolveCreationPermissions(options, true);
|
|
41
|
+
const assertion = options.assertBeforeMutation;
|
|
42
|
+
const selected = prepareCreationPath(targetPath, admission.expectedParentIdentity);
|
|
43
|
+
const backend = permissions.private && process.platform === "win32" ? privateDirectoryBackend() : undefined;
|
|
44
|
+
const parentIdentity = permissions.private && process.platform === "win32"
|
|
45
|
+
? await inspectCreationDirectory(selected.parent.dir, false) : undefined;
|
|
46
|
+
if (permissions.private)
|
|
47
|
+
assertDarwinCreationDirectoryAcl(selected.parent.dir, selected.parent.stat, "directory");
|
|
48
|
+
assertSynchronousCallbackResult(assertion?.(), "assertBeforeMutation");
|
|
49
|
+
selected.assertParent();
|
|
50
|
+
if (permissions.private)
|
|
51
|
+
assertDarwinPrivateDirectoryMode(permissions.mode);
|
|
52
|
+
if (permissions.private)
|
|
53
|
+
assertDarwinCreationDirectoryAcl(selected.parent.dir, selected.parent.stat, "directory");
|
|
54
|
+
let created = false;
|
|
55
|
+
let windowsIdentity;
|
|
56
|
+
try {
|
|
57
|
+
if (parentIdentity !== undefined) {
|
|
58
|
+
const receipt = backend
|
|
59
|
+
? backend(selected.target, parentIdentity)
|
|
60
|
+
: await createPrivateWindowsDirectoryCommand(selected.target, parentIdentity);
|
|
61
|
+
created = true;
|
|
62
|
+
windowsIdentity = receipt.identity;
|
|
63
|
+
if ((await inspectCreationDirectory(selected.target, true)) !== receipt.identity) {
|
|
64
|
+
throw new FsSafeError("path-mismatch", "created private directory changed before adoption");
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
await fs.mkdir(selected.target, { mode: permissions.mode });
|
|
69
|
+
created = true;
|
|
70
|
+
if (permissions.private)
|
|
71
|
+
await assertPrivateDirectory(selected.target);
|
|
72
|
+
}
|
|
73
|
+
selected.assertParent();
|
|
74
|
+
return { stat: inspectDirectoryIdentitySync(selected.target), windowsIdentity };
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
if (created)
|
|
78
|
+
throw directoryAdoptionFailure(error, selected.target, windowsIdentity);
|
|
79
|
+
throw creationCollision(error);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export function createDirectorySync(targetPath, options = {}) {
|
|
83
|
+
createDirectoryWithReceiptSync(targetPath, options, {});
|
|
84
|
+
}
|
|
85
|
+
export function createDirectorySyncWithAdmission(targetPath, options = {}, admission = {}) {
|
|
86
|
+
createDirectoryWithReceiptSync(targetPath, options, admission);
|
|
87
|
+
}
|
|
88
|
+
export function createDirectoryWithReceiptSync(targetPath, options = {}, admission = {}) {
|
|
89
|
+
const permissions = resolveCreationPermissions(options, true);
|
|
90
|
+
const assertion = options.assertBeforeMutation;
|
|
91
|
+
const selected = prepareCreationPath(targetPath, admission.expectedParentIdentity);
|
|
92
|
+
const backend = permissions.private && process.platform === "win32" ? privateDirectoryBackend() : undefined;
|
|
93
|
+
const parentIdentity = permissions.private && process.platform === "win32"
|
|
94
|
+
? inspectCreationDirectorySync(selected.parent.dir, false) : undefined;
|
|
95
|
+
if (permissions.private)
|
|
96
|
+
assertDarwinCreationDirectoryAcl(selected.parent.dir, selected.parent.stat, "directory");
|
|
97
|
+
assertSynchronousCallbackResult(assertion?.(), "assertBeforeMutation");
|
|
98
|
+
selected.assertParent();
|
|
99
|
+
if (permissions.private)
|
|
100
|
+
assertDarwinPrivateDirectoryMode(permissions.mode);
|
|
101
|
+
if (permissions.private)
|
|
102
|
+
assertDarwinCreationDirectoryAcl(selected.parent.dir, selected.parent.stat, "directory");
|
|
103
|
+
let created = false;
|
|
104
|
+
let windowsIdentity;
|
|
105
|
+
try {
|
|
106
|
+
if (parentIdentity !== undefined) {
|
|
107
|
+
const receipt = backend
|
|
108
|
+
? backend(selected.target, parentIdentity)
|
|
109
|
+
: createPrivateWindowsDirectoryCommandSync(selected.target, parentIdentity);
|
|
110
|
+
created = true;
|
|
111
|
+
windowsIdentity = receipt.identity;
|
|
112
|
+
if (inspectCreationDirectorySync(selected.target, true) !== receipt.identity) {
|
|
113
|
+
throw new FsSafeError("path-mismatch", "created private directory changed before adoption");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
fsSync.mkdirSync(selected.target, { mode: permissions.mode });
|
|
118
|
+
created = true;
|
|
119
|
+
if (permissions.private)
|
|
120
|
+
assertPrivateDirectorySync(selected.target);
|
|
121
|
+
}
|
|
122
|
+
selected.assertParent();
|
|
123
|
+
return { stat: inspectDirectoryIdentitySync(selected.target), windowsIdentity };
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
if (created)
|
|
127
|
+
throw directoryAdoptionFailure(error, selected.target, windowsIdentity);
|
|
128
|
+
throw creationCollision(error);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type FileHandle } from "node:fs/promises";
|
|
2
|
+
import { type CreationPath } from "./creation-path.js";
|
|
3
|
+
export declare function createPrivateWindowsFileHandle(selected: CreationPath, options: {
|
|
4
|
+
mode: number;
|
|
5
|
+
assertBeforeMutation?: () => void;
|
|
6
|
+
}): Promise<FileHandle>;
|
|
7
|
+
//# sourceMappingURL=create-file-async.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-file-async.d.ts","sourceRoot":"","sources":["../src/create-file-async.ts"],"names":[],"mappings":"AAEA,OAAW,EAAE,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAevD,OAAO,EAAmD,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAKxG,wBAAsB,8BAA8B,CAClD,QAAQ,EAAE,YAAY,EACtB,OAAO,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,oBAAoB,CAAC,EAAE,MAAM,IAAI,CAAA;CAAE,GAC3D,OAAO,CAAC,UAAU,CAAC,CA0FrB"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import fsSync, {} from "node:fs";
|
|
3
|
+
import fs, {} from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { createDirectoryWithReceipt } from "./create-directory.js";
|
|
6
|
+
import { assertDirectoryIdentitySync } from "./directory-guard.js";
|
|
7
|
+
import { FsSafeError } from "./errors.js";
|
|
8
|
+
import { assertSynchronousCallbackResult } from "./mutation-authority.js";
|
|
9
|
+
import { assertCreationFile, creationPublicationAfterFailure, privateFileSettlementFailure, rethrowPrivateStageCreationFailure, removeRecordedCreationFile, } from "./creation-file-state.js";
|
|
10
|
+
import { inspectCreationDirectory, protectCreatedFile, verifyCreatedFile } from "./creation-permissions.js";
|
|
11
|
+
import { creationCollision, removeCreationDirectoryAsync } from "./creation-path.js";
|
|
12
|
+
import { handoffCreatedFile } from "./private-producer-handoff.js";
|
|
13
|
+
import { resolveReadOpenFlags } from "./read-open-flags.js";
|
|
14
|
+
import { hasUnsettledWindowsSecurityCommand } from "./windows-security-command.js";
|
|
15
|
+
export async function createPrivateWindowsFileHandle(selected, options) {
|
|
16
|
+
const assertion = options.assertBeforeMutation;
|
|
17
|
+
const existing = await fs.lstat(selected.target).catch((error) => {
|
|
18
|
+
if (error.code !== "ENOENT")
|
|
19
|
+
throw error;
|
|
20
|
+
return undefined;
|
|
21
|
+
});
|
|
22
|
+
selected.assertParent();
|
|
23
|
+
if (existing)
|
|
24
|
+
throw new FsSafeError("already-exists", "creation target already exists");
|
|
25
|
+
const parentIdentity = await inspectCreationDirectory(selected.parent.dir, false);
|
|
26
|
+
selected.assertParent();
|
|
27
|
+
const stageDirectory = path.join(selected.parent.dir, `.fs-safe-create-${randomUUID()}`);
|
|
28
|
+
let stage;
|
|
29
|
+
try {
|
|
30
|
+
stage = await createDirectoryWithReceipt(stageDirectory, { private: true, assertBeforeMutation: assertion }, {
|
|
31
|
+
expectedParentIdentity: selected.parent.stat,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
rethrowPrivateStageCreationFailure(error, selected.target, stageDirectory);
|
|
36
|
+
}
|
|
37
|
+
const stageIdentity = stage.windowsIdentity;
|
|
38
|
+
const stagePath = path.join(stageDirectory, "file");
|
|
39
|
+
const assertStage = () => {
|
|
40
|
+
selected.assertParent();
|
|
41
|
+
assertDirectoryIdentitySync(stageDirectory, stage.stat);
|
|
42
|
+
};
|
|
43
|
+
let file;
|
|
44
|
+
let identity;
|
|
45
|
+
let publication = "not-published";
|
|
46
|
+
try {
|
|
47
|
+
if (await inspectCreationDirectory(stageDirectory, true) !== stageIdentity) {
|
|
48
|
+
throw new FsSafeError("path-mismatch", "private staging directory changed before file creation");
|
|
49
|
+
}
|
|
50
|
+
assertSynchronousCallbackResult(assertion?.(), "assertBeforeMutation");
|
|
51
|
+
assertStage();
|
|
52
|
+
file = await fs.open(stagePath, fsSync.constants.O_RDWR | fsSync.constants.O_CREAT |
|
|
53
|
+
fsSync.constants.O_EXCL | resolveReadOpenFlags(), 0o600);
|
|
54
|
+
assertStage();
|
|
55
|
+
identity = assertCreationFile(file.fd, stagePath);
|
|
56
|
+
assertSynchronousCallbackResult(assertion?.(), "assertBeforeMutation");
|
|
57
|
+
assertStage();
|
|
58
|
+
assertCreationFile(file.fd, stagePath, identity);
|
|
59
|
+
const windowsIdentity = await protectCreatedFile(file.fd, stagePath, stageIdentity);
|
|
60
|
+
assertStage();
|
|
61
|
+
assertCreationFile(file.fd, stagePath, identity);
|
|
62
|
+
const source = file;
|
|
63
|
+
file = undefined;
|
|
64
|
+
// The handoff consumes the source pin on both success and failure.
|
|
65
|
+
file = await handoffCreatedFile({
|
|
66
|
+
source, sourcePath: stagePath, targetPath: selected.target, identity,
|
|
67
|
+
assertSourceParent: assertStage, assertTargetParent: selected.assertParent,
|
|
68
|
+
assertBeforeMutation: assertion,
|
|
69
|
+
verifyDescriptor: (fd, pathname, links) => verifyCreatedFile(fd, pathname, windowsIdentity, pathname === stagePath ? stageIdentity : parentIdentity, links),
|
|
70
|
+
onPublished: () => { publication = "published"; },
|
|
71
|
+
});
|
|
72
|
+
assertSynchronousCallbackResult(assertion?.(), "assertBeforeMutation");
|
|
73
|
+
selected.assertParent();
|
|
74
|
+
assertCreationFile(file.fd, selected.target, identity);
|
|
75
|
+
await verifyCreatedFile(file.fd, selected.target, windowsIdentity, parentIdentity);
|
|
76
|
+
selected.assertParent();
|
|
77
|
+
assertCreationFile(file.fd, selected.target, identity);
|
|
78
|
+
await file.chmod(options.mode);
|
|
79
|
+
await verifyCreatedFile(file.fd, selected.target, windowsIdentity, parentIdentity);
|
|
80
|
+
selected.assertParent();
|
|
81
|
+
assertCreationFile(file.fd, selected.target, identity);
|
|
82
|
+
await removeCreationDirectoryAsync(stageDirectory, stage.stat, selected.assertParent);
|
|
83
|
+
selected.assertParent();
|
|
84
|
+
assertCreationFile(file.fd, selected.target, identity);
|
|
85
|
+
return file;
|
|
86
|
+
}
|
|
87
|
+
catch (primary) {
|
|
88
|
+
publication = creationPublicationAfterFailure(primary, publication);
|
|
89
|
+
const preserved = publication === "indeterminate" || hasUnsettledWindowsSecurityCommand(primary);
|
|
90
|
+
const cleanup = [];
|
|
91
|
+
if (file) {
|
|
92
|
+
const owned = file;
|
|
93
|
+
file = undefined;
|
|
94
|
+
try {
|
|
95
|
+
await owned.close();
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
cleanup.push(error);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!preserved) {
|
|
102
|
+
if (identity) {
|
|
103
|
+
try {
|
|
104
|
+
await removeRecordedCreationFile(stagePath, identity, assertStage);
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
cleanup.push(error);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
await removeCreationDirectoryAsync(stageDirectory, stage.stat, selected.assertParent);
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
cleanup.push(error);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (publication === "not-published" && cleanup.length === 0 && !preserved)
|
|
118
|
+
throw creationCollision(primary);
|
|
119
|
+
throw privateFileSettlementFailure({ primary, cleanup, publication, path: selected.target, stageDirectory, preserved });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type FileHandle } from "node:fs/promises";
|
|
2
|
+
import { type CreateDirectoryOptions, type CreationAdmission } from "./create-directory.js";
|
|
3
|
+
import { type OwnedFileDescriptorSync } from "./create-owned-file.js";
|
|
4
|
+
export type CreateFileOptions = CreateDirectoryOptions;
|
|
5
|
+
export declare function createFileSync(targetPath: string, options?: CreateFileOptions): OwnedFileDescriptorSync;
|
|
6
|
+
/** Internal async writer; Windows preparation uses the nonblocking stage owner. */
|
|
7
|
+
export declare function createFileHandle(targetPath: string, options?: CreateFileOptions, admission?: CreationAdmission): Promise<FileHandle>;
|
|
8
|
+
//# sourceMappingURL=create-file.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-file.d.ts","sourceRoot":"","sources":["../src/create-file.ts"],"names":[],"mappings":"AAEA,OAAgB,EAAE,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE5D,OAAO,EAAkC,KAAK,sBAAsB,EAAE,KAAK,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC5H,OAAO,EAAyB,KAAK,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AA2B7F,MAAM,MAAM,iBAAiB,GAAG,sBAAsB,CAAC;AA8GvD,wBAAgB,cAAc,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,uBAAuB,CAE3G;AAED,mFAAmF;AACnF,wBAAsB,gBAAgB,CACpC,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE,iBAAsB,EAC/B,SAAS,GAAE,iBAAsB,GAChC,OAAO,CAAC,UAAU,CAAC,CA+BrB"}
|
|
@@ -0,0 +1,190 @@
|
|
|
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 { createDirectoryWithReceiptSync } from "./create-directory.js";
|
|
6
|
+
import { ownFileDescriptorSync } from "./create-owned-file.js";
|
|
7
|
+
import { assertDirectoryIdentitySync } from "./directory-guard.js";
|
|
8
|
+
import { FsSafeError } from "./errors.js";
|
|
9
|
+
import { assertSynchronousCallbackResult } from "./mutation-authority.js";
|
|
10
|
+
import { assertPrivateFileCreationAvailable, inspectCreationDirectorySync, protectCreatedFileSync, resolveCreationPermissions, verifyCreatedFileSync, } from "./creation-permissions.js";
|
|
11
|
+
import { creationCollision, prepareCreationPath, removeCreationDirectory } from "./creation-path.js";
|
|
12
|
+
import { handoffCreatedFileSync } from "./private-producer-handoff-sync.js";
|
|
13
|
+
import { resolveReadOpenFlags } from "./read-open-flags.js";
|
|
14
|
+
import { assertCreationFile, assertPrivateCreationFile, creationPublicationAfterFailure, privateFileSettlementFailure, rethrowPrivateStageCreationFailure, removeRecordedCreationFileSync, } from "./creation-file-state.js";
|
|
15
|
+
import { createPrivateWindowsFileHandle } from "./create-file-async.js";
|
|
16
|
+
import { assertDarwinCreationDirectoryAcl } from "./creation-darwin.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
|
+
if (permissions.private)
|
|
117
|
+
assertDarwinCreationDirectoryAcl(selected.parent.dir, selected.parent.stat, "file");
|
|
118
|
+
assertSynchronousCallbackResult(assertion?.(), "assertBeforeMutation");
|
|
119
|
+
selected.assertParent();
|
|
120
|
+
if (permissions.private)
|
|
121
|
+
assertDarwinCreationDirectoryAcl(selected.parent.dir, selected.parent.stat, "file");
|
|
122
|
+
let file;
|
|
123
|
+
try {
|
|
124
|
+
file = ownFileDescriptorSync(fs.openSync(selected.target, fs.constants.O_RDWR | fs.constants.O_CREAT |
|
|
125
|
+
fs.constants.O_EXCL | resolveReadOpenFlags(), permissions.mode ?? 0o666));
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
throw creationCollision(error);
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
const identity = assertCreationFile(file.fd, selected.target);
|
|
132
|
+
selected.assertParent();
|
|
133
|
+
if (permissions.private)
|
|
134
|
+
assertPrivateCreationFile(identity, file.fd);
|
|
135
|
+
return file;
|
|
136
|
+
}
|
|
137
|
+
catch (primary) {
|
|
138
|
+
let failure = primary;
|
|
139
|
+
try {
|
|
140
|
+
file.close();
|
|
141
|
+
}
|
|
142
|
+
catch (closeError) {
|
|
143
|
+
failure = new AggregateError([primary, closeError], "created file admission and close failed");
|
|
144
|
+
}
|
|
145
|
+
throw new FsSafeError("helper-failed", "created file admission failed", {
|
|
146
|
+
cause: failure, details: { publication: { status: "published" }, path: selected.target, cleanup: "preserved" },
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
export function createFileSync(targetPath, options = {}) {
|
|
151
|
+
return createFileCore(targetPath, options, {});
|
|
152
|
+
}
|
|
153
|
+
/** Internal async writer; Windows preparation uses the nonblocking stage owner. */
|
|
154
|
+
export async function createFileHandle(targetPath, options = {}, admission = {}) {
|
|
155
|
+
const permissions = resolveCreationPermissions(options, false);
|
|
156
|
+
const assertion = options.assertBeforeMutation;
|
|
157
|
+
if (process.platform !== "win32" || !permissions.private) {
|
|
158
|
+
const selected = prepareCreationPath(targetPath, admission.expectedParentIdentity);
|
|
159
|
+
if (permissions.private)
|
|
160
|
+
assertDarwinCreationDirectoryAcl(selected.parent.dir, selected.parent.stat, "file");
|
|
161
|
+
assertSynchronousCallbackResult(assertion?.(), "assertBeforeMutation");
|
|
162
|
+
selected.assertParent();
|
|
163
|
+
if (permissions.private)
|
|
164
|
+
assertDarwinCreationDirectoryAcl(selected.parent.dir, selected.parent.stat, "file");
|
|
165
|
+
const handle = await fsAsync.open(selected.target, fs.constants.O_RDWR | fs.constants.O_CREAT |
|
|
166
|
+
fs.constants.O_EXCL | resolveReadOpenFlags(), permissions.mode ?? 0o666)
|
|
167
|
+
.catch(error => { throw creationCollision(error); });
|
|
168
|
+
try {
|
|
169
|
+
const identity = assertCreationFile(handle.fd, selected.target);
|
|
170
|
+
selected.assertParent();
|
|
171
|
+
if (permissions.private)
|
|
172
|
+
assertPrivateCreationFile(identity, handle.fd);
|
|
173
|
+
return handle;
|
|
174
|
+
}
|
|
175
|
+
catch (primary) {
|
|
176
|
+
let failure = primary;
|
|
177
|
+
try {
|
|
178
|
+
await handle.close();
|
|
179
|
+
}
|
|
180
|
+
catch (closeError) {
|
|
181
|
+
failure = new AggregateError([primary, closeError], "created file admission and close failed");
|
|
182
|
+
}
|
|
183
|
+
throw new FsSafeError("helper-failed", "created file handle admission failed", {
|
|
184
|
+
cause: failure, details: { publication: { status: "published" }, path: selected.target, cleanup: "preserved" },
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
assertPrivateFileCreationAvailable();
|
|
189
|
+
return await createPrivateWindowsFileHandle(prepareCreationPath(targetPath, admission.expectedParentIdentity), { mode: permissions.mode, assertBeforeMutation: assertion });
|
|
190
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type OwnedFileDescriptorSync = {
|
|
2
|
+
readonly fd: number;
|
|
3
|
+
close(): void;
|
|
4
|
+
[Symbol.dispose](): void;
|
|
5
|
+
};
|
|
6
|
+
/** Capture the descriptor's creator-owned closer before it can be reused. */
|
|
7
|
+
export declare function ownFileDescriptorSync(fd: number, closeFd?: (fd: number) => void): OwnedFileDescriptorSync;
|
|
8
|
+
//# sourceMappingURL=create-owned-file.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-owned-file.d.ts","sourceRoot":"","sources":["../src/create-owned-file.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,uBAAuB,GAAG;IACpC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,KAAK,IAAI,IAAI,CAAC;IACd,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC;CAC1B,CAAC;AAEF,6EAA6E;AAC7E,wBAAgB,qBAAqB,CACnC,EAAE,EAAE,MAAM,EACV,OAAO,GAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAmB,GAC3C,uBAAuB,CAYzB"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
/** Capture the descriptor's creator-owned closer before it can be reused. */
|
|
3
|
+
export function ownFileDescriptorSync(fd, closeFd = fs.closeSync) {
|
|
4
|
+
let open = true;
|
|
5
|
+
const owner = {
|
|
6
|
+
fd,
|
|
7
|
+
close() {
|
|
8
|
+
if (!open)
|
|
9
|
+
return;
|
|
10
|
+
open = false;
|
|
11
|
+
closeFd(fd);
|
|
12
|
+
},
|
|
13
|
+
[Symbol.dispose]() { owner.close(); },
|
|
14
|
+
};
|
|
15
|
+
return Object.freeze(owner);
|
|
16
|
+
}
|
package/dist/create.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { createDirectory, createDirectorySync, createDirectoryWithAdmission, createDirectorySyncWithAdmission, type CreateDirectoryOptions, type CreationAdmission, } from "./create-directory.js";
|
|
2
|
+
export { createFileSync, createFileHandle, type CreateFileOptions } from "./create-file.js";
|
|
3
|
+
export type { OwnedFileDescriptorSync } from "./create-owned-file.js";
|
|
4
|
+
//# sourceMappingURL=create.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EAAE,mBAAmB,EACpC,4BAA4B,EAAE,gCAAgC,EAC9D,KAAK,sBAAsB,EAAE,KAAK,iBAAiB,GACpD,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAC5F,YAAY,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC"}
|
package/dist/create.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type BigIntStats } from "node:fs";
|
|
2
|
+
export declare function assertDarwinPrivateCreationAvailable(): void;
|
|
3
|
+
export declare function assertDarwinPrivateDirectoryMode(mode: number): void;
|
|
4
|
+
export declare function assertDarwinCreationAcl(fd: number, inheritanceTarget?: "file" | "directory"): void;
|
|
5
|
+
export declare function assertDarwinCreationDirectoryAcl(pathname: string, expected: BigIntStats, inheritanceTarget?: "file" | "directory"): void;
|
|
6
|
+
export declare function privateFileMutationAssertion(fd: number, assertion?: () => void): (() => void) | undefined;
|
|
7
|
+
//# sourceMappingURL=creation-darwin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"creation-darwin.d.ts","sourceRoot":"","sources":["../src/creation-darwin.ts"],"names":[],"mappings":"AAAA,OAAW,EAAE,KAAK,WAAW,EAAE,MAAM,SAAS,CAAC;AAgB/C,wBAAgB,oCAAoC,IAAI,IAAI,CAE3D;AAED,wBAAgB,gCAAgC,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAInE;AAED,wBAAgB,uBAAuB,CAAC,EAAE,EAAE,MAAM,EAAE,iBAAiB,CAAC,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI,CAYlG;AAED,wBAAgB,gCAAgC,CAC9C,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,iBAAiB,CAAC,EAAE,MAAM,GAAG,WAAW,GAChF,IAAI,CAqBN;AAED,wBAAgB,4BAA4B,CAAC,EAAE,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,CAMzG"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import fs, {} from "node:fs";
|
|
2
|
+
import { inspectDirectoryIdentitySync } from "./directory-guard.js";
|
|
3
|
+
import { nodeDarwinDirectoryMetadataFlags, nodeDirectorySearchOnlyFlags } from "./directory-mode-node.js";
|
|
4
|
+
import { FsSafeError } from "./errors.js";
|
|
5
|
+
import { assertSynchronousCallbackResult } from "./mutation-authority.js";
|
|
6
|
+
import { getNativeBinding } from "./native.js";
|
|
7
|
+
import { inspectFileIdentitySync } from "./strict-file-identity.js";
|
|
8
|
+
function inspector() {
|
|
9
|
+
const native = getNativeBinding();
|
|
10
|
+
if (typeof native?.inspectDarwinAcl !== "function") {
|
|
11
|
+
throw new FsSafeError("helper-unavailable", "private Darwin creation requires native descriptor ACL inspection");
|
|
12
|
+
}
|
|
13
|
+
return native.inspectDarwinAcl.bind(native);
|
|
14
|
+
}
|
|
15
|
+
export function assertDarwinPrivateCreationAvailable() {
|
|
16
|
+
if (process.platform === "darwin")
|
|
17
|
+
inspector();
|
|
18
|
+
}
|
|
19
|
+
export function assertDarwinPrivateDirectoryMode(mode) {
|
|
20
|
+
if (process.platform === "darwin" && ((mode & ~process.umask()) & 0o500) === 0) {
|
|
21
|
+
throw new FsSafeError("helper-unavailable", "private Darwin directory ACL inspection requires owner read or search permission");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export function assertDarwinCreationAcl(fd, inheritanceTarget) {
|
|
25
|
+
if (process.platform !== "darwin")
|
|
26
|
+
return;
|
|
27
|
+
const inspect = inspector();
|
|
28
|
+
const state = inspect(fd, inheritanceTarget)?.state;
|
|
29
|
+
if (state === "present") {
|
|
30
|
+
throw new FsSafeError("insecure-permissions", inheritanceTarget
|
|
31
|
+
? "private creation parent has inheritable Darwin ACL entries"
|
|
32
|
+
: "private creation object has Darwin ACL entries");
|
|
33
|
+
}
|
|
34
|
+
if (state !== "absent" && state !== "empty") {
|
|
35
|
+
throw new FsSafeError("helper-unavailable", "native Darwin ACL inspection returned incomplete facts");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export function assertDarwinCreationDirectoryAcl(pathname, expected, inheritanceTarget) {
|
|
39
|
+
if (process.platform !== "darwin")
|
|
40
|
+
return;
|
|
41
|
+
assertDarwinPrivateCreationAvailable();
|
|
42
|
+
let fd;
|
|
43
|
+
try {
|
|
44
|
+
fd = fs.openSync(pathname, nodeDarwinDirectoryMetadataFlags());
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (error.code !== "EACCES")
|
|
48
|
+
throw error;
|
|
49
|
+
const search = nodeDirectorySearchOnlyFlags();
|
|
50
|
+
if (!search)
|
|
51
|
+
throw error;
|
|
52
|
+
try {
|
|
53
|
+
fd = fs.openSync(pathname, search.flags | fs.constants.O_DIRECTORY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK);
|
|
54
|
+
}
|
|
55
|
+
catch (cause) {
|
|
56
|
+
if (cause.code !== "EACCES")
|
|
57
|
+
throw cause;
|
|
58
|
+
throw new FsSafeError("helper-unavailable", "private Darwin directory ACL inspection requires owner read or search permission", { cause });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
const opened = inspectFileIdentitySync(() => fs.fstatSync(fd, { bigint: true }), expected);
|
|
63
|
+
if (!opened.isDirectory())
|
|
64
|
+
throw new FsSafeError("path-mismatch", "ACL target is not the admitted directory");
|
|
65
|
+
assertDarwinCreationAcl(fd, inheritanceTarget);
|
|
66
|
+
inspectDirectoryIdentitySync(pathname, expected);
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
fs.closeSync(fd);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export function privateFileMutationAssertion(fd, assertion) {
|
|
73
|
+
if (process.platform !== "darwin")
|
|
74
|
+
return assertion;
|
|
75
|
+
return () => {
|
|
76
|
+
assertSynchronousCallbackResult(assertion?.(), "assertBeforeMutation");
|
|
77
|
+
assertDarwinCreationAcl(fd);
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type BigIntStats } from "node:fs";
|
|
2
|
+
import { FsSafeError } from "./errors.js";
|
|
3
|
+
export type CreationPublicationStatus = "not-published" | "published" | "indeterminate";
|
|
4
|
+
export declare function assertCreationFile(fd: number, pathname: string, expected?: BigIntStats): BigIntStats;
|
|
5
|
+
export declare function assertPrivateCreationFile(stat: BigIntStats, fd: number): void;
|
|
6
|
+
export declare function removeRecordedCreationFileSync(pathname: string, identity: BigIntStats, assertParent: () => void): void;
|
|
7
|
+
export declare function removeRecordedCreationFile(pathname: string, identity: BigIntStats, assertParent: () => void): Promise<void>;
|
|
8
|
+
export declare function creationPublicationAfterFailure(error: unknown, recorded: CreationPublicationStatus): CreationPublicationStatus;
|
|
9
|
+
export declare function hasPreservedCreationArtifacts(error: unknown): boolean;
|
|
10
|
+
export declare function rethrowPrivateStageCreationFailure(error: unknown, path: string, stageDirectory: string): never;
|
|
11
|
+
export declare function privateFileSettlementFailure(params: {
|
|
12
|
+
primary: unknown;
|
|
13
|
+
cleanup: readonly unknown[];
|
|
14
|
+
publication: CreationPublicationStatus;
|
|
15
|
+
path: string;
|
|
16
|
+
stageDirectory: string;
|
|
17
|
+
preserved?: boolean;
|
|
18
|
+
}): FsSafeError;
|
|
19
|
+
//# sourceMappingURL=creation-file-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"creation-file-state.d.ts","sourceRoot":"","sources":["../src/creation-file-state.ts"],"names":[],"mappings":"AAAA,OAAW,EAAE,KAAK,WAAW,EAAE,MAAM,SAAS,CAAC;AAE/C,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAM1C,MAAM,MAAM,yBAAyB,GAAG,eAAe,GAAG,WAAW,GAAG,eAAe,CAAC;AAExF,wBAAgB,kBAAkB,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,WAAW,GAAG,WAAW,CAOpG;AAED,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,WAAW,EAAE,EAAE,EAAE,MAAM,GAAG,IAAI,CAS7E;AAQD,wBAAgB,8BAA8B,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,IAAI,GAAG,IAAI,CAUtH;AAED,wBAAsB,0BAA0B,CAC9C,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,IAAI,GAChE,OAAO,CAAC,IAAI,CAAC,CAaf;AAED,wBAAgB,+BAA+B,CAC7C,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,yBAAyB,GAClD,yBAAyB,CAO3B;AAED,wBAAgB,6BAA6B,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAMrE;AAED,wBAAgB,kCAAkC,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,GAAG,KAAK,CAuB9G;AAED,wBAAgB,4BAA4B,CAAC,MAAM,EAAE;IACnD,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,SAAS,OAAO,EAAE,CAAC;IAC5B,WAAW,EAAE,yBAAyB,CAAC;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,GAAG,WAAW,CAUd"}
|