Skip to content

Commit

Permalink
feat: implement path_remove_directory and path_unlink_file system cal…
Browse files Browse the repository at this point in the history
…ls (#50)

feat: add path_remove_directory and path_unlink_file system calls

Signed-off-by: prakharagarwal1 <[email protected]>
  • Loading branch information
Prakhar-Agarwal-byte authored Nov 21, 2023
1 parent 7349f7d commit 88878d6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/fs_core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ export class Directory {
return entry;
}

get_parent_dir_for_path(path: string): Directory | null {
if (path === "") return null;
let entry: File | Directory | SyncOPFSFile = this;
let parentEntry: File | Directory | SyncOPFSFile = entry;
for (const component of path.split("/")) {
if (component === "") break;
if (component === ".") continue;
if (!(entry instanceof Directory)) {
debug.log(entry);
return null;
}
if (entry.contents[component] === undefined) {
debug.log(component);
return null;
}
parentEntry = entry;
entry = entry.contents[component];
}
return parentEntry;
}

create_entry_for_path(
path: string,
is_dir: boolean,
Expand Down
50 changes: 50 additions & 0 deletions src/fs_fd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,56 @@ export class OpenDirectory extends Fd {
0,
).ret;
}

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];
const entry = this.dir.get_entry_for_path(path);
if (entry === null) {
return wasi.ERRNO_NOENT;
}
if (entry.stat().filetype === wasi.FILETYPE_DIRECTORY) {
return wasi.ERRNO_ISDIR;
}
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) {
return wasi.ERRNO_NOENT;
}
if (
!(entry instanceof Directory) ||
entry.stat().filetype !== wasi.FILETYPE_DIRECTORY
) {
return wasi.ERRNO_NOTDIR;
}
if (Object.keys(entry.contents).length !== 0) {
return wasi.ERRNO_NOTEMPTY;
}
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 88878d6

Please sign in to comment.