@narumitw/pi-sync-daemon 0.6.2 → 0.8.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 +19 -0
- package/README.md +17 -3
- package/dist/consent.js +26 -1
- package/dist/engine/backends/git/git-backend.d.ts +1 -1
- package/dist/engine/backends/git/git-backend.js +17 -2
- package/dist/engine/backends/git/git-storage.d.ts +1 -1
- package/dist/engine/backends/git/git-storage.js +39 -13
- package/dist/engine/backends/s3/s3-backend.js +8 -0
- package/dist/engine/backends/sync-backend.d.ts +1 -0
- package/dist/engine/backends/webdav/webdav-backend.js +8 -0
- package/dist/engine/ignore/effective-ignore-policy.d.ts +10 -0
- package/dist/engine/ignore/effective-ignore-policy.js +33 -0
- package/dist/engine/ignore/ignore-edit.d.ts +13 -0
- package/dist/engine/ignore/ignore-edit.js +102 -0
- package/dist/engine/ignore/ignore-initialization.d.ts +6 -0
- package/dist/engine/ignore/ignore-initialization.js +186 -0
- package/dist/engine/ignore/ignore-migration-prompt.d.ts +7 -0
- package/dist/engine/ignore/ignore-migration-prompt.js +139 -0
- package/dist/engine/ignore/ignore-settings.d.ts +15 -0
- package/dist/engine/ignore/ignore-settings.js +322 -0
- package/dist/engine/ignore/ignore-source.d.ts +27 -0
- package/dist/engine/ignore/ignore-source.js +250 -0
- package/dist/engine/paths.js +1 -1
- package/dist/engine/settings/config-file.d.ts +9 -2
- package/dist/engine/settings/config-file.js +126 -28
- package/dist/engine/settings/config.d.ts +5 -4
- package/dist/engine/settings/config.js +114 -13
- package/dist/engine/settings/settings-management.js +34 -32
- package/dist/engine/settings/settings-types.d.ts +40 -5
- package/dist/engine/settings/settings-validation.js +52 -24
- package/dist/engine/snapshot/session-paths.d.ts +6 -4
- package/dist/engine/snapshot/session-paths.js +84 -15
- package/dist/engine/snapshot/snapshot-apply.d.ts +7 -8
- package/dist/engine/snapshot/snapshot-apply.js +107 -21
- package/dist/engine/snapshot/snapshot-bounds.d.ts +4 -0
- package/dist/engine/snapshot/snapshot-bounds.js +20 -0
- package/dist/engine/snapshot/snapshot-codec.d.ts +1 -1
- package/dist/engine/snapshot/snapshot-codec.js +15 -7
- package/dist/engine/snapshot/snapshot-entry-projection.d.ts +10 -0
- package/dist/engine/snapshot/snapshot-entry-projection.js +78 -0
- package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +9 -2
- package/dist/engine/snapshot/snapshot-exclude-safety.js +239 -24
- package/dist/engine/snapshot/snapshot-payload.d.ts +2 -0
- package/dist/engine/snapshot/snapshot-payload.js +10 -0
- package/dist/engine/snapshot/snapshot-reviewed-preimages.d.ts +2 -0
- package/dist/engine/snapshot/snapshot-reviewed-preimages.js +60 -0
- package/dist/engine/snapshot/snapshot-root-transition.d.ts +4 -0
- package/dist/engine/snapshot/snapshot-root-transition.js +16 -0
- package/dist/engine/snapshot/snapshot-transaction-images.d.ts +39 -0
- package/dist/engine/snapshot/snapshot-transaction-images.js +387 -0
- package/dist/engine/snapshot/snapshot-transaction-policy.d.ts +73 -0
- package/dist/engine/snapshot/snapshot-transaction-policy.js +276 -0
- package/dist/engine/snapshot/snapshot-transaction-stage.d.ts +32 -0
- package/dist/engine/snapshot/snapshot-transaction-stage.js +303 -0
- package/dist/engine/snapshot/snapshot-transaction-verification.d.ts +7 -0
- package/dist/engine/snapshot/snapshot-transaction-verification.js +78 -0
- package/dist/engine/snapshot/snapshot-transaction.d.ts +1 -11
- package/dist/engine/snapshot/snapshot-transaction.js +390 -364
- package/dist/engine/snapshot/snapshot-types.d.ts +47 -2
- package/dist/engine/snapshot/snapshot-types.js +9 -1
- package/dist/engine/snapshot/snapshot.d.ts +22 -3
- package/dist/engine/snapshot/snapshot.js +357 -43
- package/dist/engine/state/merge-baseline-store.js +20 -9
- package/dist/engine/state/state-types.d.ts +3 -0
- package/dist/engine/sync/apply-review.d.ts +3 -3
- package/dist/engine/sync/apply-review.js +5 -4
- package/dist/engine/sync/continuous-sync.js +37 -12
- package/dist/engine/sync/continuous-watch.d.ts +4 -1
- package/dist/engine/sync/continuous-watch.js +90 -15
- package/dist/engine/sync/exclude-policy.d.ts +4 -0
- package/dist/engine/sync/exclude-policy.js +8 -1
- package/dist/engine/sync/initialization.js +9 -4
- package/dist/engine/sync/local-fields.d.ts +3 -1
- package/dist/engine/sync/local-fields.js +88 -8
- package/dist/engine/sync/merge-apply.js +190 -47
- package/dist/engine/sync/merge-journal.d.ts +18 -4
- package/dist/engine/sync/merge-journal.js +126 -10
- package/dist/engine/sync/merged-sync.d.ts +1 -1
- package/dist/engine/sync/merged-sync.js +124 -68
- package/dist/engine/sync/partial-progress.js +5 -3
- package/dist/engine/sync/remote-snapshot.d.ts +10 -2
- package/dist/engine/sync/remote-snapshot.js +59 -17
- package/dist/engine/sync/session-push.js +7 -6
- package/dist/engine/sync/sync-decision.d.ts +4 -0
- package/dist/engine/sync/sync-decision.js +17 -2
- package/dist/engine/sync/sync-inspection.js +8 -6
- package/dist/engine/sync/sync-mutations.d.ts +10 -2
- package/dist/engine/sync/sync-mutations.js +245 -101
- package/dist/engine/sync/sync-policy.d.ts +36 -10
- package/dist/engine/sync/sync-policy.js +285 -20
- package/dist/engine/sync/sync-queries.js +49 -32
- package/dist/engine/sync/sync-state.d.ts +11 -0
- package/dist/engine/sync/sync-state.js +42 -5
- package/dist/engine/ui/sync-format.js +22 -7
- package/dist/engine-service.js +24 -2
- package/docs/architecture.md +28 -0
- package/package.json +2 -1
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import nodeFs, { constants } from "node:fs";
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { syncDirectory } from "../state/json-file.js";
|
|
6
|
+
import { mergePathIdentity } from "../sync/file-merge-planner.js";
|
|
7
|
+
import { MAX_AGENT_DIRECTORY_ENTRIES, MAX_AGENT_DIRECTORY_FILES } from "../sync/sync-policy.js";
|
|
8
|
+
import { MAX_AGENT_DIRECTORY_BYTES } from "./snapshot-bounds.js";
|
|
9
|
+
import { fileImage } from "./snapshot-transaction-plan.js";
|
|
10
|
+
import { isScopedIdentity } from "./snapshot-transaction-policy.js";
|
|
11
|
+
export const MAX_TRANSACTION_JOURNAL_BYTES = 32 * 1024 * 1024;
|
|
12
|
+
const CHUNK_BYTES = 64 * 1024;
|
|
13
|
+
export async function scopedPathIdentity(target, guards) {
|
|
14
|
+
validate(guards);
|
|
15
|
+
const stat = await fs.lstat(target, { bigint: true });
|
|
16
|
+
validate(guards);
|
|
17
|
+
return scopedIdentity(stat);
|
|
18
|
+
}
|
|
19
|
+
export function scopedIdentity(stat) {
|
|
20
|
+
const identity = `${stat.dev}:${stat.ino}:${stat.birthtimeNs}`;
|
|
21
|
+
if (!isScopedIdentity(identity))
|
|
22
|
+
changedImage();
|
|
23
|
+
return identity;
|
|
24
|
+
}
|
|
25
|
+
export function installedVacancyIdentityMatches(image, expected) {
|
|
26
|
+
if (image.kind === "missing" || !isScopedIdentity(expected))
|
|
27
|
+
return false;
|
|
28
|
+
if (scopedIdentity(image.stat) === expected)
|
|
29
|
+
return true;
|
|
30
|
+
if (process.platform !== "win32" || image.kind !== "file" || image.stat.ino === 0n)
|
|
31
|
+
return false;
|
|
32
|
+
const [device, inode] = expected.split(":");
|
|
33
|
+
return image.stat.dev.toString() === device && image.stat.ino.toString() === inode;
|
|
34
|
+
}
|
|
35
|
+
export function stableScopedIdentity(target, stat) {
|
|
36
|
+
const identity = scopedIdentity(stat);
|
|
37
|
+
if (stat.birthtimeNs > 0n) {
|
|
38
|
+
if (stat.birthtimeNs !== stat.ctimeNs)
|
|
39
|
+
return identity;
|
|
40
|
+
if (["darwin", "freebsd", "netbsd", "win32"].includes(process.platform))
|
|
41
|
+
return identity;
|
|
42
|
+
if (process.platform === "linux" || process.platform === "android") {
|
|
43
|
+
const current = nodeFs.lstatSync(target, { bigint: true });
|
|
44
|
+
if (!sameObservation(stat, current))
|
|
45
|
+
changedImage();
|
|
46
|
+
for (let ancestor = target; ancestor !== path.dirname(ancestor);) {
|
|
47
|
+
ancestor = path.dirname(ancestor);
|
|
48
|
+
const witness = nodeFs.lstatSync(ancestor, { bigint: true });
|
|
49
|
+
if (!witness.isDirectory() || witness.dev !== stat.dev)
|
|
50
|
+
break;
|
|
51
|
+
if (witness.birthtimeNs > 0n && witness.birthtimeNs !== witness.ctimeNs)
|
|
52
|
+
return identity;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
throw new Error("Stable filesystem birth time is unavailable; preserve transaction evidence for reviewed recovery.");
|
|
57
|
+
}
|
|
58
|
+
function validate(guards) {
|
|
59
|
+
guards.signal?.throwIfAborted();
|
|
60
|
+
guards.validateMutation?.();
|
|
61
|
+
}
|
|
62
|
+
function changedImage() {
|
|
63
|
+
throw new Error("Transaction image changed or is unsafe; preserve evidence for review.");
|
|
64
|
+
}
|
|
65
|
+
function sameIdentity(a, b) {
|
|
66
|
+
return (a.dev === b.dev &&
|
|
67
|
+
a.ino === b.ino &&
|
|
68
|
+
a.mode === b.mode &&
|
|
69
|
+
(a.birthtimeNs <= 0n || a.birthtimeNs === a.ctimeNs || a.birthtimeNs === b.birthtimeNs));
|
|
70
|
+
}
|
|
71
|
+
function sameObservation(a, b) {
|
|
72
|
+
return (sameIdentity(a, b) &&
|
|
73
|
+
a.nlink === b.nlink &&
|
|
74
|
+
a.size === b.size &&
|
|
75
|
+
a.mtimeNs === b.mtimeNs &&
|
|
76
|
+
a.ctimeNs === b.ctimeNs &&
|
|
77
|
+
a.birthtimeNs === b.birthtimeNs);
|
|
78
|
+
}
|
|
79
|
+
async function lstat(target, guards) {
|
|
80
|
+
validate(guards);
|
|
81
|
+
let stat;
|
|
82
|
+
try {
|
|
83
|
+
stat = await fs.lstat(target, { bigint: true });
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
validate(guards);
|
|
87
|
+
const code = error.code;
|
|
88
|
+
if (code === "ENOENT" || code === "ENOTDIR")
|
|
89
|
+
return undefined;
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
validate(guards);
|
|
93
|
+
return stat;
|
|
94
|
+
}
|
|
95
|
+
async function recheck(target, original, guards, identityOnly = false) {
|
|
96
|
+
const current = await lstat(target, guards);
|
|
97
|
+
if (!current || !(identityOnly ? sameIdentity : sameObservation)(original, current))
|
|
98
|
+
changedImage();
|
|
99
|
+
}
|
|
100
|
+
async function readStableFile(target, original, guards, consume) {
|
|
101
|
+
validate(guards);
|
|
102
|
+
const handle = await fs.open(target, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0));
|
|
103
|
+
try {
|
|
104
|
+
const opened = await handle.stat({ bigint: true });
|
|
105
|
+
validate(guards);
|
|
106
|
+
if (!opened.isFile() || !sameObservation(original, opened))
|
|
107
|
+
changedImage();
|
|
108
|
+
await recheck(target, original, guards);
|
|
109
|
+
const size = Number(original.size);
|
|
110
|
+
const buffer = Buffer.alloc(Math.min(CHUNK_BYTES, size));
|
|
111
|
+
for (let offset = 0; offset < size;) {
|
|
112
|
+
validate(guards);
|
|
113
|
+
const { bytesRead } = await handle.read(buffer, 0, Math.min(buffer.length, size - offset), offset);
|
|
114
|
+
validate(guards);
|
|
115
|
+
if (bytesRead === 0)
|
|
116
|
+
changedImage();
|
|
117
|
+
await consume(buffer.subarray(0, bytesRead), offset);
|
|
118
|
+
offset += bytesRead;
|
|
119
|
+
}
|
|
120
|
+
if (!sameObservation(original, await handle.stat({ bigint: true })))
|
|
121
|
+
changedImage();
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
await handle.close();
|
|
125
|
+
}
|
|
126
|
+
await recheck(target, original, guards);
|
|
127
|
+
}
|
|
128
|
+
export async function readTransactionJournal(file, guards, maxBytes = MAX_TRANSACTION_JOURNAL_BYTES) {
|
|
129
|
+
const stat = await lstat(file, guards);
|
|
130
|
+
if (!stat)
|
|
131
|
+
return undefined;
|
|
132
|
+
if (!stat.isFile() || stat.nlink !== 1n || stat.size > maxBytes)
|
|
133
|
+
changedImage();
|
|
134
|
+
const content = Buffer.alloc(Number(stat.size));
|
|
135
|
+
await readStableFile(file, stat, guards, (chunk, offset) => {
|
|
136
|
+
chunk.copy(content, offset);
|
|
137
|
+
});
|
|
138
|
+
return content.toString("utf8");
|
|
139
|
+
}
|
|
140
|
+
export async function scopedTransactionImage(target, guards, copyTo, checkPath, beforeCopyFile) {
|
|
141
|
+
const stat = await lstat(target, guards);
|
|
142
|
+
if (!stat)
|
|
143
|
+
return { kind: "missing", value: "missing" };
|
|
144
|
+
let entries = 1;
|
|
145
|
+
let files = 0;
|
|
146
|
+
let bytes = 0;
|
|
147
|
+
async function visit(source, observed, destination) {
|
|
148
|
+
if (checkPath)
|
|
149
|
+
await checkPath(source);
|
|
150
|
+
validate(guards);
|
|
151
|
+
if (observed.isDirectory()) {
|
|
152
|
+
const names = [];
|
|
153
|
+
for await (const entry of await fs.opendir(source)) {
|
|
154
|
+
validate(guards);
|
|
155
|
+
if (++entries > MAX_AGENT_DIRECTORY_ENTRIES)
|
|
156
|
+
throw new Error("Transaction image exceeds the agent-directory entry bound.");
|
|
157
|
+
names.push(entry.name);
|
|
158
|
+
}
|
|
159
|
+
await recheck(source, observed, guards);
|
|
160
|
+
if (destination)
|
|
161
|
+
await fs.mkdir(destination, { mode: 0o700 });
|
|
162
|
+
const children = [];
|
|
163
|
+
for (const name of names.sort()) {
|
|
164
|
+
const child = path.join(source, name);
|
|
165
|
+
const stat = await lstat(child, guards);
|
|
166
|
+
if (!stat)
|
|
167
|
+
changedImage();
|
|
168
|
+
children.push([name, await visit(child, stat, destination && path.join(destination, name))]);
|
|
169
|
+
}
|
|
170
|
+
if (destination) {
|
|
171
|
+
validate(guards);
|
|
172
|
+
await fs.chmod(destination, Number(observed.mode & 511n));
|
|
173
|
+
validate(guards);
|
|
174
|
+
await fs.utimes(destination, observed.atime, observed.mtime);
|
|
175
|
+
validate(guards);
|
|
176
|
+
await syncDirectory(destination);
|
|
177
|
+
}
|
|
178
|
+
await recheck(source, observed, guards);
|
|
179
|
+
const rows = children.map(([name, child]) => [name, child.value]);
|
|
180
|
+
const value = `directory:${createHash("sha256").update(JSON.stringify(rows)).digest("hex")}`;
|
|
181
|
+
return { kind: "directory", value, stat: observed, children };
|
|
182
|
+
}
|
|
183
|
+
if (!observed.isFile() || observed.nlink !== 1n)
|
|
184
|
+
changedImage();
|
|
185
|
+
if (++files > MAX_AGENT_DIRECTORY_FILES)
|
|
186
|
+
throw new Error("Transaction image exceeds the agent-directory file bound.");
|
|
187
|
+
bytes += Number(observed.size);
|
|
188
|
+
if (bytes > MAX_AGENT_DIRECTORY_BYTES)
|
|
189
|
+
throw new Error("Transaction image exceeds the 64 MiB byte bound.");
|
|
190
|
+
const hash = createHash("sha256");
|
|
191
|
+
const copy = destination ? await fs.open(destination, "wx", 0o600) : undefined;
|
|
192
|
+
try {
|
|
193
|
+
if (copy && beforeCopyFile) {
|
|
194
|
+
if (process.platform === "darwin") {
|
|
195
|
+
validate(guards);
|
|
196
|
+
await copy.utimes(observed.atime, observed.mtime);
|
|
197
|
+
validate(guards);
|
|
198
|
+
await copy.sync();
|
|
199
|
+
validate(guards);
|
|
200
|
+
}
|
|
201
|
+
await beforeCopyFile(copy);
|
|
202
|
+
}
|
|
203
|
+
await readStableFile(source, observed, guards, async (chunk) => {
|
|
204
|
+
hash.update(chunk);
|
|
205
|
+
if (copy)
|
|
206
|
+
await copy.writeFile(chunk);
|
|
207
|
+
});
|
|
208
|
+
if (copy) {
|
|
209
|
+
validate(guards);
|
|
210
|
+
await copy.chmod(Number(observed.mode & 511n));
|
|
211
|
+
validate(guards);
|
|
212
|
+
await copy.utimes(observed.atime, observed.mtime);
|
|
213
|
+
validate(guards);
|
|
214
|
+
await copy.sync();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
finally {
|
|
218
|
+
await copy?.close();
|
|
219
|
+
}
|
|
220
|
+
validate(guards);
|
|
221
|
+
return { kind: "file", value: `file:${hash.digest("hex")}`, stat: observed };
|
|
222
|
+
}
|
|
223
|
+
return visit(target, stat, copyTo);
|
|
224
|
+
}
|
|
225
|
+
export function ownedScopedSubset(image, expected) {
|
|
226
|
+
if (image.kind !== expected.kind)
|
|
227
|
+
return false;
|
|
228
|
+
if (image.kind !== "directory" || expected.kind !== "directory")
|
|
229
|
+
return image.value === expected.value;
|
|
230
|
+
const children = new Map(expected.children);
|
|
231
|
+
return image.children.every(([name, child]) => {
|
|
232
|
+
const original = children.get(name);
|
|
233
|
+
return original !== undefined && ownedScopedSubset(child, original);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
export async function matchesScopedFilePrefix(source, original, partial, guards) {
|
|
237
|
+
if (original.kind !== "file" || partial.kind !== "file" || partial.stat.size > original.stat.size)
|
|
238
|
+
return false;
|
|
239
|
+
const hash = createHash("sha256");
|
|
240
|
+
const size = Number(partial.stat.size);
|
|
241
|
+
await readStableFile(source, original.stat, guards, (chunk, offset) => {
|
|
242
|
+
if (offset < size)
|
|
243
|
+
hash.update(chunk.subarray(0, size - offset));
|
|
244
|
+
});
|
|
245
|
+
return partial.value === `file:${hash.digest("hex")}`;
|
|
246
|
+
}
|
|
247
|
+
export function scopedDescendant(image, relative) {
|
|
248
|
+
let node = image;
|
|
249
|
+
for (const name of relative ? relative.split(path.sep) : []) {
|
|
250
|
+
if (node?.kind !== "directory")
|
|
251
|
+
return;
|
|
252
|
+
const directory = node;
|
|
253
|
+
node = directory.children.find(([child]) => child === name)?.[1];
|
|
254
|
+
}
|
|
255
|
+
return node;
|
|
256
|
+
}
|
|
257
|
+
export async function recheckScopedImage(target, image, guards) {
|
|
258
|
+
if (image.kind !== "missing")
|
|
259
|
+
await recheck(target, image.stat, guards);
|
|
260
|
+
else if (await lstat(target, guards))
|
|
261
|
+
changedImage();
|
|
262
|
+
}
|
|
263
|
+
export async function removeScopedImage(target, image, guards, validateIgnorePolicy) {
|
|
264
|
+
if (image.kind === "missing")
|
|
265
|
+
return;
|
|
266
|
+
async function remove(target, node) {
|
|
267
|
+
await recheck(target, node.stat, guards);
|
|
268
|
+
if (node.kind === "file") {
|
|
269
|
+
const observed = await scopedTransactionImage(target, guards);
|
|
270
|
+
if (observed.kind !== "file" || !sameObservation(node.stat, observed.stat) || observed.value !== node.value)
|
|
271
|
+
changedImage();
|
|
272
|
+
if (validateIgnorePolicy) {
|
|
273
|
+
await validateIgnorePolicy();
|
|
274
|
+
await recheck(target, node.stat, guards);
|
|
275
|
+
}
|
|
276
|
+
validate(guards);
|
|
277
|
+
await fs.unlink(target);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
for (const [name, child] of node.children)
|
|
281
|
+
await remove(path.join(target, name), child);
|
|
282
|
+
if (validateIgnorePolicy)
|
|
283
|
+
await validateIgnorePolicy();
|
|
284
|
+
await recheck(target, node.stat, guards, true);
|
|
285
|
+
validate(guards);
|
|
286
|
+
await fs.rmdir(target);
|
|
287
|
+
}
|
|
288
|
+
await remove(target, image);
|
|
289
|
+
}
|
|
290
|
+
export async function legacyImage(target, durable = false) {
|
|
291
|
+
const stat = await lstat(target, {});
|
|
292
|
+
if (!stat)
|
|
293
|
+
return "missing";
|
|
294
|
+
if (stat.isSymbolicLink())
|
|
295
|
+
return `symlink:${await fs.readlink(target)}`;
|
|
296
|
+
if (stat.isFile()) {
|
|
297
|
+
const content = await fs.readFile(target);
|
|
298
|
+
if (durable) {
|
|
299
|
+
const handle = await fs.open(target, process.platform === "win32" ? "r+" : "r");
|
|
300
|
+
try {
|
|
301
|
+
await handle.sync();
|
|
302
|
+
}
|
|
303
|
+
finally {
|
|
304
|
+
await handle.close();
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return fileImage(content);
|
|
308
|
+
}
|
|
309
|
+
if (!stat.isDirectory())
|
|
310
|
+
throw new Error("Unsupported transaction image.");
|
|
311
|
+
const entries = [];
|
|
312
|
+
for (const name of (await fs.readdir(target)).sort())
|
|
313
|
+
entries.push([name, await legacyImage(path.join(target, name), durable)]);
|
|
314
|
+
if (durable)
|
|
315
|
+
await syncDirectory(target);
|
|
316
|
+
return `directory:${createHash("sha256").update(JSON.stringify(entries)).digest("hex")}`;
|
|
317
|
+
}
|
|
318
|
+
function postTreePaths(files, key) {
|
|
319
|
+
const expected = new Map(files.map((file) => [key(file.relative), file.image]));
|
|
320
|
+
const prefixes = new Set();
|
|
321
|
+
for (const relative of expected.keys()) {
|
|
322
|
+
for (let parent = path.dirname(relative); parent !== "." && parent !== path.dirname(parent); parent = path.dirname(parent))
|
|
323
|
+
prefixes.add(parent);
|
|
324
|
+
}
|
|
325
|
+
return { expected, prefixes };
|
|
326
|
+
}
|
|
327
|
+
export function ownedScopedPostTree(image, files, requireComplete) {
|
|
328
|
+
if (image.kind !== "directory" || !files.length)
|
|
329
|
+
return false;
|
|
330
|
+
const { expected, prefixes } = postTreePaths(files, (relative) => relative);
|
|
331
|
+
if (expected.size !== files.length)
|
|
332
|
+
return false;
|
|
333
|
+
const seen = new Set();
|
|
334
|
+
let seenFiles = 0;
|
|
335
|
+
function visit(directory, parent) {
|
|
336
|
+
for (const [name, child] of directory.children) {
|
|
337
|
+
const relative = path.join(parent, name);
|
|
338
|
+
if (seen.has(relative))
|
|
339
|
+
return false;
|
|
340
|
+
seen.add(relative);
|
|
341
|
+
if (child.kind === "directory") {
|
|
342
|
+
if (!prefixes.has(relative) || !visit(child, relative))
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
if (expected.get(relative) !== child.value)
|
|
347
|
+
return false;
|
|
348
|
+
seenFiles++;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
return visit(image, "") && (!requireComplete || seenFiles === expected.size);
|
|
354
|
+
}
|
|
355
|
+
export async function ownedLegacyPostTree(target, files, requireComplete) {
|
|
356
|
+
if (!files.length)
|
|
357
|
+
return false;
|
|
358
|
+
const { expected, prefixes } = postTreePaths(files, mergePathIdentity);
|
|
359
|
+
const seen = new Set();
|
|
360
|
+
async function visit(directory) {
|
|
361
|
+
let stat;
|
|
362
|
+
try {
|
|
363
|
+
stat = await fs.lstat(directory);
|
|
364
|
+
}
|
|
365
|
+
catch {
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
if (!stat.isDirectory() || stat.isSymbolicLink())
|
|
369
|
+
return false;
|
|
370
|
+
for (const name of await fs.readdir(directory)) {
|
|
371
|
+
const child = path.join(directory, name);
|
|
372
|
+
const relative = mergePathIdentity(path.relative(target, child));
|
|
373
|
+
const stat = await fs.lstat(child);
|
|
374
|
+
if (stat.isDirectory() && !stat.isSymbolicLink()) {
|
|
375
|
+
if (!prefixes.has(relative) || !(await visit(child)))
|
|
376
|
+
return false;
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
if (expected.get(relative) !== (await legacyImage(child)))
|
|
380
|
+
return false;
|
|
381
|
+
seen.add(relative);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return true;
|
|
385
|
+
}
|
|
386
|
+
return (await visit(target)) && (!requireComplete || [...expected.keys()].every((key) => seen.has(key)));
|
|
387
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type SyncSelectionConfig } from "../sync/sync-policy.js";
|
|
2
|
+
import { type SessionRootTransition } from "./snapshot-root-transition.js";
|
|
3
|
+
import type { EffectiveIgnorePolicy, SnapshotOptions, SnapshotSelection } from "./snapshot-types.js";
|
|
4
|
+
export declare const LEGACY_AGENT_DIRECTORY_JOURNAL_VERSION = 8;
|
|
5
|
+
export declare const FIXED_WORKSPACE_JOURNAL_VERSION = 9;
|
|
6
|
+
export declare const AGENT_DIRECTORY_JOURNAL_VERSION = 10;
|
|
7
|
+
export declare const IGNORE_POLICY_JOURNAL_VERSION = 11;
|
|
8
|
+
export declare function isScopedJournal(journal: TransactionJournalPolicy): boolean;
|
|
9
|
+
export declare function isRecoveryStageId(value: unknown): value is string;
|
|
10
|
+
export declare function isScopedIdentity(value: unknown): value is string;
|
|
11
|
+
interface IgnoreSourceBinding {
|
|
12
|
+
version: 1;
|
|
13
|
+
filename: string;
|
|
14
|
+
sourceDigest: string;
|
|
15
|
+
reservedNames: string[];
|
|
16
|
+
}
|
|
17
|
+
export interface TransactionJournalPolicy {
|
|
18
|
+
version: number;
|
|
19
|
+
exclude?: string[];
|
|
20
|
+
selection?: SnapshotSelection;
|
|
21
|
+
ignoreSource?: IgnoreSourceBinding;
|
|
22
|
+
}
|
|
23
|
+
export declare function ignoreSourceBinding(policy: Pick<EffectiveIgnorePolicy, "filename" | "sourceDigest" | "reservedNames">): IgnoreSourceBinding;
|
|
24
|
+
type TransactionGuards = Pick<SnapshotOptions, "validateIgnorePolicy" | "signal" | "validateMutation">;
|
|
25
|
+
export interface TransactionOptions extends SyncSelectionConfig, TransactionGuards {
|
|
26
|
+
sessionDir?: string;
|
|
27
|
+
exclude?: readonly string[];
|
|
28
|
+
protectedTargets?: readonly string[];
|
|
29
|
+
expectedPreimages?: ReadonlyMap<string, string>;
|
|
30
|
+
resolveConfiguredSessionDir?: boolean;
|
|
31
|
+
}
|
|
32
|
+
export declare class StaleIgnorePolicyError extends Error {
|
|
33
|
+
}
|
|
34
|
+
export declare function ignorePolicyValidator(options: SyncSelectionConfig & TransactionGuards): (() => Promise<void>) | undefined;
|
|
35
|
+
export declare function validateJournalPolicy(journal: TransactionJournalPolicy): void;
|
|
36
|
+
export declare function assertRecoveryPolicy(journal: TransactionJournalPolicy, options: SyncSelectionConfig): void;
|
|
37
|
+
export interface TransactionEntry {
|
|
38
|
+
target: string;
|
|
39
|
+
afterTarget?: string;
|
|
40
|
+
backupName: string;
|
|
41
|
+
kind: "missing" | "file" | "directory" | "symlink";
|
|
42
|
+
linkTarget?: string;
|
|
43
|
+
beforeImage?: string;
|
|
44
|
+
afterImage?: string;
|
|
45
|
+
postFiles?: {
|
|
46
|
+
relative: string;
|
|
47
|
+
image: string;
|
|
48
|
+
}[];
|
|
49
|
+
removalPending?: boolean;
|
|
50
|
+
replacementStarted?: boolean;
|
|
51
|
+
recoveryStaged?: boolean;
|
|
52
|
+
recoveryWorkspace?: string;
|
|
53
|
+
recoveryWorkspaceId?: string;
|
|
54
|
+
scopedRemoval?: {
|
|
55
|
+
image: "before" | "after";
|
|
56
|
+
identity: string;
|
|
57
|
+
};
|
|
58
|
+
vacantInstall?: string;
|
|
59
|
+
}
|
|
60
|
+
export interface TransactionJournal extends TransactionJournalPolicy {
|
|
61
|
+
completed?: boolean;
|
|
62
|
+
root: string;
|
|
63
|
+
sessionRoot?: string;
|
|
64
|
+
sessionRootTransition?: SessionRootTransition;
|
|
65
|
+
entries: TransactionEntry[];
|
|
66
|
+
}
|
|
67
|
+
export declare function validateJournal(directory: string, journal: TransactionJournal, sessionDir?: string): void;
|
|
68
|
+
export declare function transactionHasSessionTargets(journal: TransactionJournal): boolean;
|
|
69
|
+
export declare function isStrictlyInside(root: string, target: string): boolean;
|
|
70
|
+
export declare function touchesProtectedTarget(target: string, protectedTargets?: readonly string[]): boolean | undefined;
|
|
71
|
+
export declare function assertSafeParents(root: string, sessionRoot: string | undefined, target: string): Promise<void>;
|
|
72
|
+
export declare function assertAllowedTarget(root: string, sessionRoot: string | undefined, target: string): void;
|
|
73
|
+
export {};
|