aboutsummaryrefslogtreecommitdiff
path: root/src/shared/paths.js
diff options
context:
space:
mode:
authorLily <[email protected]>2026-07-28 16:12:21 +1000
committerGitHub <[email protected]>2026-07-28 16:12:21 +1000
commitc11e17573334dfb487a8e1cc25fa84fa9e138f5c (patch)
treee91dbd19b524f9432da6bcb86b1fc193e0d02e26 /src/shared/paths.js
parent6a2f098e72e71437154ec00506e8e736aa8e1026 (diff)
downloadbnu-c11e17573334dfb487a8e1cc25fa84fa9e138f5c.tar.gz
bnu-c11e17573334dfb487a8e1cc25fa84fa9e138f5c.zip
Refactor coreutils into single-call command modules (#1)
* Refactor to single call
Diffstat (limited to 'src/shared/paths.js')
-rw-r--r--src/shared/paths.js92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/shared/paths.js b/src/shared/paths.js
new file mode 100644
index 0000000..9fdce56
--- /dev/null
+++ b/src/shared/paths.js
@@ -0,0 +1,92 @@
+import { lstat, readlink, realpath, stat } from "node:fs/promises";
+import { isAbsolute, join, normalize, basename as pathBasename, dirname as pathDirname, resolve } from "node:path";
+import { pathDisplayName, shellEscapeLsName } from "./common.js";
+
+export function readlinkQuotedName(path) {
+ const name = pathDisplayName(path);
+ return name === "" ? "''" : shellEscapeLsName(name);
+}
+
+export async function canonicalPath(path, opts = {}) {
+ if (opts.s || opts.strip || opts["no-symlinks"]) return lexicalAbsolute(path);
+ if (opts.L || opts.logical) return logicalCanonicalPath(path, opts);
+ if (opts.m || opts["canonicalize-missing"]) return canonicalizeMissing(path);
+ return physicalCanonicalPath(path, opts);
+}
+
+export async function logicalCanonicalPath(path, opts = {}) {
+ const lexical = lexicalAbsolute(path);
+ if (opts.m || opts["canonicalize-missing"] || opts.E) return lexical;
+ if (opts.e || opts["canonicalize-existing"]) return realpath(lexical);
+ try {
+ return await realpath(lexical);
+ } catch (error) {
+ if (error.code !== "ENOENT") throw error;
+ return join(await realpath(pathDirname(lexical)), pathBasename(lexical));
+ }
+}
+
+export async function physicalCanonicalPath(path, opts = {}) {
+ const mustExist = opts.e || opts["canonicalize-existing"];
+ const allowMissingFinal = !mustExist;
+ const allowMissingAny = opts.m || opts["canonicalize-missing"];
+ const trailingSlash = /\/$/.test(path) && path !== "/";
+ const absolute = isAbsolute(path) ? path : `${process.cwd().replace(/\/+$/, "")}/${path}`;
+ const parts = absolute.split("/").filter(Boolean);
+ let current = "/";
+ let symlinkDepth = 0;
+ for (let i = 0; i < parts.length; i++) {
+ const part = parts[i];
+ if (part === ".") continue;
+ if (part === "..") {
+ current = pathDirname(current.replace(/\/+$/, "") || "/");
+ continue;
+ }
+ const candidate = join(current, part);
+ let s;
+ try {
+ s = await lstat(candidate);
+ } catch (error) {
+ if (allowMissingAny) return normalize(join(candidate, ...parts.slice(i + 1)));
+ if (allowMissingFinal && error.code === "ENOENT" && i === parts.length - 1) return candidate;
+ throw error;
+ }
+ if (s.isSymbolicLink()) {
+ if (++symlinkDepth > 40) {
+ const error = new Error("Too many levels of symbolic links");
+ error.code = "ELOOP";
+ throw error;
+ }
+ const target = await readlink(candidate);
+ const targetParts = target.split("/").filter(Boolean);
+ const rest = parts.slice(i + 1);
+ parts.splice(i, parts.length - i, ...targetParts, ...rest);
+ if (isAbsolute(target)) current = "/";
+ i--;
+ } else {
+ current = candidate;
+ }
+ }
+ if (!allowMissingAny && trailingSlash && !(await stat(current).catch(() => null))?.isDirectory()) {
+ const error = new Error("Not a directory");
+ error.code = "ENOTDIR";
+ throw error;
+ }
+ return current;
+}
+
+export async function canonicalizeMissing(path) {
+ return physicalCanonicalPath(path, { m: true });
+}
+
+export function readlinkErrorMessage(error) {
+ if (error?.code === "EINVAL") return "Invalid argument";
+ if (error?.code === "ENOENT") return "No such file or directory";
+ if (error?.code === "ENOTDIR") return "Not a directory";
+ if (error?.code === "ELOOP") return "Too many levels of symbolic links";
+ return error?.message || String(error);
+}
+
+export function lexicalAbsolute(path) {
+ return normalize(isAbsolute(path) ? path : resolve(process.cwd(), path));
+}