Public: anyone can read this; the crew can change it.
A hosted forge's snapshot never follows a symlink swapped for a repository's parent directory #14978
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14958-a-hosted-forge-s-snapshot-never-follows → main 1 commit fork
2 files
changed +81
−5
src/snapshot/hosted.ts +41 −2
| @@ -389,6 +389,36 @@ async function dirsIn(dir: string): Promise<string[]> { | ||
| 389 | 389 | return entries.filter((e) => e.isDirectory() && SEGMENT.test(e.name)).map((e) => e.name).sort(); |
| 390 | 390 | } |
| 391 | 391 | |
| 392 | /** | |
| 393 | * The first directory on the path from `root` to `root/parts…` that is not a directory of the | |
| 394 | * forge's own, by its path under `root`; undefined when every one is. Each is looked at with | |
| 395 | * lstat (a link is not a directory), and then the whole path is resolved and must be the path | |
| 396 | * itself: Bun has no openat, so a component swapped between the two looks shows as a different | |
| 397 | * real path. | |
| 398 | */ | |
| 399 | export function notOwnDir(root: string, parts: readonly string[]): string | undefined { | |
| 400 | for (let i = 1; i <= parts.length; i++) { | |
| 401 | const at = join(root, ...parts.slice(0, i)); | |
| 402 | let s: import("node:fs").Stats | undefined; | |
| 403 | try { | |
| 404 | s = lstatSync(at, { throwIfNoEntry: false }); | |
| 405 | } catch { | |
| 406 | s = undefined; | |
| 407 | } | |
| 408 | if (s?.isDirectory() !== true) return parts.slice(0, i).join("/"); | |
| 409 | } | |
| 410 | const full = join(root, ...parts); | |
| 411 | let real: string | undefined; | |
| 412 | try { | |
| 413 | real = realpathSync(full); | |
| 414 | } catch { | |
| 415 | real = undefined; | |
| 416 | } | |
| 417 | return real === full && within(real, root) ? undefined : parts.join("/"); | |
| 418 | } | |
| 419 | ||
| 420 | const notOwnWords = (path: string): string => `${path} is not a directory of its own inside its forge`; | |
| 421 | ||
| 392 | 422 | /** Whether anything under `dir` is a link. */ |
| 393 | 423 | async function holdsLink(dir: string): Promise<boolean> { |
| 394 | 424 | const entries = await readdir(dir, { withFileTypes: true }).catch(() => []); |
| @@ -607,9 +637,13 @@ export async function copyHosted(child: HostedSource, tools: HostedTools): Promi | ||
| 607 | 637 | } |
| 608 | 638 | // Git runs here, outside the forge's cell, on files the forge wrote: only on a repository that |
| 609 | 639 | // holds nothing git would run or follow out of the forge. |
| 640 | const parts = ["repos", owner, `${name}.git`]; | |
| 610 | 641 | const found = findings.get(repo); |
| 642 | const swapped = notOwnDir(root, parts); | |
| 611 | 643 | const why = |
| 612 | found !== undefined | |
| 644 | swapped !== undefined | |
| 645 | ? notOwnWords(swapped) | |
| 646 | : found !== undefined | |
| 613 | 647 | ? `it holds what git would run on this host (${findingWords(found).slice(repo.length + 2)})` |
| 614 | 648 | : (await holdsLink(dir)) |
| 615 | 649 | ? "it holds a link" |
| @@ -628,7 +662,12 @@ export async function copyHosted(child: HostedSource, tools: HostedTools): Promi | ||
| 628 | 662 | continue; |
| 629 | 663 | } |
| 630 | 664 | // Still the directory that was looked at: one changed for a link while git read it is not |
| 631 | // kept. | |
| 665 | // kept, nor one any directory above it was changed for. | |
| 666 | const moved = notOwnDir(root, parts); | |
| 667 | if (moved !== undefined) { | |
| 668 | absent.push({ repo, why: `it changed into something else while it was read: ${notOwnWords(moved)}` }); | |
| 669 | continue; | |
| 670 | } | |
| 632 | 671 | if ((await lstat(dir).catch(() => undefined))?.isDirectory() !== true || (await holdsLink(dir)) || borrowsOutside(dir, reposRoot)) { |
| 633 | 672 | absent.push({ repo, why: "it changed into something else while it was read" }); |
| 634 | 673 | continue; |
test/snapshot-hosted-inside.test.ts +40 −3
| @@ -15,7 +15,8 @@ | ||
| 15 | 15 | * or that borrows its objects from outside its forge, is left out by name, |
| 16 | 16 | * in the manifest; so is one its database lists and its disk lacks, one |
| 17 | 17 | * whose name cannot stand in a snapshot, and one that changed while git |
| 18 | * read it; | |
| 18 | * read it, or whose parent directory was swapped for a link meanwhile | |
| 19 | * (each directory on its path is looked at, and the path is named); | |
| 19 | 20 | * - a forge that holds more files than one copy reads is left out, by name; |
| 20 | 21 | * - the forge's own lists are read in the copy's process: a view under a |
| 21 | 22 | * table's name is never run by the snapshot; |
| @@ -24,7 +25,7 @@ | ||
| 24 | 25 | */ |
| 25 | 26 | import { Database } from "bun:sqlite"; |
| 26 | 27 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 27 | import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; | |
| 28 | import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; | |
| 28 | 29 | import { rm } from "node:fs/promises"; |
| 29 | 30 | import { tmpdir } from "node:os"; |
| 30 | 31 | import { join } from "node:path"; |
| @@ -35,7 +36,7 @@ import { createUser, type User } from "../src/core/users.ts"; | ||
| 35 | 36 | import { runGit } from "../src/git/env.ts"; |
| 36 | 37 | import { repoDir } from "../src/git/repos.ts"; |
| 37 | 38 | import { chunkKey, openChunk } from "../src/snapshot/chunks.ts"; |
| 38 | import { copyInProcess, hostedSources, type HostedSource } from "../src/snapshot/hosted.ts"; | |
| 39 | import { copyInProcess, hostedSources, notOwnDir, type HostedSource } from "../src/snapshot/hosted.ts"; | |
| 39 | 40 | import { restoreHosted } from "../src/snapshot/restore.ts"; |
| 40 | 41 | import { takeSnapshot, type Manifest } from "../src/snapshot/snapshot.ts"; |
| 41 | 42 | import type { Store } from "../src/snapshot/store.ts"; |
| @@ -250,6 +251,42 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 250 | 251 | expect(filesOf(manifest, "shifty").filter((p) => p.startsWith("repos/"))).toEqual(["repos/shifty/fine.bundle"]); |
| 251 | 252 | }); |
| 252 | 253 | |
| 254 | test("a repository whose parent directory is swapped for a link while git reads it is not kept, and the path is named", async () => { | |
| 255 | const f = await hostForge("parent", "body-parent"); | |
| 256 | await commit(f, "moved"); | |
| 257 | f.open.close(); | |
| 258 | const repos = join(f.source.dir, "repos"); | |
| 259 | const moved = repoDir(repos, "parent", "moved"); | |
| 260 | let swapped = false; | |
| 261 | const { manifest } = await snap(["parent"], { | |
| 262 | // The forge is awake: as git starts on the repository, its owner's directory goes outside | |
| 263 | // the forge and a link to it stands in its place. Git, run by path, would follow it. | |
| 264 | hostedGit: async (args, options) => { | |
| 265 | if (!swapped && options?.cwd === moved) { | |
| 266 | swapped = true; | |
| 267 | renameSync(join(repos, "parent"), join(root, "outside-owner")); | |
| 268 | symlinkSync(join(root, "outside-owner"), join(repos, "parent")); | |
| 269 | } | |
| 270 | return runGit(args, options); | |
| 271 | }, | |
| 272 | }); | |
| 273 | expect(swapped).toBe(true); | |
| 274 | const entry = manifest.hosted![0]!; | |
| 275 | expect([entry.why, entry.repos]).toEqual([undefined, 0]); | |
| 276 | expect(entry.absent).toEqual([{ repo: "parent/moved", why: "it changed into something else while it was read: repos/parent is not a directory of its own inside its forge" }]); | |
| 277 | expect(filesOf(manifest, "parent").filter((p) => p.startsWith("repos/"))).toEqual([]); | |
| 278 | }); | |
| 279 | ||
| 280 | test("every directory on a repository's path is looked at, not only its last", () => { | |
| 281 | const forge = fresh("forge"); | |
| 282 | mkdirSync(join(forge, "repos", "own", "a.git"), { recursive: true }); | |
| 283 | expect(notOwnDir(forge, ["repos", "own", "a.git"])).toBeUndefined(); | |
| 284 | mkdirSync(join(root, "elsewhere", "b.git"), { recursive: true }); | |
| 285 | symlinkSync(join(root, "elsewhere"), join(forge, "repos", "linked")); | |
| 286 | expect(notOwnDir(forge, ["repos", "linked", "b.git"])).toBe("repos/linked"); | |
| 287 | expect(notOwnDir(forge, ["repos", "gone", "c.git"])).toBe("repos/gone"); | |
| 288 | }); | |
| 289 | ||
| 253 | 290 | test("a forge that holds more files than one copy reads is left out, by name", async () => { |
| 254 | 291 | const f = await hostForge("many", "body-many"); |
| 255 | 292 | f.open.close(); |
new above
offline