openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
84 lines (83 loc) • 2.59 kB
JavaScript
import { a as openRootFile } from "./boundary-file-read-BfaFLDqt.mjs";
import "./file-access-runtime-BD2Fjfbw.mjs";
import { n as matchesFileIdentity, r as readPathBinding, t as fileIdentity } from "./path-binding-CgKQ8yCF.mjs";
import { a as resolveCanonicalReadPath, n as readAbsolutePath, r as rejectCanonicalPathChange, t as createReadErrorClassifier } from "./path-errors-Bc8by22e.mjs";
import fs from "node:fs";
import path from "node:path";
//#region extensions/file-transfer/src/node-host/file-stat.ts
const classifyError = createReadErrorClassifier({
ENOENT: "NOT_FOUND",
ENOTDIR: "NOT_FOUND",
EACCES: "PERMISSION_DENIED",
EPERM: "PERMISSION_DENIED"
});
async function handleFileStat(e) {
const t = readAbsolutePath(e.path);
if (typeof t !== "string") return t;
const n = await resolveCanonicalReadPath({
requestedPath: t,
followSymlinks: e.followSymlinks === true,
classifyError,
notFoundMessage: "path not found"
});
if (typeof n !== "string") return n;
const r = rejectCanonicalPathChange(e.expectedCanonicalPath, n);
if (r) return r;
try {
const t = await fs.promises.lstat(n);
if (!t.isFile() && !t.isDirectory()) return {
ok: false,
code: "UNSUPPORTED_FILE_TYPE",
message: "only regular files and directories are supported",
canonicalPath: n
};
const r = await openRootFile({
absolutePath: n,
rootPath: path.dirname(n),
boundaryLabel: "file.stat",
allowedType: t.isDirectory() ? "directory" : "file",
rejectHardlinks: false
});
if (!r.ok) return {
ok: false,
code: classifyError(r.error),
message: "could not open the metadata target",
canonicalPath: n
};
try {
const t = rejectCanonicalPathChange(n, r.path);
if (t) return t;
const i = fs.fstatSync(r.fd, { bigint: true });
const a = readPathBinding(e.expectedBinding);
if (e.expectedBinding !== void 0 && (a?.kind !== "existing" || !matchesFileIdentity(i, a))) return {
ok: false,
code: "CANONICAL_PATH_CHANGED",
message: "filesystem identity differs from the authorized target",
canonicalPath: n
};
return {
ok: true,
path: r.path,
type: i.isDirectory() ? "directory" : "file",
size: Number(i.size),
mtimeMs: Number(i.mtimeNs) / 1e6,
binding: {
kind: "existing",
...fileIdentity(i)
},
...e.preflightOnly === true ? { preflightOnly: true } : {}
};
} finally {
fs.closeSync(r.fd);
}
} catch (e) {
return {
ok: false,
code: classifyError(e),
message: "could not read file metadata",
canonicalPath: n
};
}
}
//#endregion
export { handleFileStat };