- refactored resolvePath to have parity with resolveInode and take in ShellState instead of FileSystem and cwd
- reverted change "removed inode 0 from tree traversal results"
This commit is contained in:
@@ -1,25 +1,27 @@
|
||||
import { getCurrentSession } from "@core/session_manager";
|
||||
import type { ShellState } from "@core/shell_state";
|
||||
import type { NodeNotFoundError, NotADirectoryError } from "../filesystem/errors";
|
||||
import type { FileSystem } from "../filesystem/filesystem";
|
||||
import { TreeNodeType, type DirectoryNode, type TreeNode } from "../filesystem/treenode";
|
||||
|
||||
export function resolvePath(fs: FileSystem, cwd: number, path: string): number {
|
||||
export function resolvePath(state: ShellState, path: string): number {
|
||||
const session = getCurrentSession(state);
|
||||
const isAbsolute = path.startsWith("/");
|
||||
|
||||
const normalizedPath: string = path.replace(/^\/+|\/+$/g, "");
|
||||
const pathComponents: string[] = normalizedPath.split("/").filter(name => name.length > 0);
|
||||
|
||||
if (pathComponents.length === 0 && isAbsolute) return 0;
|
||||
if (pathComponents.length === 0) return cwd;
|
||||
if (pathComponents.length === 0) return session.cwd;
|
||||
|
||||
let currentNode = isAbsolute ? fs.getRoot() : (fs.getNode(cwd) as DirectoryNode);
|
||||
let currentNode = isAbsolute
|
||||
? state.filesystem.getRoot()
|
||||
: (state.filesystem.getNode(session.cwd) as DirectoryNode);
|
||||
if (!currentNode) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `CWD node under inode ${cwd} not found`,
|
||||
message: `CWD node under inode ${session.cwd} not found`,
|
||||
timestamp: Date.now(),
|
||||
inode: cwd,
|
||||
inode: session.cwd,
|
||||
} satisfies NodeNotFoundError;
|
||||
}
|
||||
|
||||
@@ -27,7 +29,7 @@ export function resolvePath(fs: FileSystem, cwd: number, path: string): number {
|
||||
if (name === ".") continue;
|
||||
|
||||
if (name === "..") {
|
||||
currentNode = fs.getNode(currentNode.parent) as DirectoryNode;
|
||||
currentNode = state.filesystem.getNode(currentNode.parent) as DirectoryNode;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -40,7 +42,7 @@ export function resolvePath(fs: FileSystem, cwd: number, path: string): number {
|
||||
timestamp: Date.now(),
|
||||
inode: currentNode.inode,
|
||||
} satisfies NodeNotFoundError;
|
||||
if (fs.isFile(inode))
|
||||
if (state.filesystem.isFile(inode))
|
||||
throw {
|
||||
type: "NotADirectory",
|
||||
message: `Child node ${name} is not a directory`,
|
||||
@@ -49,7 +51,7 @@ export function resolvePath(fs: FileSystem, cwd: number, path: string): number {
|
||||
nodeType: TreeNodeType.File,
|
||||
} satisfies NotADirectoryError;
|
||||
|
||||
currentNode = fs.getNode(inode) as DirectoryNode;
|
||||
currentNode = state.filesystem.getNode(inode) as DirectoryNode;
|
||||
}
|
||||
|
||||
return currentNode.inode;
|
||||
|
||||
Reference in New Issue
Block a user