added an inode resolver to convert inodes to absolute path strings
This commit is contained in:
@@ -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("/");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user