- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/clean-files.ts?format=json
138 linesspeed 100%Last change speed in version 46, 2 versions changed it
1// Which files `abso status` found the same as their known copy last time, so the
2// next status reads only the files that may have changed, as git's index does.
3//
4// An entry is kept per file: the size, modified time and inode of the file on
5// disk and of its known copy, each taken before either was read, and whether
6// the file held a CR. It's trusted only while both still match, and only when
7// both were changed clearly before the entry was made: a file changed in the
8// same moment can't be told apart from one changed just after, so it's read.
9// Anything abso writes into the folder or the known copy changes one side's
10// size, time or inode, so no writer has to remember to drop entries.
11//
12// Only status and the checks it prints use this. record reads every file: a
13// line status misses is a nuisance, a change record misses is lost work.
14//
15// The gap it shares with git: a tool that puts back a file's exact old time
16// at the same size and inode (rsync -t, touch -d) hides that edit from status
17// until something else changes the file. record still sends it.
19import { lstatSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
20import { join } from "node:path";
21import { clientVersion } from "./client-version.ts";
22import { stateDir } from "./state-dir.ts";
24const FILE = "clean.json";
25const FORMAT = 1;
26/** How much older than an entry a file's time must be to trust it. */
27const RACY_MS = 2_000;
29/** A file's place in the record now, taken before it is read. */
30export interface CleanKey {
31 readonly id: string;
32 /** The newer of the two modified times. */
33 readonly newest: number;
34 /** The file on disk's mode bits. */
35 readonly mode: number;
36}
38interface Entry {
39 readonly id: string;
40 readonly at: number;
41 readonly cr: boolean;
42}
44export class CleanFiles {
45 readonly #root: string;
46 readonly #entries: Map<string, Entry>;
47 /** When this run started looking; entries made in it carry this time. */
48 readonly #at = Date.now();
49 readonly #keys = new Map<string, CleanKey | undefined>();
50 #changed = false;
52 private constructor(root: string, entries: Map<string, Entry>) {
53 this.#root = root;
54 this.#entries = entries;
55 }
57 /** The folder's record, or an empty one when it's missing, unreadable, or from another format or client. */
58 static load(root: string): CleanFiles {
59 const entries = new Map<string, Entry>();
60 try {
61 const file = JSON.parse(readFileSync(join(stateDir(root), FILE), "utf8")) as {
62 format?: unknown;
63 client?: unknown;
64 files?: Record<string, [string, number, boolean]>;
65 };
66 if (file.format === FORMAT && file.client === clientVersion() && typeof file.files === "object" && file.files)
67 for (const [rel, [id, at, cr]] of Object.entries(file.files))
68 if (typeof id === "string" && typeof at === "number" && typeof cr === "boolean")
69 entries.set(rel, { id, at, cr });
70 } catch {}
71 return new CleanFiles(root, entries);
72 }
74 /**
75 * The key of a plain file on disk and its known copy; undefined for a link, or when either is missing.
76 * Taken once per run: a key taken before a later read is still safe to mark with, since any change
77 * after it was taken makes the next run's key differ.
78 */
79 key(diskPath: string, knownPath: string): CleanKey | undefined {
80 if (this.#keys.has(diskPath)) return this.#keys.get(diskPath);
81 let key: CleanKey | undefined;
82 try {
83 const disk = lstatSync(diskPath);
84 if (disk.isFile()) {
85 const known = statSync(knownPath);
86 key = {
87 id: `${disk.size}:${disk.mtimeMs}:${disk.ino}|${known.size}:${known.mtimeMs}:${known.ino}`,
88 newest: Math.max(disk.mtimeMs, known.mtimeMs),
89 mode: disk.mode,
90 };
91 }
92 } catch {}
93 this.#keys.set(diskPath, key);
94 return key;
95 }
97 /** Whether the file was the same as its known copy at this key, and if so whether it held a CR; undefined: read it. */
98 check(rel: string, key: CleanKey): { cr: boolean } | undefined {
99 const entry = this.#entries.get(rel);
100 if (entry === undefined || entry.id !== key.id || key.newest >= entry.at - RACY_MS) return undefined;
101 return { cr: entry.cr };
102 }
104 /** The file, read after `key` was taken, is the same as its known copy. */
105 mark(rel: string, key: CleanKey, cr: boolean): void {
106 const entry = this.#entries.get(rel);
107 if (key.newest >= this.#at - RACY_MS) {
108 // Too new to trust next time either; an old entry for it is no use now.
109 if (entry !== undefined) {
110 this.#entries.delete(rel);
111 this.#changed = true;
112 }
113 return;
114 }
115 if (entry?.id === key.id && entry.cr === cr) return;
116 this.#entries.set(rel, { id: key.id, at: this.#at, cr });
117 this.#changed = true;
118 }
120 /** The file differs from its known copy, or is gone. */
121 drop(rel: string): void {
122 if (this.#entries.delete(rel)) this.#changed = true;
123 }
125 /** Write the record, if anything changed. Best effort: a run that can't write it just reads more next time. */
126 save(): void {
127 if (!this.#changed) return;
128 try {
129 const dir = stateDir(this.#root);
130 mkdirSync(dir, { recursive: true });
131 const files = Object.fromEntries([...this.#entries].map(([rel, e]) => [rel, [e.id, e.at, e.cr]]));
132 const next = join(dir, `${FILE}.${process.pid}.tmp`);
133 writeFileSync(next, JSON.stringify({ format: FORMAT, client: clientVersion(), files }));
134 renameSync(next, join(dir, FILE));
135 this.#changed = false;
136 } catch {}
137 }
138}