diff --git a/src/library_fs.js b/src/library_fs.js index 68c53ad670bb5..971bfcd73c2d5 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -1053,9 +1053,11 @@ FS.staticInit(); mode = 0; } var node; + var isDirPath; if (typeof path == 'object') { node = path; } else { + isDirPath = path.endsWith("/"); // noent_okay makes it so that if the final component of the path // doesn't exist, lookupPath returns `node: undefined`. `path` will be // updated to point to the target of all symlinks. @@ -1074,6 +1076,8 @@ FS.staticInit(); if ((flags & {{{ cDefs.O_EXCL }}})) { throw new FS.ErrnoError({{{ cDefs.EEXIST }}}); } + } else if (isDirPath) { + throw new FS.ErrnoError({{{ cDefs.EISDIR }}}); } else { // node doesn't exist, try to create it node = FS.mknod(path, mode, 0); diff --git a/test/fs/test_enotdir.c b/test/fs/test_enotdir.c deleted file mode 100644 index 0f9bb5f4509ad..0000000000000 --- a/test/fs/test_enotdir.c +++ /dev/null @@ -1,17 +0,0 @@ -#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 deleted file mode 100644 index 55c2464fc101c..0000000000000 --- a/test/fs/test_enotdir.out +++ /dev/null @@ -1 +0,0 @@ -target_fd: -1, errno: 54 Not a directory diff --git a/test/fs/test_fs_enotdir.c b/test/fs/test_fs_enotdir.c new file mode 100644 index 0000000000000..e221ed8e59304 --- /dev/null +++ b/test/fs/test_fs_enotdir.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +#include +#include + +int main() { + { + int src_fd = open("file", O_CREAT | O_WRONLY, 0777); + assert(src_fd >= 0); + assert(close(src_fd) == 0); + } + { + assert(mkdir("file/blah", 0777) == -1); + assert(errno == ENOTDIR); + } + { + assert(open("./does-not-exist/", O_CREAT) == -1); + assert(errno == EISDIR); + } + printf("success\n"); +} diff --git a/test/test_core.py b/test/test_core.py index 87d04cfdc1615..37e734e75ecf2 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -5756,9 +5756,11 @@ def test_fs_emptyPath(self): @no_windows('https://github.com/emscripten-core/emscripten/issues/8882') @crossplatform - @also_with_noderawfs + @also_with_nodefs_both def test_fs_enotdir(self): - self.do_run_in_out_file_test('fs/test_enotdir.c') + if MACOS and '-DNODERAWFS' in self.emcc_args: + self.skipTest('BSD libc sets a different errno') + self.do_runf('fs/test_fs_enotdir.c', 'success') @also_with_noderawfs def test_fs_append(self):