- Fixed all of path_resolver.ts functions
- `resolveInode` functions now `resolvePointer` - Fixed all resolve pointer -> path functions
This commit is contained in:
@@ -3,12 +3,6 @@ import { fromPtr, NodeType, toPtr, type Node, type NodePointer } from "@core/fil
|
||||
import { getCurrentSession } from "@core/session_manager";
|
||||
import type { ShellState } from "@core/shell_state";
|
||||
|
||||
/*
|
||||
/home/grzegorz/Documents/homework
|
||||
|
||||
""
|
||||
*/
|
||||
|
||||
export function resolvePath(state: ShellState, path: string): { device_id: number; inode: number } {
|
||||
let linkDepth = 0;
|
||||
const maxDepth = state.filesystem.maxSymlinkDepth;
|
||||
@@ -198,19 +192,50 @@ export function resolvePath(state: ShellState, path: string): { device_id: numbe
|
||||
return { device_id: currentDeviceId, inode: currentNode.inode };
|
||||
}
|
||||
|
||||
export function resolveInode(state: ShellState, inode: number): string {
|
||||
const node = state.filesystem.getNode(inode);
|
||||
export function resolvePointer(state: ShellState, pointer: NodePointer): string {
|
||||
const node = state.filesystem.getNode(pointer);
|
||||
const components: string[] = [];
|
||||
|
||||
if (node === undefined) return "";
|
||||
|
||||
let currentInode = inode;
|
||||
while (currentInode !== 0) {
|
||||
const node = state.filesystem.getNode(currentInode) as TreeNode;
|
||||
if (!node) break;
|
||||
let ptr = pointer;
|
||||
// TODO: Change while statements condition to not be hardcoded
|
||||
while (ptr !== "0:0") {
|
||||
if (state.filesystem.isMountTarget(ptr)) {
|
||||
const mount = state.filesystem.getMountPoint(ptr);
|
||||
if (!mount) {
|
||||
throw {
|
||||
type: "FileSystemCorruption",
|
||||
message: "Mount registry entry missing, FileSystem corrupted",
|
||||
timestamp: Date.now(),
|
||||
} satisfies FileSystemCorruptionError;
|
||||
}
|
||||
|
||||
components.push(node.name);
|
||||
currentInode = node.parent;
|
||||
const entries = state.filesystem.getNodeEntries(mount);
|
||||
if (!entries?.[0]) {
|
||||
throw {
|
||||
type: "FileSystemCorruption",
|
||||
message: "Reverse index entry missing, FileSystem corrupted",
|
||||
timestamp: Date.now(),
|
||||
} satisfies FileSystemCorruptionError;
|
||||
}
|
||||
|
||||
components.push(entries[0].name);
|
||||
ptr = toPtr({ device_id: entries[0].pdevice_id, inode: entries[0].pinode });
|
||||
continue;
|
||||
}
|
||||
|
||||
const entries = state.filesystem.getNodeEntries(ptr);
|
||||
if (!entries?.[0]) {
|
||||
throw {
|
||||
type: "FileSystemCorruption",
|
||||
message: "Reverse index entry missing, FileSystem corrupted",
|
||||
timestamp: Date.now(),
|
||||
} satisfies FileSystemCorruptionError;
|
||||
}
|
||||
|
||||
components.push(entries[0].name);
|
||||
ptr = toPtr({ device_id: entries[0].pdevice_id, inode: entries[0].pinode });
|
||||
}
|
||||
if (components.length === 0) {
|
||||
components.push("/");
|
||||
@@ -223,9 +248,9 @@ export function resolveInode(state: ShellState, inode: number): string {
|
||||
return result;
|
||||
}
|
||||
|
||||
export function resolveInodeToHome(state: ShellState, inode: number): string {
|
||||
const home = resolveInode(state, getCurrentSession(state).user.home);
|
||||
let result = resolveInode(state, inode);
|
||||
export function resolvePointerToHome(state: ShellState, ptr: NodePointer): string {
|
||||
const home = resolvePointer(state, getCurrentSession(state).user.home);
|
||||
let result = resolvePointer(state, ptr);
|
||||
|
||||
if (result.startsWith(home)) {
|
||||
result = `~${result.slice(home.length)}`;
|
||||
@@ -234,9 +259,9 @@ export function resolveInodeToHome(state: ShellState, inode: number): string {
|
||||
return result;
|
||||
}
|
||||
|
||||
export function resolveInodeToCwd(state: ShellState, inode: number): string {
|
||||
const cwd = resolveInode(state, getCurrentSession(state).cwd);
|
||||
let result = resolveInode(state, inode);
|
||||
export function resolvePointerToCwd(state: ShellState, ptr: NodePointer): string {
|
||||
const cwd = resolvePointer(state, getCurrentSession(state).cwd);
|
||||
let result = resolvePointer(state, ptr);
|
||||
|
||||
if (result.startsWith(cwd)) {
|
||||
result = `.${result.slice(cwd.length)}`;
|
||||
@@ -245,9 +270,9 @@ export function resolveInodeToCwd(state: ShellState, inode: number): string {
|
||||
return result;
|
||||
}
|
||||
|
||||
export function resolveInodeRelative(state: ShellState, inode: number, relative: number): string {
|
||||
const rel = resolveInode(state, relative);
|
||||
let result = resolveInode(state, inode);
|
||||
export function resolvePointerRelative(state: ShellState, ptr: NodePointer, relative: NodePointer): string {
|
||||
const rel = resolvePointer(state, relative);
|
||||
let result = resolvePointer(state, ptr);
|
||||
|
||||
if (result.startsWith(rel)) {
|
||||
result = `.${result.slice(rel.length)}`;
|
||||
|
||||
Reference in New Issue
Block a user