Public: anyone can read this; the crew can change it.

deleteRepo orphans files when rm fails partway: presence is keyed on HEAD, not the directory #7153

Parked opened this
Closed without merging

scout/forge:scout/7140-deleterepo-orphans-files-when-rm-fails-p → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +106 −12
src/core/repos.ts +11 −5
@@ -12,6 +12,7 @@ import { installHooks } from "../git/hooks.ts";
1212 import { closeReader } from "../git/read.ts";
1313 import {
1414 deleteRepoDir,
15 repoDirExists,
1516 detachFork,
1617 forkBareRepo,
1718 initBareRepo,
@@ -866,7 +867,8 @@ export function listReposByOwner(
866867 }
867868
868869 /**
869 * Delete a repo. Row first, then disk — inverse of creation, same invariant.
870 * Delete a repo. Disk first, then the row: a directory that will not go keeps
871 * the row, so the repo stays listed and a retry finishes the removal.
870872 * Before removing the objects, detach every fork that borrows them (repack so
871873 * the fork owns its objects, then drop its alternates link) — otherwise a
872874 * fork would be silently corrupted. This is the detach-before-delete rule.
@@ -893,15 +895,19 @@ export async function deleteRepo(
893895 );
894896 }
895897
896 db.query("DELETE FROM repo WHERE id = ?1").run(repo.id);
897 forgetListings(db);
898898 const dir = repoDir(reposRoot, repo.ownerName, repo.name);
899899 // A pooled `cat-file` holds this directory open. Closing it first is what
900900 // keeps the space actually free after the unlink instead of pinned to a
901901 // descriptor nobody will ever read again.
902902 closeReader(dir);
903 const removed = await deleteRepoDir(dir);
904 if (!removed.ok) return err("delete_failed");
903 // Disk first: a directory that will not go keeps the row, so a retry finds
904 // the repo and finishes. An already-absent directory is nothing to remove.
905 if (await repoDirExists(dir)) {
906 const removed = await deleteRepoDir(dir);
907 if (!removed.ok) return err("delete_failed");
908 }
909 db.query("DELETE FROM repo WHERE id = ?1").run(repo.id);
910 forgetListings(db);
905911 return ok();
906912 }
907913
src/git/repos.ts +20 −4
@@ -5,7 +5,7 @@
55 */
66
77 import { join } from "node:path";
8 import { rm } from "node:fs/promises";
8 import { rm, stat } from "node:fs/promises";
99 import { defect } from "../kernel/error.ts";
1010 import { err, ok, type Result } from "../kernel/result.ts";
1111 import { runGit, type GitFailure } from "./env.ts";
@@ -119,15 +119,31 @@ export async function setDefaultBranch(
119119 return result.ok ? ok() : result;
120120 }
121121
122 /** Permanently delete a repository directory. The caller owns the ceremony. */
122 /** Is anything at this path? Keyed on the directory, so a half-removed repo counts. */
123 export async function repoDirExists(dir: string): Promise<boolean> {
124 return await stat(dir).then(
125 () => true,
126 () => false,
127 );
128 }
129
130 /**
131 * Permanently delete a repository directory. The caller owns the ceremony.
132 * Presence is the directory, not HEAD: a removal that died partway leaves no
133 * HEAD but still leaves files, and a retry must finish the job.
134 */
123135 export async function deleteRepoDir(
124136 dir: string,
125137 ): Promise<Result<void, { message: string }>> {
126138 if (!dir.endsWith(".git")) {
127139 defect("git.refuse_delete", `refusing to delete non-repo path: ${dir}`);
128140 }
129 if (!(await repoExists(dir)))
141 if (!(await repoDirExists(dir)))
130142 return err({ message: `not a repository: ${dir}` });
131 await rm(dir, { recursive: true, force: true });
143 try {
144 await rm(dir, { recursive: true, force: true });
145 } catch (e) {
146 return err({ message: e instanceof Error ? e.message : String(e) });
147 }
132148 return ok();
133149 }
test/accounts-retire.test.ts +4 −3
@@ -1,5 +1,5 @@
11 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2 import { existsSync, renameSync } from "node:fs";
2 import { chmodSync, existsSync } from "node:fs";
33 import { mkdtemp, rm } from "node:fs/promises";
44 import { tmpdir } from "node:os";
55 import { join } from "node:path";
@@ -158,9 +158,10 @@ describe("accounts: the confirm page for removing an organization too", () => {
158158 if (!made.ok) throw new Error(made.error);
159159 const [first, second] = listReposOwnedBy(app.db, org.id).map((r) => r.name) as [string, string];
160160 const dir = repoDir(app.reposRoot, "reco", second);
161 renameSync(dir, `${dir}.parked`);
161 // Read-only, so its entries cannot be unlinked and the repository stays.
162 chmodSync(dir, 0o500);
162163
163 const failed = await post("re", admin);
164 const failed = await post("re", admin).finally(() => chmodSync(dir, 0o700));
164165 expect(failed.status).toBe(500);
165166 const body = await failed.text();
166167 expect(body).toContain(`reco/${second}`);
test/delete-repo-partial.test.ts added +71
@@ -0,0 +1,71 @@
1 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2 import { chmodSync, existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { createRepo, deleteRepo, getRepo } from "../src/core/repos.ts";
8 import { createUser } from "../src/core/users.ts";
9 import { repoDir } from "../src/git/repos.ts";
10
11 /**
12 * deleteRepo keys presence on the directory, not on HEAD: a removal that died
13 * partway (HEAD gone, a subdirectory stuck) keeps the row, and the retry
14 * finishes the job instead of dropping the row over orphaned files.
15 */
16
17 const root = process.getuid?.() === 0;
18
19 describe("deleteRepo after a partial removal", () => {
20 let dataDir: string;
21 let app: App;
22
23 beforeAll(async () => {
24 dataDir = await mkdtemp(join(tmpdir(), "forge-delpartial-"));
25 app = createApp({ dataDir, port: 0 });
26 });
27 afterAll(async () => {
28 app.db.close();
29 await rm(dataDir, { recursive: true, force: true });
30 });
31
32 test.skipIf(root)("a retry finishes what a failed removal left", async () => {
33 const made = await createUser(app.db, {
34 origin: "seed",
35 trusted: true,
36 username: "alice",
37 email: "[email protected]",
38 password: "a-good-passphrase",
39 adminIfFirst: true,
40 });
41 if (!made.ok) throw new Error("user");
42 const created = await createRepo(app.db, app.reposRoot, {
43 ownerId: made.value.id,
44 name: "stuck",
45 });
46 if (!created.ok) throw new Error("repo");
47 const repo = created.value;
48 const dir = repoDir(app.reposRoot, repo.ownerName, repo.name);
49
50 // HEAD is already gone; a locked subdirectory will not let rm finish.
51 rmSync(join(dir, "HEAD"));
52 const locked = join(dir, "objects");
53 writeFileSync(join(locked, "keep"), "x");
54 mkdirSync(join(locked, "sub"), { recursive: true });
55 chmodSync(locked, 0o500);
56
57 try {
58 const first = await deleteRepo(app.db, app.reposRoot, repo);
59 expect(first.ok).toBe(false);
60 expect(existsSync(dir)).toBe(true);
61 expect(getRepo(app.db, repo.ownerName, repo.name)).toBeDefined();
62 } finally {
63 chmodSync(locked, 0o700);
64 }
65
66 const second = await deleteRepo(app.db, app.reposRoot, repo);
67 expect(second.ok).toBe(true);
68 expect(existsSync(dir)).toBe(false);
69 expect(getRepo(app.db, repo.ownerName, repo.name)).toBeUndefined();
70 });
71 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs