a lot of mess, keep working on VDisk for now.
This commit is contained in:
@@ -10,13 +10,31 @@ import type {
|
||||
NodeNotFoundError,
|
||||
NotADirectoryError,
|
||||
} from "./errors";
|
||||
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "./treenode";
|
||||
import type { IDataStore, NodeHash, ShadowHash } from "./vdisk";
|
||||
import {
|
||||
type DirectoryNode,
|
||||
type FileNode,
|
||||
type NodeEntry,
|
||||
type NodeHash,
|
||||
type NodePointer,
|
||||
type ShadowHash,
|
||||
} from "./node";
|
||||
import type { IDataStore } from "./vdisk";
|
||||
|
||||
export interface FileSystemConfig {
|
||||
maxPathLength: number;
|
||||
maxSymlinkDepth: number;
|
||||
maxNameLength: number;
|
||||
}
|
||||
|
||||
export class FileSystem {
|
||||
private _SysVDisk: number = 0;
|
||||
private _Mounts: Map<number, number> = new Map();
|
||||
private readonly _maxNameLength: number;
|
||||
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 = new VDiskManager();
|
||||
private _EntryCache: Map<NodePointer, NodeEntry>;
|
||||
|
||||
private constructor(store: IDataStore[], base: NodeHash[], overlay: ShadowHash[], id: number[]) {
|
||||
for (let i = 0; i < id.length; i++) {
|
||||
@@ -26,18 +44,33 @@ export class FileSystem {
|
||||
}
|
||||
this._VDiskManager.registerVDisk(store[i]!, base[i], overlay[i], id[i]);
|
||||
}
|
||||
|
||||
this._EntryCache = this.buildNodeEntryCache();
|
||||
}
|
||||
|
||||
private resolveNode(inode: number): TreeNode | undefined {
|
||||
const node = this._OverlayFS.get(inode);
|
||||
private updateDentryCache(key: NodePointer, entry: NodeEntry): void {}
|
||||
|
||||
private resolveNode(ptr: NodePointer): TreeNode | undefined {
|
||||
const dev = this._VDiskManager.getVDisk(ptr[1]);
|
||||
if (!dev) return undefined;
|
||||
|
||||
const node = dev.overlay.get(ptr[0]);
|
||||
if (node) return node;
|
||||
if (node === null) return undefined;
|
||||
|
||||
return this._BaseFS.get(inode);
|
||||
return dev.base.get(ptr[0]);
|
||||
}
|
||||
|
||||
public static init(store: IDataStore, base: NodeHash, overlay: ShadowHash, index: number): FileSystem {
|
||||
if (base.get(0) === undefined) {
|
||||
public static init(store: IDataStore[], base: NodeHash[], overlay: ShadowHash[], index: number[]): FileSystem {
|
||||
if (store.length === 0 || base.length === 0 || index.length === 0) {
|
||||
throw {
|
||||
type: "FileSystemInitializationError",
|
||||
message: "File System tried to initialized without a base hashmap and store",
|
||||
timestamp: Date.now(),
|
||||
} satisfies FileSystemInitializationError;
|
||||
}
|
||||
|
||||
if (base[0]!.get(0) === undefined) {
|
||||
throw {
|
||||
type: "FileSystemInitializationError",
|
||||
message: "File System failed to initialize because root node is undefined or not assigned to inode 0",
|
||||
@@ -45,7 +78,7 @@ export class FileSystem {
|
||||
} satisfies FileSystemInitializationError;
|
||||
}
|
||||
|
||||
if (base.get(0)?.type !== TreeNodeType.Directory) {
|
||||
if (base[0]!.get(0)?.type !== TreeNodeType.Directory) {
|
||||
throw {
|
||||
type: "FileSystemInitializationError",
|
||||
message: "File System failed to initialize because root node is not a directory",
|
||||
@@ -57,66 +90,71 @@ export class FileSystem {
|
||||
}
|
||||
|
||||
public getRoot(): DirectoryNode {
|
||||
const node = this.resolveNode(0);
|
||||
if (node === undefined) throw new Error("FileSystem corrupted! INode 0 did not resolve properly.");
|
||||
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;
|
||||
}
|
||||
|
||||
public getNode(inode: number): TreeNode | undefined {
|
||||
return this.resolveNode(inode);
|
||||
public getSysPartitionID(): number {
|
||||
return this._SysPartitionID;
|
||||
}
|
||||
|
||||
public exists(inode: number): boolean {
|
||||
return this.resolveNode(inode) !== undefined;
|
||||
public getNode(ptr: NodePointer): TreeNode | undefined {
|
||||
return this.resolveNode(ptr);
|
||||
}
|
||||
|
||||
public isFile(inode: number): boolean {
|
||||
const node = this.resolveNode(inode);
|
||||
public exists(ptr: NodePointer): boolean {
|
||||
return this.resolveNode(ptr) !== undefined;
|
||||
}
|
||||
|
||||
public isFile(ptr: NodePointer): boolean {
|
||||
const node = this.resolveNode(ptr);
|
||||
|
||||
if (node === undefined) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Node under inode ${inode} not found`,
|
||||
message: `Node under inode ${ptr[0]} not found`,
|
||||
timestamp: Date.now(),
|
||||
inode: inode,
|
||||
inode: ptr[0],
|
||||
} satisfies NodeNotFoundError;
|
||||
}
|
||||
|
||||
return node.type === TreeNodeType.File ? true : false;
|
||||
}
|
||||
|
||||
public isDirectory(inode: number): boolean {
|
||||
const node = this.resolveNode(inode);
|
||||
public isDirectory(ptr: NodePointer): boolean {
|
||||
const node = this.resolveNode(ptr);
|
||||
|
||||
if (node === undefined) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Node under inode ${inode} not found`,
|
||||
message: `Node under inode ${ptr[0]} not found`,
|
||||
timestamp: Date.now(),
|
||||
inode: inode,
|
||||
inode: ptr[0],
|
||||
} satisfies NodeNotFoundError;
|
||||
}
|
||||
|
||||
return node.type === TreeNodeType.Directory ? true : false;
|
||||
}
|
||||
|
||||
/* public isSymlink(inode: number): boolean {
|
||||
const node = this.resolveNode(inode);
|
||||
public isSymlink(ptr: NodePointer): boolean {
|
||||
const node = this.resolveNode(ptr);
|
||||
|
||||
if (node === undefined) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Node under inode ${inode} not found`,
|
||||
message: `Node under inode ${ptr[0]} not found`,
|
||||
timestamp: Date.now(),
|
||||
inode: inode,
|
||||
inode: ptr[0],
|
||||
} satisfies NodeNotFoundError;
|
||||
}
|
||||
|
||||
return node.type === TreeNodeType.Symlink ? true : false;
|
||||
} */
|
||||
}
|
||||
|
||||
public moveNode(sourceInode: number, destinationInode: number, name?: string): FileSystem {
|
||||
if (sourceInode === 0) {
|
||||
public moveNode(source: NodePointer, destination: NodePointer, name?: string): FileSystem {
|
||||
if (source[0] === 0) {
|
||||
throw {
|
||||
type: "IllegalMutation",
|
||||
message: `Attempt at illegal mutation of node under inode 0 (root)`,
|
||||
@@ -124,20 +162,18 @@ export class FileSystem {
|
||||
} satisfies IllegalMutationError;
|
||||
}
|
||||
|
||||
const sourceNode = this.resolveNode(sourceInode);
|
||||
const destinationNode = this.resolveNode(destinationInode);
|
||||
const sourceNode = this.resolveNode(source);
|
||||
const destinationNode = this.resolveNode(destination);
|
||||
|
||||
if (sourceNode === undefined) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Source node under inode ${sourceInode} not found`,
|
||||
message: `Source node under inode ${source[0]} not found`,
|
||||
timestamp: Date.now(),
|
||||
inode: sourceInode,
|
||||
inode: source[0],
|
||||
} satisfies NodeNotFoundError;
|
||||
}
|
||||
|
||||
const nodeName = name ?? sourceNode.name;
|
||||
|
||||
if (destinationNode === undefined) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
@@ -177,6 +213,8 @@ export class FileSystem {
|
||||
} satisfies DirectoryLoopError;
|
||||
}
|
||||
|
||||
const nodeName = name ?? sourceNode;
|
||||
|
||||
let newNode: TreeNode;
|
||||
|
||||
if (sourceNode.type === TreeNodeType.File) {
|
||||
|
||||
Reference in New Issue
Block a user