- filesystem fixes

- tree traversal up fixes
This commit is contained in:
2026-09-21 04:44:10 +02:00
parent f2b5f86a6c
commit 2a6e6c57c3
4 changed files with 143 additions and 68 deletions
+7
View File
@@ -32,6 +32,13 @@ export interface NodeNotFoundError {
timestamp: number;
}
export interface DeviceNotFoundError {
type: "DeviceNotFound";
message: string;
device_id: number;
timestamp: number;
}
export interface IllegalMutationError {
type: "IllegalMutation";
message: string;
+92 -50
View File
@@ -4,13 +4,23 @@ import { dnTraverseTree, upTraverseTree } from "@core/util/tree_traversal";
import { VDiskManager } from "@core/util/vdisk_manager";
import type {
AlreadyExistsError,
DeviceNotFoundError,
DirectoryLoopError,
FileSystemInitializationError,
IllegalMutationError,
NodeNotFoundError,
NotADirectoryError,
} from "./errors";
import { NodeType, type DirectoryNode, type FileNode, type Node, type NodePointer } from "./node";
import {
fromPtr,
NodeType,
toPtr,
type DirectoryNode,
type FileNode,
type Node,
type NodeEntry,
type NodePointer,
} from "./node";
import type { IDataStore, IMetaStore, VDiskConfig } from "./vdisk";
export interface FileSystemConfig {
@@ -24,13 +34,12 @@ export class FileSystem {
private readonly _maxPathLength: number;
private readonly _maxSymlinkDepth: number;
private _SysPartitionID: number = 0;
private _Mounts: Map<NodePointer, { tinode: number; tdevice_id: number }> = new Map();
private _VDiskManager: VDiskManager;
private constructor(
config: FileSystemConfig,
devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>,
config: FileSystemConfig,
) {
this._maxNameLength = config.maxNameLength;
this._maxPathLength = config.maxPathLength;
@@ -43,19 +52,16 @@ export class FileSystem {
}
private resolveNode(ptr: NodePointer): Node | undefined {
const dev = this._VDiskManager.getVDisk(ptr[1]);
const fptr = fromPtr(ptr);
const dev = this._VDiskManager.getDisk(fptr.device_id);
if (!dev) return undefined;
const node = dev.overlay.get(ptr[0]);
if (node) return node;
if (node === null) return undefined;
return dev.base.get(ptr[0]);
return dev.getNode(fptr.inode);
}
public static init(
config: FileSystemConfig,
devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>,
config: FileSystemConfig = { maxNameLength: 255, maxPathLength: Infinity, maxSymlinkDepth: 40 },
): FileSystem {
if (devices.length === 0) {
throw {
@@ -81,21 +87,24 @@ export class FileSystem {
} satisfies FileSystemInitializationError;
}
return new FileSystem(config, devices);
return new FileSystem(devices, config);
}
public getRoot(): DirectoryNode {
const node = this.resolveNode([0, this._SysPartitionID]);
if (node === undefined || node.type !== TreeNodeType.Directory)
throw new Error("System partitions root inode did not resolve properly.");
return node as DirectoryNode;
const dev = this._VDiskManager.getDisk(0);
if (!dev) throw new Error("Kernel Panic - Filesystem corrupted, system device not found!");
return dev.getNode(0) as DirectoryNode;
}
public getSysPartitionID(): number {
return this._SysPartitionID;
public getNodeEntries(ptr: NodePointer): readonly NodeEntry[] | undefined {
const fptr = fromPtr(ptr);
const dev = this._VDiskManager.getDisk(fptr.device_id)!;
return dev.getIndex().get(ptr);
}
public getNode(ptr: NodePointer): TreeNode | undefined {
public getNode(ptr: NodePointer): Node | undefined {
return this.resolveNode(ptr);
}
@@ -104,52 +113,89 @@ export class FileSystem {
}
public isFile(ptr: NodePointer): boolean {
const fptr = fromPtr(ptr);
const node = this.resolveNode(ptr);
if (node === undefined) {
throw {
type: "NodeNotFound",
message: `Node under inode ${ptr[0]} not found`,
message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`,
timestamp: Date.now(),
inode: ptr[0],
inode: fptr.inode,
} satisfies NodeNotFoundError;
}
return node.type === TreeNodeType.File ? true : false;
return node.type === NodeType.File ? true : false;
}
public isDirectory(ptr: NodePointer): boolean {
const fptr = fromPtr(ptr);
const node = this.resolveNode(ptr);
if (node === undefined) {
throw {
type: "NodeNotFound",
message: `Node under inode ${ptr[0]} not found`,
message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`,
timestamp: Date.now(),
inode: ptr[0],
inode: fptr.inode,
} satisfies NodeNotFoundError;
}
return node.type === TreeNodeType.Directory ? true : false;
return node.type === NodeType.Directory ? true : false;
}
public isSymlink(ptr: NodePointer): boolean {
const fptr = fromPtr(ptr);
const node = this.resolveNode(ptr);
if (node === undefined) {
throw {
type: "NodeNotFound",
message: `Node under inode ${ptr[0]} not found`,
message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`,
timestamp: Date.now(),
inode: ptr[0],
inode: fptr.inode,
} satisfies NodeNotFoundError;
}
return node.type === TreeNodeType.Symlink ? true : false;
return node.type === NodeType.Symlink ? true : false;
}
public moveNode(source: NodePointer, destination: NodePointer, name?: string): FileSystem {
if (source[0] === 0) {
const sptr = fromPtr(source);
const dptr = fromPtr(destination);
const sdev = this._VDiskManager.getDisk(sptr.device_id);
const ddev = this._VDiskManager.getDisk(dptr.device_id);
const sourceNode = this.resolveNode(source);
const destinationNode = this.resolveNode(destination);
if (!sdev) {
throw {
type: "DeviceNotFound",
message: `Device under id ${sptr.device_id} not found`,
timestamp: Date.now(),
device_id: sptr.device_id,
} satisfies DeviceNotFoundError;
}
if (!ddev) {
throw {
type: "DeviceNotFound",
message: `Device under id ${dptr.device_id} not found`,
timestamp: Date.now(),
device_id: dptr.device_id,
} satisfies DeviceNotFoundError;
}
if (sourceNode === undefined) {
throw {
type: "NodeNotFound",
message: `Source node under inode ${sptr.inode} not found`,
timestamp: Date.now(),
inode: sptr.inode,
} satisfies NodeNotFoundError;
}
if (sourceNode.inode === 0) {
throw {
type: "IllegalMutation",
message: `Attempt at illegal mutation of node under inode 0 (root)`,
@@ -157,43 +203,41 @@ export class FileSystem {
} satisfies IllegalMutationError;
}
const sourceNode = this.resolveNode(source);
const destinationNode = this.resolveNode(destination);
if (sourceNode === undefined) {
throw {
type: "NodeNotFound",
message: `Source node under inode ${source[0]} not found`,
timestamp: Date.now(),
inode: source[0],
} satisfies NodeNotFoundError;
}
if (destinationNode === undefined) {
throw {
type: "NodeNotFound",
message: `Destination node under inode ${destinationInode} not found`,
message: `Destination node under inode ${dptr.inode} not found`,
timestamp: Date.now(),
inode: destinationInode,
inode: dptr.inode,
} satisfies NodeNotFoundError;
}
if (destinationNode.type !== TreeNodeType.Directory) {
if (destinationNode.type !== NodeType.Directory) {
throw {
type: "NotADirectory",
message: `Destination node under inode ${destinationInode} is not a directory`,
inode: destinationInode,
message: `Destination node under inode ${dptr.inode} is not a directory`,
inode: dptr.inode,
timestamp: Date.now(),
nodeType: destinationNode.type,
} satisfies NotADirectoryError;
}
if (destinationNode.children[nodeName] !== undefined) {
const sdevIndex = sdev.getIndex();
const s_entry = sdevIndex.get(source);
if (!s_entry || !s_entry[0]) throw new Error("File System corruption, reverse index is missing a field");
const pinode = s_entry[0].pinode;
const p_entry = sdevIndex.get(toPtr(sdev.ID, pinode));
if (!p_entry || !p_entry[0]) throw new Error("File System corruption, reverse index is missing a field");
const nodeName = name ?? s_entry[0].name;
if (destinationNode.children.get(nodeName) !== undefined) {
throw {
type: "AlreadyExists",
message: `A node inside the destination inode ${destinationNode} under the name ${nodeName} already exists`,
message: `A node inside the destination inode ${destinationNode.inode} under the name ${nodeName} already exists`,
timestamp: Date.now(),
parentInode: destinationInode,
parentInode: destinationNode.inode,
name: nodeName,
} satisfies AlreadyExistsError;
}
@@ -208,8 +252,6 @@ export class FileSystem {
} satisfies DirectoryLoopError;
}
const nodeName = name ?? sourceNode;
let newNode: TreeNode;
if (sourceNode.type === TreeNodeType.File) {