diff options
| author | Soccera1 <[email protected]> | 2026-07-18 09:27:17 +1000 |
|---|---|---|
| committer | Soccera1 <[email protected]> | 2026-07-18 09:27:17 +1000 |
| commit | 6f5241afb5bd1d6a1a8724bf06cc6f205c279402 (patch) | |
| tree | 2aa852e968f6b89f2c8458333ce3e7c38b3c2450 /src/shared/paths.js | |
| parent | a753f669c9bedfccb9f7f400da018bb487dca5d0 (diff) | |
| download | bnu-6f5241afb5bd1d6a1a8724bf06cc6f205c279402.tar.gz bnu-6f5241afb5bd1d6a1a8724bf06cc6f205c279402.zip | |
Refactor to single call
Diffstat (limited to 'src/shared/paths.js')
| -rw-r--r-- | src/shared/paths.js | 92 |
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)); +} |
