UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

47 lines (46 loc) • 1.34 kB
import fs from "node:fs"; import koffi from "koffi"; //#region src/agents/worktrees/git-pack-cleanup.worker.ts process.on("SIGIO", () => {}); const fcntl = koffi.load(null).func("int fcntl(int fd, int command, ...)"); const F_SETLEASE = 1024; const F_GETLEASE = 1025; const F_WRLCK = 1; const F_UNLCK = 2; const { files, olderThan } = JSON.parse(fs.readFileSync(0, "utf8")); let removed = 0; let retained = 0; for (const file of files) { if (removed >= 256) break; let fd; try { fd = fs.openSync(file, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK); const original = fs.fstatSync(fd); if (!original.isFile() || original.mtimeMs >= olderThan) continue; if (fcntl(fd, F_SETLEASE, "int", F_WRLCK) !== 0) { retained++; continue; } try { const current = fs.lstatSync(file); if (current.dev !== original.dev || current.ino !== original.ino || current.size !== original.size || current.mtimeMs !== original.mtimeMs || current.ctimeMs !== original.ctimeMs || fcntl(fd, F_GETLEASE) !== F_WRLCK) { retained++; continue; } fs.unlinkSync(file); removed++; } finally { fcntl(fd, F_SETLEASE, "int", F_UNLCK); } } catch { retained++; } finally { if (fd !== void 0) fs.closeSync(fd); } } process.stdout.write(JSON.stringify({ removed, retained })); //#endregion export {};