- Fixed resolvePath

- Added symlink resolution
- Added mount resolution
- Fixed filesystem not awaiting for disks to be mounted
(now `FileSystem.init()` needs to be called with `await`)
- Removed old test fixture files
- Added a feature-full `resolvePath`test suite

*all tests passing green*
This commit is contained in:
2026-11-10 08:04:57 +01:00
parent dd804f6f14
commit 4fa284de13
14 changed files with 4608 additions and 657 deletions
+157 -41
View File
@@ -7,6 +7,7 @@ import type {
AlreadyExistsError,
DeviceNotFoundError,
DirectoryLoopError,
FileSystemCorruptionError,
FileSystemInitializationError,
IllegalMutationError,
InvalidNodeNameError,
@@ -32,25 +33,20 @@ export interface FileSystemConfig {
}
export class FileSystem {
private readonly _maxNameLength: number;
private readonly _maxPathLength: number;
private readonly _maxSymlinkDepth: number;
private _Mounts: Map<NodePointer, { tinode: number; tdevice_id: number }> = new Map();
private _Mounts: Map<NodePointer, NodePointer> = new Map();
private _RMounts: Map<NodePointer, NodePointer> = new Map();
private _VDiskManager: VDiskManager;
private constructor(
devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>,
config: FileSystemConfig,
) {
this._maxNameLength = config.maxNameLength;
this._maxPathLength = config.maxPathLength;
this._maxSymlinkDepth = config.maxSymlinkDepth;
readonly maxNameLength: number;
readonly maxPathLength: number;
readonly maxSymlinkDepth: number;
this._VDiskManager = new VDiskManager();
for (const dev of devices) {
this._VDiskManager.registerDisk(dev.config, dev.dstore, dev.mstore);
}
private constructor(manager: VDiskManager, config: FileSystemConfig) {
this.maxNameLength = config.maxNameLength;
this.maxPathLength = config.maxPathLength;
this.maxSymlinkDepth = config.maxSymlinkDepth;
this._VDiskManager = manager;
}
private resolveNode(ptr: NodePointer): Node | undefined {
@@ -61,10 +57,11 @@ export class FileSystem {
return dev.getNode(fptr.inode);
}
public static init(
// Make async
public static async init(
devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>,
config: FileSystemConfig = { maxNameLength: 255, maxPathLength: Infinity, maxSymlinkDepth: 40 },
): FileSystem {
): Promise<FileSystem> {
if (devices.length === 0) {
throw {
type: "FileSystemInitializationError",
@@ -89,19 +86,39 @@ export class FileSystem {
} satisfies FileSystemInitializationError;
}
return new FileSystem(devices, config);
const manager = new VDiskManager();
for (const dev of devices) {
await manager.registerDisk(dev.config, dev.dstore, dev.mstore);
}
return new FileSystem(manager, config);
}
public getRoot(): DirectoryNode {
const dev = this._VDiskManager.getDisk(0);
if (!dev) throw new Error("Filesystem corrupted, system device not found!");
if (!dev) {
throw {
type: "FileSystemCorruption",
message: "System device not registered. File System corrupted!",
timestamp: Date.now(),
} satisfies FileSystemCorruptionError;
}
return dev.getNode(0) as DirectoryNode;
}
public getNodeEntries(ptr: NodePointer): readonly NodeEntry[] | undefined {
const fptr = fromPtr(ptr);
const dev = this._VDiskManager.getDisk(fptr.device_id)!;
const dev = this._VDiskManager.getDisk(fptr.device_id);
if (!dev) {
throw {
type: "DeviceNotFound",
message: `Device under id ${fptr.device_id} not found`,
timestamp: Date.now(),
device_id: fptr.device_id,
} satisfies DeviceNotFoundError;
}
return dev.getIndex().get(ptr);
}
@@ -114,10 +131,79 @@ export class FileSystem {
return this.resolveNode(ptr) !== undefined;
}
public mount(mount: NodePointer, target: NodePointer) {
const mptr = fromPtr(mount);
const tptr = fromPtr(target);
if (!this._VDiskManager.hasDisk(mptr.device_id)) {
throw {
type: "DeviceNotFound",
message: `Device under id ${mptr.device_id} not found`,
timestamp: Date.now(),
device_id: mptr.device_id,
} satisfies DeviceNotFoundError;
}
if (!this._VDiskManager.hasDisk(tptr.device_id)) {
throw {
type: "DeviceNotFound",
message: `Device under id ${tptr.device_id} not found`,
timestamp: Date.now(),
device_id: tptr.device_id,
} satisfies DeviceNotFoundError;
}
if (!this.getNode(mount)) {
throw {
type: "NodeNotFound",
message: `Node under inode ${mptr.inode} not found under device ${mptr.device_id}`,
timestamp: Date.now(),
pointer: mount,
} satisfies NodeNotFoundError;
}
if (!this.getNode(target)) {
throw {
type: "NodeNotFound",
message: `Node under inode ${tptr.inode} not found under device ${tptr.device_id}`,
timestamp: Date.now(),
pointer: target,
} satisfies NodeNotFoundError;
}
this._Mounts.set(mount, target);
this._RMounts.set(target, mount);
}
public isMountPoint(ptr: NodePointer): boolean {
return this._Mounts.has(ptr);
}
public isMountTarget(target: NodePointer): boolean {
return this._RMounts.has(target);
}
public getMountTarget(ptr: NodePointer): NodePointer | undefined {
return this._Mounts.get(ptr);
}
public getMountPoint(target: NodePointer): NodePointer | undefined {
return this._RMounts.get(target);
}
public isFile(ptr: NodePointer): boolean {
const fptr = fromPtr(ptr);
const node = this.resolveNode(ptr);
if (!this._VDiskManager.hasDisk(fptr.device_id)) {
throw {
type: "DeviceNotFound",
message: `Device under id ${fptr.device_id} not found`,
timestamp: Date.now(),
device_id: fptr.device_id,
} satisfies DeviceNotFoundError;
}
if (node === undefined) {
throw {
type: "NodeNotFound",
@@ -134,6 +220,15 @@ export class FileSystem {
const fptr = fromPtr(ptr);
const node = this.resolveNode(ptr);
if (!this._VDiskManager.hasDisk(fptr.device_id)) {
throw {
type: "DeviceNotFound",
message: `Device under id ${fptr.device_id} not found`,
timestamp: Date.now(),
device_id: fptr.device_id,
} satisfies DeviceNotFoundError;
}
if (node === undefined) {
throw {
type: "NodeNotFound",
@@ -150,6 +245,15 @@ export class FileSystem {
const fptr = fromPtr(ptr);
const node = this.resolveNode(ptr);
if (!this._VDiskManager.hasDisk(fptr.device_id)) {
throw {
type: "DeviceNotFound",
message: `Device under id ${fptr.device_id} not found`,
timestamp: Date.now(),
device_id: fptr.device_id,
} satisfies DeviceNotFoundError;
}
if (node === undefined) {
throw {
type: "NodeNotFound",
@@ -162,7 +266,7 @@ export class FileSystem {
return node.type === NodeType.Symlink ? true : false;
}
public moveNode(source: NodePointer, destination: NodePointer, name?: string) {
public async moveNode(source: NodePointer, destination: NodePointer, name?: string) {
const sptr = fromPtr(source);
const dptr = fromPtr(destination);
const sdev = this._VDiskManager.getDisk(sptr.device_id);
@@ -226,7 +330,13 @@ export class FileSystem {
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");
if (!s_entry || !s_entry[0]) {
throw {
type: "FileSystemCorruption",
message: "Reverse index entry missing, FileSystem corrupted",
timestamp: Date.now(),
} satisfies FileSystemCorruptionError;
}
const pinode = s_entry[0].pinode;
@@ -242,7 +352,7 @@ export class FileSystem {
} satisfies AlreadyExistsError;
}
if (nodeName.length > this._maxNameLength) {
if (nodeName.length > this.maxNameLength) {
throw {
type: "InvalidNodeName",
message: `Invalid name for the ${source} source node`,
@@ -263,13 +373,13 @@ export class FileSystem {
}
if (sptr.device_id === dptr.device_id) {
ddev.moveNodeEntry(pinode, dptr.inode, s_entry[0].name, name);
await ddev.moveNodeEntry(pinode, dptr.inode, s_entry[0].name, name);
} else {
console.error("cross device node movement not yet implemented");
}
}
public createNode(
public async createNode(
parent: NodePointer,
name: string,
data:
@@ -326,7 +436,7 @@ export class FileSystem {
} satisfies AlreadyExistsError;
}
if (name.length > this._maxNameLength) {
if (name.length > this.maxNameLength) {
throw {
type: "InvalidNodeName",
message: `Invalid name for the new node`,
@@ -336,14 +446,14 @@ export class FileSystem {
} satisfies InvalidNodeNameError;
}
pdev.createNodeEntry(pptr.inode, name, data);
await pdev.createNodeEntry(pptr.inode, name, data);
}
public deleteNode(target: NodePointer) {
public async deleteNode(target: NodePointer) {
const tptr = fromPtr(target);
const tdev = this._VDiskManager.getDisk(tptr.device_id);
if (tptr.device_id === 0 && tptr.inode === 0) {
if (tptr.inode === 0) {
throw {
type: "IllegalMutation",
message: `Attempt at illegal mutation of node under inode 0 (root)`,
@@ -362,12 +472,18 @@ export class FileSystem {
const tdevIndex = tdev.getIndex();
const t_entry = tdevIndex.get(target);
if (!t_entry || !t_entry[0]) throw new Error("File System corruption, reverse index is missing a field");
if (!t_entry || !t_entry[0]) {
throw {
type: "FileSystemCorruption",
message: "Reverse index entry missing, FileSystem corrupted",
timestamp: Date.now(),
} satisfies FileSystemCorruptionError;
}
tdev.unlinkNodeEntry(tptr.inode, t_entry[0].name);
await tdev.unlinkNodeEntry(t_entry[0].pinode, t_entry[0].name);
}
public writeContent(target: NodePointer, content: Blob) {
public async writeContent(target: NodePointer, content: Blob) {
const ptr = fromPtr(target);
const dev = this._VDiskManager.getDisk(ptr.device_id);
const node = this.resolveNode(target);
@@ -390,7 +506,7 @@ export class FileSystem {
} satisfies NodeNotFoundError;
}
if (node.type === NodeType.Directory) {
if (node.type === NodeType.Directory || node.type === NodeType.Symlink) {
throw {
type: "NotAFile",
message: `Node is not a file`,
@@ -400,7 +516,7 @@ export class FileSystem {
} satisfies NotAFileError;
}
dev.writeContent((node as FileNode).content, content);
await dev.writeContent((node as FileNode).content, content);
}
public streamContent(target: NodePointer): Readable {
@@ -439,7 +555,7 @@ export class FileSystem {
return dev.streamContent((node as FileNode).content);
}
public modifyNode(target: NodePointer, data: { mode?: number; owner?: number; group?: number }) {
public async modifyNode(target: NodePointer, data: { mode?: number; owner?: number; group?: number }) {
const ptr = fromPtr(target);
const dev = this._VDiskManager.getDisk(ptr.device_id);
const node = this.resolveNode(target);
@@ -462,7 +578,7 @@ export class FileSystem {
} satisfies NodeNotFoundError;
}
if (node.type === NodeType.Directory) {
if (node.type === NodeType.Directory || node.type === NodeType.Symlink) {
throw {
type: "NotAFile",
message: `Node is not a file`,
@@ -472,7 +588,7 @@ export class FileSystem {
} satisfies NotAFileError;
}
dev.writeNodeEntry(node.inode, data);
await dev.writeNodeEntry(node.inode, data);
}
public canRead(nodep: NodePointer, user: UserInfo): boolean {
@@ -490,7 +606,7 @@ export class FileSystem {
if (user.uid === node.owner) {
return (node.mode & 0o400) !== 0;
} else if (user.gid === node.group) {
} else if (user.gid === node.group || user.group.includes(node.group)) {
return (node.mode & 0o040) !== 0;
} else {
return (node.mode & 0o004) !== 0;
@@ -512,7 +628,7 @@ export class FileSystem {
if (user.uid === node.owner) {
return (node.mode & 0o200) !== 0;
} else if (user.gid === node.group) {
} else if (user.gid === node.group || user.group.includes(node.group)) {
return (node.mode & 0o020) !== 0;
} else {
return (node.mode & 0o002) !== 0;
@@ -534,7 +650,7 @@ export class FileSystem {
if (user.uid === node.owner) {
return (node.mode & 0o100) !== 0;
} else if (user.gid === node.group) {
} else if (user.gid === node.group || user.group.includes(node.group)) {
return (node.mode & 0o010) !== 0;
} else {
return (node.mode & 0o001) !== 0;