- 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 { CommandDescriptor, CommandOutput } from "@core/command";
|
||||||
import type { CommandIO } from "@core/command_io";
|
import type { CommandIO } from "@core/command_io";
|
||||||
import type { FileSystem } from "@core/filesystem/filesystem";
|
|
||||||
import { createErrorResult, createSuccessResult } from "@core/result";
|
import { createErrorResult, createSuccessResult } from "@core/result";
|
||||||
import { getCurrentSession } from "@core/session_manager";
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
import { ShellState, withCwd } from "@core/shell_state";
|
import { ShellState, withCwd } from "@core/shell_state";
|
||||||
@@ -40,7 +39,7 @@ async function main(
|
|||||||
} else if (args[0] === "-") {
|
} else if (args[0] === "-") {
|
||||||
inode = session.pwd;
|
inode = session.pwd;
|
||||||
} else {
|
} else {
|
||||||
const result = safeResolvePath(state.filesystem, session.cwd, args[0]);
|
const result = safeResolvePath(state, args[0]);
|
||||||
if (result.type === "error") {
|
if (result.type === "error") {
|
||||||
return {
|
return {
|
||||||
state,
|
state,
|
||||||
@@ -76,13 +75,9 @@ async function main(
|
|||||||
result: createSuccessResult(COMMAND_NAME),
|
result: createSuccessResult(COMMAND_NAME),
|
||||||
};
|
};
|
||||||
|
|
||||||
function safeResolvePath(
|
function safeResolvePath(state: ShellState, path: string): { type: "success"; inode: number } | { type: "error" } {
|
||||||
fs: FileSystem,
|
|
||||||
cwd: number,
|
|
||||||
path: string,
|
|
||||||
): { type: "success"; inode: number } | { type: "error" } {
|
|
||||||
try {
|
try {
|
||||||
return { type: "success", inode: resolvePath(fs, cwd, path) };
|
return { type: "success", inode: resolvePath(state, path) };
|
||||||
} catch {
|
} catch {
|
||||||
return { type: "error" };
|
return { type: "error" };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,27 @@
|
|||||||
import { getCurrentSession } from "@core/session_manager";
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
import type { ShellState } from "@core/shell_state";
|
import type { ShellState } from "@core/shell_state";
|
||||||
import type { NodeNotFoundError, NotADirectoryError } from "../filesystem/errors";
|
import type { NodeNotFoundError, NotADirectoryError } from "../filesystem/errors";
|
||||||
import type { FileSystem } from "../filesystem/filesystem";
|
|
||||||
import { TreeNodeType, type DirectoryNode, type TreeNode } from "../filesystem/treenode";
|
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 isAbsolute = path.startsWith("/");
|
||||||
|
|
||||||
const normalizedPath: string = path.replace(/^\/+|\/+$/g, "");
|
const normalizedPath: string = path.replace(/^\/+|\/+$/g, "");
|
||||||
const pathComponents: string[] = normalizedPath.split("/").filter(name => name.length > 0);
|
const pathComponents: string[] = normalizedPath.split("/").filter(name => name.length > 0);
|
||||||
|
|
||||||
if (pathComponents.length === 0 && isAbsolute) return 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) {
|
if (!currentNode) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `CWD node under inode ${cwd} not found`,
|
message: `CWD node under inode ${session.cwd} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: cwd,
|
inode: session.cwd,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,7 +29,7 @@ export function resolvePath(fs: FileSystem, cwd: number, path: string): number {
|
|||||||
if (name === ".") continue;
|
if (name === ".") continue;
|
||||||
|
|
||||||
if (name === "..") {
|
if (name === "..") {
|
||||||
currentNode = fs.getNode(currentNode.parent) as DirectoryNode;
|
currentNode = state.filesystem.getNode(currentNode.parent) as DirectoryNode;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +42,7 @@ export function resolvePath(fs: FileSystem, cwd: number, path: string): number {
|
|||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: currentNode.inode,
|
inode: currentNode.inode,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
if (fs.isFile(inode))
|
if (state.filesystem.isFile(inode))
|
||||||
throw {
|
throw {
|
||||||
type: "NotADirectory",
|
type: "NotADirectory",
|
||||||
message: `Child node ${name} is not a directory`,
|
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,
|
nodeType: TreeNodeType.File,
|
||||||
} satisfies NotADirectoryError;
|
} satisfies NotADirectoryError;
|
||||||
|
|
||||||
currentNode = fs.getNode(inode) as DirectoryNode;
|
currentNode = state.filesystem.getNode(inode) as DirectoryNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
return currentNode.inode;
|
return currentNode.inode;
|
||||||
|
|||||||
Reference in New Issue
Block a user