Skip to content

Commit e42ca23

Browse files
authored
Trying to create a directory in a file should return ENOTDIR not EPERM (#23025)
In linux, `mkdir("some_file/dir")` returns `ENOTDIR` but Emscripten returns `EPERM`. This fixes it.
1 parent 463fe75 commit e42ca23

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

src/library_fs.js

+3
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ FS.staticInit();
372372
return 0;
373373
},
374374
mayCreate(dir, name) {
375+
if (!FS.isDir(dir.mode)) {
376+
return {{{ cDefs.ENOTDIR }}};
377+
}
375378
try {
376379
var node = FS.lookupNode(dir, name);
377380
return {{{ cDefs.EEXIST }}};

test/fs/test_enotdir.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
8+
int main() {
9+
{
10+
int src_fd = open("file", O_CREAT | O_WRONLY, 0777);
11+
close(src_fd);
12+
}
13+
{
14+
int target_fd = mkdir("file/blah", 0777);
15+
printf("target_fd: %d, errno: %d %s\n", target_fd, errno, strerror(errno));
16+
}
17+
}

test/fs/test_enotdir.out

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target_fd: -1, errno: 54 Not a directory

test/test_core.py

+4
Original file line numberDiff line numberDiff line change
@@ -5832,6 +5832,10 @@ def test_fs_write(self):
58325832
def test_fs_emptyPath(self):
58335833
self.do_run_in_out_file_test('fs/test_emptyPath.c')
58345834

5835+
@also_with_noderawfs
5836+
def test_fs_enotdir(self):
5837+
self.do_run_in_out_file_test('fs/test_enotdir.c')
5838+
58355839
@also_with_noderawfs
58365840
def test_fs_append(self):
58375841
self.do_runf('fs/test_append.c', 'success')

test/wasmfs/wasmfs_mkdir.c

-6
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,7 @@ int main() {
8181
// Try to make a directory with a path component that is not a directory.
8282
errno = 0;
8383
mkdir("/dev/stdout/fake-directory", 0777);
84-
// TODO: This may have to change when access modes are implemented, depending
85-
// on if we check access mode before file type.
86-
#ifdef WASMFS
8784
assert(errno == ENOTDIR);
88-
#else
89-
assert(errno == EACCES);
90-
#endif
9185

9286
// Try to make a directory with a path component that does not exist.
9387
errno = 0;

0 commit comments

Comments
 (0)