some work, i dont really know what and how much i did
This commit is contained in:
@@ -25,22 +25,14 @@ export const cmd_cd = function (this: Bash, args: CommandArgs): Result {
|
|||||||
return 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;
|
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 === null) return result;
|
||||||
if (targetNode.type !== Type.Directory) return result;
|
if (targetNode.type !== Type.Directory) return result;
|
||||||
//if () return ExitCode.ERROR; // Check for read permissions on node and user
|
//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;
|
result.exitCode = ExitCode.SUCCESS;
|
||||||
console.log(this.getCwd());
|
console.log(this.getCwd());
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -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
|
//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));
|
const invalidItems = args.flags.filter((flag) => !ls.flags.includes(flag));
|
||||||
console.log(invalidItems);
|
console.log(invalidItems);
|
||||||
|
|
||||||
if (invalidItems.length > 0) {
|
if (invalidItems.length > 0) {
|
||||||
this.throwError(result); //No such flag
|
this.throwError(result); //No such flag/s
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.args.length === 0) {
|
if (args.args.length === 0) nodes.push(this.getFs().getNodeByINode(this.getFs().cwd));
|
||||||
const node = this.getFs()._getNodeByPathArray(this.getFs().cwd);
|
|
||||||
if (node === null) this.throwError(result); //no such path
|
|
||||||
|
|
||||||
nodes.push(node!);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = 0; i < args.args.length; i++) {
|
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]);
|
nodes.push(node);
|
||||||
if (node === null) this.throwError(result); //no such path
|
|
||||||
|
|
||||||
nodes.push(node!);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result.exitCode = ExitCode.SUCCESS;
|
result.exitCode = ExitCode.SUCCESS;
|
||||||
@@ -83,17 +77,19 @@ function result_ls(this: Bash, data: any, args: CommandArgs): HTMLElement {
|
|||||||
const w: HTMLElement = document.createElement('div');
|
const w: HTMLElement = document.createElement('div');
|
||||||
|
|
||||||
for (const node of nodes) {
|
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 elem: HTMLElement = document.createElement('div');
|
||||||
const rows: string[] = [];
|
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
|
//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 sizes = node.children.map((child) => (child.type === Type.Directory ? '4096' : '1'));
|
||||||
const maxSizeWidth = Math.max(...sizes.map((size) => size.length));
|
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;
|
if (child.name.startsWith('.') && !(f_a || flagInfo.has('A'))) continue;
|
||||||
|
|
||||||
const cols: LsEntry = {
|
const cols: LsEntry = {
|
||||||
|
|||||||
@@ -55,20 +55,19 @@ export class VirtualFS {
|
|||||||
console.log(this.cwd);
|
console.log(this.cwd);
|
||||||
console.log(this.pwd);
|
console.log(this.pwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
private _iNodeToPathString(inode: number): string {
|
private _iNodeToPathString(inode: number): string {
|
||||||
let components: Stack<string> = new Stack<string>();
|
let components: Stack<string> = new Stack<string>();
|
||||||
let currentNode = this.FsTable.get(inode);
|
let currentNode = this.FsTable.get(inode);
|
||||||
let path: string = '';
|
let path: string = '';
|
||||||
if(!currentNode) throw new Error('iNode does not exist,');
|
if (!currentNode) throw new Error('iNode does not exist,');
|
||||||
|
|
||||||
components.push(currentNode.name);
|
components.push(currentNode.name);
|
||||||
|
|
||||||
if(!currentNode.parent) {
|
if (!currentNode.parent) {
|
||||||
for(let i = 0; i < components.size(); i++) {
|
for (let i = 0; i < components.size(); i++) {
|
||||||
path += components.pop() + '/';
|
path += components.pop() + '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
this._iNodeToPathString(currentNode.parent);
|
this._iNodeToPathString(currentNode.parent);
|
||||||
}
|
}
|
||||||
@@ -76,21 +75,24 @@ export class VirtualFS {
|
|||||||
return path;
|
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 {
|
private _pathStringToINode(path: string): number {
|
||||||
const normalizedPath = path.replace(/^\/+|\/+$/g, '');
|
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);
|
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);
|
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);
|
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;
|
currentNode = nextNode;
|
||||||
}
|
}
|
||||||
@@ -99,11 +101,11 @@ export class VirtualFS {
|
|||||||
|
|
||||||
private _findChildNodeByName(node: TreeNode, name: string): number {
|
private _findChildNodeByName(node: TreeNode, name: string): number {
|
||||||
console.log(name, node);
|
console.log(name, node);
|
||||||
for(const childINode of node.children) {
|
for (const childINode of node.children) {
|
||||||
const child = this.FsTable.get(childINode);
|
const child = this.FsTable.get(childINode);
|
||||||
if(child && child.name === name) {
|
if (child && child.name === name) {
|
||||||
return childINode;
|
return childINode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new Error('could not find the specified child node');
|
throw new Error('could not find the specified child node');
|
||||||
}
|
}
|
||||||
@@ -123,31 +125,28 @@ export class VirtualFS {
|
|||||||
} else return path;
|
} else return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
resolvePath(path: string): TreeNode{
|
resolvePath(path: string): TreeNode {
|
||||||
if(path === '/') return this._getNodeByINode(this.rootINode);
|
if (path === '/') return this.getNodeByINode(this.rootINode);
|
||||||
|
|
||||||
if (!this._isAbsolutePath(path)) {
|
if (!this._isAbsolutePath(path)) {
|
||||||
const trail: string = this._iNodeToPathString(this.cwd);
|
const trail: string = this._iNodeToPathString(this.cwd);
|
||||||
path = trail + path;
|
path = trail + path;
|
||||||
|
} else if (path.startsWith('~')) {
|
||||||
}
|
|
||||||
else if (path.startsWith('~')) {
|
|
||||||
const trail: string = this._iNodeToPathString(this.home);
|
const trail: string = this._iNodeToPathString(this.home);
|
||||||
path = trail + path
|
path = trail + path;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(path);
|
console.log(path);
|
||||||
|
|
||||||
const INode: number = this._pathStringToINode(path);
|
const INode: number = this._pathStringToINode(path);
|
||||||
const Node: TreeNode = this._getNodeByINode(INode);
|
const Node: TreeNode = this.getNodeByINode(INode);
|
||||||
|
|
||||||
return Node;
|
return Node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private _getNodeByINode(inode: number): TreeNode {
|
getNodeByINode(inode: number): TreeNode {
|
||||||
const node: TreeNode | undefined = this.FsTable.get(inode);
|
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;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user