Skip to content

Commit d9d60a1

Browse files
authored
Return EISDIR when trying to create a path ending in / with open (#23135)
If we open a path that in a / and we're about to create a file, instead return EISDIR.
1 parent 669e01a commit d9d60a1

File tree

5 files changed

+32
-20
lines changed

5 files changed

+32
-20
lines changed

src/library_fs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,9 +1060,11 @@ FS.staticInit();
10601060
mode = 0;
10611061
}
10621062
var node;
1063+
var isDirPath;
10631064
if (typeof path == 'object') {
10641065
node = path;
10651066
} else {
1067+
isDirPath = path.endsWith("/");
10661068
// noent_okay makes it so that if the final component of the path
10671069
// doesn't exist, lookupPath returns `node: undefined`. `path` will be
10681070
// updated to point to the target of all symlinks.
@@ -1081,6 +1083,8 @@ FS.staticInit();
10811083
if ((flags & {{{ cDefs.O_EXCL }}})) {
10821084
throw new FS.ErrnoError({{{ cDefs.EEXIST }}});
10831085
}
1086+
} else if (isDirPath) {
1087+
throw new FS.ErrnoError({{{ cDefs.EISDIR }}});
10841088
} else {
10851089
// node doesn't exist, try to create it
10861090
node = FS.mknod(path, mode, 0);

test/fs/test_enotdir.c

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

test/fs/test_enotdir.out

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fs/test_fs_enotdir.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <errno.h>
2+
#include <fcntl.h>
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include <unistd.h>
6+
#include <sys/stat.h>
7+
#include <assert.h>
8+
9+
int main() {
10+
{
11+
int src_fd = open("file", O_CREAT | O_WRONLY, 0777);
12+
assert(src_fd >= 0);
13+
assert(close(src_fd) == 0);
14+
}
15+
{
16+
assert(mkdir("file/blah", 0777) == -1);
17+
assert(errno == ENOTDIR);
18+
}
19+
{
20+
assert(open("./does-not-exist/", O_CREAT) == -1);
21+
assert(errno == EISDIR);
22+
}
23+
printf("success\n");
24+
}

test/test_core.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5756,9 +5756,11 @@ def test_fs_emptyPath(self):
57565756

57575757
@no_windows('https://github.com/emscripten-core/emscripten/issues/8882')
57585758
@crossplatform
5759-
@also_with_noderawfs
5759+
@also_with_nodefs_both
57605760
def test_fs_enotdir(self):
5761-
self.do_run_in_out_file_test('fs/test_enotdir.c')
5761+
if MACOS and '-DNODERAWFS' in self.emcc_args:
5762+
self.skipTest('BSD libc sets a different errno')
5763+
self.do_runf('fs/test_fs_enotdir.c', 'success')
57625764

57635765
@also_with_noderawfs
57645766
def test_fs_append(self):

0 commit comments

Comments
 (0)