diff --git a/src/library_fs.js b/src/library_fs.js index 98e575e549fc4..c5e16ad1e92ea 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -372,6 +372,9 @@ FS.staticInit(); return 0; }, mayCreate(dir, name) { + if (!FS.isDir(dir.mode)) { + return {{{ cDefs.ENOTDIR }}}; + } try { var node = FS.lookupNode(dir, name); return {{{ cDefs.EEXIST }}}; diff --git a/test/fs/test_enotdir.c b/test/fs/test_enotdir.c new file mode 100644 index 0000000000000..0f9bb5f4509ad --- /dev/null +++ b/test/fs/test_enotdir.c @@ -0,0 +1,17 @@ +#include +#include +#include +#include +#include +#include + +int main() { + { + int src_fd = open("file", O_CREAT | O_WRONLY, 0777); + close(src_fd); + } + { + int target_fd = mkdir("file/blah", 0777); + printf("target_fd: %d, errno: %d %s\n", target_fd, errno, strerror(errno)); + } +} diff --git a/test/fs/test_enotdir.out b/test/fs/test_enotdir.out new file mode 100644 index 0000000000000..55c2464fc101c --- /dev/null +++ b/test/fs/test_enotdir.out @@ -0,0 +1 @@ +target_fd: -1, errno: 54 Not a directory diff --git a/test/test_core.py b/test/test_core.py index f3a2297e73194..fd18476c6ccea 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -5832,6 +5832,10 @@ def test_fs_write(self): def test_fs_emptyPath(self): self.do_run_in_out_file_test('fs/test_emptyPath.c') + @also_with_noderawfs + def test_fs_enotdir(self): + self.do_run_in_out_file_test('fs/test_enotdir.c') + @also_with_noderawfs def test_fs_append(self): self.do_runf('fs/test_append.c', 'success') diff --git a/test/wasmfs/wasmfs_mkdir.c b/test/wasmfs/wasmfs_mkdir.c index 174e3f9dcf618..830cdadfcd24b 100644 --- a/test/wasmfs/wasmfs_mkdir.c +++ b/test/wasmfs/wasmfs_mkdir.c @@ -81,13 +81,7 @@ int main() { // Try to make a directory with a path component that is not a directory. errno = 0; mkdir("/dev/stdout/fake-directory", 0777); - // TODO: This may have to change when access modes are implemented, depending - // on if we check access mode before file type. -#ifdef WASMFS assert(errno == ENOTDIR); -#else - assert(errno == EACCES); -#endif // Try to make a directory with a path component that does not exist. errno = 0;