Skip to content

Commit e6c3dbb

Browse files
committed
fs_mem: poor man's symlink via hardlink
1 parent a218dd8 commit e6c3dbb

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/fd.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ export abstract class Fd {
105105
path_readlink(path: string): { ret: number; data: string | null } {
106106
return { ret: wasi.ERRNO_NOTSUP, data: null };
107107
}
108+
path_symlink(old_path: string, new_path: string): number {
109+
return wasi.ERRNO_NOTSUP;
110+
}
108111
path_remove_directory(path: string): number {
109112
return wasi.ERRNO_NOTSUP;
110113
}

src/fs_mem.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,34 @@ export class OpenDirectory extends Fd {
398398
return wasi.ERRNO_SUCCESS;
399399
}
400400

401+
path_symlink(old_path_str: string, new_path_str: string): number {
402+
const { ret: new_ret, path: new_path } = Path.from(new_path_str);
403+
if (new_path == null) {
404+
return new_ret;
405+
}
406+
407+
const {
408+
ret: parent_ret,
409+
parent_entry,
410+
filename,
411+
} = this.dir.get_parent_dir_and_entry_for_path(new_path, true);
412+
if (parent_entry == null || filename == null) {
413+
return parent_ret;
414+
}
415+
416+
const { ret: old_ret, path: old_path } = Path.from(old_path_str);
417+
if (old_path == null) {
418+
return old_ret;
419+
}
420+
421+
const { ret: entry_ret, entry } = parent_entry.get_entry_for_path(old_path);
422+
if (entry == null) {
423+
return entry_ret;
424+
}
425+
426+
return this.path_link(new_path_str, entry, true);
427+
}
428+
401429
path_unlink(path_str: string): { ret: number; inode_obj: InodeMem | null } {
402430
const { ret: path_ret, path } = Path.from(path_str);
403431
if (path == null) {

src/wasi.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -813,15 +813,13 @@ export default class WASI {
813813
): number {
814814
const buffer8 = new Uint8Array(self.inst.exports.memory.buffer);
815815
if (self.fds[fd] != undefined) {
816-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
817816
const old_path = new TextDecoder("utf-8").decode(
818817
buffer8.slice(old_path_ptr, old_path_ptr + old_path_len),
819818
);
820-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
821819
const new_path = new TextDecoder("utf-8").decode(
822820
buffer8.slice(new_path_ptr, new_path_ptr + new_path_len),
823821
);
824-
return wasi.ERRNO_NOTSUP;
822+
return self.fds[fd].path_symlink(old_path, new_path);
825823
} else {
826824
return wasi.ERRNO_BADF;
827825
}

0 commit comments

Comments
 (0)