Skip to content

Commit 0a87589

Browse files
authored
WasmFS JS API: Update rmdir (#19532)
This PR updates FS.rmdir to throw errors, as per the new convention.
1 parent b713859 commit 0a87589

File tree

4 files changed

+53
-58
lines changed

4 files changed

+53
-58
lines changed

src/library_wasmfs.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,7 @@ FS.createPreloadedFile = FS_createPreloadedFile;
115115
},
116116
// TODO: mkdirTree
117117
rmdir: (path) => {
118-
return withStackSave(() => {
119-
var buffer = stringToUTF8OnStack(path);
120-
return __wasmfs_rmdir(buffer);
121-
})
118+
return FS.handleError(withStackSave(() => __wasmfs_rmdir(stringToUTF8OnStack(path))));
122119
},
123120
open: (path, flags, mode) => {
124121
flags = typeof flags == 'string' ? FS_modeStringToFlags(flags) : flags;

test/fs/test_dir.c

Lines changed: 0 additions & 49 deletions
This file was deleted.

test/fs/test_fs_js_api.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,58 @@ int main() {
111111
assert(ex.name === 'ErrnoError' && ex.errno == 8 /* EBADF */);
112112
);
113113

114+
/********** test FS.rmdir() **********/
115+
EM_ASM(
116+
// Create multiple directories
117+
FS.mkdir('/dir1');
118+
FS.mkdir('/dir2');
119+
);
120+
121+
struct stat s;
122+
stat("/dir1", &s);
123+
assert(S_ISDIR(s.st_mode));
124+
stat("/dir2", &s);
125+
assert(S_ISDIR(s.st_mode));
126+
127+
128+
EM_ASM(
129+
// Remove the multiple directories
130+
FS.rmdir('/dir1');
131+
FS.rmdir('/dir2');
132+
);
133+
134+
int err = open("/dir1", O_RDWR);
135+
assert(err);
136+
err = open("/dir2", O_RDWR);
137+
assert(err);
138+
139+
EM_ASM(
140+
// Create a directory with a file inside it
141+
FS.mkdir('/test_dir');
142+
FS.writeFile('/test_dir/file.txt', 'Hello World!');
143+
144+
// Attempt to remove the directory (should fail)
145+
var ex;
146+
try {
147+
FS.rmdir('/test_dir');
148+
} catch (err) {
149+
ex = err;
150+
}
151+
assert(ex.name === "ErrnoError" && ex.errno === 55 /* ENOTEMPTY */);
152+
153+
// Remove the file and then the directory
154+
FS.unlink('/test_dir/file.txt');
155+
FS.rmdir('/test_dir');
156+
157+
// Attempt to remove a non-existent directory (should fail)
158+
try {
159+
FS.rmdir('/non_existent_dir');
160+
} catch (err) {
161+
ex = err;
162+
}
163+
assert(ex.name === "ErrnoError" && ex.errno === 44 /* ENOEN */);
164+
);
165+
114166
/********** test FS.close() **********/
115167
EM_ASM(
116168
FS.writeFile("closetestfile", 'a=1\nb=2\n');

test/test_core.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5951,11 +5951,6 @@ def test_istream(self):
59515951
self.set_setting('LINKABLE', linkable)
59525952
self.do_core_test('test_istream.cpp')
59535953

5954-
def test_fs_dir_wasmfs(self):
5955-
self.emcc_args += ['-sWASMFS']
5956-
self.emcc_args += ['-sFORCE_FILESYSTEM']
5957-
self.do_runf(test_file('fs/test_dir.c'), 'success')
5958-
59595954
def test_fs_base(self):
59605955
self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', ['$FS'])
59615956
self.uses_es6 = True

0 commit comments

Comments
 (0)