some small work on the filesystem class
This commit is contained in:
@@ -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 {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user