Skip to content

Commit

Permalink
Merge pull request #72 from bjorn3/couple_of_fixes
Browse files Browse the repository at this point in the history
Couple of fixes
  • Loading branch information
bjorn3 authored Apr 8, 2024
2 parents 04004aa + d915a01 commit aba1aa4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/fd.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint @typescript-eslint/no-unused-vars:0 */
import * as wasi from "./wasi_defs.js";

export class Fd {
export abstract class Fd {
fd_advise(offset: bigint, len: bigint, advice: number): number {
return wasi.ERRNO_NOTSUP;
return wasi.ERRNO_SUCCESS;
}
fd_allocate(offset: bigint, len: bigint): number {
return wasi.ERRNO_NOTSUP;
Expand Down
47 changes: 47 additions & 0 deletions src/fs_core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,37 @@ export class OpenFile extends Fd {
this.file = file;
}

fd_allocate(offset: bigint, len: bigint): number {
if (this.file.size > offset + len) {
// already big enough
} else {
// extend
const new_data = new Uint8Array(Number(offset + len));
new_data.set(this.file.data, 0);
this.file.data = new_data;
}
return wasi.ERRNO_SUCCESS;
}

fd_fdstat_get(): { ret: number; fdstat: wasi.Fdstat | null } {
return { ret: 0, fdstat: new wasi.Fdstat(wasi.FILETYPE_REGULAR_FILE, 0) };
}

fd_filestat_set_size(size: bigint): number {
if (this.file.size > size) {
// truncate
this.file.data = new Uint8Array(
this.file.data.buffer.slice(0, Number(size)),
);
} else {
// extend
const new_data = new Uint8Array(Number(size));
new_data.set(this.file.data, 0);
this.file.data = new_data;
}
return wasi.ERRNO_SUCCESS;
}

fd_read(
view8: Uint8Array,
iovs: Array<wasi.Iovec>,
Expand Down Expand Up @@ -147,6 +174,16 @@ export class OpenSyncOPFSFile extends Fd {
this.file = file;
}

fd_allocate(offset: bigint, len: bigint): number {
if (BigInt(this.file.handle.getSize()) > offset + len) {
// already big enough
} else {
// extend
this.file.handle.truncate(Number(offset + len));
}
return wasi.ERRNO_SUCCESS;
}

fd_fdstat_get(): { ret: number; fdstat: wasi.Fdstat | null } {
return { ret: 0, fdstat: new wasi.Fdstat(wasi.FILETYPE_REGULAR_FILE, 0) };
}
Expand All @@ -161,6 +198,11 @@ export class OpenSyncOPFSFile extends Fd {
};
}

fd_filestat_set_size(size: bigint): number {
this.file.handle.truncate(Number(size));
return wasi.ERRNO_SUCCESS;
}

fd_read(
view8: Uint8Array,
iovs: Array<wasi.Iovec>,
Expand Down Expand Up @@ -242,6 +284,11 @@ export class OpenDirectory extends Fd {
this.dir = dir;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
fd_seek(offset: bigint, whence: number): { ret: number; offset: bigint } {
return { ret: wasi.ERRNO_ISDIR, offset: 0n };
}

fd_fdstat_get(): { ret: number; fdstat: wasi.Fdstat | null } {
return { ret: 0, fdstat: new wasi.Fdstat(wasi.FILETYPE_DIRECTORY, 0) };
}
Expand Down
2 changes: 0 additions & 2 deletions test/skip.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"WASI Rust tests": {
"sched_yield": "not implemented yet",
"path_rename": "fail",
"fd_advise": "fail",
"path_exists": "fail",
"path_open_dirfd_not_dir": "fail",
"fd_filestat_set": "fail",
Expand All @@ -32,7 +31,6 @@
"nofollow_errors": "fail",
"path_open_preopen": "fail",
"fd_readdir": "fail",
"directory_seek": "fail",
"symlink_filestat": "fail",
"symlink_loop": "fail",
"interesting_paths": "fail"
Expand Down

0 comments on commit aba1aa4

Please sign in to comment.