- fixed a bug in dnTaverseTree where children would get pushed into the result in reverse order
- added an inode display to the `printTree` algorithm - removed a redundant type in *`signal.ts`* - improved `resolveInode` to now take `ShellState` instead of `FileSystem` and added an optional `abs` *`boolean`* argument to determine if the retuned path should be normalized or absolute - implemented a builtin `TreeNode` quicksort function - changed the `TreeNodeType` to a numeric enum - added a standalone `Posix` class that holds a const with character weights compliant with posix
This commit is contained in:
@@ -69,8 +69,10 @@ async function main(
|
||||
nodes.push(...treeArray.filter(node => state.filesystem.getNode(node)?.type !== TreeNodeType.File));
|
||||
}
|
||||
|
||||
for (const node of nodes) {
|
||||
const treenode: DirectoryNode = state.filesystem.getNode(node) as DirectoryNode;
|
||||
for (let i = 0; i < nodes.length; i++) {
|
||||
const treenode: DirectoryNode = state.filesystem.getNode(nodes[i]!) as DirectoryNode;
|
||||
|
||||
io.writeStdout(new TextEncoder().encode(`total ${Object.entries(treenode.children).length}\n`));
|
||||
|
||||
// Object.entries returns the touple of Record<string, number> so we get the number by doing c[1] as index 1 in the touple is the number
|
||||
// We do null assertion because we already made sure in the recursive check that all inodes are correct
|
||||
@@ -80,6 +82,7 @@ async function main(
|
||||
|
||||
if (fSet.has("l") || fSet.has("g") || fSet.has("o")) {
|
||||
longMode(io, state, children, fSet);
|
||||
io.writeStdout(new TextEncoder().encode("\n"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,13 +131,12 @@ function longMode(io: CommandIO, state: ShellState, nodes: TreeNode[], flags: Se
|
||||
);
|
||||
// Holy abomination, pad the stringified bytesize number with whitespaces so the actual length of the string matches across all rows
|
||||
// Sets the folder size to an arbitrary 4096, could change it later to smth else
|
||||
io.writeStdout(
|
||||
e.encode(
|
||||
const size =
|
||||
node.type === TreeNodeType.File
|
||||
? node.content.byteSize().toString().padStart(maxSizeWidth, " ") + " "
|
||||
: "4096".padStart(maxSizeWidth, " ") + " ",
|
||||
),
|
||||
);
|
||||
: "4096".padStart(maxSizeWidth, " ") + " ";
|
||||
|
||||
io.writeStdout(e.encode(size));
|
||||
|
||||
// for now only mtime, need a helper function for all the formatting later on
|
||||
const mtime = new Date(node.mtime);
|
||||
|
||||
@@ -45,7 +45,7 @@ export interface DirectoryNode {
|
||||
} */
|
||||
|
||||
export enum TreeNodeType {
|
||||
File = "0",
|
||||
Directory = "1",
|
||||
//Symlink = "2",
|
||||
File = 0,
|
||||
Directory = 1,
|
||||
//Symlink = 2,
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
export type Input = string;
|
||||
|
||||
//Payload optional because command that handles that signal
|
||||
// is going to know what shape it is, therefore also able to cast it;
|
||||
export type Signal = {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { getCurrentSession } from "@core/session_manager";
|
||||
import type { ShellState } from "@core/shell_state";
|
||||
import type { NodeNotFoundError, NotADirectoryError } from "../filesystem/errors";
|
||||
import type { FileSystem } from "../filesystem/filesystem";
|
||||
import { TreeNodeType, type DirectoryNode, type TreeNode } from "../filesystem/treenode";
|
||||
@@ -53,15 +55,15 @@ export function resolvePath(fs: FileSystem, cwd: number, path: string): number {
|
||||
return currentNode.inode;
|
||||
}
|
||||
|
||||
export function resolveInode(fs: FileSystem, inode: number): string {
|
||||
const node = fs.getNode(inode);
|
||||
export function resolveInode(state: ShellState, inode: number, abs: boolean = false): string {
|
||||
const node = state.filesystem.getNode(inode);
|
||||
const components: string[] = [];
|
||||
|
||||
if (node === undefined) return "";
|
||||
|
||||
let currentInode = inode;
|
||||
while (currentInode !== 0) {
|
||||
const node = fs.getNode(currentInode) as TreeNode;
|
||||
const node = state.filesystem.getNode(currentInode) as TreeNode;
|
||||
if (!node) break;
|
||||
|
||||
components.push(node.name);
|
||||
@@ -72,7 +74,19 @@ export function resolveInode(fs: FileSystem, inode: number): string {
|
||||
return components.join("");
|
||||
}
|
||||
|
||||
components.reverse();
|
||||
components.unshift("");
|
||||
return components.join("/");
|
||||
components.reverse().unshift("");
|
||||
let result = components.join("/");
|
||||
|
||||
if (abs) return result;
|
||||
|
||||
const home = resolveInode(state, getCurrentSession(state).user.home, true);
|
||||
const cwd = resolveInode(state, getCurrentSession(state).cwd, true);
|
||||
|
||||
if (result.startsWith(home)) {
|
||||
result = `~${result.slice(home.length)}`;
|
||||
} else if (result.startsWith(cwd) && cwd !== "/") {
|
||||
result = `.${result.slice(cwd.length)}`;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
import type { FileNode, TreeNode } from "@core/filesystem/treenode";
|
||||
import type { ShellState } from "@core/shell_state";
|
||||
import { Posix } from "@termos/util/posix";
|
||||
|
||||
export enum SortTreeNodeBy {
|
||||
NAME = "name",
|
||||
INODE = "inode",
|
||||
SIZE = "size",
|
||||
EXTENSION = "extension",
|
||||
TYPE = "type",
|
||||
MTIME = "mtime",
|
||||
ATIME = "atime",
|
||||
CTIME = "ctime",
|
||||
}
|
||||
|
||||
export function sortTreeNodes<T extends TreeNode | number>(
|
||||
state: ShellState,
|
||||
arr: T[],
|
||||
r: boolean = false,
|
||||
by: SortTreeNodeBy = SortTreeNodeBy.NAME,
|
||||
): T[] {
|
||||
if (arr.length === 0) return [];
|
||||
|
||||
const nodes: TreeNode[] = [];
|
||||
|
||||
if (typeof arr === "number") {
|
||||
for (const e of arr as number[]) {
|
||||
const node = state.filesystem.getNode(e);
|
||||
if (!node) continue;
|
||||
|
||||
nodes.push(node);
|
||||
}
|
||||
} else {
|
||||
nodes.push(...(arr as TreeNode[]));
|
||||
}
|
||||
|
||||
nodeQS(nodes, r, by, 0, nodes.length - 1);
|
||||
|
||||
if (typeof arr === "number") {
|
||||
return nodes.map(n => n.inode) as T[];
|
||||
} else {
|
||||
return nodes as T[];
|
||||
}
|
||||
}
|
||||
|
||||
function nodeQS(arr: TreeNode[], r: boolean, by: SortTreeNodeBy, start: number, end: number): void {
|
||||
if (end <= start) return;
|
||||
|
||||
const pivot = partitionNodeArray(arr, r, by, start, end);
|
||||
nodeQS(arr, r, by, start, pivot - 1);
|
||||
nodeQS(arr, r, by, pivot + 1, end);
|
||||
}
|
||||
|
||||
function partitionNodeArray(part: TreeNode[], r: boolean, by: SortTreeNodeBy, start: number, end: number): number {
|
||||
const pivot = part[end];
|
||||
let i = start - 1;
|
||||
|
||||
for (let j = start; j <= end; j++) {
|
||||
if (compareTreeNode(part[j]!, pivot!, by, r) < 0) {
|
||||
i++;
|
||||
const temp = part[i]!;
|
||||
part[i] = part[j]!;
|
||||
part[j] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
i++;
|
||||
const temp = part[i]!;
|
||||
part[i] = part[end]!;
|
||||
part[end] = temp;
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
function compareTreeNode(a: TreeNode, b: TreeNode, by: SortTreeNodeBy, r: boolean): number {
|
||||
switch (by) {
|
||||
case SortTreeNodeBy.NAME: {
|
||||
const A: string = a.name.startsWith(".") ? a.name.slice(1) : a.name;
|
||||
const B: string = b.name.startsWith(".") ? b.name.slice(1) : b.name;
|
||||
const min = Math.min(A.length, B.length);
|
||||
|
||||
for (let i = 0; i < min; i++) {
|
||||
const charA = Posix.GetCharWeight(A.charAt(i));
|
||||
const charB = Posix.GetCharWeight(B.charAt(i));
|
||||
|
||||
if (charA !== charB) {
|
||||
return r ? charB - charA : charA - charB;
|
||||
}
|
||||
}
|
||||
|
||||
return r ? B.length - A.length : A.length - B.length;
|
||||
}
|
||||
case SortTreeNodeBy.MTIME:
|
||||
case SortTreeNodeBy.ATIME:
|
||||
case SortTreeNodeBy.CTIME: {
|
||||
const A: number = a[by];
|
||||
const B: number = b[by];
|
||||
|
||||
return r ? A - B : B - A;
|
||||
}
|
||||
|
||||
case SortTreeNodeBy.SIZE: {
|
||||
const A: number = (a as FileNode).content.byteSize();
|
||||
const B: number = (b as FileNode).content.byteSize();
|
||||
|
||||
return r ? A - B : B - A;
|
||||
}
|
||||
|
||||
case SortTreeNodeBy.EXTENSION: {
|
||||
const A: string = a.name.split(".").pop() ?? "";
|
||||
const B: string = b.name.split(".").pop() ?? "";
|
||||
const min = Math.min(A.length, B.length);
|
||||
|
||||
for (let i = 0; i < min; i++) {
|
||||
const charA = Posix.GetCharWeight(A.charAt(i));
|
||||
const charB = Posix.GetCharWeight(B.charAt(i));
|
||||
|
||||
if (charA !== charB) {
|
||||
return r ? charB - charA : charA - charB;
|
||||
}
|
||||
}
|
||||
|
||||
return r ? B.length - A.length : A.length - B.length;
|
||||
}
|
||||
|
||||
case SortTreeNodeBy.INODE: {
|
||||
return r ? a.inode - b.inode : b.inode - a.inode;
|
||||
}
|
||||
|
||||
case SortTreeNodeBy.TYPE: {
|
||||
return r ? b.type - a.type : a.type - b.type;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,9 @@ export function dnTraverseTree(fs: FileSystem, root: number): number[] {
|
||||
result.push(node.inode);
|
||||
|
||||
if (node.type === TreeNodeType.Directory) {
|
||||
for (const value of Object.values(node.children)) {
|
||||
stack.push(value);
|
||||
const children = Object.values(node.children);
|
||||
for (let i = children.length - 1; i >= 0; i--) {
|
||||
stack.push(children[i] as number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+9
-6
@@ -33,14 +33,14 @@ const groups: Map<number, GroupInfo> = new Map();
|
||||
const user: UserInfo = {
|
||||
username: "root",
|
||||
uid: 1000,
|
||||
gid: 0,
|
||||
group: [1000],
|
||||
home: 0,
|
||||
gid: 1000, // <- PRIMARY GID
|
||||
group: [1000], // <- GROUPS ARRAY
|
||||
home: 3,
|
||||
};
|
||||
const group: GroupInfo = {
|
||||
groupname: "root",
|
||||
gid: 1000,
|
||||
members: [0],
|
||||
members: [1000], // <- UID
|
||||
};
|
||||
users.set(1000, user);
|
||||
groups.set(1000, group);
|
||||
@@ -48,10 +48,13 @@ groups.set(1000, group);
|
||||
const state: ShellState = createInitialShellState(fs, users, groups, user);
|
||||
await Shell.init(state);
|
||||
|
||||
console.log(resolveInode(Shell.Instance?.State!, 6));
|
||||
console.log(resolveInode(Shell.Instance?.State!, 6, true));
|
||||
|
||||
Shell.Instance?.on("result", payload => {
|
||||
const decoded = new TextDecoder().decode(payload.result.stdout.toArray());
|
||||
process.stdout.write(decoded + "\n");
|
||||
process.stdout.write(resolveInode(payload.state.filesystem, getCurrentSession(payload.state).cwd) + "\n> ");
|
||||
process.stdout.write(resolveInode(payload.state, getCurrentSession(payload.state).cwd) + "\n> ");
|
||||
});
|
||||
|
||||
/*
|
||||
@@ -66,7 +69,7 @@ Shell.Instance?.on("result", payload => {
|
||||
}); */
|
||||
|
||||
//console.clear();
|
||||
process.stdout.write(resolveInode(Shell.Instance?.State.filesystem!, getCurrentSession(state).cwd) + "\n> ");
|
||||
process.stdout.write(resolveInode(Shell.Instance?.State!, getCurrentSession(state).cwd) + "\n> ");
|
||||
process.stdin.on("data", async data => {
|
||||
if (Shell.Instance?.BoundPID !== null) {
|
||||
Shell.Instance?.sendInput(data.toString().trim());
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
export class Posix {
|
||||
private static readonly CharTable = {
|
||||
// Space and punctuation (32-47)
|
||||
" ": 32,
|
||||
"!": 33,
|
||||
'"': 34,
|
||||
"#": 35,
|
||||
$: 36,
|
||||
"%": 37,
|
||||
"&": 38,
|
||||
"'": 39,
|
||||
"(": 40,
|
||||
")": 41,
|
||||
"*": 42,
|
||||
"+": 43,
|
||||
",": 44,
|
||||
"-": 45,
|
||||
".": 46,
|
||||
"/": 47,
|
||||
|
||||
// Numbers 0-9 (48-57)
|
||||
"0": 48,
|
||||
"1": 49,
|
||||
"2": 50,
|
||||
"3": 51,
|
||||
"4": 52,
|
||||
"5": 53,
|
||||
"6": 54,
|
||||
"7": 55,
|
||||
"8": 56,
|
||||
"9": 57,
|
||||
|
||||
// More punctuation (58-64)
|
||||
":": 58,
|
||||
";": 59,
|
||||
"<": 60,
|
||||
"=": 61,
|
||||
">": 62,
|
||||
"?": 63,
|
||||
"@": 64,
|
||||
|
||||
// LETTERS - INTERLEAVED AaBbCc... (65-116)
|
||||
A: 65,
|
||||
a: 66,
|
||||
B: 67,
|
||||
b: 68,
|
||||
C: 69,
|
||||
c: 70,
|
||||
D: 71,
|
||||
d: 72,
|
||||
E: 73,
|
||||
e: 74,
|
||||
F: 75,
|
||||
f: 76,
|
||||
G: 77,
|
||||
g: 78,
|
||||
H: 79,
|
||||
h: 80,
|
||||
I: 81,
|
||||
i: 82,
|
||||
J: 83,
|
||||
j: 84,
|
||||
K: 85,
|
||||
k: 86,
|
||||
L: 87,
|
||||
l: 88,
|
||||
M: 89,
|
||||
m: 90,
|
||||
N: 91,
|
||||
n: 92,
|
||||
O: 93,
|
||||
o: 94,
|
||||
P: 95,
|
||||
p: 96,
|
||||
Q: 97,
|
||||
q: 98,
|
||||
R: 99,
|
||||
r: 100,
|
||||
S: 101,
|
||||
s: 102,
|
||||
T: 103,
|
||||
t: 104,
|
||||
U: 105,
|
||||
u: 106,
|
||||
V: 107,
|
||||
v: 108,
|
||||
W: 109,
|
||||
w: 110,
|
||||
X: 111,
|
||||
x: 112,
|
||||
Y: 113,
|
||||
y: 114,
|
||||
Z: 115,
|
||||
z: 116,
|
||||
|
||||
// Specials after letters (now 117-126)
|
||||
"[": 117,
|
||||
"\\": 118,
|
||||
"]": 119,
|
||||
"^": 120,
|
||||
_: 121,
|
||||
"`": 122,
|
||||
"{": 123,
|
||||
"|": 124,
|
||||
"}": 125,
|
||||
"~": 126,
|
||||
} as const;
|
||||
|
||||
public static GetCharWeight(char: string): number {
|
||||
return this.CharTable[char as keyof typeof this.CharTable];
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ export function printTree(fs: FileSystem, root: number): string {
|
||||
|
||||
const lines: string[] = [];
|
||||
|
||||
lines.push(rootNode.name);
|
||||
lines.push(rootNode.name + ` (${rootNode.inode})`);
|
||||
|
||||
type Frame = {
|
||||
inode: number;
|
||||
@@ -42,7 +42,7 @@ export function printTree(fs: FileSystem, root: number): string {
|
||||
|
||||
const name = node.name;
|
||||
const connector = frame.isLast ? "└── " : "├── ";
|
||||
lines.push(frame.prefix + connector + name);
|
||||
lines.push(frame.prefix + connector + name + ` (${node.inode})`);
|
||||
|
||||
if (node.type === TreeNodeType.Directory) {
|
||||
const childPrefix = frame.prefix + (frame.isLast ? " " : "│ ");
|
||||
|
||||
Reference in New Issue
Block a user