diff --git a/src/lib/stores/bash/commands/cd.ts b/src/lib/stores/bash/commands/cd.ts index 5a77440..ba98ab2 100644 --- a/src/lib/stores/bash/commands/cd.ts +++ b/src/lib/stores/bash/commands/cd.ts @@ -25,22 +25,14 @@ export const cmd_cd = function (this: Bash, args: CommandArgs): Result { return result; } - // Change the input STRING path from relative to absolute by replacing ~ with the home directory path - - //TODO: Change that to a global function inside fs class to parse all possible path formats????? already exists, need to verify - - let resolvedPath = path.startsWith('~') - ? path.replace('~', this.getFs().pathArrayToString(this.getFs().home)) - : path; - this.getFs().pwd = this.getFs().cwd; - targetNode = this.getFs()._getNodeByPathArray(this.getFs().resolvePath(resolvedPath)); // Conversion from STRING path to ARRAY + targetNode = this.getFs().resolvePath(path); // Conversion from STRING path to TREENODE if (targetNode === null) return result; if (targetNode.type !== Type.Directory) return result; //if () return ExitCode.ERROR; // Check for read permissions on node and user - this.getFs().cwd = this.getFs().resolvePath(resolvedPath); // CD was successfull, change current dir to the verified target dir + this.getFs().cwd = targetNode.inode; // CD was successfull, change current dir to the verified target dir result.exitCode = ExitCode.SUCCESS; console.log(this.getCwd()); return result; diff --git a/src/lib/stores/bash/commands/ls.ts b/src/lib/stores/bash/commands/ls.ts index 7c9368c..6f51248 100644 --- a/src/lib/stores/bash/commands/ls.ts +++ b/src/lib/stores/bash/commands/ls.ts @@ -45,24 +45,18 @@ export const cmd_ls = function (this: Bash, args: CommandArgs): Result { //Check if args contain any nonexistent flags, if so add it to an array and check its length. if 0 no bad flags const invalidItems = args.flags.filter((flag) => !ls.flags.includes(flag)); console.log(invalidItems); + if (invalidItems.length > 0) { - this.throwError(result); //No such flag + this.throwError(result); //No such flag/s } - if (args.args.length === 0) { - const node = this.getFs()._getNodeByPathArray(this.getFs().cwd); - if (node === null) this.throwError(result); //no such path - - nodes.push(node!); - } + if (args.args.length === 0) nodes.push(this.getFs().getNodeByINode(this.getFs().cwd)); for (let i = 0; i < args.args.length; i++) { - if (args.args.length !== 0) paths.push(this.getFs().resolvePath(args.args[i])); + const node = this.getFs().resolvePath(args.args[i]); + if (node === null) this.throwError(result); //no such path (i think this will never occur as backed methods have error cases implemented - which is wrong) - const node = this.getFs()._getNodeByPathArray(paths[i]); - if (node === null) this.throwError(result); //no such path - - nodes.push(node!); + nodes.push(node); } result.exitCode = ExitCode.SUCCESS; @@ -83,17 +77,19 @@ function result_ls(this: Bash, data: any, args: CommandArgs): HTMLElement { const w: HTMLElement = document.createElement('div'); for (const node of nodes) { - if (!flagInfo.has('U') && !flagInfo.has('f')) - asciiByteQSort(node.children, flagInfo.has('r')); - const elem: HTMLElement = document.createElement('div'); const rows: string[] = []; + if (!flagInfo.has('U') && !flagInfo.has('f')) + asciiByteQSort(node.children, flagInfo.has('r')); + //TODO: Actually calculate sizes here instead of defining numbers for types of nodes const sizes = node.children.map((child) => (child.type === Type.Directory ? '4096' : '1')); const maxSizeWidth = Math.max(...sizes.map((size) => size.length)); - for (const child of node.children) { + for (const inode of node.children) { + const child: TreeNode = this.getFs().getNodeByINode(inode); + if (child.name.startsWith('.') && !(f_a || flagInfo.has('A'))) continue; const cols: LsEntry = { diff --git a/src/lib/stores/bash/fs.ts b/src/lib/stores/bash/fs.ts index e301f72..12671be 100644 --- a/src/lib/stores/bash/fs.ts +++ b/src/lib/stores/bash/fs.ts @@ -55,20 +55,19 @@ export class VirtualFS { console.log(this.cwd); console.log(this.pwd); } - + private _iNodeToPathString(inode: number): string { let components: Stack = new Stack(); let currentNode = this.FsTable.get(inode); - let path: string = ''; - if(!currentNode) throw new Error('iNode does not exist,'); + let path: string = ''; + if (!currentNode) throw new Error('iNode does not exist,'); components.push(currentNode.name); - if(!currentNode.parent) { - for(let i = 0; i < components.size(); i++) { + if (!currentNode.parent) { + for (let i = 0; i < components.size(); i++) { path += components.pop() + '/'; } - } else { this._iNodeToPathString(currentNode.parent); } @@ -76,21 +75,24 @@ export class VirtualFS { return path; } + //TODO: Make all backend methods NOT throw errors. Just return null, and let more closely connected with bash functions call throwError() so user can see the error. + // this will save a lot of ass pain later. + private _pathStringToINode(path: string): number { const normalizedPath = path.replace(/^\/+|\/+$/g, ''); - const pathComponents = normalizedPath.split('/').filter(component => component.length > 0); + const pathComponents = normalizedPath.split('/').filter((component) => component.length > 0); - if(pathComponents.length === 0) return this.rootINode; + if (pathComponents.length === 0) return this.rootINode; let currentNode = this.FsTable.get(this.rootINode); - if(!currentNode) throw new Error('iNode does not exist,'); + if (!currentNode) throw new Error('iNode does not exist,'); - for(const component of pathComponents) { + for (const component of pathComponents) { const childINode = this._findChildNodeByName(currentNode, component); - if(childINode === null) throw new Error('this child iNode does not exist,'); + if (childINode === null) throw new Error('this child iNode does not exist,'); const nextNode = this.FsTable.get(childINode); - if(!nextNode) throw new Error('iNode child does not exist,'); + if (!nextNode) throw new Error('iNode child does not exist,'); currentNode = nextNode; } @@ -99,11 +101,11 @@ export class VirtualFS { private _findChildNodeByName(node: TreeNode, name: string): number { console.log(name, node); - for(const childINode of node.children) { + for (const childINode of node.children) { const child = this.FsTable.get(childINode); - if(child && child.name === name) { + if (child && child.name === name) { return childINode; - } + } } throw new Error('could not find the specified child node'); } @@ -123,31 +125,28 @@ export class VirtualFS { } else return path; } - resolvePath(path: string): TreeNode{ - if(path === '/') return this._getNodeByINode(this.rootINode); + resolvePath(path: string): TreeNode { + if (path === '/') return this.getNodeByINode(this.rootINode); if (!this._isAbsolutePath(path)) { const trail: string = this._iNodeToPathString(this.cwd); path = trail + path; - - } - else if (path.startsWith('~')) { + } else if (path.startsWith('~')) { const trail: string = this._iNodeToPathString(this.home); - path = trail + path + path = trail + path; } console.log(path); const INode: number = this._pathStringToINode(path); - const Node: TreeNode = this._getNodeByINode(INode); + const Node: TreeNode = this.getNodeByINode(INode); return Node; } - - private _getNodeByINode(inode: number): TreeNode { + 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 i node exists'); return node; }