- Fixed sorting - Many minor changes (need to fix the json serializer to include size in its results *the C++ one*)
46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
import type { FileSystem } from "@core/filesystem/filesystem";
|
|
import { NodePointer, NodeType, type NodeEntry } from "@core/filesystem/node";
|
|
|
|
export function dnTraverseTree(fs: FileSystem, root: NodePointer): NodePointer[] {
|
|
if (fs.getNode(root) === undefined) return [];
|
|
|
|
const result: NodePointer[] = [];
|
|
const stack: NodePointer[] = [];
|
|
stack.push(root);
|
|
while (stack.length > 0) {
|
|
const ptr = stack.pop()!;
|
|
const node = fs.getNode(ptr);
|
|
if (!node) continue;
|
|
|
|
result.push(ptr);
|
|
|
|
if (node.type === NodeType.Directory) {
|
|
const children = Array.from(node.children.values());
|
|
for (let i = children.length - 1; i >= 0; i--) {
|
|
const child = children[i]!;
|
|
const pointer = NodePointer.of(ptr.device, child);
|
|
stack.push(pointer);
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function upTraverseTree(fs: FileSystem, ptr: NodePointer): NodePointer[] {
|
|
if (fs.getNode(ptr) === undefined) return [];
|
|
|
|
const result: NodePointer[] = [ptr];
|
|
let currentPtr = ptr;
|
|
|
|
while (currentPtr.inode !== 0) {
|
|
const entries = fs.getNodeEntries(currentPtr);
|
|
if (!entries || entries.length === 0) throw new Error(`No reverse-index entry found for ${currentPtr}`);
|
|
|
|
const parent = entries[0] as NodeEntry;
|
|
currentPtr = NodePointer.of(parent.pdevice_id, parent.pinode);
|
|
result.push(currentPtr);
|
|
}
|
|
|
|
return result;
|
|
}
|