Skip to content

Commit d09310e

Browse files
committed
Return EISDIR when trying to create a path ending in / with open
If we open a path that in a / and we're about to create a file, instead return EISDIR.
1 parent 1171ada commit d09310e

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/library_fs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@ FS.staticInit();
10531053
mode = 0;
10541054
}
10551055
var node;
1056+
var isDirPath = path.endsWith("/");
10561057
if (typeof path == 'object') {
10571058
node = path;
10581059
} else {
@@ -1074,6 +1075,8 @@ FS.staticInit();
10741075
if ((flags & {{{ cDefs.O_EXCL }}})) {
10751076
throw new FS.ErrnoError({{{ cDefs.EEXIST }}});
10761077
}
1078+
} else if(isDirPath) {
1079+
throw new FS.ErrnoError({{{ cDefs.EISDIR }}});
10771080
} else {
10781081
// node doesn't exist, try to create it
10791082
node = FS.mknod(path, mode, 0);

test/fs/test_fs_eisdir.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <unistd.h>
2+
#include <sys/stat.h>
3+
#include <stdio.h>
4+
#include <fcntl.h>
5+
#include <errno.h>
6+
#include <string.h>
7+
#include <assert.h>
8+
9+
#if defined(__EMSCRIPTEN__)
10+
#include <emscripten.h>
11+
#endif
12+
13+
void makedir(const char *dir) {
14+
int rtn = mkdir(dir, 0777);
15+
assert(rtn == 0);
16+
}
17+
18+
void changedir(const char *dir) {
19+
int rtn = chdir(dir);
20+
assert(rtn == 0);
21+
}
22+
23+
void setup() {
24+
#if defined(__EMSCRIPTEN__) && defined(NODEFS)
25+
makedir("working");
26+
EM_ASM(FS.mount(NODEFS, { root: '.' }, 'working'));
27+
changedir("working");
28+
#endif
29+
}
30+
31+
int main() {
32+
setup();
33+
open("./does-not-exist/", O_CREAT);
34+
assert(errno == EISDIR);
35+
printf("success\n");
36+
}

test/test_core.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5900,6 +5900,16 @@ def test_fs_rename_on_existing(self, args):
59005900
self.set_setting('FORCE_FILESYSTEM')
59015901
self.do_runf('fs/test_fs_rename_on_existing.c', 'success', emcc_args=args)
59025902

5903+
@parameterized({
5904+
'': ([],),
5905+
'nodefs': (['-DNODEFS', '-lnodefs.js'],),
5906+
'noderawfs': (['-sNODERAWFS'],)
5907+
})
5908+
def test_fs_eisdir(self, args):
5909+
if self.get_setting('WASMFS'):
5910+
self.set_setting('FORCE_FILESYSTEM')
5911+
self.do_runf('fs/test_fs_eisdir.c', 'success', emcc_args=args)
5912+
59035913
def test_sigalrm(self):
59045914
self.do_runf('test_sigalrm.c', 'Received alarm!')
59055915
self.set_setting('EXIT_RUNTIME')

0 commit comments

Comments
 (0)