From a0874285561d21cfb59f8b7922f5b7ac31d3de5c Mon Sep 17 00:00:00 2001 From: prakharagarwal1 Date: Tue, 21 Nov 2023 19:16:26 +0530 Subject: [PATCH] chore: remove console logs --- src/fs_core.ts | 19 +++++++++---------- src/fs_fd.ts | 23 ++++++++++++++++------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/fs_core.ts b/src/fs_core.ts index eaa6936..48fb89b 100644 --- a/src/fs_core.ts +++ b/src/fs_core.ts @@ -122,24 +122,23 @@ export class Directory { return entry; } - get_parent_dir_for_path( - path: string, - ): File | Directory | SyncOPFSFile | null { + get_parent_dir_for_path(path: string): Directory | null { + if (path === "") return null; let entry: File | Directory | SyncOPFSFile = this; - let parentEntry = entry; + let parentEntry: File | Directory | SyncOPFSFile = entry; for (const component of path.split("/")) { - if (component == "") break; - if (component == ".") continue; + if (component === "") break; + if (component === ".") continue; if (!(entry instanceof Directory)) { + debug.log(entry); return null; } - if (entry.contents[component] != undefined) { - parentEntry = entry; - entry = entry.contents[component]; - } else { + if (entry.contents[component] === undefined) { debug.log(component); return null; } + parentEntry = entry; + entry = entry.contents[component]; } return parentEntry; } diff --git a/src/fs_fd.ts b/src/fs_fd.ts index 71d98bc..b513e31 100644 --- a/src/fs_fd.ts +++ b/src/fs_fd.ts @@ -316,6 +316,7 @@ export class OpenDirectory extends Fd { } path_unlink_file(path: string): number { + path = this.clean_path(path); const parentDirEntry = this.dir.get_parent_dir_for_path(path); const pathComponents = path.split("/"); const fileName = pathComponents[pathComponents.length - 1]; @@ -324,17 +325,18 @@ export class OpenDirectory extends Fd { return wasi.ERRNO_NOENT; } if (entry.stat().filetype === wasi.FILETYPE_DIRECTORY) { - console.log("file is actually a directory"); return wasi.ERRNO_ISDIR; } - delete (parentDirEntry as Directory).contents[fileName]; + delete parentDirEntry.contents[fileName]; return wasi.ERRNO_SUCCESS; } path_remove_directory(path: string): number { + path = this.clean_path(path); const parentDirEntry = this.dir.get_parent_dir_for_path(path); const pathComponents = path.split("/"); const fileName = pathComponents[pathComponents.length - 1]; + const entry = this.dir.get_entry_for_path(path); if (entry === null) { @@ -344,17 +346,24 @@ export class OpenDirectory extends Fd { !(entry instanceof Directory) || entry.stat().filetype !== wasi.FILETYPE_DIRECTORY ) { - console.log("file is not a directory. Entry: ", entry); return wasi.ERRNO_NOTDIR; } - const entryD = entry as Directory; - if (Object.keys(entryD.contents).length !== 0) { - console.log("directory not empty"); + if (Object.keys(entry.contents).length !== 0) { return wasi.ERRNO_NOTEMPTY; } - delete (parentDirEntry as Directory).contents[fileName]; + if (parentDirEntry.contents[fileName] === undefined) { + return wasi.ERRNO_NOENT; + } + delete parentDirEntry.contents[fileName]; return wasi.ERRNO_SUCCESS; } + + clean_path(path: string): string { + while (path.length > 0 && path[path.length - 1] === "/") { + path = path.slice(0, path.length - 1); + } + return path; + } } export class PreopenDirectory extends OpenDirectory {