Skip to content

Commit

Permalink
chore: remove console logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Prakhar-Agarwal-byte committed Nov 21, 2023
1 parent d2bc625 commit a087428
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
19 changes: 9 additions & 10 deletions src/fs_core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
23 changes: 16 additions & 7 deletions src/fs_fd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit a087428

Please sign in to comment.