- 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,6 +1,5 @@
|
||||
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||
import type { CommandIO } from "@core/command_io";
|
||||
import type { FileSystem } from "@core/filesystem/filesystem";
|
||||
import { createErrorResult, createSuccessResult } from "@core/result";
|
||||
import { getCurrentSession } from "@core/session_manager";
|
||||
import { ShellState, withCwd } from "@core/shell_state";
|
||||
@@ -40,7 +39,7 @@ async function main(
|
||||
} else if (args[0] === "-") {
|
||||
inode = session.pwd;
|
||||
} else {
|
||||
const result = safeResolvePath(state.filesystem, session.cwd, args[0]);
|
||||
const result = safeResolvePath(state, args[0]);
|
||||
if (result.type === "error") {
|
||||
return {
|
||||
state,
|
||||
@@ -76,13 +75,9 @@ async function main(
|
||||
result: createSuccessResult(COMMAND_NAME),
|
||||
};
|
||||
|
||||
function safeResolvePath(
|
||||
fs: FileSystem,
|
||||
cwd: number,
|
||||
path: string,
|
||||
): { type: "success"; inode: number } | { type: "error" } {
|
||||
function safeResolvePath(state: ShellState, path: string): { type: "success"; inode: number } | { type: "error" } {
|
||||
try {
|
||||
return { type: "success", inode: resolvePath(fs, cwd, path) };
|
||||
return { type: "success", inode: resolvePath(state, path) };
|
||||
} catch {
|
||||
return { type: "error" };
|
||||
}
|
||||
|
||||
@@ -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