- 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; timestamp: number;
} }
export interface DeviceNotFoundError {
type: "DeviceNotFound";
message: string;
device_id: number;
timestamp: number;
}
export interface IllegalMutationError { export interface IllegalMutationError {
type: "IllegalMutation"; type: "IllegalMutation";
message: string; 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 { VDiskManager } from "@core/util/vdisk_manager";
import type { import type {
AlreadyExistsError, AlreadyExistsError,
DeviceNotFoundError,
DirectoryLoopError, DirectoryLoopError,
FileSystemInitializationError, FileSystemInitializationError,
IllegalMutationError, IllegalMutationError,
NodeNotFoundError, NodeNotFoundError,
NotADirectoryError, NotADirectoryError,
} from "./errors"; } 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"; import type { IDataStore, IMetaStore, VDiskConfig } from "./vdisk";
export interface FileSystemConfig { export interface FileSystemConfig {
@@ -24,13 +34,12 @@ export class FileSystem {
private readonly _maxPathLength: number; private readonly _maxPathLength: number;
private readonly _maxSymlinkDepth: number; private readonly _maxSymlinkDepth: number;
private _SysPartitionID: number = 0;
private _Mounts: Map<NodePointer, { tinode: number; tdevice_id: number }> = new Map(); private _Mounts: Map<NodePointer, { tinode: number; tdevice_id: number }> = new Map();
private _VDiskManager: VDiskManager; private _VDiskManager: VDiskManager;
private constructor( private constructor(
config: FileSystemConfig,
devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>, devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>,
config: FileSystemConfig,
) { ) {
this._maxNameLength = config.maxNameLength; this._maxNameLength = config.maxNameLength;
this._maxPathLength = config.maxPathLength; this._maxPathLength = config.maxPathLength;
@@ -43,19 +52,16 @@ export class FileSystem {
} }
private resolveNode(ptr: NodePointer): Node | undefined { 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; if (!dev) return undefined;
const node = dev.overlay.get(ptr[0]); return dev.getNode(fptr.inode);
if (node) return node;
if (node === null) return undefined;
return dev.base.get(ptr[0]);
} }
public static init( public static init(
config: FileSystemConfig,
devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>, devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>,
config: FileSystemConfig = { maxNameLength: 255, maxPathLength: Infinity, maxSymlinkDepth: 40 },
): FileSystem { ): FileSystem {
if (devices.length === 0) { if (devices.length === 0) {
throw { throw {
@@ -81,21 +87,24 @@ export class FileSystem {
} satisfies FileSystemInitializationError; } satisfies FileSystemInitializationError;
} }
return new FileSystem(config, devices); return new FileSystem(devices, config);
} }
public getRoot(): DirectoryNode { public getRoot(): DirectoryNode {
const node = this.resolveNode([0, this._SysPartitionID]); const dev = this._VDiskManager.getDisk(0);
if (node === undefined || node.type !== TreeNodeType.Directory) if (!dev) throw new Error("Kernel Panic - Filesystem corrupted, system device not found!");
throw new Error("System partitions root inode did not resolve properly.");
return node as DirectoryNode; return dev.getNode(0) as DirectoryNode;
} }
public getSysPartitionID(): number { public getNodeEntries(ptr: NodePointer): readonly NodeEntry[] | undefined {
return this._SysPartitionID; 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); return this.resolveNode(ptr);
} }
@@ -104,52 +113,89 @@ export class FileSystem {
} }
public isFile(ptr: NodePointer): boolean { public isFile(ptr: NodePointer): boolean {
const fptr = fromPtr(ptr);
const node = this.resolveNode(ptr); const node = this.resolveNode(ptr);
if (node === undefined) { if (node === undefined) {
throw { throw {
type: "NodeNotFound", 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(), timestamp: Date.now(),
inode: ptr[0], inode: fptr.inode,
} satisfies NodeNotFoundError; } satisfies NodeNotFoundError;
} }
return node.type === TreeNodeType.File ? true : false; return node.type === NodeType.File ? true : false;
} }
public isDirectory(ptr: NodePointer): boolean { public isDirectory(ptr: NodePointer): boolean {
const fptr = fromPtr(ptr);
const node = this.resolveNode(ptr); const node = this.resolveNode(ptr);
if (node === undefined) { if (node === undefined) {
throw { throw {
type: "NodeNotFound", 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(), timestamp: Date.now(),
inode: ptr[0], inode: fptr.inode,
} satisfies NodeNotFoundError; } satisfies NodeNotFoundError;
} }
return node.type === TreeNodeType.Directory ? true : false; return node.type === NodeType.Directory ? true : false;
} }
public isSymlink(ptr: NodePointer): boolean { public isSymlink(ptr: NodePointer): boolean {
const fptr = fromPtr(ptr);
const node = this.resolveNode(ptr); const node = this.resolveNode(ptr);
if (node === undefined) { if (node === undefined) {
throw { throw {
type: "NodeNotFound", 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(), timestamp: Date.now(),
inode: ptr[0], inode: fptr.inode,
} satisfies NodeNotFoundError; } 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 { 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 { throw {
type: "IllegalMutation", type: "IllegalMutation",
message: `Attempt at illegal mutation of node under inode 0 (root)`, message: `Attempt at illegal mutation of node under inode 0 (root)`,
@@ -157,43 +203,41 @@ export class FileSystem {
} satisfies IllegalMutationError; } 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) { if (destinationNode === undefined) {
throw { throw {
type: "NodeNotFound", type: "NodeNotFound",
message: `Destination node under inode ${destinationInode} not found`, message: `Destination node under inode ${dptr.inode} not found`,
timestamp: Date.now(), timestamp: Date.now(),
inode: destinationInode, inode: dptr.inode,
} satisfies NodeNotFoundError; } satisfies NodeNotFoundError;
} }
if (destinationNode.type !== TreeNodeType.Directory) { if (destinationNode.type !== NodeType.Directory) {
throw { throw {
type: "NotADirectory", type: "NotADirectory",
message: `Destination node under inode ${destinationInode} is not a directory`, message: `Destination node under inode ${dptr.inode} is not a directory`,
inode: destinationInode, inode: dptr.inode,
timestamp: Date.now(), timestamp: Date.now(),
nodeType: destinationNode.type, nodeType: destinationNode.type,
} satisfies NotADirectoryError; } 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 { throw {
type: "AlreadyExists", 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(), timestamp: Date.now(),
parentInode: destinationInode, parentInode: destinationNode.inode,
name: nodeName, name: nodeName,
} satisfies AlreadyExistsError; } satisfies AlreadyExistsError;
} }
@@ -208,8 +252,6 @@ export class FileSystem {
} satisfies DirectoryLoopError; } satisfies DirectoryLoopError;
} }
const nodeName = name ?? sourceNode;
let newNode: TreeNode; let newNode: TreeNode;
if (sourceNode.type === TreeNodeType.File) { if (sourceNode.type === TreeNodeType.File) {
+18 -11
View File
@@ -1,5 +1,5 @@
import type { FileSystem } from "@core/filesystem/filesystem"; import type { FileSystem } from "@core/filesystem/filesystem";
import { NodeType, type Node } from "@core/filesystem/node"; import { fromPtr, NodeType, toPtr, type NodePointer } from "@core/filesystem/node";
export function dnTraverseTree(fs: FileSystem, root: number): number[] { export function dnTraverseTree(fs: FileSystem, root: number): number[] {
if (fs.getNode(root) === undefined) return []; if (fs.getNode(root) === undefined) return [];
@@ -24,19 +24,26 @@ export function dnTraverseTree(fs: FileSystem, root: number): number[] {
return result; return result;
} }
export function upTraverseTree(fs: FileSystem, inode: number): number[] { export function upTraverseTree(fs: FileSystem, ptr: NodePointer): NodePointer[] {
if (fs.getNode(inode) === undefined) return []; if (fs.getNode(ptr) === undefined) return [];
const result: number[] = []; const fptr = fromPtr(ptr);
result.push(inode); const result: NodePointer[] = [];
result.push(ptr);
let currentInode = inode; let currentInode = fptr.inode;
while (currentInode !== 0) { let currentDev = fptr.device_id;
const node = fs.getNode(currentInode) as Node; while (currentInode !== 0 && currentDev !== 0) {
if (!node) break; const ptr = toPtr(currentDev, currentInode);
result.push(node.parent); const rindex = fs.getNodeEntries(ptr)!;
currentInode = node.parent; if (!rindex || rindex.length === 0) throw new Error("Fuck...");
const pptr = toPtr(rindex[0].pdevice_id, rindex[0].pinode);
result.push(pptr);
currentInode = rindex[0].pinode;
currentDev = rindex[0].pdevice_id;
} }
return result; return result;
+26 -7
View File
File diff suppressed because one or more lines are too long