Read host file in wasm runtime #19964
-
I want to use emcc to compile c source file to wasm and run it on wasm runtime such as wasmedge. Here is a example: // test.c
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main() {
FILE *file = fopen("./a.txt", "r");
if (!file) {
puts(strerror(errno));
printf("cannot open file\n");
return 1;
}
while (!feof(file)) {
char c = fgetc(file);
if (c != EOF) {
putchar(c);
}
}
fclose (file);
return 0;
} Compile the C code into WebAssembly format using Emscripten: ~$ emcc test.c -o test.wasm The current directory's contents are as follows: ~$ ls
add.c a.txt example onnxruntime test.c test.wasm test_wasmedge.c
~$ cat a.txt
hello,emcc
~$ ls -al a.txt
-rwxrwxrwx 1 chris chris 11 7月 31 14:44 a.txt execution: $ wasmedge --dir .:. test.wasm
Operation not permitted
cannot open file Here is my system environment: $ uname -a
Linux chris-NbDE-WXX9 5.19.0-46-generic #47~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Jun 21 15:35:31 UTC 2 x86_64 x86_64 x86_64 GNU/Linux But I use wasi-sdk to compile it and it works.I noticed that the emcc compiler seems to be using its own virtual file system for documentation. Is this the cause of the issue? Is there an emcc compiler option to specify using only the WASI virtual file system? Alternatively, are there any other solutions available? Thanks for your time and help |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Currently filesystem support in standalone mode is limited in emscripten. Our main focus is on the Web use case where we use a virtual filesystem (edit: or Web APIs like OPFS). So atm using the wasi-sdk makes more sense for file-using code that will run off the web. |
Beta Was this translation helpful? Give feedback.
Currently filesystem support in standalone mode is limited in emscripten. Our main focus is on the Web use case where we use a virtual filesystem (edit: or Web APIs like OPFS). So atm using the wasi-sdk makes more sense for file-using code that will run off the web.