- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/modes.ts?format=json
149 linesFirst import 99% · review 1%Last change review in version 17, 2 versions changed it
1// The executable bit and symbolic links (#27, contract 7d49f0a).
2//
3// The server holds each file's mode beside its text: whether it is
4// executable, and whether it is a symbolic link (whose text is ""). The client
5// keeps the modes it knows in .avcs/modes.json, as it keeps the known copy of
6// the text, and sends only what changed.
7//
8// Windows has no executable bit, so there (like git's core.fileMode=false)
9// the bit is never read from disk: a file keeps the bit the server has. A
10// link is never followed. Where a link can't be made (Windows without the
11// right to make links), the client writes a plain file holding the target,
12// as git does with core.symlinks=false, and that file counts as the link.
14import {
15 chmodSync,
16 lstatSync,
17 mkdirSync,
18 readFileSync,
19 readlinkSync,
20 statSync,
21 symlinkSync,
22 unlinkSync,
23 writeFileSync,
24} from "node:fs";
25import { dirname, join } from "node:path";
26import { readText, writeFileAtomic } from "./folder.ts";
27import { stateDir } from "./state-dir.ts";
29/** A change to a file's mode: only the fields given change. */
30export interface Mode {
31 readonly exec?: boolean;
32 readonly link?: string | null;
33}
34export type Modes = Readonly<Record<string, Mode>>;
36/** A file's whole mode. */
37export interface FullMode {
38 readonly exec: boolean;
39 readonly link: string | null;
40}
42export const PLAIN: FullMode = { exec: false, link: null };
44/** Whether this disk keeps the executable bit (git's core.fileMode). */
45export const FILE_MODE = process.platform !== "win32";
47const MODES_FILE = "modes.json";
49function modesPath(root: string): string {
50 return join(stateDir(root), MODES_FILE);
51}
53/** The modes the server has, as far as the client knows: only files that are executable or links. */
54export function readKnownModes(root: string): Map<string, FullMode> {
55 try {
56 const raw = JSON.parse(readFileSync(modesPath(root), "utf8")) as Record<string, Partial<FullMode>>;
57 return new Map(
58 Object.entries(raw).map(([rel, m]) => [
59 rel,
60 { exec: m.exec === true, link: typeof m.link === "string" ? m.link : null },
61 ]),
62 );
63 } catch {
64 return new Map();
65 }
66}
68export function writeKnownModes(root: string, modes: ReadonlyMap<string, FullMode>): void {
69 const out: Record<string, Partial<FullMode>> = {};
70 for (const [rel, m] of [...modes].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
71 if (m.exec || m.link !== null)
72 out[rel] = { ...(m.exec ? { exec: true } : {}), ...(m.link !== null ? { link: m.link } : {}) };
73 }
74 writeFileAtomic(modesPath(root), `${JSON.stringify(out, null, 1)}\n`);
75}
77/** Fold mode changes (only the fields given) into the known modes; a deleted file loses its entry. */
78export function foldModes(known: Map<string, FullMode>, modes: Modes, deleted: Iterable<string> = []): void {
79 for (const [rel, m] of Object.entries(modes)) {
80 const was = known.get(rel) ?? PLAIN;
81 known.set(rel, { exec: m.exec ?? was.exec, link: m.link === undefined ? was.link : m.link });
82 }
83 for (const rel of deleted) known.delete(rel);
84}
86/** What a path holds, judged as the server sees it; undefined when it is gone. `text` is undefined for a binary file. */
87export interface OnDisk {
88 readonly text: string | undefined;
89 readonly exec: boolean;
90 readonly link: string | null;
91 /** A plain file standing in for a link that couldn't be made. */
92 readonly standIn: boolean;
93}
95export function onDisk(path: string, known: FullMode = PLAIN): OnDisk | undefined {
96 let st;
97 try {
98 st = lstatSync(path);
99 } catch {
100 return undefined;
101 }
102 if (st.isSymbolicLink())
103 return { text: "", exec: known.exec, link: readlinkSync(path).replace(/\\/g, "/"), standIn: false };
104 if (known.link !== null) {
105 // A stand-in: the file holds the target; editing it changes the target, as in git.
106 const raw = readText(path);
107 if (raw !== undefined) return { text: "", exec: known.exec, link: raw.replace(/\r?\n$/, ""), standIn: true };
108 }
109 return { text: readText(path), exec: FILE_MODE ? (st.mode & 0o111) !== 0 : known.exec, link: null, standIn: false };
110}
112/** The fields of `now` that differ from `was`, or undefined when none do. */
113export function modeChange(was: FullMode, now: FullMode): Mode | undefined {
114 const out: { exec?: boolean; link?: string | null } = {};
115 if (now.exec !== was.exec) out.exec = now.exec;
116 if (now.link !== was.link) out.link = now.link;
117 return Object.keys(out).length === 0 ? undefined : out;
118}
120/**
121 * Put a file's mode on disk: make the link (or its stand-in), or set the
122 * executable bit where the disk keeps it. The file's text is already written.
123 */
124export function applyMode(path: string, mode: FullMode): void {
125 if (mode.link !== null) {
126 try {
127 unlinkSync(path);
128 } catch {
129 // Not there yet.
130 }
131 mkdirSync(dirname(path), { recursive: true });
132 try {
133 symlinkSync(mode.link, path);
134 } catch (err) {
135 const code = (err as NodeJS.ErrnoException).code;
136 if (code !== "EPERM" && code !== "EACCES" && code !== "ENOTSUP") throw err;
137 writeFileSync(path, mode.link);
138 }
139 return;
140 }
141 if (!FILE_MODE) return;
142 try {
143 const bits = statSync(path).mode & 0o777;
144 const want = mode.exec ? bits | ((bits & 0o444) >> 2) : bits & ~0o111;
145 if (want !== bits) chmodSync(path, want);
146 } catch {
147 // Gone, or not ours to change.
148 }
149}