a lot of mess, keep working on VDisk for now.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { TreeNodeType } from "./treenode";
|
||||
import type { TreeNodeType } from "./node";
|
||||
|
||||
export type FileSystemError =
|
||||
NotADirectoryError | AlreadyExistsError | NodeNotFoundError | IllegalMutationError | FileSystemInitializationError;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -1,7 +1,18 @@
|
||||
export type TreeNode = FileNode | DirectoryNode;
|
||||
export type Node = FileNode | DirectoryNode | SymlinkNode;
|
||||
|
||||
export type NodeHash = ReadonlyMap<number, Node>;
|
||||
export type ShadowHash = ReadonlyMap<number, Node | null>;
|
||||
|
||||
export type NodePointer = string;
|
||||
|
||||
export interface NodeEntry {
|
||||
pinode: number;
|
||||
pdevice_id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface FileNode {
|
||||
readonly type: TreeNodeType.File;
|
||||
readonly type: NodeType.File;
|
||||
readonly inode: number;
|
||||
readonly link_count: number;
|
||||
readonly mode: number;
|
||||
@@ -11,11 +22,11 @@ export interface FileNode {
|
||||
readonly ctime: number;
|
||||
readonly atime: number;
|
||||
readonly content: string;
|
||||
readonly device_id: number;
|
||||
readonly size: number;
|
||||
}
|
||||
|
||||
export interface DirectoryNode {
|
||||
readonly type: TreeNodeType.Directory;
|
||||
readonly type: NodeType.Directory;
|
||||
readonly inode: number;
|
||||
readonly link_count: number;
|
||||
readonly mode: number;
|
||||
@@ -24,12 +35,11 @@ export interface DirectoryNode {
|
||||
readonly mtime: number;
|
||||
readonly ctime: number;
|
||||
readonly atime: number;
|
||||
readonly content: ReadonlyMap<string, number>;
|
||||
readonly device_id: number;
|
||||
readonly children: Map<string, number>;
|
||||
}
|
||||
|
||||
export interface SymlinkNode {
|
||||
readonly type: TreeNodeType.Symlink;
|
||||
readonly type: NodeType.Symlink;
|
||||
readonly inode: number;
|
||||
readonly link_count: number;
|
||||
readonly mode: number;
|
||||
@@ -38,27 +48,11 @@ export interface SymlinkNode {
|
||||
readonly mtime: number;
|
||||
readonly ctime: number;
|
||||
readonly atime: number;
|
||||
readonly content: string;
|
||||
readonly device_id: number;
|
||||
readonly target: string;
|
||||
}
|
||||
|
||||
export interface DeviceNode {
|
||||
readonly type: TreeNodeType.Device;
|
||||
readonly inode: number;
|
||||
readonly link_count: number;
|
||||
readonly mode: number;
|
||||
readonly owner: number;
|
||||
readonly group: number;
|
||||
readonly mtime: number;
|
||||
readonly ctime: number;
|
||||
readonly atime: number;
|
||||
readonly content: number; // Device-ID
|
||||
readonly device_id: number;
|
||||
}
|
||||
|
||||
export enum TreeNodeType {
|
||||
export enum NodeType {
|
||||
File = 0,
|
||||
Directory = 1,
|
||||
Symlink = 2,
|
||||
Device = 3,
|
||||
}
|
||||
+272
-12
@@ -1,18 +1,278 @@
|
||||
import type { ReadableStream } from "@core/util/stream";
|
||||
import type { TreeNode } from "./treenode";
|
||||
import { NodeType, type DirectoryNode, type Node, type NodeEntry, type NodeHash, type NodePointer } from "./node";
|
||||
|
||||
export type NodeHash = ReadonlyMap<number, TreeNode>;
|
||||
export type ShadowHash = ReadonlyMap<number, TreeNode | null>;
|
||||
export interface VDiskConfig {
|
||||
id: number;
|
||||
label: number;
|
||||
index: number;
|
||||
base: NodeHash;
|
||||
dataC: DataConfig;
|
||||
//metaC: MetaConfig;
|
||||
}
|
||||
|
||||
export interface DataConfig {
|
||||
chunkSize: number;
|
||||
cacheSize: number;
|
||||
cacheTTL: number;
|
||||
}
|
||||
|
||||
//export interface MetaConfig {}
|
||||
|
||||
export interface IDataStore {
|
||||
put(data: Uint8Array | ReadableStream): Promise<string>;
|
||||
get(hash: string): Promise<ReadableStream>;
|
||||
delete(hash: string): Promise<void>;
|
||||
read(uuid: string): Promise<Uint8Array>;
|
||||
stream(uuid: string): null; // Stream
|
||||
put(uuid: string, blob: Blob): Promise<void>;
|
||||
delete(uuid: string): Promise<void>;
|
||||
has(uuid: string): Promise<boolean>;
|
||||
list(): Promise<string[]>;
|
||||
}
|
||||
|
||||
export interface VDisk {
|
||||
readonly id: number;
|
||||
readonly store: IDataStore;
|
||||
readonly base: NodeHash;
|
||||
readonly overlay: ShadowHash;
|
||||
export interface IMetaStore {
|
||||
loadAll(): Promise<Map<number, Node | null>>;
|
||||
write(inode: number, node: Node): Promise<void>;
|
||||
delete(inode: number): Promise<void>;
|
||||
}
|
||||
|
||||
export class VDisk {
|
||||
private readonly _id: number;
|
||||
private readonly _label: string;
|
||||
private readonly _readonly: boolean;
|
||||
private readonly _totalSize: number;
|
||||
private readonly _base: NodeHash;
|
||||
private readonly _dataStore: IDataStore;
|
||||
|
||||
private _iIndex: number;
|
||||
private _rIndex: ReadonlyMap<NodePointer, ReadonlyArray<NodeEntry>>;
|
||||
private _overlay: Map<number, Node | null>;
|
||||
private _metaStore: IMetaStore;
|
||||
|
||||
private _rIndexInvalid: boolean;
|
||||
|
||||
private _isWritable(): void {
|
||||
if (this._readonly) throw Error(`The ${this._label} device under id - ${this._id} is marked readonly`);
|
||||
}
|
||||
|
||||
private _putNode(inode: number, node: Node | null): void {
|
||||
this._overlay.set(inode, node);
|
||||
}
|
||||
|
||||
public getNode(inode: number): Node | undefined {
|
||||
const node = this._overlay.get(inode);
|
||||
if (node) return node;
|
||||
if (node === null) return undefined;
|
||||
|
||||
return this._base.get(inode);
|
||||
}
|
||||
|
||||
public createNodeEntry(
|
||||
pinode: number,
|
||||
name: string,
|
||||
data:
|
||||
| { type: NodeType.File; mode: number; owner: number; group: number; content: string; size: number }
|
||||
| { type: NodeType.Directory; mode: number; owner: number; group: number }
|
||||
| { type: NodeType.Symlink; mode: number; owner: number; group: number; target: string },
|
||||
): number {
|
||||
this._isWritable();
|
||||
|
||||
const index = this._iIndex++;
|
||||
const time = Date.now();
|
||||
const link_count = data.type === NodeType.Directory ? 2 : 1;
|
||||
|
||||
const base = {
|
||||
inode: index,
|
||||
link_count,
|
||||
type: data.type,
|
||||
mode: data.mode,
|
||||
owner: data.owner,
|
||||
group: data.group,
|
||||
mtime: time,
|
||||
ctime: time,
|
||||
atime: time,
|
||||
};
|
||||
|
||||
const pnode = this.getNode(pinode) as DirectoryNode;
|
||||
|
||||
let node: Node;
|
||||
switch (data.type) {
|
||||
case NodeType.File:
|
||||
node = { ...base, type: NodeType.File, content: data.content, size: data.size };
|
||||
break;
|
||||
case NodeType.Directory:
|
||||
node = { ...base, type: NodeType.Directory, children: new Map() };
|
||||
break;
|
||||
case NodeType.Symlink:
|
||||
node = { ...base, type: NodeType.Symlink, target: data.target };
|
||||
break;
|
||||
}
|
||||
|
||||
const children = new Map(pnode.children);
|
||||
children.set(name, index);
|
||||
|
||||
const parent = {
|
||||
...pnode,
|
||||
children,
|
||||
link_count: pnode.link_count + (data.type === NodeType.Directory ? 1 : 0),
|
||||
mtime: time,
|
||||
ctime: time,
|
||||
};
|
||||
|
||||
this._putNode(index, node);
|
||||
this._putNode(pinode, parent);
|
||||
this.invalidateRIndex();
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
public linkNodeEntry(pinode: number, name: string, tinode: number): void {
|
||||
this._isWritable();
|
||||
|
||||
const pnode = this.getNode(pinode) as DirectoryNode;
|
||||
const tnode = this.getNode(tinode) as Node;
|
||||
const time = Date.now();
|
||||
|
||||
const children = new Map(pnode.children);
|
||||
children.set(name, tnode.inode);
|
||||
|
||||
const parent = { ...pnode, children, mtime: time, ctime: time };
|
||||
const target = { ...tnode, link_count: tnode.link_count + 1, ctime: time };
|
||||
|
||||
this._putNode(tinode, target);
|
||||
this._putNode(pinode, parent);
|
||||
this.invalidateRIndex();
|
||||
}
|
||||
|
||||
public unlinkNodeEntry(inode: number, name: string): void {
|
||||
this._isWritable();
|
||||
|
||||
const pnode = this.getNode(inode) as DirectoryNode;
|
||||
const tinode = pnode.children.get(name)!;
|
||||
const tnode = this.getNode(tinode) as Node;
|
||||
const time = Date.now();
|
||||
|
||||
const children = new Map(pnode.children);
|
||||
children.delete(name);
|
||||
const parent = { ...pnode, children, mtime: time, ctime: time };
|
||||
|
||||
const link_count = tnode.link_count - 1;
|
||||
const wasLast = link_count === 0;
|
||||
|
||||
this._putNode(inode, parent);
|
||||
this._putNode(tinode, wasLast ? null : { ...tnode, link_count, ctime: Date.now() });
|
||||
this.invalidateRIndex();
|
||||
}
|
||||
|
||||
public moveNodeEntry(pinode: number, dinode: number, tname: string, dname?: string): void {
|
||||
this._isWritable();
|
||||
|
||||
const pnode = this.getNode(pinode) as DirectoryNode;
|
||||
const dnode = this.getNode(dinode) as DirectoryNode;
|
||||
|
||||
const tinode = pnode.children.get(tname)!;
|
||||
const tnode = this.getNode(tinode)!;
|
||||
|
||||
const name = dname ?? tname;
|
||||
const isDir = tnode.type === NodeType.Directory;
|
||||
const time = Date.now();
|
||||
|
||||
if (pinode === dinode) {
|
||||
const pchildren = new Map(pnode.children);
|
||||
pchildren.delete(tname);
|
||||
pchildren.set(name, tinode);
|
||||
|
||||
const parent = { ...pnode, children: pchildren, mtime: time, ctime: time };
|
||||
const target = { ...tnode, ctime: time };
|
||||
|
||||
this._putNode(pinode, parent);
|
||||
this._putNode(tinode, target);
|
||||
this.invalidateRIndex();
|
||||
return;
|
||||
}
|
||||
|
||||
const pchildren = new Map(pnode.children);
|
||||
pchildren.delete(tname);
|
||||
|
||||
const dchildren = new Map(dnode.children);
|
||||
dchildren.set(name, tinode);
|
||||
|
||||
const parent = {
|
||||
...pnode,
|
||||
children: pchildren,
|
||||
mtime: time,
|
||||
ctime: time,
|
||||
link_count: pnode.link_count - (isDir ? 1 : 0),
|
||||
};
|
||||
const target = { ...tnode, ctime: time };
|
||||
const destination = {
|
||||
...dnode,
|
||||
children: dchildren,
|
||||
mtime: time,
|
||||
ctime: time,
|
||||
link_count: dnode.link_count + (isDir ? 1 : 0),
|
||||
};
|
||||
|
||||
this._putNode(pinode, parent);
|
||||
this._putNode(tinode, target);
|
||||
this._putNode(dinode, destination);
|
||||
this.invalidateRIndex();
|
||||
}
|
||||
|
||||
public writeNodeEntry(inode: number, data: { mode?: number; owner?: number; group?: number }): void {
|
||||
this._isWritable();
|
||||
|
||||
const node = this.getNode(inode)!;
|
||||
|
||||
const newNode: Node = {
|
||||
...node,
|
||||
mode: data.mode ?? node.mode,
|
||||
owner: data.owner ?? node.owner,
|
||||
group: data.group ?? node.group,
|
||||
ctime: Date.now(),
|
||||
};
|
||||
|
||||
this._putNode(inode, newNode);
|
||||
}
|
||||
|
||||
public readContent(uuid: string): Promise<Uint8Array> {}
|
||||
|
||||
public streamContent(uuid: string): Stream {}
|
||||
|
||||
public writeContent(uuid: string, blob: Blob): Promise<void> {
|
||||
this.isWritable();
|
||||
}
|
||||
|
||||
public deleteContent(uuid: string): Promise<void> {
|
||||
this.isWritable();
|
||||
}
|
||||
|
||||
public hasContent(uuid: string): Promise<boolean> {}
|
||||
|
||||
public loadOverlay(): Promise<void> {}
|
||||
|
||||
public createOverlayEntry(inode: number, node: Node | null): Promise<void> {
|
||||
this.isWritable();
|
||||
}
|
||||
|
||||
public invalidateRIndex(): void {
|
||||
this._rIndexInvalid = true;
|
||||
}
|
||||
|
||||
public getIndex(): ReadonlyMap<NodePointer, ReadonlyArray<NodeEntry>> {}
|
||||
|
||||
// public sweepOrphans(): Promise<void> {}
|
||||
// public sweepDangling(): Promise<void> {}
|
||||
|
||||
public get ID(): number {
|
||||
return this._id;
|
||||
}
|
||||
|
||||
public get Label(): string {
|
||||
return this._label;
|
||||
}
|
||||
|
||||
public get IsReadonly(): boolean {
|
||||
return this._readonly;
|
||||
}
|
||||
|
||||
public get ByteSize(): number {
|
||||
return this._totalSize;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user