Skip to content

Commit 5db9a87

Browse files
authored
Implement open() in library_wasmfs.js (#18919)
Implemented FS.open() in library_wasmfs.js Added tests to test FS.open() with different flags.
1 parent 999cca3 commit 5db9a87

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

emcc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,6 +2329,7 @@ def phase_linker_setup(options, state, newargs):
23292329
# JS API directly.
23302330
settings.REQUIRED_EXPORTS += [
23312331
'_wasmfs_write_file',
2332+
'_wasmfs_open',
23322333
'_wasmfs_mkdir',
23332334
'_wasmfs_unlink',
23342335
'_wasmfs_chdir',

src/library_wasmfs.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ mergeInto(LibraryManager.library, {
6161
if (canWrite) mode |= {{{ cDefine('S_IWUGO') }}};
6262
return mode;
6363
},
64+
modeStringToFlags: (str) => {
65+
var flags = FS.flagModes[str];
66+
if (typeof flags == 'undefined') {
67+
throw new Error('Unknown file open mode: ' + str);
68+
}
69+
return flags;
70+
},
71+
flagModes: {
72+
// copied directly from library_fs.js
73+
// Extra quotes used here on the keys to this object otherwise jsifier will
74+
// erase them in the process of reading and then writing the JS library
75+
// code.
76+
'"r"': {{{ cDefine('O_RDONLY') }}},
77+
'"r+"': {{{ cDefine('O_RDWR') }}},
78+
'"w"': {{{ cDefine('O_TRUNC') }}} | {{{ cDefine('O_CREAT') }}} | {{{ cDefine('O_WRONLY') }}},
79+
'"w+"': {{{ cDefine('O_TRUNC') }}} | {{{ cDefine('O_CREAT') }}} | {{{ cDefine('O_RDWR') }}},
80+
'"a"': {{{ cDefine('O_APPEND') }}} | {{{ cDefine('O_CREAT') }}} | {{{ cDefine('O_WRONLY') }}},
81+
'"a+"': {{{ cDefine('O_APPEND') }}} | {{{ cDefine('O_CREAT') }}} | {{{ cDefine('O_RDWR') }}},
82+
},
6483
createDataFile: (parent, name, data, canRead, canWrite, canOwn) => {
6584
// Data files must be cached until the file system itself has been initialized.
6685
var mode = FS.getMode(canRead, canWrite);
@@ -122,6 +141,14 @@ mergeInto(LibraryManager.library, {
122141
// TODO: mkdirTree
123142
// TDOO: rmdir
124143
// TODO: open
144+
open: (path, flags, mode) => {
145+
flags = typeof flags == 'string' ? FS.modeStringToFlags(flags) : flags;
146+
mode = typeof mode == 'undefined' ? 438 /* 0666 */ : mode;
147+
return withStackSave(() => {
148+
var buffer = allocateUTF8OnStack(path);
149+
return __wasmfs_open({{{ to64('buffer') }}}, flags, mode);
150+
})
151+
},
125152
// TODO: create
126153
// TODO: close
127154
unlink: (path) => {

system/lib/wasmfs/js_api.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ int _wasmfs_mkdir(char* path, int mode) {
100100
return __syscall_mkdirat(AT_FDCWD, (intptr_t)path, mode);
101101
}
102102

103+
int _wasmfs_open(char* path, int flags, mode_t mode) {
104+
return __syscall_openat(AT_FDCWD, (intptr_t)path, flags, mode);
105+
}
106+
103107
int _wasmfs_unlink(char* path) {
104108
return __syscall_unlinkat(AT_FDCWD, (intptr_t)path, 0);
105109
}

test/fs/test_open_wasmfs.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2023 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#include <assert.h>
9+
#include <emscripten/emscripten.h>
10+
#include <stdio.h>
11+
12+
13+
int main() {
14+
EM_ASM(
15+
FS.writeFile('testfile', 'a=1\nb=2\n');
16+
var readStream = FS.open('testfile', 'r');
17+
assert(readStream >= 0);
18+
19+
var writeStream = FS.open('testfile', 'w');
20+
assert(writeStream >= 0);
21+
22+
var writePlusStream = FS.open('testfile', 'w+');
23+
assert(writePlusStream >= 0);
24+
25+
var appendStream = FS.open('testfile', 'a');
26+
assert(appendStream >= 0);
27+
28+
var notFound = FS.open('filenothere', 'r');
29+
assert(notFound < 0);
30+
31+
var createFileNotHere = FS.open('filenothere', 'w+');
32+
assert(createFileNotHere >= 0);
33+
);
34+
puts("success");
35+
36+
37+
return 0;
38+
}

test/test_core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6037,13 +6037,14 @@ def test_fs_trackingdelegate(self):
60376037
self.do_run_in_out_file_test('fs/test_trackingdelegate.c')
60386038

60396039
@also_with_noderawfs
6040+
@also_with_wasmfs_js
60406041
def test_fs_writeFile(self):
60416042
self.do_run_in_out_file_test('fs/test_writeFile.cpp')
60426043

6043-
def test_fs_writeFile_wasmfs(self):
6044+
def test_fs_open_wasmfs(self):
60446045
self.emcc_args += ['-sWASMFS']
60456046
self.emcc_args += ['-sFORCE_FILESYSTEM']
6046-
self.do_run_in_out_file_test('fs/test_writeFile.cpp')
6047+
self.do_runf(test_file('fs/test_open_wasmfs.c'), 'success')
60476048

60486049
def test_fs_write(self):
60496050
self.do_run_in_out_file_test('fs/test_write.cpp')

0 commit comments

Comments
 (0)