Skip to content

Commit fc1e4d5

Browse files
authored
[WasmFS] Implement FS.unlink (#18315)
1 parent 9210d96 commit fc1e4d5

File tree

6 files changed

+18
-2
lines changed

6 files changed

+18
-2
lines changed

emcc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,6 +2172,7 @@ def phase_linker_setup(options, state, newargs):
21722172
settings.REQUIRED_EXPORTS += [
21732173
'_wasmfs_write_file',
21742174
'_wasmfs_mkdir',
2175+
'_wasmfs_unlink',
21752176
'_wasmfs_chdir',
21762177
'_wasmfs_symlink',
21772178
'_wasmfs_chmod',

src/library_wasmfs.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ mergeInto(LibraryManager.library, {
125125
// TODO: open
126126
// TODO: create
127127
// TODO: close
128-
// TODO: unlink
128+
unlink: (path) => {
129+
return withStackSave(() => {
130+
var buffer = allocateUTF8OnStack(path);
131+
return __wasmfs_unlink(buffer);
132+
});
133+
},
129134
chdir: (path) => {
130135
return withStackSave(() => {
131136
var buffer = allocateUTF8OnStack(path);

system/lib/wasmfs/js_api.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ int _wasmfs_mkdir(char* path, int mode) {
100100
return __syscall_mkdirat(AT_FDCWD, (intptr_t)path, mode);
101101
}
102102

103+
int _wasmfs_unlink(char* path) {
104+
return __syscall_unlinkat(AT_FDCWD, (intptr_t)path, 0);
105+
}
106+
103107
int _wasmfs_chdir(char* path) { return __syscall_chdir((intptr_t)path); }
104108

105109
int _wasmfs_symlink(char* old_path, char* new_path) {

test/test_other.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12206,10 +12206,12 @@ def test_wasmfs_getdents(self):
1220612206

1220712207
@wasmfs_all_backends
1220812208
def test_wasmfs_readfile(self):
12209+
self.set_setting('FORCE_FILESYSTEM')
1220912210
self.do_run_in_out_file_test(test_file('wasmfs/wasmfs_readfile.c'))
1221012211

1221112212
@wasmfs_all_backends
1221212213
def test_wasmfs_readfile_bigint(self):
12214+
self.set_setting('FORCE_FILESYSTEM')
1221312215
self.set_setting('WASM_BIGINT')
1221412216
self.node_args += shared.node_bigint_flags()
1221512217
self.do_run_in_out_file_test(test_file('wasmfs/wasmfs_readfile.c'))

test/wasmfs/wasmfs_readfile.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ int main() {
3333
var output = FS.readFile("/root/test");
3434
out(UTF8ArrayToString(output, 0));
3535
out("Length: " + output.byteLength);
36+
var err = FS.unlink("/root/test");
37+
out("FS.unlink: " + err);
3638
});
3739

3840
EM_ASM({
@@ -44,7 +46,8 @@ int main() {
4446

4547
EM_ASM({
4648
try {
47-
var output = FS.readFile("", {encoding : 'utf8'});
49+
// Already unlinked above, file should not exist anymore
50+
var output = FS.readFile("/root/test", {encoding : 'utf8'});
4851
} catch (err) {
4952
}
5053
});

test/wasmfs/wasmfs_readfile.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Success
22

33
Length: 8
4+
FS.unlink: 0
45
Fatal error in FS.readFile
56
Aborted(native code called abort())
67
Fatal error in FS.readFile

0 commit comments

Comments
 (0)