Skip to content

Commit 021e174

Browse files
authored
Opening a directory with O_CREAT should return EISDIR (#23218)
So that the behavior matches the behavior on linux. This makes the test pass on nodefs and noderawfs. This allows merging the test added in #23137 into test_fcntl_open.c
1 parent 89c946e commit 021e174

14 files changed

+22
-21
lines changed

src/library_fs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ FS.staticInit();
426426
if (FS.isLink(node.mode)) {
427427
return {{{ cDefs.ELOOP }}};
428428
} else if (FS.isDir(node.mode)) {
429-
if (FS.flagsToPermissionString(flags) !== 'r' || // opening for write
430-
(flags & {{{ cDefs.O_TRUNC }}})) { // TODO: check for O_SEARCH? (== search for dir only)
429+
if (FS.flagsToPermissionString(flags) !== 'r' // opening for write
430+
|| (flags & ({{{ cDefs.O_TRUNC }}} | {{{ cDefs.O_CREAT }}}))) { // TODO: check for O_SEARCH? (== search for dir only)
431431
return {{{ cDefs.EISDIR }}};
432432
}
433433
}

system/lib/wasmfs/syscalls.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ static __wasi_fd_t doOpen(path::ParsedParent parsed,
520520
return -EEXIST;
521521
}
522522

523-
if (child->is<Directory>() && accessMode != O_RDONLY) {
523+
if (child->is<Directory>() && (accessMode != O_RDONLY || (flags & O_CREAT))) {
524524
return -EISDIR;
525525
}
526526

test/fcntl/test_fcntl_open.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void test() {
7878
if ((flags & O_CREAT) && (flags & O_EXCL)) {
7979
assert(!success);
8080
assert(errno == EEXIST);
81-
} else if ((flags & O_TRUNC) || i != 0 /*mode != O_RDONLY*/) {
81+
} else if ((flags & O_TRUNC) || i != 0 /*mode != O_RDONLY*/ || (flags & O_CREAT)) {
8282
assert(!success);
8383
assert(errno == EISDIR);
8484
} else {

test/fcntl/test_fcntl_open.out

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ errno: 0
1919
st_mode: 0100000
2020

2121
EXISTING FOLDER 0,1
22-
success: 1
23-
errno: 0
22+
success: 0
23+
errno: 31
2424
st_mode: 040000
2525

2626
NON-EXISTING 0,1
@@ -139,8 +139,8 @@ errno: 0
139139
st_mode: 0100000
140140

141141
EXISTING FOLDER 0,9
142-
success: 1
143-
errno: 0
142+
success: 0
143+
errno: 31
144144
st_mode: 040000
145145

146146
NON-EXISTING 0,9
@@ -259,8 +259,8 @@ errno: 0
259259
st_mode: 0100000
260260

261261
EXISTING FOLDER 0,17
262-
success: 1
263-
errno: 0
262+
success: 0
263+
errno: 31
264264
st_mode: 040000
265265

266266
NON-EXISTING 0,17
@@ -379,8 +379,8 @@ errno: 0
379379
st_mode: 0100000
380380

381381
EXISTING FOLDER 0,25
382-
success: 1
383-
errno: 0
382+
success: 0
383+
errno: 31
384384
st_mode: 040000
385385

386386
NON-EXISTING 0,25
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8407
1+
8408
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8390
1+
8392
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9411
1+
9413
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8373
1+
8374
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8373
1+
8374
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8404
1+
8406
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9416
1+
9417
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8407
1+
8408
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7702
1+
7704

test/test_core.py

+1
Original file line numberDiff line numberDiff line change
@@ -5543,6 +5543,7 @@ def test_fcntl(self):
55435543
self.add_pre_run("FS.createDataFile('/', 'test', 'abcdef', true, true, false);")
55445544
self.do_run_in_out_file_test('fcntl/test_fcntl.c')
55455545

5546+
@also_with_nodefs_both
55465547
def test_fcntl_open(self):
55475548
self.do_run_in_out_file_test('fcntl/test_fcntl_open.c')
55485549

0 commit comments

Comments
 (0)