- Fixed ls (mostly)
- Fixed sorting - Many minor changes (need to fix the json serializer to include size in its results *the C++ one*)
This commit is contained in:
@@ -15,15 +15,7 @@ import type {
|
||||
NotADirectoryError,
|
||||
NotAFileError,
|
||||
} from "./errors";
|
||||
import {
|
||||
fromPtr,
|
||||
NodeType,
|
||||
type DirectoryNode,
|
||||
type FileNode,
|
||||
type Node,
|
||||
type NodeEntry,
|
||||
type NodePointer,
|
||||
} from "./node";
|
||||
import { NodePointer, NodeType, type DirectoryNode, type FileNode, type Node, type NodeEntry } from "./node";
|
||||
import type { IDataStore, IMetaStore, VDiskConfig } from "./vdisk";
|
||||
|
||||
export interface FileSystemConfig {
|
||||
@@ -50,11 +42,10 @@ export class FileSystem {
|
||||
}
|
||||
|
||||
private resolveNode(ptr: NodePointer): Node | undefined {
|
||||
const fptr = fromPtr(ptr);
|
||||
const dev = this._VDiskManager.getDisk(fptr.device_id);
|
||||
const dev = this._VDiskManager.getDisk(ptr.device);
|
||||
if (!dev) return undefined;
|
||||
|
||||
return dev.getNode(fptr.inode);
|
||||
return dev.getNode(ptr.inode);
|
||||
}
|
||||
|
||||
// Make async
|
||||
@@ -108,15 +99,14 @@ export class FileSystem {
|
||||
}
|
||||
|
||||
public getNodeEntries(ptr: NodePointer): readonly NodeEntry[] | undefined {
|
||||
const fptr = fromPtr(ptr);
|
||||
const dev = this._VDiskManager.getDisk(fptr.device_id);
|
||||
const dev = this._VDiskManager.getDisk(ptr.device);
|
||||
|
||||
if (!dev) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${fptr.device_id} not found`,
|
||||
message: `Device under id ${ptr.device} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: fptr.device_id,
|
||||
device_id: ptr.device,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
@@ -132,31 +122,28 @@ export class FileSystem {
|
||||
}
|
||||
|
||||
public mount(mount: NodePointer, target: NodePointer) {
|
||||
const mptr = fromPtr(mount);
|
||||
const tptr = fromPtr(target);
|
||||
|
||||
if (!this._VDiskManager.hasDisk(mptr.device_id)) {
|
||||
if (!this._VDiskManager.hasDisk(mount.device)) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${mptr.device_id} not found`,
|
||||
message: `Device under id ${mount.device} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: mptr.device_id,
|
||||
device_id: mount.device,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
if (!this._VDiskManager.hasDisk(tptr.device_id)) {
|
||||
if (!this._VDiskManager.hasDisk(target.device)) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${tptr.device_id} not found`,
|
||||
message: `Device under id ${target.device} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: tptr.device_id,
|
||||
device_id: target.device,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
if (!this.getNode(mount)) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Node under inode ${mptr.inode} not found under device ${mptr.device_id}`,
|
||||
message: `Node under inode ${mount.inode} not found under device ${mount.device}`,
|
||||
timestamp: Date.now(),
|
||||
pointer: mount,
|
||||
} satisfies NodeNotFoundError;
|
||||
@@ -165,7 +152,7 @@ export class FileSystem {
|
||||
if (!this.getNode(target)) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Node under inode ${tptr.inode} not found under device ${tptr.device_id}`,
|
||||
message: `Node under inode ${target.inode} not found under device ${target.device}`,
|
||||
timestamp: Date.now(),
|
||||
pointer: target,
|
||||
} satisfies NodeNotFoundError;
|
||||
@@ -192,22 +179,21 @@ export class FileSystem {
|
||||
}
|
||||
|
||||
public isFile(ptr: NodePointer): boolean {
|
||||
const fptr = fromPtr(ptr);
|
||||
const node = this.resolveNode(ptr);
|
||||
|
||||
if (!this._VDiskManager.hasDisk(fptr.device_id)) {
|
||||
if (!this._VDiskManager.hasDisk(ptr.device)) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${fptr.device_id} not found`,
|
||||
message: `Device under id ${ptr.device} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: fptr.device_id,
|
||||
device_id: ptr.device,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
if (node === undefined) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`,
|
||||
message: `Node under inode ${ptr.inode} not found under device ${ptr.device}`,
|
||||
timestamp: Date.now(),
|
||||
pointer: ptr,
|
||||
} satisfies NodeNotFoundError;
|
||||
@@ -217,22 +203,21 @@ export class FileSystem {
|
||||
}
|
||||
|
||||
public isDirectory(ptr: NodePointer): boolean {
|
||||
const fptr = fromPtr(ptr);
|
||||
const node = this.resolveNode(ptr);
|
||||
|
||||
if (!this._VDiskManager.hasDisk(fptr.device_id)) {
|
||||
if (!this._VDiskManager.hasDisk(ptr.device)) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${fptr.device_id} not found`,
|
||||
message: `Device under id ${ptr.device} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: fptr.device_id,
|
||||
device_id: ptr.device,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
if (node === undefined) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`,
|
||||
message: `Node under inode ${ptr.inode} not found under device ${ptr.device}`,
|
||||
timestamp: Date.now(),
|
||||
pointer: ptr,
|
||||
} satisfies NodeNotFoundError;
|
||||
@@ -242,22 +227,21 @@ export class FileSystem {
|
||||
}
|
||||
|
||||
public isSymlink(ptr: NodePointer): boolean {
|
||||
const fptr = fromPtr(ptr);
|
||||
const node = this.resolveNode(ptr);
|
||||
|
||||
if (!this._VDiskManager.hasDisk(fptr.device_id)) {
|
||||
if (!this._VDiskManager.hasDisk(ptr.device)) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${fptr.device_id} not found`,
|
||||
message: `Device under id ${ptr.device} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: fptr.device_id,
|
||||
device_id: ptr.device,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
if (node === undefined) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`,
|
||||
message: `Node under inode ${ptr.inode} not found under device ${ptr.device}`,
|
||||
timestamp: Date.now(),
|
||||
pointer: ptr,
|
||||
} satisfies NodeNotFoundError;
|
||||
@@ -267,28 +251,26 @@ export class FileSystem {
|
||||
}
|
||||
|
||||
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);
|
||||
const ddev = this._VDiskManager.getDisk(dptr.device_id);
|
||||
const sdev = this._VDiskManager.getDisk(source.device);
|
||||
const ddev = this._VDiskManager.getDisk(destination.device);
|
||||
const sourceNode = this.resolveNode(source);
|
||||
const destinationNode = this.resolveNode(destination);
|
||||
|
||||
if (!sdev) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${sptr.device_id} not found`,
|
||||
message: `Device under id ${source.device} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: sptr.device_id,
|
||||
device_id: source.device,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
if (!ddev) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${dptr.device_id} not found`,
|
||||
message: `Device under id ${destination.device} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: dptr.device_id,
|
||||
device_id: destination.device,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
@@ -321,8 +303,8 @@ export class FileSystem {
|
||||
if (destinationNode.type !== NodeType.Directory) {
|
||||
throw {
|
||||
type: "NotADirectory",
|
||||
message: `Destination node under inode ${dptr.inode} is not a directory`,
|
||||
inode: dptr.inode,
|
||||
message: `Destination node under inode ${destination.inode} is not a directory`,
|
||||
inode: destination.inode,
|
||||
timestamp: Date.now(),
|
||||
nodeType: destinationNode.type,
|
||||
} satisfies NotADirectoryError;
|
||||
@@ -372,8 +354,8 @@ export class FileSystem {
|
||||
} satisfies DirectoryLoopError;
|
||||
}
|
||||
|
||||
if (sptr.device_id === dptr.device_id) {
|
||||
await ddev.moveNodeEntry(pinode, dptr.inode, s_entry[0].name, name);
|
||||
if (source.device === destination.device) {
|
||||
await ddev.moveNodeEntry(pinode, destination.inode, s_entry[0].name, name);
|
||||
} else {
|
||||
console.error("cross device node movement not yet implemented");
|
||||
}
|
||||
@@ -394,16 +376,15 @@ export class FileSystem {
|
||||
| { 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 pdev = this._VDiskManager.getDisk(parent.device);
|
||||
const parentNode = this.resolveNode(parent);
|
||||
|
||||
if (!pdev) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${pptr.device_id} not found`,
|
||||
message: `Device under id ${parent.device} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: pptr.device_id,
|
||||
device_id: parent.device,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
@@ -419,8 +400,8 @@ export class FileSystem {
|
||||
if (parentNode.type !== NodeType.Directory) {
|
||||
throw {
|
||||
type: "NotADirectory",
|
||||
message: `Destination node under inode ${pptr.inode} is not a directory`,
|
||||
inode: pptr.inode,
|
||||
message: `Destination node under inode ${parent.inode} is not a directory`,
|
||||
inode: parent.inode,
|
||||
timestamp: Date.now(),
|
||||
nodeType: parentNode.type,
|
||||
} satisfies NotADirectoryError;
|
||||
@@ -446,14 +427,13 @@ export class FileSystem {
|
||||
} satisfies InvalidNodeNameError;
|
||||
}
|
||||
|
||||
await pdev.createNodeEntry(pptr.inode, name, data);
|
||||
await pdev.createNodeEntry(parent.inode, name, data);
|
||||
}
|
||||
|
||||
public async deleteNode(target: NodePointer) {
|
||||
const tptr = fromPtr(target);
|
||||
const tdev = this._VDiskManager.getDisk(tptr.device_id);
|
||||
const tdev = this._VDiskManager.getDisk(target.device);
|
||||
|
||||
if (tptr.inode === 0) {
|
||||
if (target.inode === 0) {
|
||||
throw {
|
||||
type: "IllegalMutation",
|
||||
message: `Attempt at illegal mutation of node under inode 0 (root)`,
|
||||
@@ -464,9 +444,9 @@ export class FileSystem {
|
||||
if (!tdev) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${tptr.device_id} not found`,
|
||||
message: `Device under id ${target.device} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: tptr.device_id,
|
||||
device_id: target.device,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
@@ -484,16 +464,15 @@ export class FileSystem {
|
||||
}
|
||||
|
||||
public async writeContent(target: NodePointer, content: Blob) {
|
||||
const ptr = fromPtr(target);
|
||||
const dev = this._VDiskManager.getDisk(ptr.device_id);
|
||||
const dev = this._VDiskManager.getDisk(target.device);
|
||||
const node = this.resolveNode(target);
|
||||
|
||||
if (!dev) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${ptr.device_id} not found`,
|
||||
message: `Device under id ${target.device} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: ptr.device_id,
|
||||
device_id: target.device,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
@@ -510,7 +489,7 @@ export class FileSystem {
|
||||
throw {
|
||||
type: "NotAFile",
|
||||
message: `Node is not a file`,
|
||||
inode: ptr.inode,
|
||||
inode: target.inode,
|
||||
timestamp: Date.now(),
|
||||
nodeType: node.type,
|
||||
} satisfies NotAFileError;
|
||||
@@ -520,16 +499,15 @@ export class FileSystem {
|
||||
}
|
||||
|
||||
public streamContent(target: NodePointer): Readable {
|
||||
const ptr = fromPtr(target);
|
||||
const dev = this._VDiskManager.getDisk(ptr.device_id);
|
||||
const dev = this._VDiskManager.getDisk(target.device);
|
||||
const node = this.resolveNode(target);
|
||||
|
||||
if (!dev) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${ptr.device_id} not found`,
|
||||
message: `Device under id ${target.device} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: ptr.device_id,
|
||||
device_id: target.device,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
@@ -546,7 +524,7 @@ export class FileSystem {
|
||||
throw {
|
||||
type: "NotAFile",
|
||||
message: `Node is not a file`,
|
||||
inode: ptr.inode,
|
||||
inode: target.inode,
|
||||
timestamp: Date.now(),
|
||||
nodeType: node.type,
|
||||
} satisfies NotAFileError;
|
||||
@@ -556,16 +534,15 @@ export class FileSystem {
|
||||
}
|
||||
|
||||
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 dev = this._VDiskManager.getDisk(target.device);
|
||||
const node = this.resolveNode(target);
|
||||
|
||||
if (!dev) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${ptr.device_id} not found`,
|
||||
message: `Device under id ${target.device} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: ptr.device_id,
|
||||
device_id: target.device,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
@@ -582,7 +559,7 @@ export class FileSystem {
|
||||
throw {
|
||||
type: "NotAFile",
|
||||
message: `Node is not a file`,
|
||||
inode: ptr.inode,
|
||||
inode: target.device,
|
||||
timestamp: Date.now(),
|
||||
nodeType: node.type,
|
||||
} satisfies NotAFileError;
|
||||
@@ -591,14 +568,14 @@ export class FileSystem {
|
||||
await dev.writeNodeEntry(node.inode, data);
|
||||
}
|
||||
|
||||
public canRead(nodep: NodePointer, user: UserInfo): boolean {
|
||||
const node = this.resolveNode(nodep);
|
||||
public canRead(ptr: NodePointer, user: UserInfo): boolean {
|
||||
const node = this.resolveNode(ptr);
|
||||
if (!node) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Node under ${nodep} not found`,
|
||||
message: `Node under ${ptr} not found`,
|
||||
timestamp: Date.now(),
|
||||
pointer: nodep,
|
||||
pointer: ptr,
|
||||
} satisfies NodeNotFoundError;
|
||||
}
|
||||
|
||||
@@ -613,14 +590,14 @@ export class FileSystem {
|
||||
}
|
||||
}
|
||||
|
||||
public canWrite(nodep: NodePointer, user: UserInfo): boolean {
|
||||
const node = this.resolveNode(nodep);
|
||||
public canWrite(ptr: NodePointer, user: UserInfo): boolean {
|
||||
const node = this.resolveNode(ptr);
|
||||
if (!node) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Node under ${nodep} not found`,
|
||||
message: `Node under ${ptr} not found`,
|
||||
timestamp: Date.now(),
|
||||
pointer: nodep,
|
||||
pointer: ptr,
|
||||
} satisfies NodeNotFoundError;
|
||||
}
|
||||
|
||||
@@ -635,14 +612,14 @@ export class FileSystem {
|
||||
}
|
||||
}
|
||||
|
||||
public canExecute(nodep: NodePointer, user: UserInfo): boolean {
|
||||
const node = this.resolveNode(nodep);
|
||||
public canExecute(ptr: NodePointer, user: UserInfo): boolean {
|
||||
const node = this.resolveNode(ptr);
|
||||
if (!node) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Node under ${nodep} not found`,
|
||||
message: `Node under ${ptr} not found`,
|
||||
timestamp: Date.now(),
|
||||
pointer: nodep,
|
||||
pointer: ptr,
|
||||
} satisfies NodeNotFoundError;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,7 @@
|
||||
import type { Blob } from "node:buffer";
|
||||
import { Readable } from "node:stream";
|
||||
import { blob } from "node:stream/consumers";
|
||||
import {
|
||||
NodeType,
|
||||
toPtr,
|
||||
type DirectoryNode,
|
||||
type Node,
|
||||
type NodeEntry,
|
||||
type NodeHash,
|
||||
type NodePointer,
|
||||
} from "./node";
|
||||
import { NodePointer, NodeType, type DirectoryNode, type Node, type NodeEntry, type NodeHash } from "./node";
|
||||
|
||||
export function DefaultVDiskConfig(id: number): VDiskConfig {
|
||||
const base = new Map();
|
||||
@@ -149,6 +141,7 @@ export class VDisk {
|
||||
|
||||
const root = this.getNode(0) as DirectoryNode;
|
||||
for (const [name, inode] of root.children) {
|
||||
if (name === "." || name === "..") continue;
|
||||
stack.push([inode, 0, this._id, name]);
|
||||
}
|
||||
|
||||
@@ -157,7 +150,7 @@ export class VDisk {
|
||||
const node = this.getNode(inode);
|
||||
if (node === undefined) continue;
|
||||
|
||||
const key = toPtr({ device_id: this._id, inode });
|
||||
const key = NodePointer.of(this._id, inode);
|
||||
const entry = {
|
||||
pinode,
|
||||
pdevice_id,
|
||||
@@ -170,6 +163,7 @@ export class VDisk {
|
||||
|
||||
if (node.type === NodeType.Directory) {
|
||||
for (const [name, cinode] of node.children) {
|
||||
if (name === "." || name === "..") continue;
|
||||
stack.push([cinode, inode, pdevice_id, name]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user