Skip to content

Commit b7c39a1

Browse files
committed
Make open O_CREATE mode 0 work
In nodefs we first create the node with permissions 0 and then try to open it. The open fails because we don't have read permissions.
1 parent 1171ada commit b7c39a1

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/library_fs.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ FS.staticInit();
10761076
}
10771077
} else {
10781078
// node doesn't exist, try to create it
1079-
node = FS.mknod(path, mode, 0);
1079+
node = FS.mknod(path, mode | 0o777, 0);
10801080
created = true;
10811081
}
10821082
}
@@ -1126,6 +1126,9 @@ FS.staticInit();
11261126
if (stream.stream_ops.open) {
11271127
stream.stream_ops.open(stream);
11281128
}
1129+
if (created) {
1130+
FS.chmod(node, mode & 0o777);
1131+
}
11291132
#if expectToReceiveOnModule('logReadFiles')
11301133
if (Module['logReadFiles'] && !(flags & {{{ cDefs.O_WRONLY}}})) {
11311134
if (!(path in FS.readFiles)) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <assert.h>
4+
#include <fcntl.h>
5+
#include <string.h>
6+
#include <sys/stat.h>
7+
#include <assert.h>
8+
#include <errno.h>
9+
#include <string.h>
10+
11+
12+
#if defined(__EMSCRIPTEN__)
13+
#include <emscripten.h>
14+
#endif
15+
16+
void makedir(const char *dir) {
17+
int rtn = mkdir(dir, 0777);
18+
assert(rtn == 0);
19+
}
20+
21+
void changedir(const char *dir) {
22+
int rtn = chdir(dir);
23+
assert(rtn == 0);
24+
}
25+
26+
void setup() {
27+
#if defined(__EMSCRIPTEN__) && defined(NODEFS)
28+
makedir("working");
29+
EM_ASM(FS.mount(NODEFS, { root: '.' }, 'working'));
30+
changedir("working");
31+
#endif
32+
}
33+
34+
int main() {
35+
setup();
36+
37+
int res = open("a", O_CREAT, 0);
38+
printf("error: %s\n", strerror(errno));
39+
assert(res >= 0);
40+
struct stat st;
41+
assert(stat("a", &st) == 0);
42+
assert((st.st_mode & 0777) == 0);
43+
printf("success\n");
44+
}

test/test_core.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5900,6 +5900,16 @@ def test_fs_rename_on_existing(self, args):
59005900
self.set_setting('FORCE_FILESYSTEM')
59015901
self.do_runf('fs/test_fs_rename_on_existing.c', 'success', emcc_args=args)
59025902

5903+
@parameterized({
5904+
'': ([],),
5905+
'nodefs': (['-DNODEFS', '-lnodefs.js'],),
5906+
'noderawfs': (['-sNODERAWFS'],)
5907+
})
5908+
def test_fs_open_no_permissions(self, args):
5909+
if self.get_setting('WASMFS'):
5910+
self.set_setting('FORCE_FILESYSTEM')
5911+
self.do_runf('fs/test_fs_open_no_permissions.c', 'success', emcc_args=args)
5912+
59035913
def test_sigalrm(self):
59045914
self.do_runf('test_sigalrm.c', 'Received alarm!')
59055915
self.set_setting('EXIT_RUNTIME')

0 commit comments

Comments
 (0)