Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420import fs from "node:fs";import Fuse from "fuse-native";import type { AtprotoOxaClient, MountedDocument } from "./atproto.js";import type { DocMountConfig } from "./config.js";import { resolveDocumentRkey } from "./paths.js";
interface FileSnapshot { rkey: string; uri: string; cid: string; text: string; record: Record<string, unknown>; fetchedAt: number;}
interface OpenHandle { path: string; rkey: string; writable: boolean; dirty: boolean; buffer: Buffer; baseCid?: string;}
function now(): number { return Date.now();}
function stat(mode: number, size: number) { return { mtime: new Date(), atime: new Date(), ctime: new Date(), nlink: 1, size, mode, uid: process.getuid?.() ?? 0, gid: process.getgid?.() ?? 0, };}
export class DocMountFS { private readonly client: AtprotoOxaClient; private readonly config: DocMountConfig;
private readonly indexCache = new Map<string, string>(); private indexFetchedAt = 0; private readonly snapshotCache = new Map<string, FileSnapshot>(); private readonly openHandles = new Map<number, OpenHandle>(); private nextFd = 1;
constructor(client: AtprotoOxaClient, config: DocMountConfig) { this.client = client; this.config = config; }
async mount(): Promise<void> { fs.mkdirSync(this.config.mountPoint, { recursive: true });
const fuse = new Fuse(this.config.mountPoint, this.ops, { force: true, mkdir: false, debug: false, });
await new Promise<void>((resolve, reject) => { fuse.mount((error?: unknown) => { if (error) { reject(error instanceof Error ? error : new Error(String(error))); return; } resolve(); }); });
process.once("SIGINT", () => { fuse.unmount(() => process.exit(0)); });
process.once("SIGTERM", () => { fuse.unmount(() => process.exit(0)); });
// Keep the fuse object alive via the closure above. }
private get ops() { return { init: (cb: (code: number) => void) => cb(0), statfs: (_: string, cb: (code: number, value?: unknown) => void) => { cb(0, { bsize: 4096, frsize: 4096, blocks: 1_000_000, bfree: 900_000, bavail: 900_000, files: 1_000_000, ffree: 900_000, favail: 900_000, fsid: 1, flag: 0, namemax: 255, }); }, getattr: async (filePath: string, cb: (code: number, value?: unknown) => void) => { try { if (filePath === "/") { cb(0, stat(0o40755, 4096)); return; }
const snapshot = await this.loadSnapshot(filePath); if (!snapshot) { cb(Fuse.ENOENT); return; }
cb(0, stat(0o100644, Buffer.byteLength(snapshot.text))); } catch (error) { cb(Fuse.EIO); } }, readdir: async (_filePath: string, cb: (code: number, value?: unknown) => void) => { try { const names = await this.listNames(); cb(0, [".", "..", ...names]); } catch (error) { cb(Fuse.EIO); } }, open: async (filePath: string, flags: number, cb: (code: number, value?: unknown) => void) => { try { await this.openFile(filePath, flags, cb); } catch (error) { cb(Fuse.EIO); } }, create: async (filePath: string, _mode: number, cb: (code: number, value?: unknown) => void) => { try { const rkey = this.pathToRkey(filePath); if (!rkey) { cb(Fuse.ENOENT); return; }
const handle = this.makeHandle(filePath, "", true, true); cb(0, this.registerHandle(handle)); } catch (error) { cb(Fuse.EIO); } }, read: (filePath: string, fd: number, buffer: Buffer, length: number, position: number, cb: (code: number) => void) => { const handle = this.openHandles.get(fd); if (!handle) { cb(Fuse.EBADF); return; }
const end = Math.min(handle.buffer.length, position + length); if (position >= handle.buffer.length) { cb(0); return; }
const bytes = handle.buffer.copy(buffer, 0, position, end); cb(bytes); }, write: (filePath: string, fd: number, buffer: Buffer, length: number, position: number, cb: (code: number) => void) => { const handle = this.openHandles.get(fd); if (!handle) { cb(Fuse.EBADF); return; }
handle.buffer = this.writeIntoBuffer(handle.buffer, buffer.subarray(0, length), position); handle.dirty = true; cb(length); }, truncate: async (filePath: string, size: number, cb: (code: number) => void) => { try { const rkey = this.pathToRkey(filePath); if (!rkey) { cb(Fuse.EISDIR); return; }
const snapshot = await this.loadSnapshot(filePath); if (!snapshot) { cb(Fuse.ENOENT); return; }
const handle = this.makeHandle(filePath, snapshot.text, true, false, snapshot.cid); handle.buffer = this.resizeBuffer(handle.buffer, size); handle.dirty = true; this.snapshotCache.set(rkey, { ...snapshot, text: handle.buffer.toString("utf8"), fetchedAt: now(), }); cb(0); } catch (error) { cb(Fuse.EIO); } }, ftruncate: (filePath: string, fd: number, size: number, cb: (code: number) => void) => { const handle = this.openHandles.get(fd); if (!handle) { cb(Fuse.EBADF); return; }
handle.buffer = this.resizeBuffer(handle.buffer, size); handle.dirty = true; cb(0); }, flush: (_filePath: string, _fd: number, cb: (code: number) => void) => cb(0), fsync: (_filePath: string, _fd: number, _datasync: number, cb: (code: number) => void) => cb(0), release: async (filePath: string, fd: number, cb: (code: number) => void) => { try { const handle = this.openHandles.get(fd); this.openHandles.delete(fd);
if (!handle) { cb(0); return; }
if (handle.writable && handle.dirty) { await this.persistHandle(handle); }
cb(0); } catch (error) { cb(Fuse.EIO); } }, opendir: (_filePath: string, _flags: number, cb: (code: number, value?: unknown) => void) => { cb(0, this.nextFd += 1); }, releasedir: (_filePath: string, _fd: number, cb: (code: number) => void) => cb(0), unlink: async (filePath: string, cb: (code: number) => void) => { try { const rkey = this.pathToRkey(filePath); if (!rkey) { cb(Fuse.EISDIR); return; }
const snapshot = await this.loadSnapshot(filePath); await this.client.deleteDocument(rkey, snapshot?.cid); this.snapshotCache.delete(rkey); cb(0); } catch (error) { cb(Fuse.ENOENT); } }, rename: async (src: string, dest: string, cb: (code: number) => void) => { try { const srcRkey = this.pathToRkey(src); const destRkey = this.pathToRkey(dest); if (!srcRkey || !destRkey) { cb(Fuse.EINVAL); return; }
const srcSnapshot = await this.loadSnapshot(src); if (!srcSnapshot) { cb(Fuse.ENOENT); return; }
await this.client.writeDocument(destRkey, srcSnapshot.text); await this.client.deleteDocument(srcRkey, srcSnapshot.cid); this.snapshotCache.delete(srcRkey); this.snapshotCache.delete(destRkey); cb(0); } catch (error) { cb(Fuse.EIO); } }, chmod: (_filePath: string, _mode: number, cb: (code: number) => void) => cb(0), chown: (_filePath: string, _uid: number, _gid: number, cb: (code: number) => void) => cb(0), utimens: (_filePath: string, _atime: Date, _mtime: Date, cb: (code: number) => void) => cb(0), access: (_filePath: string, _mode: number, cb: (code: number) => void) => cb(0), destroy: (_cb: (code: number) => void) => undefined, }; }
private async openFile(filePath: string, flags: number, cb: (code: number, value?: unknown) => void) { const rkey = this.pathToRkey(filePath); if (!rkey) { cb(Fuse.ENOENT); return; }
const writable = (flags & 3) !== 0; const snapshot = await this.loadSnapshot(filePath);
if (!snapshot) { cb(Fuse.ENOENT); return; }
const handle = this.makeHandle(filePath, snapshot.text, writable, false, snapshot.cid); cb(0, this.registerHandle(handle)); }
private makeHandle( filePath: string, text: string, writable: boolean, dirty: boolean, baseCid?: string, ): OpenHandle { const rkey = this.pathToRkey(filePath); if (!rkey) { throw new Error(`Path is not a file: ${filePath}`); }
return { path: filePath, rkey, writable, dirty, buffer: Buffer.from(text, "utf8"), ...(baseCid ? { baseCid } : {}), }; }
private registerHandle(handle: OpenHandle): number { const fd = this.nextFd; this.nextFd += 1; this.openHandles.set(fd, handle); return fd; }
private async persistHandle(handle: OpenHandle): Promise<void> { const text = handle.buffer.toString("utf8"); const snapshot = await this.client.writeDocument(handle.rkey, text, handle.baseCid); this.snapshotCache.set(handle.rkey, { rkey: handle.rkey, uri: snapshot.uri, cid: snapshot.cid, text, record: snapshot.record, fetchedAt: now(), }); }
private async loadSnapshot(filePath: string): Promise<FileSnapshot | null> { const rkey = this.pathToRkey(filePath); if (!rkey) { return null; }
const cached = this.snapshotCache.get(rkey); if (cached && now() - cached.fetchedAt < this.config.ttlMs) { return cached; }
const snapshot = await this.client.readDocument(rkey); if (!snapshot) { return null; }
const loaded: FileSnapshot = { rkey: snapshot.rkey, uri: snapshot.uri, cid: snapshot.cid, text: snapshot.text, record: snapshot.record, fetchedAt: now(), };
this.snapshotCache.set(rkey, loaded); return loaded; }
private async listNames(): Promise<string[]> { if (now() - this.indexFetchedAt < this.config.ttlMs && this.indexCache.size > 0) { return [...this.indexCache.keys()].map((rkey) => this.rkeyToFileName(rkey)); }
const summaries = await this.client.listDocuments(); this.indexCache.clear(); for (const summary of summaries) { this.indexCache.set(summary.rkey, summary.cid); } this.indexFetchedAt = now(); return summaries.map((summary) => this.rkeyToFileName(summary.rkey)); }
private pathToRkey(filePath: string): string | null { return resolveDocumentRkey(filePath, this.config.extension); }
private rkeyToFileName(rkey: string): string { return `${rkey}${this.config.extension}`; }
private writeIntoBuffer(base: Buffer, chunk: Buffer, position: number): Buffer { const required = Math.max(base.length, position + chunk.length); const next = Buffer.alloc(required); base.copy(next, 0, 0, base.length); chunk.copy(next, position, 0, chunk.length); return next; }
private resizeBuffer(base: Buffer, size: number): Buffer { if (size === base.length) { return base; }
const next = Buffer.alloc(size); base.copy(next, 0, 0, Math.min(base.length, size)); return next; }}