Skip to content

Commit

Permalink
Implement path_link
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Dec 23, 2023
1 parent e23fa38 commit d846038
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 28 deletions.
23 changes: 17 additions & 6 deletions src/fd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ export abstract class Fd {
): number {
return wasi.ERRNO_NOTSUP;
}
path_link(
old_fd: number,
old_flags: number,
old_path: string,
new_path: string,
): number {
path_link(path: string, inode: Inode): number {
return wasi.ERRNO_NOTSUP;
}
path_lookup(
path: string,
dirflags: number,
): { ret: number; inode_obj: Inode | null } {
return { ret: wasi.ERRNO_NOTSUP, inode_obj: null };
}
path_open(
dirflags: number,
path: string,
Expand All @@ -134,3 +135,13 @@ export abstract class Fd {
return wasi.ERRNO_NOTSUP;
}
}

export abstract class Inode {
abstract path_open(
oflags: number,
fs_rights_base: bigint,
fd_flags: number,
): { ret: number; fd_obj: Fd | null };

abstract stat(): wasi.Filestat;
}
53 changes: 51 additions & 2 deletions src/fs_core.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { debug } from "./debug.js";
import * as wasi from "./wasi_defs.js";
import { Fd } from "./fd.js";
import { Inode } from "./inode.js";
import { Fd, Inode } from "./fd.js";

export class OpenFile extends Fd {
file: File;
Expand Down Expand Up @@ -349,6 +348,23 @@ export class OpenDirectory extends Fd {
return { ret: 0, filestat: entry.stat() };
}

path_lookup(
path_str: string,
dirflags: number,

Check failure on line 353 in src/fs_core.ts

View workflow job for this annotation

GitHub Actions / build

'dirflags' is defined but never used
): { ret: number; inode_obj: Inode | null } {
let { ret: path_ret, path } = Path.from(path_str);

Check failure on line 355 in src/fs_core.ts

View workflow job for this annotation

GitHub Actions / build

'path_ret' is never reassigned. Use 'const' instead

Check failure on line 355 in src/fs_core.ts

View workflow job for this annotation

GitHub Actions / build

'path' is never reassigned. Use 'const' instead
if (path == null) {
return { ret: path_ret, inode_obj: null };
}

let { ret, entry } = this.dir.get_entry_for_path(path);
if (entry == null) {
return { ret, inode_obj: null };
}

return { ret: wasi.ERRNO_SUCCESS, inode_obj: entry };
}

path_open(
dirflags: number,
path_str: string,
Expand Down Expand Up @@ -406,6 +422,39 @@ export class OpenDirectory extends Fd {
).ret;
}

path_link(path_str: string, inode: Inode): number {
let { ret: path_ret, path } = Path.from(path_str);
if (path_str == null) {
return path_ret;
}

if (path.is_dir) {
return wasi.ERRNO_NOENT;
}

const {
ret: parent_ret,
parent_entry,
filename,
entry,
} = this.dir.get_parent_dir_and_entry_for_path(path, true);
if (parent_entry == null || filename == null) {
return parent_ret;
}

if (entry != null) {
return wasi.ERRNO_EXIST;
}

if (inode.stat().filetype == wasi.FILETYPE_DIRECTORY) {
return wasi.ERRNO_PERM;
}

parent_entry.contents.set(filename, inode);

return wasi.ERRNO_SUCCESS;
}

path_unlink_file(path_str: string): number {
let { ret: path_ret, path } = Path.from(path_str);
if (path == null) {
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import WASI from "./wasi.js";
export { WASI };

export { Fd } from "./fd.js";
export { Inode } from "./inode.js";
export { Fd, Inode } from "./fd.js";
export {
File,
SyncOPFSFile,
Expand Down
13 changes: 0 additions & 13 deletions src/inode.ts

This file was deleted.

10 changes: 6 additions & 4 deletions src/wasi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,12 +592,14 @@ export default class WASI {
const new_path = new TextDecoder("utf-8").decode(
buffer8.slice(new_path_ptr, new_path_ptr + new_path_len),
);
return self.fds[new_fd].path_link(
old_fd,
old_flags,
const { ret, inode_obj } = self.fds[old_fd].path_lookup(
old_path,
new_path,
old_flags,
);
if (inode_obj == null) {
return ret;
}
return self.fds[new_fd].path_link(new_path, inode_obj);
} else {
return wasi.ERRNO_BADF;
}
Expand Down
1 change: 0 additions & 1 deletion test/skip.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"path_link": "fail",
"fd_fdstat_set_rights": "fail",
"readlink": "fail",
"unlink_file_trailing_slashes": "fail",
"path_symlink_trailing_slashes": "fail",
"poll_oneoff_stdio": "fail",
"dangling_symlink": "fail",
Expand Down

0 comments on commit d846038

Please sign in to comment.