- 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:
+58
-44
@@ -1,17 +1,22 @@
|
|||||||
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||||
import type { CommandIO } from "@core/command_io";
|
import type { CommandIO } from "@core/command_io";
|
||||||
import { type DirectoryNode, type NodePointer } from "@core/filesystem/node";
|
import { NodePointer, NodeType, type DirectoryNode, type FileNode, type Node } from "@core/filesystem/node";
|
||||||
import { createErrorResult, createSuccessResult } from "@core/result";
|
import { createErrorResult, createSuccessResult } from "@core/result";
|
||||||
import { getCurrentSession } from "@core/session_manager";
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
import type { ShellState } from "@core/shell_state";
|
import type { ShellState } from "@core/shell_state";
|
||||||
import type { ShellError } from "@core/util/errors";
|
import type { ShellError } from "@core/util/errors";
|
||||||
import { resolvePath } from "@core/util/path_resolver";
|
import { resolvePath } from "@core/util/path_resolver";
|
||||||
import { SortTreeNodeBy, sortTreeNodes } from "@core/util/sort";
|
import { SortNodeBy, sortNodes } from "@core/util/sort";
|
||||||
import { concatUint8Arrays } from "@termos/util/concat";
|
import { concatUint8Arrays } from "@termos/util/concat";
|
||||||
import { convertToUnit } from "@termos/util/format_unit";
|
import { convertToUnit } from "@termos/util/format_unit";
|
||||||
|
|
||||||
export const COMMAND_NAME = "ls";
|
export const COMMAND_NAME = "ls";
|
||||||
|
|
||||||
|
interface DirEntry {
|
||||||
|
name: string;
|
||||||
|
ptr: NodePointer;
|
||||||
|
}
|
||||||
|
|
||||||
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
const entries: { ptr: NodePointer; path: string }[] = [];
|
const entries: { ptr: NodePointer; path: string }[] = [];
|
||||||
const paths: string[] = [];
|
const paths: string[] = [];
|
||||||
@@ -24,7 +29,7 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
|
|||||||
noSort: false,
|
noSort: false,
|
||||||
noOwner: false,
|
noOwner: false,
|
||||||
noGroup: false,
|
noGroup: false,
|
||||||
sortBy: SortTreeNodeBy.NAME,
|
sortBy: SortNodeBy.NAME,
|
||||||
humanReadable: false,
|
humanReadable: false,
|
||||||
f: false,
|
f: false,
|
||||||
x: false,
|
x: false,
|
||||||
@@ -55,7 +60,7 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (flag === "sort" && isValidSortMethod(val)) {
|
if (flag === "sort" && isValidSortMethod(val)) {
|
||||||
options.sortBy = val as SortTreeNodeBy;
|
options.sortBy = val as SortNodeBy;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
@@ -154,16 +159,16 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
|
|||||||
options.x = true;
|
options.x = true;
|
||||||
break;
|
break;
|
||||||
case "X":
|
case "X":
|
||||||
options.sortBy = SortTreeNodeBy.EXTENSION;
|
options.sortBy = SortNodeBy.EXTENSION;
|
||||||
break;
|
break;
|
||||||
case "u":
|
case "u":
|
||||||
options.timestamp = "atime";
|
options.timestamp = "atime";
|
||||||
break;
|
break;
|
||||||
case "t":
|
case "t":
|
||||||
options.sortBy = options.timestamp as SortTreeNodeBy;
|
options.sortBy = options.timestamp as SortNodeBy;
|
||||||
break;
|
break;
|
||||||
case "S":
|
case "S":
|
||||||
options.sortBy = SortTreeNodeBy.SIZE;
|
options.sortBy = SortNodeBy.SIZE;
|
||||||
break;
|
break;
|
||||||
case "r":
|
case "r":
|
||||||
options.reverse = true;
|
options.reverse = true;
|
||||||
@@ -252,64 +257,77 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
|
|||||||
if (options.recursive) io.writeStdout(e.encode(`${entry.path}:\n`));
|
if (options.recursive) io.writeStdout(e.encode(`${entry.path}:\n`));
|
||||||
io.writeStdout(e.encode(`total ${Object.entries(node.children).length}\n`));
|
io.writeStdout(e.encode(`total ${Object.entries(node.children).length}\n`));
|
||||||
|
|
||||||
let children: TreeNode[] = Object.entries(node.children).map(c => state.filesystem.getNode(c[1])!);
|
let children: DirEntry[] = Array.from(node.children.entries()).map(([name, inode]) => ({
|
||||||
if (!options.noSort) children = sortTreeNodes(state, children, options.reverse, options.sortBy);
|
name,
|
||||||
|
ptr: NodePointer.of(entry.ptr.device, inode),
|
||||||
|
}));
|
||||||
|
if (!options.noSort) children = sortNodes(state, children, options.reverse, options.sortBy);
|
||||||
|
|
||||||
if (!options.all && !options.almostAll) children = children.filter(c => !c.name.startsWith("."));
|
if (!options.all && !options.almostAll) {
|
||||||
|
children = children.filter(c => !c.name.startsWith("."));
|
||||||
|
}
|
||||||
|
|
||||||
if (options.long) {
|
if (options.long) {
|
||||||
longMode(io, state, children, node);
|
longMode(io, state, children);
|
||||||
if (options.recursive) io.writeStdout(e.encode("\n"));
|
if (options.recursive) io.writeStdout(e.encode("\n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.recursive) {
|
if (options.recursive) {
|
||||||
for (const child of children) {
|
for (const c of children) {
|
||||||
if (child.type === TreeNodeType.Directory) {
|
const child = state.filesystem.getNode(c.ptr)!;
|
||||||
|
if (child.type === NodeType.Directory) {
|
||||||
let path;
|
let path;
|
||||||
if (entry.path === "/") {
|
if (entry.path === "/") {
|
||||||
path = "/" + child.name;
|
path = "/" + c.name;
|
||||||
} else if (entry.path.endsWith("/")) {
|
} else if (entry.path.endsWith("/")) {
|
||||||
path = entry.path + child.name;
|
path = entry.path + c.name;
|
||||||
} else path = entry.path + "/" + child.name;
|
} else path = entry.path + "/" + c.name;
|
||||||
listDirEntries({ inode: child.inode, path });
|
listDirEntries({ ptr: c.ptr, path });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function longMode(io: CommandIO, state: ShellState, children: TreeNode[], current: TreeNode): void {
|
function longMode(io: CommandIO, state: ShellState, children: DirEntry[]): void {
|
||||||
const e = new TextEncoder();
|
const e = new TextEncoder();
|
||||||
const buff: Uint8Array[] = [];
|
const buff: Uint8Array[] = [];
|
||||||
const nodes: TreeNode[] = [...children];
|
const nodes: DirEntry[] = [...children];
|
||||||
|
|
||||||
if (options.all) {
|
/* if (options.all) {
|
||||||
const parentNode = state.filesystem.getNode(current.parent)!;
|
const parentNode = state.filesystem.getNode(current.parent)!;
|
||||||
|
|
||||||
const parent: TreeNode = { ...parentNode, name: ".." };
|
const parent: NodePointer = { ...parentNode, name: ".." };
|
||||||
nodes.unshift(parent);
|
nodes.unshift(parent);
|
||||||
|
|
||||||
const cwd: TreeNode = { ...current, name: "." };
|
const cwd: TreeNode = { ...current, name: "." };
|
||||||
nodes.unshift(cwd);
|
nodes.unshift(cwd);
|
||||||
}
|
} */
|
||||||
|
|
||||||
// Calculate the string size of the biggerst node in the array;
|
// Calculate the string size of the biggerst node in the array;
|
||||||
// Sets 4 length for directories because the arbitrary size is 4096 later in the code
|
// Sets 4 length for directories because the arbitrary size is 4096 later in the code
|
||||||
const maxInodeWidth = Math.max(...nodes.map(c => c.inode.toString().length));
|
const maxInodeWidth = Math.max(...nodes.map(c => c.ptr.inode.toString().length));
|
||||||
|
|
||||||
const maxSizeWidth = Math.max(
|
const maxSizeWidth = Math.max(
|
||||||
...nodes.map(c => (c.type === TreeNodeType.File ? c.content.byteSize().toString().length : 4)),
|
...nodes.map(c =>
|
||||||
|
state.filesystem.getNode(c.ptr)!.type === NodeType.File
|
||||||
|
? (state.filesystem.getNode(c.ptr) as FileNode).size.toString().length
|
||||||
|
: 4,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
const maxChildrenWidth = Math.max(
|
const maxChildrenWidth = Math.max(
|
||||||
...nodes.map(c =>
|
...nodes.map(c =>
|
||||||
c.type === TreeNodeType.Directory ? Object.entries(c.children).length.toString().length : 2,
|
state.filesystem.getNode(c.ptr)!.type === NodeType.Directory
|
||||||
|
? Object.entries((state.filesystem.getNode(c.ptr) as DirectoryNode)!.children).length
|
||||||
|
: 2,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
const namePadding = nodes.every(c => !c.name.includes(" "));
|
const namePadding = nodes.every(c => !c.name.includes(" "));
|
||||||
|
|
||||||
for (const node of nodes) {
|
for (const n of nodes) {
|
||||||
//Inode
|
//Inode
|
||||||
|
const node = state.filesystem.getNode(n.ptr)!;
|
||||||
if (options.inode) {
|
if (options.inode) {
|
||||||
const inode = node.inode.toString().padStart(maxInodeWidth, " ");
|
const inode = node.inode.toString().padStart(maxInodeWidth, " ");
|
||||||
buff.push(e.encode(inode + " "));
|
buff.push(e.encode(inode + " "));
|
||||||
@@ -320,17 +338,17 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
|
|||||||
const mode: string = parseMode(node.mode);
|
const mode: string = parseMode(node.mode);
|
||||||
|
|
||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
case TreeNodeType.Directory:
|
case NodeType.Directory:
|
||||||
buff.push(e.encode("d" + mode + " "));
|
buff.push(e.encode("d" + mode + " "));
|
||||||
break;
|
break;
|
||||||
case TreeNodeType.File:
|
case NodeType.File:
|
||||||
buff.push(e.encode("-" + mode + " "));
|
buff.push(e.encode("-" + mode + " "));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Children amnt (later will be amnt of links)
|
// Children amnt (later will be amnt of links)
|
||||||
const childAmnt =
|
const childAmnt =
|
||||||
node.type === TreeNodeType.Directory
|
node.type === NodeType.Directory
|
||||||
? `${Object.entries(node.children).length.toString().padStart(maxChildrenWidth, " ")} `
|
? `${Object.entries(node.children).length.toString().padStart(maxChildrenWidth, " ")} `
|
||||||
: `${"10".padStart(maxChildrenWidth, " ")} `;
|
: `${"10".padStart(maxChildrenWidth, " ")} `;
|
||||||
|
|
||||||
@@ -354,12 +372,12 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
|
|||||||
// Sets the folder size to an arbitrary 4096, could change it later to smth else
|
// Sets the folder size to an arbitrary 4096, could change it later to smth else
|
||||||
let size;
|
let size;
|
||||||
if (options.humanReadable) {
|
if (options.humanReadable) {
|
||||||
const bytes = node.type === TreeNodeType.File ? node.content.byteSize() : 4096;
|
const bytes = node.type === NodeType.File ? node.size : 4096;
|
||||||
size = convertToUnit(bytes, 1, options.si ? 1000 : 1024);
|
size = convertToUnit(bytes, 1, options.si ? 1000 : 1024);
|
||||||
} else {
|
} else {
|
||||||
size =
|
size =
|
||||||
node.type === TreeNodeType.File
|
node.type === NodeType.File
|
||||||
? `${node.content.byteSize().toString().padStart(maxSizeWidth, " ")} `
|
? `${node.size.toString().padStart(maxSizeWidth, " ")} `
|
||||||
: `${"4096".padStart(maxSizeWidth, " ")} `;
|
: `${"4096".padStart(maxSizeWidth, " ")} `;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -386,15 +404,15 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
|
|||||||
if (options.quoteName) {
|
if (options.quoteName) {
|
||||||
buff.push(
|
buff.push(
|
||||||
e.encode(
|
e.encode(
|
||||||
`"${node.type === TreeNodeType.Directory ? node.name + '"' + (options.appendIndicator ? "/" : "") : node.name + '"'}\n`,
|
`"${node.type === NodeType.Directory ? n.name + '"' + (options.appendIndicator ? "/" : "") : n.name + '"'}\n`,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
const unpadded = `${node.name.includes(" ") && !options.literal ? "'" + node.name + "'" : node.name}`;
|
const unpadded = `${n.name.includes(" ") && !options.literal ? "'" + n.name + "'" : n.name}`;
|
||||||
const name = `${!namePadding ? (unpadded.includes(" ") ? unpadded : " " + unpadded) : unpadded}`;
|
const name = `${!namePadding ? (unpadded.includes(" ") ? unpadded : " " + unpadded) : unpadded}`;
|
||||||
buff.push(
|
buff.push(
|
||||||
e.encode(
|
e.encode(
|
||||||
`${node.type === TreeNodeType.Directory ? name + (options.appendIndicator ? "/" : "") : name}\n`,
|
`${node.type === NodeType.Directory ? name + (options.appendIndicator ? "/" : "") : name}\n`,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -404,7 +422,7 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
|
|||||||
io.writeStdout(out);
|
io.writeStdout(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDateTimestamp(node: TreeNode, ts: string): Date {
|
function getDateTimestamp(node: Node, ts: string): Date {
|
||||||
if (ts === "atime") return new Date(node.atime);
|
if (ts === "atime") return new Date(node.atime);
|
||||||
if (ts === "ctime") return new Date(node.ctime);
|
if (ts === "ctime") return new Date(node.ctime);
|
||||||
if (ts === "mtime") return new Date(node.mtime);
|
if (ts === "mtime") return new Date(node.mtime);
|
||||||
@@ -427,16 +445,12 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
|
|||||||
return map(owner) + map(group) + map(other);
|
return map(owner) + map(group) + map(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isValidSortMethod(value: string): value is SortTreeNodeBy {
|
function isValidSortMethod(value: string): value is SortNodeBy {
|
||||||
return Object.values(SortTreeNodeBy).includes(value as SortTreeNodeBy);
|
return Object.values(SortNodeBy).includes(value as SortNodeBy);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isValidTimestamp(
|
function isValidTimestamp(value: string): value is SortNodeBy.ATIME | SortNodeBy.MTIME | SortNodeBy.CTIME {
|
||||||
value: string,
|
return Object.values(SortNodeBy).includes(value as SortNodeBy.ATIME | SortNodeBy.MTIME | SortNodeBy.CTIME);
|
||||||
): value is SortTreeNodeBy.ATIME | SortTreeNodeBy.MTIME | SortTreeNodeBy.CTIME {
|
|
||||||
return Object.values(SortTreeNodeBy).includes(
|
|
||||||
value as SortTreeNodeBy.ATIME | SortTreeNodeBy.MTIME | SortTreeNodeBy.CTIME,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,15 +15,7 @@ import type {
|
|||||||
NotADirectoryError,
|
NotADirectoryError,
|
||||||
NotAFileError,
|
NotAFileError,
|
||||||
} from "./errors";
|
} from "./errors";
|
||||||
import {
|
import { NodePointer, NodeType, type DirectoryNode, type FileNode, type Node, type NodeEntry } from "./node";
|
||||||
fromPtr,
|
|
||||||
NodeType,
|
|
||||||
type DirectoryNode,
|
|
||||||
type FileNode,
|
|
||||||
type Node,
|
|
||||||
type NodeEntry,
|
|
||||||
type NodePointer,
|
|
||||||
} from "./node";
|
|
||||||
import type { IDataStore, IMetaStore, VDiskConfig } from "./vdisk";
|
import type { IDataStore, IMetaStore, VDiskConfig } from "./vdisk";
|
||||||
|
|
||||||
export interface FileSystemConfig {
|
export interface FileSystemConfig {
|
||||||
@@ -50,11 +42,10 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private resolveNode(ptr: NodePointer): Node | undefined {
|
private resolveNode(ptr: NodePointer): Node | undefined {
|
||||||
const fptr = fromPtr(ptr);
|
const dev = this._VDiskManager.getDisk(ptr.device);
|
||||||
const dev = this._VDiskManager.getDisk(fptr.device_id);
|
|
||||||
if (!dev) return undefined;
|
if (!dev) return undefined;
|
||||||
|
|
||||||
return dev.getNode(fptr.inode);
|
return dev.getNode(ptr.inode);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make async
|
// Make async
|
||||||
@@ -108,15 +99,14 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public getNodeEntries(ptr: NodePointer): readonly NodeEntry[] | undefined {
|
public getNodeEntries(ptr: NodePointer): readonly NodeEntry[] | undefined {
|
||||||
const fptr = fromPtr(ptr);
|
const dev = this._VDiskManager.getDisk(ptr.device);
|
||||||
const dev = this._VDiskManager.getDisk(fptr.device_id);
|
|
||||||
|
|
||||||
if (!dev) {
|
if (!dev) {
|
||||||
throw {
|
throw {
|
||||||
type: "DeviceNotFound",
|
type: "DeviceNotFound",
|
||||||
message: `Device under id ${fptr.device_id} not found`,
|
message: `Device under id ${ptr.device} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
device_id: fptr.device_id,
|
device_id: ptr.device,
|
||||||
} satisfies DeviceNotFoundError;
|
} satisfies DeviceNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,31 +122,28 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public mount(mount: NodePointer, target: NodePointer) {
|
public mount(mount: NodePointer, target: NodePointer) {
|
||||||
const mptr = fromPtr(mount);
|
if (!this._VDiskManager.hasDisk(mount.device)) {
|
||||||
const tptr = fromPtr(target);
|
|
||||||
|
|
||||||
if (!this._VDiskManager.hasDisk(mptr.device_id)) {
|
|
||||||
throw {
|
throw {
|
||||||
type: "DeviceNotFound",
|
type: "DeviceNotFound",
|
||||||
message: `Device under id ${mptr.device_id} not found`,
|
message: `Device under id ${mount.device} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
device_id: mptr.device_id,
|
device_id: mount.device,
|
||||||
} satisfies DeviceNotFoundError;
|
} satisfies DeviceNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this._VDiskManager.hasDisk(tptr.device_id)) {
|
if (!this._VDiskManager.hasDisk(target.device)) {
|
||||||
throw {
|
throw {
|
||||||
type: "DeviceNotFound",
|
type: "DeviceNotFound",
|
||||||
message: `Device under id ${tptr.device_id} not found`,
|
message: `Device under id ${target.device} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
device_id: tptr.device_id,
|
device_id: target.device,
|
||||||
} satisfies DeviceNotFoundError;
|
} satisfies DeviceNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.getNode(mount)) {
|
if (!this.getNode(mount)) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
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(),
|
timestamp: Date.now(),
|
||||||
pointer: mount,
|
pointer: mount,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
@@ -165,7 +152,7 @@ export class FileSystem {
|
|||||||
if (!this.getNode(target)) {
|
if (!this.getNode(target)) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
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(),
|
timestamp: Date.now(),
|
||||||
pointer: target,
|
pointer: target,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
@@ -192,22 +179,21 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public isFile(ptr: NodePointer): boolean {
|
public isFile(ptr: NodePointer): boolean {
|
||||||
const fptr = fromPtr(ptr);
|
|
||||||
const node = this.resolveNode(ptr);
|
const node = this.resolveNode(ptr);
|
||||||
|
|
||||||
if (!this._VDiskManager.hasDisk(fptr.device_id)) {
|
if (!this._VDiskManager.hasDisk(ptr.device)) {
|
||||||
throw {
|
throw {
|
||||||
type: "DeviceNotFound",
|
type: "DeviceNotFound",
|
||||||
message: `Device under id ${fptr.device_id} not found`,
|
message: `Device under id ${ptr.device} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
device_id: fptr.device_id,
|
device_id: ptr.device,
|
||||||
} satisfies DeviceNotFoundError;
|
} satisfies DeviceNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node === undefined) {
|
if (node === undefined) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
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(),
|
timestamp: Date.now(),
|
||||||
pointer: ptr,
|
pointer: ptr,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
@@ -217,22 +203,21 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public isDirectory(ptr: NodePointer): boolean {
|
public isDirectory(ptr: NodePointer): boolean {
|
||||||
const fptr = fromPtr(ptr);
|
|
||||||
const node = this.resolveNode(ptr);
|
const node = this.resolveNode(ptr);
|
||||||
|
|
||||||
if (!this._VDiskManager.hasDisk(fptr.device_id)) {
|
if (!this._VDiskManager.hasDisk(ptr.device)) {
|
||||||
throw {
|
throw {
|
||||||
type: "DeviceNotFound",
|
type: "DeviceNotFound",
|
||||||
message: `Device under id ${fptr.device_id} not found`,
|
message: `Device under id ${ptr.device} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
device_id: fptr.device_id,
|
device_id: ptr.device,
|
||||||
} satisfies DeviceNotFoundError;
|
} satisfies DeviceNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node === undefined) {
|
if (node === undefined) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
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(),
|
timestamp: Date.now(),
|
||||||
pointer: ptr,
|
pointer: ptr,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
@@ -242,22 +227,21 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public isSymlink(ptr: NodePointer): boolean {
|
public isSymlink(ptr: NodePointer): boolean {
|
||||||
const fptr = fromPtr(ptr);
|
|
||||||
const node = this.resolveNode(ptr);
|
const node = this.resolveNode(ptr);
|
||||||
|
|
||||||
if (!this._VDiskManager.hasDisk(fptr.device_id)) {
|
if (!this._VDiskManager.hasDisk(ptr.device)) {
|
||||||
throw {
|
throw {
|
||||||
type: "DeviceNotFound",
|
type: "DeviceNotFound",
|
||||||
message: `Device under id ${fptr.device_id} not found`,
|
message: `Device under id ${ptr.device} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
device_id: fptr.device_id,
|
device_id: ptr.device,
|
||||||
} satisfies DeviceNotFoundError;
|
} satisfies DeviceNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node === undefined) {
|
if (node === undefined) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
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(),
|
timestamp: Date.now(),
|
||||||
pointer: ptr,
|
pointer: ptr,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
@@ -267,28 +251,26 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async moveNode(source: NodePointer, destination: NodePointer, name?: string) {
|
public async moveNode(source: NodePointer, destination: NodePointer, name?: string) {
|
||||||
const sptr = fromPtr(source);
|
const sdev = this._VDiskManager.getDisk(source.device);
|
||||||
const dptr = fromPtr(destination);
|
const ddev = this._VDiskManager.getDisk(destination.device);
|
||||||
const sdev = this._VDiskManager.getDisk(sptr.device_id);
|
|
||||||
const ddev = this._VDiskManager.getDisk(dptr.device_id);
|
|
||||||
const sourceNode = this.resolveNode(source);
|
const sourceNode = this.resolveNode(source);
|
||||||
const destinationNode = this.resolveNode(destination);
|
const destinationNode = this.resolveNode(destination);
|
||||||
|
|
||||||
if (!sdev) {
|
if (!sdev) {
|
||||||
throw {
|
throw {
|
||||||
type: "DeviceNotFound",
|
type: "DeviceNotFound",
|
||||||
message: `Device under id ${sptr.device_id} not found`,
|
message: `Device under id ${source.device} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
device_id: sptr.device_id,
|
device_id: source.device,
|
||||||
} satisfies DeviceNotFoundError;
|
} satisfies DeviceNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ddev) {
|
if (!ddev) {
|
||||||
throw {
|
throw {
|
||||||
type: "DeviceNotFound",
|
type: "DeviceNotFound",
|
||||||
message: `Device under id ${dptr.device_id} not found`,
|
message: `Device under id ${destination.device} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
device_id: dptr.device_id,
|
device_id: destination.device,
|
||||||
} satisfies DeviceNotFoundError;
|
} satisfies DeviceNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,8 +303,8 @@ export class FileSystem {
|
|||||||
if (destinationNode.type !== NodeType.Directory) {
|
if (destinationNode.type !== NodeType.Directory) {
|
||||||
throw {
|
throw {
|
||||||
type: "NotADirectory",
|
type: "NotADirectory",
|
||||||
message: `Destination node under inode ${dptr.inode} is not a directory`,
|
message: `Destination node under inode ${destination.inode} is not a directory`,
|
||||||
inode: dptr.inode,
|
inode: destination.inode,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
nodeType: destinationNode.type,
|
nodeType: destinationNode.type,
|
||||||
} satisfies NotADirectoryError;
|
} satisfies NotADirectoryError;
|
||||||
@@ -372,8 +354,8 @@ export class FileSystem {
|
|||||||
} satisfies DirectoryLoopError;
|
} satisfies DirectoryLoopError;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sptr.device_id === dptr.device_id) {
|
if (source.device === destination.device) {
|
||||||
await ddev.moveNodeEntry(pinode, dptr.inode, s_entry[0].name, name);
|
await ddev.moveNodeEntry(pinode, destination.inode, s_entry[0].name, name);
|
||||||
} else {
|
} else {
|
||||||
console.error("cross device node movement not yet implemented");
|
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.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 },
|
||||||
) {
|
) {
|
||||||
const pptr = fromPtr(parent);
|
const pdev = this._VDiskManager.getDisk(parent.device);
|
||||||
const pdev = this._VDiskManager.getDisk(pptr.device_id);
|
|
||||||
const parentNode = this.resolveNode(parent);
|
const parentNode = this.resolveNode(parent);
|
||||||
|
|
||||||
if (!pdev) {
|
if (!pdev) {
|
||||||
throw {
|
throw {
|
||||||
type: "DeviceNotFound",
|
type: "DeviceNotFound",
|
||||||
message: `Device under id ${pptr.device_id} not found`,
|
message: `Device under id ${parent.device} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
device_id: pptr.device_id,
|
device_id: parent.device,
|
||||||
} satisfies DeviceNotFoundError;
|
} satisfies DeviceNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -419,8 +400,8 @@ export class FileSystem {
|
|||||||
if (parentNode.type !== NodeType.Directory) {
|
if (parentNode.type !== NodeType.Directory) {
|
||||||
throw {
|
throw {
|
||||||
type: "NotADirectory",
|
type: "NotADirectory",
|
||||||
message: `Destination node under inode ${pptr.inode} is not a directory`,
|
message: `Destination node under inode ${parent.inode} is not a directory`,
|
||||||
inode: pptr.inode,
|
inode: parent.inode,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
nodeType: parentNode.type,
|
nodeType: parentNode.type,
|
||||||
} satisfies NotADirectoryError;
|
} satisfies NotADirectoryError;
|
||||||
@@ -446,14 +427,13 @@ export class FileSystem {
|
|||||||
} satisfies InvalidNodeNameError;
|
} satisfies InvalidNodeNameError;
|
||||||
}
|
}
|
||||||
|
|
||||||
await pdev.createNodeEntry(pptr.inode, name, data);
|
await pdev.createNodeEntry(parent.inode, name, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async deleteNode(target: NodePointer) {
|
public async deleteNode(target: NodePointer) {
|
||||||
const tptr = fromPtr(target);
|
const tdev = this._VDiskManager.getDisk(target.device);
|
||||||
const tdev = this._VDiskManager.getDisk(tptr.device_id);
|
|
||||||
|
|
||||||
if (tptr.inode === 0) {
|
if (target.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)`,
|
||||||
@@ -464,9 +444,9 @@ export class FileSystem {
|
|||||||
if (!tdev) {
|
if (!tdev) {
|
||||||
throw {
|
throw {
|
||||||
type: "DeviceNotFound",
|
type: "DeviceNotFound",
|
||||||
message: `Device under id ${tptr.device_id} not found`,
|
message: `Device under id ${target.device} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
device_id: tptr.device_id,
|
device_id: target.device,
|
||||||
} satisfies DeviceNotFoundError;
|
} satisfies DeviceNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -484,16 +464,15 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async writeContent(target: NodePointer, content: Blob) {
|
public async writeContent(target: NodePointer, content: Blob) {
|
||||||
const ptr = fromPtr(target);
|
const dev = this._VDiskManager.getDisk(target.device);
|
||||||
const dev = this._VDiskManager.getDisk(ptr.device_id);
|
|
||||||
const node = this.resolveNode(target);
|
const node = this.resolveNode(target);
|
||||||
|
|
||||||
if (!dev) {
|
if (!dev) {
|
||||||
throw {
|
throw {
|
||||||
type: "DeviceNotFound",
|
type: "DeviceNotFound",
|
||||||
message: `Device under id ${ptr.device_id} not found`,
|
message: `Device under id ${target.device} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
device_id: ptr.device_id,
|
device_id: target.device,
|
||||||
} satisfies DeviceNotFoundError;
|
} satisfies DeviceNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -510,7 +489,7 @@ export class FileSystem {
|
|||||||
throw {
|
throw {
|
||||||
type: "NotAFile",
|
type: "NotAFile",
|
||||||
message: `Node is not a file`,
|
message: `Node is not a file`,
|
||||||
inode: ptr.inode,
|
inode: target.inode,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
nodeType: node.type,
|
nodeType: node.type,
|
||||||
} satisfies NotAFileError;
|
} satisfies NotAFileError;
|
||||||
@@ -520,16 +499,15 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public streamContent(target: NodePointer): Readable {
|
public streamContent(target: NodePointer): Readable {
|
||||||
const ptr = fromPtr(target);
|
const dev = this._VDiskManager.getDisk(target.device);
|
||||||
const dev = this._VDiskManager.getDisk(ptr.device_id);
|
|
||||||
const node = this.resolveNode(target);
|
const node = this.resolveNode(target);
|
||||||
|
|
||||||
if (!dev) {
|
if (!dev) {
|
||||||
throw {
|
throw {
|
||||||
type: "DeviceNotFound",
|
type: "DeviceNotFound",
|
||||||
message: `Device under id ${ptr.device_id} not found`,
|
message: `Device under id ${target.device} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
device_id: ptr.device_id,
|
device_id: target.device,
|
||||||
} satisfies DeviceNotFoundError;
|
} satisfies DeviceNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -546,7 +524,7 @@ export class FileSystem {
|
|||||||
throw {
|
throw {
|
||||||
type: "NotAFile",
|
type: "NotAFile",
|
||||||
message: `Node is not a file`,
|
message: `Node is not a file`,
|
||||||
inode: ptr.inode,
|
inode: target.inode,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
nodeType: node.type,
|
nodeType: node.type,
|
||||||
} satisfies NotAFileError;
|
} satisfies NotAFileError;
|
||||||
@@ -556,16 +534,15 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async 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(target.device);
|
||||||
const dev = this._VDiskManager.getDisk(ptr.device_id);
|
|
||||||
const node = this.resolveNode(target);
|
const node = this.resolveNode(target);
|
||||||
|
|
||||||
if (!dev) {
|
if (!dev) {
|
||||||
throw {
|
throw {
|
||||||
type: "DeviceNotFound",
|
type: "DeviceNotFound",
|
||||||
message: `Device under id ${ptr.device_id} not found`,
|
message: `Device under id ${target.device} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
device_id: ptr.device_id,
|
device_id: target.device,
|
||||||
} satisfies DeviceNotFoundError;
|
} satisfies DeviceNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -582,7 +559,7 @@ export class FileSystem {
|
|||||||
throw {
|
throw {
|
||||||
type: "NotAFile",
|
type: "NotAFile",
|
||||||
message: `Node is not a file`,
|
message: `Node is not a file`,
|
||||||
inode: ptr.inode,
|
inode: target.device,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
nodeType: node.type,
|
nodeType: node.type,
|
||||||
} satisfies NotAFileError;
|
} satisfies NotAFileError;
|
||||||
@@ -591,14 +568,14 @@ export class FileSystem {
|
|||||||
await dev.writeNodeEntry(node.inode, data);
|
await dev.writeNodeEntry(node.inode, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public canRead(nodep: NodePointer, user: UserInfo): boolean {
|
public canRead(ptr: NodePointer, user: UserInfo): boolean {
|
||||||
const node = this.resolveNode(nodep);
|
const node = this.resolveNode(ptr);
|
||||||
if (!node) {
|
if (!node) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `Node under ${nodep} not found`,
|
message: `Node under ${ptr} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
pointer: nodep,
|
pointer: ptr,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -613,14 +590,14 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public canWrite(nodep: NodePointer, user: UserInfo): boolean {
|
public canWrite(ptr: NodePointer, user: UserInfo): boolean {
|
||||||
const node = this.resolveNode(nodep);
|
const node = this.resolveNode(ptr);
|
||||||
if (!node) {
|
if (!node) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `Node under ${nodep} not found`,
|
message: `Node under ${ptr} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
pointer: nodep,
|
pointer: ptr,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -635,14 +612,14 @@ export class FileSystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public canExecute(nodep: NodePointer, user: UserInfo): boolean {
|
public canExecute(ptr: NodePointer, user: UserInfo): boolean {
|
||||||
const node = this.resolveNode(nodep);
|
const node = this.resolveNode(ptr);
|
||||||
if (!node) {
|
if (!node) {
|
||||||
throw {
|
throw {
|
||||||
type: "NodeNotFound",
|
type: "NodeNotFound",
|
||||||
message: `Node under ${nodep} not found`,
|
message: `Node under ${ptr} not found`,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
pointer: nodep,
|
pointer: ptr,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,7 @@
|
|||||||
import type { Blob } from "node:buffer";
|
import type { Blob } from "node:buffer";
|
||||||
import { Readable } from "node:stream";
|
import { Readable } from "node:stream";
|
||||||
import { blob } from "node:stream/consumers";
|
import { blob } from "node:stream/consumers";
|
||||||
import {
|
import { NodePointer, NodeType, type DirectoryNode, type Node, type NodeEntry, type NodeHash } from "./node";
|
||||||
NodeType,
|
|
||||||
toPtr,
|
|
||||||
type DirectoryNode,
|
|
||||||
type Node,
|
|
||||||
type NodeEntry,
|
|
||||||
type NodeHash,
|
|
||||||
type NodePointer,
|
|
||||||
} from "./node";
|
|
||||||
|
|
||||||
export function DefaultVDiskConfig(id: number): VDiskConfig {
|
export function DefaultVDiskConfig(id: number): VDiskConfig {
|
||||||
const base = new Map();
|
const base = new Map();
|
||||||
@@ -149,6 +141,7 @@ export class VDisk {
|
|||||||
|
|
||||||
const root = this.getNode(0) as DirectoryNode;
|
const root = this.getNode(0) as DirectoryNode;
|
||||||
for (const [name, inode] of root.children) {
|
for (const [name, inode] of root.children) {
|
||||||
|
if (name === "." || name === "..") continue;
|
||||||
stack.push([inode, 0, this._id, name]);
|
stack.push([inode, 0, this._id, name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +150,7 @@ export class VDisk {
|
|||||||
const node = this.getNode(inode);
|
const node = this.getNode(inode);
|
||||||
if (node === undefined) continue;
|
if (node === undefined) continue;
|
||||||
|
|
||||||
const key = toPtr({ device_id: this._id, inode });
|
const key = NodePointer.of(this._id, inode);
|
||||||
const entry = {
|
const entry = {
|
||||||
pinode,
|
pinode,
|
||||||
pdevice_id,
|
pdevice_id,
|
||||||
@@ -170,6 +163,7 @@ export class VDisk {
|
|||||||
|
|
||||||
if (node.type === NodeType.Directory) {
|
if (node.type === NodeType.Directory) {
|
||||||
for (const [name, cinode] of node.children) {
|
for (const [name, cinode] of node.children) {
|
||||||
|
if (name === "." || name === "..") continue;
|
||||||
stack.push([cinode, inode, pdevice_id, name]);
|
stack.push([cinode, inode, pdevice_id, name]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-62
@@ -1,4 +1,3 @@
|
|||||||
import type { FileSystemCorruptionError } from "@core/filesystem/errors";
|
|
||||||
import { FileNode, NodePointer } from "@core/filesystem/node";
|
import { FileNode, NodePointer } from "@core/filesystem/node";
|
||||||
import type { ShellState } from "@core/shell_state";
|
import type { ShellState } from "@core/shell_state";
|
||||||
import { Posix } from "@termos/util/posix";
|
import { Posix } from "@termos/util/posix";
|
||||||
@@ -14,43 +13,26 @@ export enum SortNodeBy {
|
|||||||
CTIME = "ctime",
|
CTIME = "ctime",
|
||||||
}
|
}
|
||||||
|
|
||||||
export function sortNodes(
|
export function sortNodes<T extends { name: string; ptr: NodePointer }>(
|
||||||
state: ShellState,
|
state: ShellState,
|
||||||
arr: NodePointer[],
|
arr: T[],
|
||||||
r: boolean = false,
|
r: boolean = false,
|
||||||
by: SortNodeBy = SortNodeBy.NAME,
|
by: SortNodeBy = SortNodeBy.NAME,
|
||||||
): NodePointer[] {
|
): T[] {
|
||||||
if (arr.length === 0) return [];
|
if (arr.length === 0) return [];
|
||||||
|
|
||||||
/* const nodes: NodePointer[] = [];
|
|
||||||
|
|
||||||
if (arr[0] instanceof NodePointer) {
|
|
||||||
nodes.push(...(arr as NodePointer[]));
|
|
||||||
for (const e of arr as NodePointer[]) {
|
|
||||||
const node = state.filesystem.getNode(e);
|
|
||||||
if (!node) continue;
|
|
||||||
|
|
||||||
nodes.push(node);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (const e of arr as Node[]) {
|
|
||||||
const ptr = NodePointer.of(device, e.inode);
|
|
||||||
nodes.push(ptr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
nodeQS(arr, r, by, 0, arr.length - 1, state);
|
nodeQS(arr, r, by, 0, arr.length - 1, state);
|
||||||
|
|
||||||
return arr;
|
return arr;
|
||||||
|
|
||||||
/* if (arr[0] instanceof NodePointer) {
|
|
||||||
return nodes.map(n => state.filesystem.getNode(n as NodePointer)) as T[];
|
|
||||||
} else {
|
|
||||||
return nodes as T[];
|
|
||||||
} */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function nodeQS(arr: NodePointer[], r: boolean, by: SortNodeBy, start: number, end: number, state: ShellState): void {
|
function nodeQS(
|
||||||
|
arr: { name: string; ptr: NodePointer }[],
|
||||||
|
r: boolean,
|
||||||
|
by: SortNodeBy,
|
||||||
|
start: number,
|
||||||
|
end: number,
|
||||||
|
state: ShellState,
|
||||||
|
): void {
|
||||||
if (end <= start) return;
|
if (end <= start) return;
|
||||||
|
|
||||||
const pivot = partitionNodeArray(arr, r, by, start, end, state);
|
const pivot = partitionNodeArray(arr, r, by, start, end, state);
|
||||||
@@ -59,7 +41,7 @@ function nodeQS(arr: NodePointer[], r: boolean, by: SortNodeBy, start: number, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
function partitionNodeArray(
|
function partitionNodeArray(
|
||||||
part: NodePointer[],
|
part: { name: string; ptr: NodePointer }[],
|
||||||
r: boolean,
|
r: boolean,
|
||||||
by: SortNodeBy,
|
by: SortNodeBy,
|
||||||
start: number,
|
start: number,
|
||||||
@@ -86,21 +68,17 @@ function partitionNodeArray(
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
function compareNode(a: NodePointer, b: NodePointer, by: SortNodeBy, r: boolean, state: ShellState): number {
|
function compareNode(
|
||||||
|
a: { name: string; ptr: NodePointer },
|
||||||
|
b: { name: string; ptr: NodePointer },
|
||||||
|
by: SortNodeBy,
|
||||||
|
r: boolean,
|
||||||
|
state: ShellState,
|
||||||
|
): number {
|
||||||
switch (by) {
|
switch (by) {
|
||||||
case SortNodeBy.NAME: {
|
case SortNodeBy.NAME: {
|
||||||
const entriesA = state.filesystem.getNodeEntries(a);
|
const A: string = a.name;
|
||||||
const entriesB = state.filesystem.getNodeEntries(b);
|
const B: string = b.name;
|
||||||
if (!entriesA || !entriesB) {
|
|
||||||
throw {
|
|
||||||
type: "FileSystemCorruption",
|
|
||||||
message: "Reverse index entry missing, FileSystem corrupted",
|
|
||||||
timestamp: Date.now(),
|
|
||||||
} satisfies FileSystemCorruptionError;
|
|
||||||
}
|
|
||||||
|
|
||||||
const A: string = entriesA[0]!.name;
|
|
||||||
const B: string = entriesB[0]!.name;
|
|
||||||
const min = Math.min(A.length, B.length);
|
const min = Math.min(A.length, B.length);
|
||||||
|
|
||||||
for (let i = 0; i < min; i++) {
|
for (let i = 0; i < min; i++) {
|
||||||
@@ -118,8 +96,8 @@ function compareNode(a: NodePointer, b: NodePointer, by: SortNodeBy, r: boolean,
|
|||||||
case SortNodeBy.MTIME:
|
case SortNodeBy.MTIME:
|
||||||
case SortNodeBy.ATIME:
|
case SortNodeBy.ATIME:
|
||||||
case SortNodeBy.CTIME: {
|
case SortNodeBy.CTIME: {
|
||||||
const nodeA = state.filesystem.getNode(a)!;
|
const nodeA = state.filesystem.getNode(a.ptr)!;
|
||||||
const nodeB = state.filesystem.getNode(a)!;
|
const nodeB = state.filesystem.getNode(b.ptr)!;
|
||||||
const A: number = nodeA[by];
|
const A: number = nodeA[by];
|
||||||
const B: number = nodeB[by];
|
const B: number = nodeB[by];
|
||||||
|
|
||||||
@@ -127,8 +105,8 @@ function compareNode(a: NodePointer, b: NodePointer, by: SortNodeBy, r: boolean,
|
|||||||
}
|
}
|
||||||
|
|
||||||
case SortNodeBy.SIZE: {
|
case SortNodeBy.SIZE: {
|
||||||
const nodeA = state.filesystem.getNode(a)!;
|
const nodeA = state.filesystem.getNode(a.ptr)!;
|
||||||
const nodeB = state.filesystem.getNode(a)!;
|
const nodeB = state.filesystem.getNode(b.ptr)!;
|
||||||
const A: number = (nodeA as FileNode).size;
|
const A: number = (nodeA as FileNode).size;
|
||||||
const B: number = (nodeB as FileNode).size;
|
const B: number = (nodeB as FileNode).size;
|
||||||
|
|
||||||
@@ -136,18 +114,8 @@ function compareNode(a: NodePointer, b: NodePointer, by: SortNodeBy, r: boolean,
|
|||||||
}
|
}
|
||||||
|
|
||||||
case SortNodeBy.EXTENSION: {
|
case SortNodeBy.EXTENSION: {
|
||||||
const entriesA = state.filesystem.getNodeEntries(a);
|
const A: string = a.name.split(".").pop() ?? "";
|
||||||
const entriesB = state.filesystem.getNodeEntries(b);
|
const B: string = b.name.split(".").pop() ?? "";
|
||||||
if (!entriesA || !entriesB) {
|
|
||||||
throw {
|
|
||||||
type: "FileSystemCorruption",
|
|
||||||
message: "Reverse index entry missing, FileSystem corrupted",
|
|
||||||
timestamp: Date.now(),
|
|
||||||
} satisfies FileSystemCorruptionError;
|
|
||||||
}
|
|
||||||
|
|
||||||
const A: string = entriesA[0]!.name.split(".").pop() ?? "";
|
|
||||||
const B: string = entriesB[0]!.name.split(".").pop() ?? "";
|
|
||||||
const min = Math.min(A.length, B.length);
|
const min = Math.min(A.length, B.length);
|
||||||
|
|
||||||
for (let i = 0; i < min; i++) {
|
for (let i = 0; i < min; i++) {
|
||||||
@@ -163,12 +131,12 @@ function compareNode(a: NodePointer, b: NodePointer, by: SortNodeBy, r: boolean,
|
|||||||
}
|
}
|
||||||
|
|
||||||
case SortNodeBy.INODE: {
|
case SortNodeBy.INODE: {
|
||||||
return r ? a.inode - b.inode : b.inode - a.inode;
|
return r ? a.ptr.inode - b.ptr.inode : b.ptr.inode - a.ptr.inode;
|
||||||
}
|
}
|
||||||
|
|
||||||
case SortNodeBy.TYPE: {
|
case SortNodeBy.TYPE: {
|
||||||
const nodeA = state.filesystem.getNode(a)!;
|
const nodeA = state.filesystem.getNode(a.ptr)!;
|
||||||
const nodeB = state.filesystem.getNode(a)!;
|
const nodeB = state.filesystem.getNode(a.ptr)!;
|
||||||
return r ? nodeB.type - nodeA.type : nodeA.type - nodeB.type;
|
return r ? nodeB.type - nodeA.type : nodeA.type - nodeB.type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { FileSystem } from "@core/filesystem/filesystem";
|
import type { FileSystem } from "@core/filesystem/filesystem";
|
||||||
import { fromPtr, NodeType, toPtr, type NodeEntry, type NodePointer } from "@core/filesystem/node";
|
import { NodePointer, NodeType, type NodeEntry } from "@core/filesystem/node";
|
||||||
|
|
||||||
export function dnTraverseTree(fs: FileSystem, root: NodePointer): NodePointer[] {
|
export function dnTraverseTree(fs: FileSystem, root: NodePointer): NodePointer[] {
|
||||||
if (fs.getNode(root) === undefined) return [];
|
if (fs.getNode(root) === undefined) return [];
|
||||||
@@ -18,7 +18,7 @@ export function dnTraverseTree(fs: FileSystem, root: NodePointer): NodePointer[]
|
|||||||
const children = Array.from(node.children.values());
|
const children = Array.from(node.children.values());
|
||||||
for (let i = children.length - 1; i >= 0; i--) {
|
for (let i = children.length - 1; i >= 0; i--) {
|
||||||
const child = children[i]!;
|
const child = children[i]!;
|
||||||
const pointer = toPtr({ device_id: Number(ptr.split(":")[0]), inode: child });
|
const pointer = NodePointer.of(ptr.device, child);
|
||||||
stack.push(pointer);
|
stack.push(pointer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,12 +32,12 @@ export function upTraverseTree(fs: FileSystem, ptr: NodePointer): NodePointer[]
|
|||||||
const result: NodePointer[] = [ptr];
|
const result: NodePointer[] = [ptr];
|
||||||
let currentPtr = ptr;
|
let currentPtr = ptr;
|
||||||
|
|
||||||
while (fromPtr(currentPtr).inode !== 0) {
|
while (currentPtr.inode !== 0) {
|
||||||
const entries = fs.getNodeEntries(currentPtr);
|
const entries = fs.getNodeEntries(currentPtr);
|
||||||
if (!entries || entries.length === 0) throw new Error(`No reverse-index entry found for ${currentPtr}`);
|
if (!entries || entries.length === 0) throw new Error(`No reverse-index entry found for ${currentPtr}`);
|
||||||
|
|
||||||
const parent = entries[0] as NodeEntry;
|
const parent = entries[0] as NodeEntry;
|
||||||
currentPtr = toPtr({ device_id: parent.pdevice_id, inode: parent.pinode });
|
currentPtr = NodePointer.of(parent.pdevice_id, parent.pinode);
|
||||||
result.push(currentPtr);
|
result.push(currentPtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ export function deserializeFileSystem(json: string): { id: number; map: Map<numb
|
|||||||
const raw = JSON.parse(json);
|
const raw = JSON.parse(json);
|
||||||
|
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
function parseNode(tree: any, map: Map<number, Node>): void { // eslint-disable-line @typescript-eslint/no-explicit-any
|
function parseNode(tree: any, parent: number, map: Map<number, Node>): void { // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||||
if(tree === null || tree.type === undefined) {
|
if(tree === null || tree.type === undefined) {
|
||||||
const time = Date.now();
|
const time = Date.now();
|
||||||
const defaultNode: DirectoryNode = {
|
const defaultNode: DirectoryNode = {
|
||||||
@@ -32,7 +32,7 @@ export function deserializeFileSystem(json: string): { id: number; map: Map<numb
|
|||||||
const children: Map<string, number> = new Map();
|
const children: Map<string, number> = new Map();
|
||||||
|
|
||||||
for (const child of tree.children) {
|
for (const child of tree.children) {
|
||||||
parseNode(child, map);
|
parseNode(child, tree.inode, map);
|
||||||
children.set(child.name, child.inode);
|
children.set(child.name, child.inode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,6 +48,8 @@ export function deserializeFileSystem(json: string): { id: number; map: Map<numb
|
|||||||
atime: tree.atime,
|
atime: tree.atime,
|
||||||
children,
|
children,
|
||||||
};
|
};
|
||||||
|
children.set('..', parent);
|
||||||
|
children.set('.', tree.inode);
|
||||||
map.set(tree.inode, Object.freeze(directoryNode));
|
map.set(tree.inode, Object.freeze(directoryNode));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -92,7 +94,7 @@ export function deserializeFileSystem(json: string): { id: number; map: Map<numb
|
|||||||
const result: { id: number; map: Map<number, Node> }[] = [];
|
const result: { id: number; map: Map<number, Node> }[] = [];
|
||||||
for (const dev of raw.dev) {
|
for (const dev of raw.dev) {
|
||||||
const map = new Map();
|
const map = new Map();
|
||||||
parseNode(dev.tree, map);
|
parseNode(dev.tree, dev.tree.inode, map);
|
||||||
|
|
||||||
result.push({ id: dev.id, map });
|
result.push({ id: dev.id, map });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { FileSystem } from "@core/filesystem/filesystem";
|
import type { FileSystem } from "@core/filesystem/filesystem";
|
||||||
import { fromPtr, NodeType, toPtr, type NodePointer, type SymlinkNode } from "@core/filesystem/node";
|
import { NodePointer, NodeType, type SymlinkNode } from "@core/filesystem/node";
|
||||||
|
|
||||||
type Frame = {
|
type Frame = {
|
||||||
ptr: NodePointer;
|
ptr: NodePointer;
|
||||||
@@ -12,13 +12,12 @@ function getSortedChildren(fs: FileSystem, ptr: NodePointer): Array<{ name: stri
|
|||||||
const node = fs.getNode(ptr);
|
const node = fs.getNode(ptr);
|
||||||
if (!node || node.type !== NodeType.Directory) return [];
|
if (!node || node.type !== NodeType.Directory) return [];
|
||||||
|
|
||||||
const { device_id } = fromPtr(ptr);
|
|
||||||
const entries = Array.from(node.children.entries());
|
const entries = Array.from(node.children.entries());
|
||||||
entries.sort(([a], [b]) => a.localeCompare(b));
|
entries.sort(([a], [b]) => a.localeCompare(b));
|
||||||
|
|
||||||
return entries.map(([name, inode]) => ({
|
return entries.map(([name, inode]) => ({
|
||||||
name,
|
name,
|
||||||
ptr: toPtr({ device_id, inode }),
|
ptr: NodePointer.of(ptr.device, inode),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,8 +25,7 @@ function mountSuffix(fs: FileSystem, ptr: NodePointer): string {
|
|||||||
if (!fs.isMountPoint(ptr)) return "";
|
if (!fs.isMountPoint(ptr)) return "";
|
||||||
const target = fs.getMountPoint(ptr);
|
const target = fs.getMountPoint(ptr);
|
||||||
if (!target) return " -> [mount: target missing]";
|
if (!target) return " -> [mount: target missing]";
|
||||||
const t = fromPtr(target);
|
return ` -> ${target.toString()}`;
|
||||||
return ` -> (${t.device_id}:${t.inode})`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function symlinkSuffix(node: { type: NodeType; target?: string }): string {
|
function symlinkSuffix(node: { type: NodeType; target?: string }): string {
|
||||||
@@ -39,14 +37,14 @@ export function printTree(fs: FileSystem, root: NodePointer, rootLabel = "/"): s
|
|||||||
if (rootNode === undefined) return "";
|
if (rootNode === undefined) return "";
|
||||||
|
|
||||||
const lines: string[] = [];
|
const lines: string[] = [];
|
||||||
const rootFptr = fromPtr(root);
|
|
||||||
const rootSuffix = mountSuffix(fs, root) + symlinkSuffix(rootNode);
|
const rootSuffix = mountSuffix(fs, root) + symlinkSuffix(rootNode);
|
||||||
lines.push(`${rootLabel} (${rootFptr.device_id}:${rootFptr.inode})${rootSuffix}`);
|
lines.push(`${rootLabel} ${root.toString()}${rootSuffix}`);
|
||||||
|
|
||||||
const stack: Frame[] = [];
|
const stack: Frame[] = [];
|
||||||
const rootChildren = getSortedChildren(fs, root);
|
const rootChildren = getSortedChildren(fs, root);
|
||||||
for (let i = rootChildren.length - 1; i >= 0; i--) {
|
for (let i = rootChildren.length - 1; i >= 0; i--) {
|
||||||
const child = rootChildren[i]!;
|
const child = rootChildren[i]!;
|
||||||
|
if (child.name.startsWith(".")) continue;
|
||||||
const isLast = i === rootChildren.length - 1;
|
const isLast = i === rootChildren.length - 1;
|
||||||
stack.push({ ptr: child.ptr, name: child.name, prefix: "", isLast });
|
stack.push({ ptr: child.ptr, name: child.name, prefix: "", isLast });
|
||||||
}
|
}
|
||||||
@@ -56,10 +54,9 @@ export function printTree(fs: FileSystem, root: NodePointer, rootLabel = "/"): s
|
|||||||
const node = fs.getNode(frame.ptr);
|
const node = fs.getNode(frame.ptr);
|
||||||
if (!node) continue;
|
if (!node) continue;
|
||||||
|
|
||||||
const fptr = fromPtr(frame.ptr);
|
|
||||||
const connector = frame.isLast ? "└── " : "├── ";
|
const connector = frame.isLast ? "└── " : "├── ";
|
||||||
const suffix = mountSuffix(fs, frame.ptr) + symlinkSuffix(node);
|
const suffix = mountSuffix(fs, frame.ptr) + symlinkSuffix(node);
|
||||||
lines.push(`${frame.prefix}${connector}${frame.name} (${fptr.device_id}:${fptr.inode})${suffix}`);
|
lines.push(`${frame.prefix}${connector}${frame.name} ${frame.ptr.toString()}${suffix}`);
|
||||||
|
|
||||||
if (node.type === NodeType.Directory && !fs.isMountPoint(frame.ptr)) {
|
if (node.type === NodeType.Directory && !fs.isMountPoint(frame.ptr)) {
|
||||||
const childPrefix = frame.prefix + (frame.isLast ? " " : "│ ");
|
const childPrefix = frame.prefix + (frame.isLast ? " " : "│ ");
|
||||||
@@ -67,6 +64,7 @@ export function printTree(fs: FileSystem, root: NodePointer, rootLabel = "/"): s
|
|||||||
|
|
||||||
for (let i = children.length - 1; i >= 0; i--) {
|
for (let i = children.length - 1; i >= 0; i--) {
|
||||||
const child = children[i]!;
|
const child = children[i]!;
|
||||||
|
if (child.name.startsWith(".")) continue;
|
||||||
const childIsLast = i === children.length - 1;
|
const childIsLast = i === children.length - 1;
|
||||||
stack.push({
|
stack.push({
|
||||||
ptr: child.ptr,
|
ptr: child.ptr,
|
||||||
@@ -82,7 +80,7 @@ export function printTree(fs: FileSystem, root: NodePointer, rootLabel = "/"): s
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function printDeviceTree(fs: FileSystem, deviceId: number, rootLabel?: string): string {
|
export function printDeviceTree(fs: FileSystem, deviceId: number, rootLabel?: string): string {
|
||||||
return printTree(fs, toPtr({ device_id: deviceId, inode: 0 }), rootLabel ?? `disk-${deviceId}:/`);
|
return printTree(fs, NodePointer.of(deviceId, 0), rootLabel ?? `disk-${deviceId}:/`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function printAllDeviceTrees(fs: FileSystem, deviceIds?: number[]): string {
|
export function printAllDeviceTrees(fs: FileSystem, deviceIds?: number[]): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user