@davideasden/pi-undo 0.3.8 → 0.3.9

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.
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@davideasden/pi-undo",
3
- "version": "0.3.8",
3
+ "version": "0.3.9",
4
4
  "description": "Persistent workspace undo and redo for Pi",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -27,6 +27,7 @@ import { recoverPackedMutations } from "./packed-recovery.ts";
27
27
  import type { ManifestId, RestorePath, SnapshotManifest, SnapshotRoot } from "./model.ts";
28
28
  import {
29
29
  assertNoSymlinkEscape,
30
+ assertNoSymlinkParents,
30
31
  MutationPathIndex,
31
32
  relativeSafePath,
32
33
  sortDeletePaths,
@@ -427,22 +428,46 @@ export class RestoreEngine {
427
428
  const currentPaths = prepared === undefined
428
429
  ? await this.readOwnedPaths(current, scope)
429
430
  : prepared.currentPaths;
431
+ const fileGroups = new Map<string, OwnedPath[]>();
432
+ const nonFilePaths = new Map<string, OwnedPath>();
433
+ const absentPaths = new Set<string>();
430
434
  for (const path of scope) {
431
- if (path === "." || currentPaths.get(path)?.entry.kind === "directory") {
432
- // 目录 scope 不能由单个 lstat 证明全部 rollback 内容,继续走 scoped capture。
435
+ if (path === ".") {
433
436
  return false;
434
437
  }
435
- }
436
- const matches = await mapConcurrentOrdered(scope, RESTORE_FILE_VERIFY_CONCURRENCY, async (path) => {
437
438
  const owned = currentPaths.get(path);
438
439
  if (owned === undefined) {
439
- await assertNoSymlinkEscape(this.workspaceRoot, path);
440
- return this.pathIsAbsent(path);
440
+ absentPaths.add(path);
441
+ continue;
441
442
  }
442
- await this.verifyEntry(current.manifestId, owned);
443
- return true;
444
- });
445
- return matches.every(Boolean);
443
+ if (owned.entry.kind === "directory") {
444
+ // 目录 scope 不能由单个 lstat/hash 证明全部 rollback 内容。
445
+ return false;
446
+ }
447
+ if (owned.entry.kind === "file") {
448
+ const group = fileGroups.get(owned.root.relativeRoot) ?? [];
449
+ group.push(owned);
450
+ fileGroups.set(owned.root.relativeRoot, group);
451
+ } else {
452
+ nonFilePaths.set(path, owned);
453
+ }
454
+ }
455
+ const fileResults = await allCompleted([...fileGroups.values()].map((files) =>
456
+ this.verifySourceFiles(current.manifestId, files),
457
+ ));
458
+ if (fileResults.some((result) => !result)) return false;
459
+ for (const path of absentPaths) {
460
+ await assertNoSymlinkEscape(this.workspaceRoot, path);
461
+ if (!await this.pathIsAbsent(path)) return false;
462
+ }
463
+ for (const [path, owned] of nonFilePaths) {
464
+ try {
465
+ await this.verifyEntry(current.manifestId, owned);
466
+ } catch {
467
+ return false;
468
+ }
469
+ }
470
+ return true;
446
471
  } catch (error) {
447
472
  rethrowOperationFailure(error);
448
473
  return false;
@@ -1359,6 +1384,53 @@ export class RestoreEngine {
1359
1384
  };
1360
1385
  }
1361
1386
 
1387
+ private async verifySourceFiles(manifestId: ManifestId, files: readonly OwnedPath[]): Promise<boolean> {
1388
+ if (files.length === 0) return true;
1389
+ const hashWorkspaceFiles = (this.store as SnapshotStore & {
1390
+ readonly hashWorkspaceFiles?: (workspaceRoot: string, relativePaths: readonly string[]) => Promise<readonly string[]>;
1391
+ }).hashWorkspaceFiles;
1392
+ if (hashWorkspaceFiles === undefined) {
1393
+ await allCompleted(files.map((owned) => this.verifyEntry(manifestId, owned)));
1394
+ return true;
1395
+ }
1396
+ const root = files[0]!.root;
1397
+ if (files.some((owned) => owned.root.relativeRoot !== root.relativeRoot)) return false;
1398
+ const workspacePaths = files.map((owned) => owned.absolutePath);
1399
+ const before = await mapConcurrentOrdered(files, RESTORE_FILE_VERIFY_CONCURRENCY, (owned) =>
1400
+ this.sourceFileMetadata(owned));
1401
+ if (before.some((metadata) => metadata === undefined)) return false;
1402
+ await assertNoSymlinkParents(this.workspaceRoot, workspacePaths);
1403
+ const rootPath = root.relativeRoot === "."
1404
+ ? this.workspaceRoot
1405
+ : join(this.workspaceRoot, ...root.relativeRoot.split("/"));
1406
+ const objectIds = await hashWorkspaceFiles.call(this.store,
1407
+ rootPath,
1408
+ files.map((owned) => owned.entry.relativePath),
1409
+ );
1410
+ if (objectIds.length !== files.length) return false;
1411
+ const after = await mapConcurrentOrdered(files, RESTORE_FILE_VERIFY_CONCURRENCY, (owned) =>
1412
+ this.sourceFileMetadata(owned));
1413
+ await assertNoSymlinkParents(this.workspaceRoot, workspacePaths);
1414
+ return files.every((owned, index) =>
1415
+ objectIds[index] === owned.entry.blobId &&
1416
+ before[index] !== undefined && after[index] !== undefined &&
1417
+ sameSourceMetadata(before[index]!, after[index]!));
1418
+ }
1419
+
1420
+ private async sourceFileMetadata(owned: OwnedPath): Promise<BigIntStats | undefined> {
1421
+ try {
1422
+ const metadata = await lstat(this.absolutePath(owned.absolutePath), { bigint: true });
1423
+ if (!metadata.isFile() || metadata.isSymbolicLink() || owned.entry.blobId === null ||
1424
+ metadata.size !== BigInt(owned.entry.size) ||
1425
+ (metadata.mode & 0o111n) !== BigInt(owned.entry.mode & 0o111)) {
1426
+ return undefined;
1427
+ }
1428
+ return metadata;
1429
+ } catch (error) {
1430
+ if (hasErrorCode(error, "ENOENT") || hasErrorCode(error, "ENOTDIR")) return undefined;
1431
+ throw error;
1432
+ }
1433
+ }
1362
1434
  private async readOwnedPaths(
1363
1435
  manifest: SnapshotManifest,
1364
1436
  scopePaths?: readonly string[],
@@ -2639,6 +2711,11 @@ async function mapConcurrentOrdered<T, R>(
2639
2711
  return results;
2640
2712
  }
2641
2713
 
2714
+ function sameSourceMetadata(left: BigIntStats, right: BigIntStats): boolean {
2715
+ return left.dev === right.dev && left.ino === right.ino && left.mode === right.mode &&
2716
+ left.size === right.size && left.mtimeNs === right.mtimeNs && left.ctimeNs === right.ctimeNs;
2717
+ }
2718
+
2642
2719
  function durablePairKey(
2643
2720
  currentManifestId: ManifestId,
2644
2721
  targetManifestId: ManifestId,
@@ -382,6 +382,26 @@ export class SnapshotStore {
382
382
  return result;
383
383
  }
384
384
 
385
+ async hashWorkspaceFiles(workspaceRoot: string, relativePaths: readonly string[]): Promise<readonly string[]> {
386
+ if (relativePaths.length === 0) return [];
387
+ const paths = relativePaths.map((path) => {
388
+ relativeSafePath(workspaceRoot, path);
389
+ if (path.split("/").some((part) => part.toLowerCase() === ".git")) {
390
+ throw new SnapshotStoreError("object_missing", "不能 hash Git metadata");
391
+ }
392
+ return path;
393
+ });
394
+ const batches = workspaceHashPathBatches(paths);
395
+ const hashed = await mapConcurrentOrdered(batches, HASH_BATCH_CONCURRENCY, async (batch) => {
396
+ const output = await this.runGit(["hash-object", "--no-filters", "--", ...batch], {
397
+ cwd: workspaceRoot,
398
+ env: sourceGitEnvironment(),
399
+ });
400
+ return parseObjectIdLines(output, batch.length);
401
+ });
402
+ return hashed.flat();
403
+ }
404
+
385
405
  async durableCacheDirectory(): Promise<string> {
386
406
  const directory = join(this.storeRoot, "durable-cache");
387
407
  await mkdir(directory, { recursive: true });
@@ -2970,6 +2990,27 @@ function hashPathBatches(leaves: readonly VisibleLeaf[]): VisibleLeaf[][] {
2970
2990
  return result;
2971
2991
  }
2972
2992
 
2993
+ function workspaceHashPathBatches(paths: readonly string[]): string[][] {
2994
+ const result: string[][] = [];
2995
+ let current: string[] = [];
2996
+ let argumentBytes = 0;
2997
+ for (const path of paths) {
2998
+ const pathBytes = Buffer.byteLength(path, "utf8") + 1;
2999
+ if (
3000
+ current.length > 0 &&
3001
+ (current.length >= HASH_BATCH_MAX_PATHS || argumentBytes + pathBytes > HASH_BATCH_MAX_ARGUMENT_BYTES)
3002
+ ) {
3003
+ result.push(current);
3004
+ current = [];
3005
+ argumentBytes = 0;
3006
+ }
3007
+ current.push(path);
3008
+ argumentBytes += pathBytes;
3009
+ }
3010
+ if (current.length > 0) result.push(current);
3011
+ return result;
3012
+ }
3013
+
2973
3014
  function parseObjectIdLines(output: string, expectedCount: number): string[] {
2974
3015
  const lines = output.endsWith("\n") ? output.slice(0, -1).split("\n") : output.split("\n");
2975
3016
  if (lines.length !== expectedCount || lines.some((line) => !isObjectId(line))) {