more features, flags now mostly work, added support for -R flag in ls with a custom bash method for recursive tree traversal
This commit is contained in:
+25
-15
@@ -19,7 +19,7 @@ export type FsInitArgs = {
|
||||
|
||||
export type TreeNode = {
|
||||
inode: number;
|
||||
parent?: number;
|
||||
parent: number;
|
||||
name: string;
|
||||
type: Type;
|
||||
size: number; //Size in Bytes
|
||||
@@ -110,11 +110,11 @@ export class VirtualFS {
|
||||
return typeof path === 'string' && path.startsWith('/');
|
||||
};
|
||||
|
||||
getPathByInode(inode: number): string {
|
||||
public getPathByInode(inode: number): string {
|
||||
return this._iNodeToPathString(inode);
|
||||
}
|
||||
|
||||
formatPath(path: string): string {
|
||||
public formatPath(path: string): string {
|
||||
console.log(path, 'formatPath');
|
||||
const prefix = this._iNodeToPathString(this.home);
|
||||
|
||||
@@ -123,7 +123,7 @@ export class VirtualFS {
|
||||
} else return path;
|
||||
}
|
||||
|
||||
resolvePath(path: string): TreeNode {
|
||||
public resolvePath(path: string): TreeNode {
|
||||
if (path === '/') return this.getNodeByINode(this.rootINode);
|
||||
let parsedPath: string = path;
|
||||
|
||||
@@ -144,22 +144,32 @@ export class VirtualFS {
|
||||
return Node;
|
||||
}
|
||||
|
||||
getNodeByINode(inode: number): TreeNode {
|
||||
public getNodeByINode(inode: number): TreeNode {
|
||||
const node: TreeNode | undefined = this.FsTable.get(inode);
|
||||
if (!node) throw new Error('Could not get the node, no such i node exists');
|
||||
if (!node) throw new Error(`Could not get the node, no such inode exists - ${inode}`);
|
||||
return node;
|
||||
}
|
||||
|
||||
/* private _getPathToNode(node: TreeNode): string[] {
|
||||
const path: string[] = [];
|
||||
let current = node;
|
||||
path.push(node.name);
|
||||
public recursiveTraversalPre(node: TreeNode, callback: (param: TreeNode) => void, childIndex?: number[], depthIndex?: number): void {
|
||||
if(!depthIndex) depthIndex = 0;
|
||||
if(!childIndex) childIndex = [0];
|
||||
|
||||
while (current.parent) {
|
||||
current = current.parent;
|
||||
path.unshift(current.name);
|
||||
if(node.type != Type.File && node.children[childIndex[depthIndex]]) {
|
||||
node = this.getNodeByINode(node.children[childIndex[depthIndex]]);
|
||||
depthIndex++;
|
||||
if(!childIndex[depthIndex]) childIndex[depthIndex] = 0;
|
||||
|
||||
callback(node);
|
||||
}
|
||||
else {
|
||||
node = this.getNodeByINode(node.parent);
|
||||
childIndex[depthIndex] = 0;
|
||||
depthIndex--;
|
||||
childIndex[depthIndex]++;
|
||||
}
|
||||
|
||||
return path;
|
||||
} */
|
||||
if(depthIndex < 0) return;
|
||||
|
||||
this.recursiveTraversalPre(node, callback, childIndex, depthIndex);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user