From b9a27cea2a1fc09d0d452b7d7912a9daa08f2841 Mon Sep 17 00:00:00 2001 From: hyperion Date: Tue, 28 Jul 2026 05:32:27 +0200 Subject: [PATCH] added an inode resolver to convert inodes to absolute path strings --- src/core/path_resolver.ts | 21 ++++++++++++++++++++- test/core/path_resolver.test.ts | 12 +++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/core/path_resolver.ts b/src/core/path_resolver.ts index 7e6a11b..361d528 100644 --- a/src/core/path_resolver.ts +++ b/src/core/path_resolver.ts @@ -1,6 +1,6 @@ import type { NodeNotFoundError, NotADirectoryError } from "./filesystem/errors"; import type { FileSystem } from "./filesystem/filesystem"; -import { TreeNodeType, type DirectoryNode } from "./filesystem/treenode"; +import { TreeNodeType, type DirectoryNode, type TreeNode } from "./filesystem/treenode"; export function resolvePath(fs: FileSystem, cwd: number, path: string): number { const isAbsolute = path.startsWith("/"); @@ -52,3 +52,22 @@ export function resolvePath(fs: FileSystem, cwd: number, path: string): number { return currentNode.inode; } + +export function resolveInode(fs: FileSystem, inode: number): string { + const node = fs.getNode(inode); + const components: string[] = []; + + if (node === undefined) return ""; + + let currentInode = inode; + while (currentInode !== 0) { + const node = fs.getNode(currentInode) as TreeNode; + if (!node) break; + + components.push(node.name); + currentInode = node.parent; + } + components.reverse(); + components.unshift(""); + return components.join("/"); +} diff --git a/test/core/path_resolver.test.ts b/test/core/path_resolver.test.ts index e88f237..061207c 100644 --- a/test/core/path_resolver.test.ts +++ b/test/core/path_resolver.test.ts @@ -1,4 +1,4 @@ -import { resolvePath } from "@core/path_resolver"; +import { resolveInode, resolvePath } from "@core/path_resolver"; import { expectNodeNotFoundError, expectNotADirectoryError } from "@tests/helpers/error_matchers"; import { buildTestFS } from "@tests/helpers/filesystems_fixture"; import { depth7Tree } from "@tests/helpers/tree_fixture"; @@ -61,3 +61,13 @@ describe("resolvePath", () => { expectNodeNotFoundError(() => resolvePath(fs, 69, "/d3")); }); }); + +describe("resolveInode", () => { + test("returns an the path from the d4 node", () => { + expect(resolveInode(fs, 4)).toBe("/d1/d2/d3/d4"); + }); + + test("returns an empty string from a non existent inode", () => { + expect(resolveInode(fs, 999)).toBe(""); + }); +});