some small work on the filesystem class

This commit is contained in:
2026-07-16 13:19:27 +02:00
parent ea82327ee8
commit b6365d6221
3 changed files with 69 additions and 13 deletions
+7 -8
View File
@@ -1,13 +1,6 @@
import type { TreeNodeType } from "./treenode";
export type FileSystemError = ParentNotFoundError | NotADirectoryError | AlreadyExistsError | NodeNotFoundError;
export interface ParentNotFoundError {
type: "ParentNotFound";
message: string;
timestamp: number;
parentInode: number;
}
export type FileSystemError = NotADirectoryError | AlreadyExistsError | NodeNotFoundError | IllegalMutationError;
export interface NotADirectoryError {
inode: number;
@@ -31,3 +24,9 @@ export interface NodeNotFoundError {
inode: number;
timestamp: number;
}
export interface IllegalMutationError {
type: "IllegalMutation";
message: string;
timestamp: number;
}
+62 -5
View File
@@ -1,5 +1,5 @@
import { createImmutableBuffer, type ImmutableBuffer } from "@core/util/immutable_buffer";
import type { AlreadyExistsError, NotADirectoryError, ParentNotFoundError } from "./errors";
import type { AlreadyExistsError, IllegalMutationError, NodeNotFoundError, NotADirectoryError } from "./errors";
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "./treenode";
export class FileSystem {
@@ -73,11 +73,11 @@ export class FileSystem {
if (parentNode === undefined) {
throw {
type: "ParentNotFound",
type: "NodeNotFound",
message: `Parent inode ${parent} not found`,
timestamp: Date.now(),
parentInode: parent,
} satisfies ParentNotFoundError;
inode: parent,
} satisfies NodeNotFoundError;
}
if (parentNode.type === TreeNodeType.File) {
@@ -157,7 +157,64 @@ export class FileSystem {
return FileSystem.init(this._BaseFS, newOverlay, newInode);
}
public deleteNode(inode: number): FileSystem {}
public deleteNode(inode: number): FileSystem {
if (inode === 0) {
throw {
type: "IllegalMutation",
message: `Attempt at illegal mutation of node under inode 0 (root)`,
timestamp: Date.now(),
} satisfies IllegalMutationError;
}
const node = this.resolveNode(inode);
if (node === undefined) {
throw {
type: "NodeNotFound",
message: `Node under inode ${inode} not found`,
timestamp: Date.now(),
inode: inode,
} satisfies NodeNotFoundError;
}
const time: number = Date.now();
const descendants: number[] = [inode];
const map: Map<number, TreeNode | null> = new Map(this._OverlayFS);
for (const entry of descendants) {
map.set(entry, null);
}
const parentNode = this.resolveNode(node.parent) as DirectoryNode;
if (parentNode === undefined) {
throw {
type: "NodeNotFound",
message: `Parent inode ${node.parent} not found (should be impossible unless the filesystem corrupted!)`,
timestamp: Date.now(),
inode: node.parent,
} satisfies NodeNotFoundError;
}
const { [node.name]: _, ...newChildren } = parentNode.children;
const newParentNode: DirectoryNode = {
type: parentNode.type,
inode: parentNode.inode,
name: parentNode.name,
parent: parentNode.parent,
mode: parentNode.mode,
owner: parentNode.owner,
group: parentNode.group,
mtime: time,
atime: parentNode.atime,
ctime: time,
children: newChildren,
};
map.set(node.parent, newParentNode);
const newOverlay: ReadonlyMap<number, TreeNode | null> = map;
return FileSystem.init(this._BaseFS, newOverlay, this._INodeIndex);
}
public writeNode(inode: number, content: ImmutableBuffer): FileSystem {}