Virtual FileSystem fixed - (Needs unit testing)!
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import type { TreeNodeType } from "./node";
|
import type { NodePointer, NodeType } from "./node";
|
||||||
|
|
||||||
export type FileSystemError =
|
export type FileSystemError =
|
||||||
NotADirectoryError | AlreadyExistsError | NodeNotFoundError | IllegalMutationError | FileSystemInitializationError;
|
NotADirectoryError | AlreadyExistsError | NodeNotFoundError | IllegalMutationError | FileSystemInitializationError;
|
||||||
@@ -14,7 +14,15 @@ export interface NotADirectoryError {
|
|||||||
message: string;
|
message: string;
|
||||||
timestamp: number;
|
timestamp: number;
|
||||||
type: "NotADirectory";
|
type: "NotADirectory";
|
||||||
nodeType: TreeNodeType;
|
nodeType: NodeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface NotAFileError {
|
||||||
|
inode: number;
|
||||||
|
message: string;
|
||||||
|
timestamp: number;
|
||||||
|
type: "NotAFile";
|
||||||
|
nodeType: NodeType;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AlreadyExistsError {
|
export interface AlreadyExistsError {
|
||||||
@@ -28,7 +36,7 @@ export interface AlreadyExistsError {
|
|||||||
export interface NodeNotFoundError {
|
export interface NodeNotFoundError {
|
||||||
type: "NodeNotFound";
|
type: "NodeNotFound";
|
||||||
message: string;
|
message: string;
|
||||||
inode: number;
|
pointer: NodePointer;
|
||||||
timestamp: number;
|
timestamp: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,3 +60,11 @@ export interface DirectoryLoopError {
|
|||||||
descendantInode: number;
|
descendantInode: number;
|
||||||
ancestorInode: number;
|
ancestorInode: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface InvalidNodeNameError {
|
||||||
|
type: "InvalidNodeName";
|
||||||
|
message: string;
|
||||||
|
timestamp: number;
|
||||||
|
pointer: NodePointer;
|
||||||
|
attemptedName: string;
|
||||||
|
}
|
||||||
|
|||||||
+195
-199
@@ -1,15 +1,18 @@
|
|||||||
import type { UserInfo } from "@core/session_manager";
|
import type { UserInfo } from "@core/session_manager";
|
||||||
import { createImmutableBuffer, type ImmutableBuffer } from "@core/util/immutable_buffer";
|
import { upTraverseTree } from "@core/util/tree_traversal";
|
||||||
import { dnTraverseTree, upTraverseTree } from "@core/util/tree_traversal";
|
|
||||||
import { VDiskManager } from "@core/util/vdisk_manager";
|
import { VDiskManager } from "@core/util/vdisk_manager";
|
||||||
|
import type { Blob } from "node:buffer";
|
||||||
|
import type { Readable } from "node:stream";
|
||||||
import type {
|
import type {
|
||||||
AlreadyExistsError,
|
AlreadyExistsError,
|
||||||
DeviceNotFoundError,
|
DeviceNotFoundError,
|
||||||
DirectoryLoopError,
|
DirectoryLoopError,
|
||||||
FileSystemInitializationError,
|
FileSystemInitializationError,
|
||||||
IllegalMutationError,
|
IllegalMutationError,
|
||||||
|
InvalidNodeNameError,
|
||||||
NodeNotFoundError,
|
NodeNotFoundError,
|
||||||
NotADirectoryError,
|
NotADirectoryError,
|
||||||
|
NotAFileError,
|
||||||
} from "./errors";
|
} from "./errors";
|
||||||
import {
|
import {
|
||||||
fromPtr,
|
fromPtr,
|
||||||
@@ -91,7 +94,7 @@ export class FileSystem {
|
|||||||
|
|
||||||
public getRoot(): DirectoryNode {
|
public getRoot(): DirectoryNode {
|
||||||
const dev = this._VDiskManager.getDisk(0);
|
const dev = this._VDiskManager.getDisk(0);
|
||||||
if (!dev) throw new Error("Kernel Panic - Filesystem corrupted, system device not found!");
|
if (!dev) throw new Error("Filesystem corrupted, system device not found!");
|
||||||
|
|
||||||
return dev.getNode(0) as DirectoryNode;
|
return dev.getNode(0) as DirectoryNode;
|
||||||
}
|
}
|
||||||
@@ -120,7 +123,7 @@ export class FileSystem {
|
|||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`,
|
message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: fptr.inode,
|
pointer: ptr,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +139,7 @@ export class FileSystem {
|
|||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`,
|
message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: fptr.inode,
|
pointer: ptr,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,7 +155,7 @@ export class FileSystem {
|
|||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`,
|
message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: fptr.inode,
|
pointer: ptr,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,9 +191,9 @@ export class FileSystem {
|
|||||||
if (sourceNode === undefined) {
|
if (sourceNode === undefined) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `Source node under inode ${sptr.inode} not found`,
|
message: `Source node under ${source} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: sptr.inode,
|
pointer: source,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,9 +208,9 @@ export class FileSystem {
|
|||||||
if (destinationNode === undefined) {
|
if (destinationNode === undefined) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `Destination node under inode ${dptr.inode} not found`,
|
message: `Destination node under ${destination} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: dptr.inode,
|
pointer: destination,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,7 +232,7 @@ export class FileSystem {
|
|||||||
|
|
||||||
const nodeName = name ?? s_entry[0].name;
|
const nodeName = name ?? s_entry[0].name;
|
||||||
|
|
||||||
if (destinationNode.children.get(nodeName) !== undefined) {
|
if (destinationNode.children.has(nodeName)) {
|
||||||
throw {
|
throw {
|
||||||
type: "AlreadyExists",
|
type: "AlreadyExists",
|
||||||
message: `A node inside the destination inode ${destinationNode.inode} under the name ${nodeName} already exists`,
|
message: `A node inside the destination inode ${destinationNode.inode} under the name ${nodeName} already exists`,
|
||||||
@@ -239,6 +242,16 @@ export class FileSystem {
|
|||||||
} satisfies AlreadyExistsError;
|
} satisfies AlreadyExistsError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (nodeName.length > this._maxNameLength) {
|
||||||
|
throw {
|
||||||
|
type: "InvalidNodeName",
|
||||||
|
message: `Invalid name for the ${source} source node`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
pointer: source,
|
||||||
|
attemptedName: nodeName,
|
||||||
|
} satisfies InvalidNodeNameError;
|
||||||
|
}
|
||||||
|
|
||||||
if (upTraverseTree(this, destination).includes(source)) {
|
if (upTraverseTree(this, destination).includes(source)) {
|
||||||
throw {
|
throw {
|
||||||
type: "DirectoryLoop",
|
type: "DirectoryLoop",
|
||||||
@@ -256,165 +269,81 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public modifyNode(target: NodePointer, changes: Partial<Node>) {
|
|
||||||
const node = this.resolveNode(target);
|
|
||||||
if (node === undefined) {
|
|
||||||
throw {
|
|
||||||
type: "NodeNotFound",
|
|
||||||
message: `Node under inode ${inode} not found`,
|
|
||||||
timestamp: Date.now(),
|
|
||||||
inode: inode,
|
|
||||||
} satisfies NodeNotFoundError;
|
|
||||||
}
|
|
||||||
|
|
||||||
const tptr = fromPtr(target);
|
|
||||||
let newNode: TreeNode;
|
|
||||||
|
|
||||||
if (node.type === TreeNodeType.File) {
|
|
||||||
const fileNode: FileNode = {
|
|
||||||
type: node.type,
|
|
||||||
inode: node.inode,
|
|
||||||
name: node.name,
|
|
||||||
parent: node.parent,
|
|
||||||
mode: changes.mode ?? node.mode,
|
|
||||||
owner: changes.owner ?? node.owner,
|
|
||||||
group: changes.group ?? node.group,
|
|
||||||
mtime: changes.content ? Date.now() : node.mtime,
|
|
||||||
atime: node.atime,
|
|
||||||
ctime: Date.now(),
|
|
||||||
content: changes.content ?? node.content,
|
|
||||||
};
|
|
||||||
|
|
||||||
newNode = fileNode;
|
|
||||||
} else {
|
|
||||||
const directoryNode: DirectoryNode = {
|
|
||||||
type: node.type,
|
|
||||||
inode: node.inode,
|
|
||||||
name: node.name,
|
|
||||||
parent: node.parent,
|
|
||||||
mode: changes.mode ?? node.mode,
|
|
||||||
owner: changes.owner ?? node.owner,
|
|
||||||
group: changes.group ?? node.group,
|
|
||||||
mtime: changes.content ? Date.now() : node.mtime,
|
|
||||||
atime: node.atime,
|
|
||||||
ctime: Date.now(),
|
|
||||||
children: node.children,
|
|
||||||
};
|
|
||||||
|
|
||||||
newNode = directoryNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
const map = new Map(this.Overlay);
|
|
||||||
map.set(node.inode, Object.freeze(newNode));
|
|
||||||
|
|
||||||
const newOverlay: ReadonlyMap<number, TreeNode | null> = map;
|
|
||||||
|
|
||||||
return FileSystem.init(this._BaseFS, newOverlay, this._INodeIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
public createNode(
|
public createNode(
|
||||||
parent: number,
|
parent: NodePointer,
|
||||||
name: string,
|
name: string,
|
||||||
type: TreeNodeType,
|
data:
|
||||||
mode: number,
|
| {
|
||||||
owner: number,
|
type: NodeType.File;
|
||||||
group: number,
|
mode: number;
|
||||||
content?: ImmutableBuffer,
|
owner: number;
|
||||||
): FileSystem {
|
group: number;
|
||||||
let node: TreeNode;
|
content: Blob | Readable;
|
||||||
|
size: number;
|
||||||
|
}
|
||||||
|
| { type: NodeType.Directory; mode: number; owner: number; group: number }
|
||||||
|
| { type: NodeType.Symlink; mode: number; owner: number; group: number; target: string },
|
||||||
|
) {
|
||||||
|
const pptr = fromPtr(parent);
|
||||||
|
const pdev = this._VDiskManager.getDisk(pptr.device_id);
|
||||||
const parentNode = this.resolveNode(parent);
|
const parentNode = this.resolveNode(parent);
|
||||||
const time: number = Date.now();
|
|
||||||
const newInode: number = this._INodeIndex + 1;
|
|
||||||
|
|
||||||
if (parentNode === undefined) {
|
if (!pdev) {
|
||||||
|
throw {
|
||||||
|
type: "DeviceNotFound",
|
||||||
|
message: `Device under id ${pptr.device_id} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
device_id: pptr.device_id,
|
||||||
|
} satisfies DeviceNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!parentNode) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `Parent inode ${parent} not found`,
|
message: `Destination node under ${parent} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: parent,
|
pointer: parent,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parentNode.type === TreeNodeType.File) {
|
if (parentNode.type !== NodeType.Directory) {
|
||||||
throw {
|
throw {
|
||||||
type: "NotADirectory",
|
type: "NotADirectory",
|
||||||
message: `Parent inode ${parent} is not a directory`,
|
message: `Destination node under inode ${pptr.inode} is not a directory`,
|
||||||
|
inode: pptr.inode,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: parent,
|
|
||||||
nodeType: parentNode.type,
|
nodeType: parentNode.type,
|
||||||
} satisfies NotADirectoryError;
|
} satisfies NotADirectoryError;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parentNode.type === TreeNodeType.Directory && parentNode.children[name] !== undefined) {
|
if (parentNode.children.has(name)) {
|
||||||
throw {
|
throw {
|
||||||
type: "AlreadyExists",
|
type: "AlreadyExists",
|
||||||
message: `Child under the name of ${name} inside the parent inode ${parent} already exists`,
|
message: `A node inside the destination inode ${parentNode.inode} under the name ${name} already exists`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
parentInode: parent,
|
parentInode: parentNode.inode,
|
||||||
name,
|
name: name,
|
||||||
} satisfies AlreadyExistsError;
|
} satisfies AlreadyExistsError;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === TreeNodeType.File) {
|
if (name.length > this._maxNameLength) {
|
||||||
const fileNode: FileNode = {
|
throw {
|
||||||
type,
|
type: "InvalidNodeName",
|
||||||
inode: newInode,
|
message: `Invalid name for the new node`,
|
||||||
name,
|
timestamp: Date.now(),
|
||||||
parent,
|
pointer: parent,
|
||||||
mode,
|
attemptedName: name,
|
||||||
owner,
|
} satisfies InvalidNodeNameError;
|
||||||
group,
|
|
||||||
mtime: time,
|
|
||||||
atime: time,
|
|
||||||
ctime: time,
|
|
||||||
content: content ?? createImmutableBuffer(new Uint8Array()),
|
|
||||||
};
|
|
||||||
|
|
||||||
node = fileNode;
|
|
||||||
} else {
|
|
||||||
const directoryNode: DirectoryNode = {
|
|
||||||
type,
|
|
||||||
inode: newInode,
|
|
||||||
name,
|
|
||||||
parent,
|
|
||||||
mode,
|
|
||||||
owner,
|
|
||||||
group,
|
|
||||||
mtime: time,
|
|
||||||
atime: time,
|
|
||||||
ctime: time,
|
|
||||||
children: {},
|
|
||||||
};
|
|
||||||
|
|
||||||
node = directoryNode;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const newParentNode: DirectoryNode = {
|
pdev.createNodeEntry(pptr.inode, name, data);
|
||||||
type: parentNode.type,
|
|
||||||
inode: parent,
|
|
||||||
name: parentNode.name,
|
|
||||||
parent: parentNode.parent,
|
|
||||||
mode: parentNode.mode,
|
|
||||||
owner: parentNode.owner,
|
|
||||||
group: parentNode.group,
|
|
||||||
mtime: time,
|
|
||||||
atime: parentNode.atime,
|
|
||||||
ctime: time,
|
|
||||||
children: { ...parentNode.children, [name]: newInode },
|
|
||||||
};
|
|
||||||
|
|
||||||
const map = new Map(this.Overlay);
|
|
||||||
map.set(newInode, Object.freeze(node));
|
|
||||||
map.set(parent, Object.freeze(newParentNode));
|
|
||||||
|
|
||||||
const newOverlay: ReadonlyMap<number, TreeNode | null> = map;
|
|
||||||
|
|
||||||
return FileSystem.init(this._BaseFS, newOverlay, newInode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public deleteNode(inode: number): FileSystem {
|
public deleteNode(target: NodePointer) {
|
||||||
if (inode === 0) {
|
const tptr = fromPtr(target);
|
||||||
|
const tdev = this._VDiskManager.getDisk(tptr.device_id);
|
||||||
|
|
||||||
|
if (tptr.device_id === 0 && tptr.inode === 0) {
|
||||||
throw {
|
throw {
|
||||||
type: "IllegalMutation",
|
type: "IllegalMutation",
|
||||||
message: `Attempt at illegal mutation of node under inode 0 (root)`,
|
message: `Attempt at illegal mutation of node under inode 0 (root)`,
|
||||||
@@ -422,64 +351,138 @@ export class FileSystem {
|
|||||||
} satisfies IllegalMutationError;
|
} satisfies IllegalMutationError;
|
||||||
}
|
}
|
||||||
|
|
||||||
const node = this.resolveNode(inode);
|
if (!tdev) {
|
||||||
if (node === undefined) {
|
throw {
|
||||||
|
type: "DeviceNotFound",
|
||||||
|
message: `Device under id ${tptr.device_id} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
device_id: tptr.device_id,
|
||||||
|
} satisfies DeviceNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
|
||||||
|
tdev.unlinkNodeEntry(tptr.inode, t_entry[0].name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public writeContent(target: NodePointer, content: Blob) {
|
||||||
|
const ptr = fromPtr(target);
|
||||||
|
const dev = this._VDiskManager.getDisk(ptr.device_id);
|
||||||
|
const node = this.resolveNode(target);
|
||||||
|
|
||||||
|
if (!dev) {
|
||||||
|
throw {
|
||||||
|
type: "DeviceNotFound",
|
||||||
|
message: `Device under id ${ptr.device_id} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
device_id: ptr.device_id,
|
||||||
|
} satisfies DeviceNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!node) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `Node under inode ${inode} not found`,
|
message: `Destination node under ${target} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: inode,
|
pointer: target,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
const time: number = Date.now();
|
if (node.type === NodeType.Directory) {
|
||||||
const descendants: number[] = dnTraverseTree(this, inode);
|
throw {
|
||||||
|
type: "NotAFile",
|
||||||
const map: Map<number, TreeNode | null> = new Map(this._OverlayFS);
|
message: `Node is not a file`,
|
||||||
for (const entry of descendants) {
|
inode: ptr.inode,
|
||||||
map.set(entry, null);
|
timestamp: Date.now(),
|
||||||
|
nodeType: node.type,
|
||||||
|
} satisfies NotAFileError;
|
||||||
}
|
}
|
||||||
|
|
||||||
const parentNode = this.resolveNode(node.parent) as DirectoryNode;
|
dev.writeContent((node as FileNode).content, content);
|
||||||
if (parentNode === undefined) {
|
}
|
||||||
|
|
||||||
|
public streamContent(target: NodePointer): Readable {
|
||||||
|
const ptr = fromPtr(target);
|
||||||
|
const dev = this._VDiskManager.getDisk(ptr.device_id);
|
||||||
|
const node = this.resolveNode(target);
|
||||||
|
|
||||||
|
if (!dev) {
|
||||||
|
throw {
|
||||||
|
type: "DeviceNotFound",
|
||||||
|
message: `Device under id ${ptr.device_id} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
device_id: ptr.device_id,
|
||||||
|
} satisfies DeviceNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!node) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `Parent inode ${node.parent} not found (should be impossible unless the filesystem corrupted!)`,
|
message: `Destination node under ${target} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: node.parent,
|
pointer: target,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { [node.name]: __, ...newChildren } = parentNode.children;
|
if (node.type === NodeType.Directory) {
|
||||||
|
throw {
|
||||||
const newParentNode: DirectoryNode = {
|
type: "NotAFile",
|
||||||
type: parentNode.type,
|
message: `Node is not a file`,
|
||||||
inode: parentNode.inode,
|
inode: ptr.inode,
|
||||||
name: parentNode.name,
|
timestamp: Date.now(),
|
||||||
parent: parentNode.parent,
|
nodeType: node.type,
|
||||||
mode: parentNode.mode,
|
} satisfies NotAFileError;
|
||||||
owner: parentNode.owner,
|
|
||||||
group: parentNode.group,
|
|
||||||
mtime: time,
|
|
||||||
atime: parentNode.atime,
|
|
||||||
ctime: time,
|
|
||||||
children: newChildren,
|
|
||||||
};
|
|
||||||
|
|
||||||
map.set(node.parent, Object.freeze(newParentNode));
|
|
||||||
|
|
||||||
const newOverlay: ReadonlyMap<number, TreeNode | null> = map;
|
|
||||||
return FileSystem.init(this._BaseFS, newOverlay, this._INodeIndex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public canRead(inode: number, user: UserInfo): boolean {
|
return dev.streamContent((node as FileNode).content);
|
||||||
const node = this.resolveNode(inode);
|
}
|
||||||
if (node === undefined) {
|
|
||||||
|
public 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);
|
||||||
|
|
||||||
|
if (!dev) {
|
||||||
|
throw {
|
||||||
|
type: "DeviceNotFound",
|
||||||
|
message: `Device under id ${ptr.device_id} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
device_id: ptr.device_id,
|
||||||
|
} satisfies DeviceNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!node) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `Node under inode ${inode} not found`,
|
message: `Destination node under ${target} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: inode,
|
pointer: target,
|
||||||
|
} satisfies NodeNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.type === NodeType.Directory) {
|
||||||
|
throw {
|
||||||
|
type: "NotAFile",
|
||||||
|
message: `Node is not a file`,
|
||||||
|
inode: ptr.inode,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
nodeType: node.type,
|
||||||
|
} satisfies NotAFileError;
|
||||||
|
}
|
||||||
|
|
||||||
|
dev.writeNodeEntry(node.inode, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public canRead(nodep: NodePointer, user: UserInfo): boolean {
|
||||||
|
const node = this.resolveNode(nodep);
|
||||||
|
if (!node) {
|
||||||
|
throw {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
message: `Node under ${nodep} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
pointer: nodep,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -494,14 +497,14 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public canWrite(inode: number, user: UserInfo): boolean {
|
public canWrite(nodep: NodePointer, user: UserInfo): boolean {
|
||||||
const node = this.resolveNode(inode);
|
const node = this.resolveNode(nodep);
|
||||||
if (node === undefined) {
|
if (!node) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `Node under inode ${inode} not found`,
|
message: `Node under ${nodep} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: inode,
|
pointer: nodep,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -516,14 +519,14 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public canExecute(inode: number, user: UserInfo): boolean {
|
public canExecute(nodep: NodePointer, user: UserInfo): boolean {
|
||||||
const node = this.resolveNode(inode);
|
const node = this.resolveNode(nodep);
|
||||||
if (node === undefined) {
|
if (!node) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `Node under inode ${inode} not found`,
|
message: `Node under ${nodep} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: inode,
|
pointer: nodep,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -537,11 +540,4 @@ export class FileSystem {
|
|||||||
return (node.mode & 0o001) !== 0;
|
return (node.mode & 0o001) !== 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* public get Overlay(): ReadonlyMap<number, TreeNode | null> {
|
|
||||||
return this._OverlayFS;
|
|
||||||
}
|
|
||||||
public get INodeIndex(): number {
|
|
||||||
return this._INodeIndex;
|
|
||||||
} */
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
import type { Blob } from "node:buffer";
|
||||||
|
import { Readable } from "node:stream";
|
||||||
|
import { blob } from "node:stream/consumers";
|
||||||
import {
|
import {
|
||||||
NodeType,
|
NodeType,
|
||||||
toPtr,
|
toPtr,
|
||||||
@@ -58,7 +61,7 @@ export interface DataConfig {
|
|||||||
|
|
||||||
export interface IDataStore {
|
export interface IDataStore {
|
||||||
read(uuid: string): Promise<Uint8Array>;
|
read(uuid: string): Promise<Uint8Array>;
|
||||||
stream(uuid: string): ReadableStream;
|
stream(uuid: string): Readable;
|
||||||
put(uuid: string, blob: Blob): Promise<void>;
|
put(uuid: string, blob: Blob): Promise<void>;
|
||||||
delete(uuid: string): Promise<void>;
|
delete(uuid: string): Promise<void>;
|
||||||
has(uuid: string): Promise<boolean>;
|
has(uuid: string): Promise<boolean>;
|
||||||
@@ -204,15 +207,22 @@ export class VDisk {
|
|||||||
return this._base.get(inode);
|
return this._base.get(inode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public createNodeEntry(
|
public async createNodeEntry(
|
||||||
pinode: number,
|
pinode: number,
|
||||||
name: string,
|
name: string,
|
||||||
data:
|
data:
|
||||||
| { type: NodeType.File; mode: number; owner: number; group: number; content: string; size: number }
|
| {
|
||||||
|
type: NodeType.File;
|
||||||
|
mode: number;
|
||||||
|
owner: number;
|
||||||
|
group: number;
|
||||||
|
content: Blob | Readable;
|
||||||
|
size: number;
|
||||||
|
}
|
||||||
| { type: NodeType.Directory; mode: number; owner: number; group: number }
|
| { type: NodeType.Directory; mode: number; owner: number; group: number }
|
||||||
| { type: NodeType.Symlink; mode: number; owner: number; group: number; target: string },
|
| { type: NodeType.Symlink; mode: number; owner: number; group: number; target: string },
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
return this._batch(() => {
|
return this._batch(async () => {
|
||||||
this._isWritable();
|
this._isWritable();
|
||||||
const index = ++this._iIndex;
|
const index = ++this._iIndex;
|
||||||
const time = Date.now();
|
const time = Date.now();
|
||||||
@@ -231,11 +241,13 @@ export class VDisk {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const pnode = this.getNode(pinode) as DirectoryNode;
|
const pnode = this.getNode(pinode) as DirectoryNode;
|
||||||
|
const uuid = crypto.randomUUID();
|
||||||
|
|
||||||
let node: Node;
|
let node: Node;
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case NodeType.File:
|
case NodeType.File:
|
||||||
node = { ...base, type: NodeType.File, content: data.content, size: data.size };
|
node = { ...base, type: NodeType.File, content: uuid, size: data.size };
|
||||||
|
this.writeContent(uuid, data.content instanceof Readable ? await blob(data.content) : data.content);
|
||||||
break;
|
break;
|
||||||
case NodeType.Directory:
|
case NodeType.Directory:
|
||||||
node = { ...base, type: NodeType.Directory, children: new Map() };
|
node = { ...base, type: NodeType.Directory, children: new Map() };
|
||||||
@@ -385,7 +397,7 @@ export class VDisk {
|
|||||||
return this._dataStore.read(uuid);
|
return this._dataStore.read(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public streamContent(uuid: string): ReadableStream {
|
public streamContent(uuid: string): Readable {
|
||||||
return this._dataStore.stream(uuid);
|
return this._dataStore.stream(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import type { Node } from "@core/filesystem/node";
|
import type { Node } from "@core/filesystem/node";
|
||||||
import type { IDataStore, IMetaStore } from "@core/filesystem/vdisk";
|
import type { IDataStore, IMetaStore } from "@core/filesystem/vdisk";
|
||||||
|
import type { Blob } from "node:buffer";
|
||||||
|
import { Readable } from "node:stream";
|
||||||
|
|
||||||
export class MemMetaStore implements IMetaStore {
|
export class MemMetaStore implements IMetaStore {
|
||||||
private _data: Map<number, Node | null> = new Map();
|
private _data: Map<number, Node | null> = new Map();
|
||||||
@@ -33,11 +35,11 @@ export class MemDataStore implements IDataStore {
|
|||||||
return new Uint8Array(buff);
|
return new Uint8Array(buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
public stream(uuid: string): ReadableStream {
|
public stream(uuid: string): Readable {
|
||||||
const blob = this._data.get(uuid);
|
const blob = this._data.get(uuid);
|
||||||
if (!blob) throw new Error("Content missing - " + uuid);
|
if (!blob) throw new Error("Content missing - " + uuid);
|
||||||
|
|
||||||
return blob.stream();
|
return Readable.from(blob.stream());
|
||||||
}
|
}
|
||||||
|
|
||||||
public put(uuid: string, blob: Blob): Promise<void> {
|
public put(uuid: string, blob: Blob): Promise<void> {
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import { FileSystem } from "@core/filesystem/filesystem";
|
import { FileSystem } from "@core/filesystem/filesystem";
|
||||||
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/node";
|
import { NodeType, type FileNode } from "@core/filesystem/node";
|
||||||
|
import type { IDataStore, IMetaStore, VDiskConfig } from "@core/filesystem/vdisk";
|
||||||
import type { UserInfo } from "@core/session_manager";
|
import type { UserInfo } from "@core/session_manager";
|
||||||
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||||
|
import { MemDataStore, MemMetaStore } from "@core/util/temp_mem_stores";
|
||||||
|
import { deserializeFileSystem } from "@shell/serializer";
|
||||||
import {
|
import {
|
||||||
expectAlreadyExistsError,
|
expectAlreadyExistsError,
|
||||||
expectDirectoryLoopError,
|
expectDirectoryLoopError,
|
||||||
@@ -9,83 +12,55 @@ import {
|
|||||||
expectNodeNotFoundError,
|
expectNodeNotFoundError,
|
||||||
expectNotADirectoryError,
|
expectNotADirectoryError,
|
||||||
} from "@tests/helpers/error_matchers";
|
} from "@tests/helpers/error_matchers";
|
||||||
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
|
import { buildTestFS } from "@tests/helpers/filesystems_fixture";
|
||||||
|
import { Blob } from "node:buffer";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
const testTree: FixtureNode = {
|
const fs = buildTestFS();
|
||||||
inode: 0,
|
|
||||||
name: "root",
|
|
||||||
type: "Dir",
|
|
||||||
children: [
|
|
||||||
{
|
|
||||||
inode: 1,
|
|
||||||
name: "hello.txt",
|
|
||||||
type: "File",
|
|
||||||
content: "Goodbye World!",
|
|
||||||
children: [],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
inode: 2,
|
|
||||||
name: "sub",
|
|
||||||
type: "Dir",
|
|
||||||
children: [
|
|
||||||
{
|
|
||||||
inode: 3,
|
|
||||||
name: "world.txt",
|
|
||||||
type: "File",
|
|
||||||
content: "Goodbye!",
|
|
||||||
children: [],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
inode: 4,
|
|
||||||
name: "bye",
|
|
||||||
type: "Dir",
|
|
||||||
children: [],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
const fs = buildTestFS(testTree);
|
|
||||||
|
|
||||||
describe("FileSystem.init", () => {
|
describe("FileSystem.init", () => {
|
||||||
const map: Map<number, TreeNode> = new Map();
|
const rawJson = `{"dev": [{"id": 0, "tree": {"type": "directory", "inode": 0, "mode": 443, "owner": 0, "group": 0, "parent": 0, "atime": 0, "mtime": 0, "ctime": 0, "children": []}}]}`;
|
||||||
const time = Date.now();
|
const arr = deserializeFileSystem(rawJson);
|
||||||
|
const devices: { dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }[] = [];
|
||||||
|
|
||||||
const root: DirectoryNode = {
|
const data = new MemDataStore();
|
||||||
type: TreeNodeType.Directory,
|
const meta = new MemMetaStore();
|
||||||
inode: 0,
|
const config: VDiskConfig = {
|
||||||
name: "root",
|
id: 0,
|
||||||
parent: 0,
|
label: `disk-0`,
|
||||||
mode: 0o555,
|
index: 0,
|
||||||
owner: 0,
|
readonly: false,
|
||||||
group: 0,
|
size: 1024 * 1024 * 1024 * 8, //8 GiB
|
||||||
mtime: time,
|
base: arr[0]!.map,
|
||||||
atime: time,
|
dataC: {
|
||||||
ctime: time,
|
chunkSize: 1024 * 8, //8 KiB
|
||||||
children: {},
|
cacheSize: 1024 * 1024 * 64, //64 MiB
|
||||||
|
cacheTTL: 1000 * 60, // 1 min
|
||||||
|
},
|
||||||
};
|
};
|
||||||
map.set(0, root);
|
devices.push({ dstore: data, mstore: meta, config });
|
||||||
|
|
||||||
const base: ReadonlyMap<number, TreeNode> = map;
|
|
||||||
const overlay: ReadonlyMap<number, TreeNode | null> = new Map();
|
|
||||||
|
|
||||||
it("initializes correctly", () => {
|
it("initializes correctly", () => {
|
||||||
const fs = FileSystem.init(base, overlay, 0);
|
const fs = FileSystem.init(devices);
|
||||||
expect(fs).toBeInstanceOf(FileSystem);
|
expect(fs).toBeInstanceOf(FileSystem);
|
||||||
expect(fs.INodeIndex).toBe(0);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("createNode", () => {
|
describe("createNode", () => {
|
||||||
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
const content = new Blob([new TextEncoder().encode("Lorem ipsum")]);
|
||||||
|
|
||||||
it("creates a file and updates parent", () => {
|
it("creates a file and updates parent", () => {
|
||||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
fs.createNode("0:0", "readme", {
|
||||||
|
type: NodeType.File,
|
||||||
|
mode: 0o644,
|
||||||
|
owner: 0,
|
||||||
|
group: 0,
|
||||||
|
content: content,
|
||||||
|
size: content.size,
|
||||||
|
});
|
||||||
|
|
||||||
expect(newFS).toBeInstanceOf(FileSystem);
|
expect(fs).toBeInstanceOf(FileSystem);
|
||||||
const newNode = newFS.getNode(5) as FileNode;
|
const newNode = fs.getNode("0") as FileNode;
|
||||||
|
|
||||||
expect(newNode.type).toBe(TreeNodeType.File);
|
expect(newNode.type).toBe(TreeNodeType.File);
|
||||||
expect(newNode.name).toBe("readme");
|
expect(newNode.name).toBe("readme");
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user