Skip to content

native filesystem is not accessible without mounting on node.js #5237

Closed

Description

@evanw

I'm trying to compile a command-line utility for cross-platform distribution but the compiled output can't access any files. Here's a simple repro:

$ cat > hello.c
#include <assert.h>
#include <stdio.h>

int main() {
  FILE *file = fopen("hello.c", "rb");
  if (!file) {
    puts("could not open file");
    return 1;
  }
  puts("could open file");
  return 0;
}
$ cc hello.c -o hello
$ ./hello
could open file
$ source emsdk/emsdk_env.sh
$ emcc --version
emcc (Emscripten gcc/clang-like replacement) 1.37.12 (commit 9330aeb094dc88d9eb165cbd63c720229dec205f)
Copyright (C) 2014 the Emscripten authors (see AUTHORS.txt)
This is free and open source software under the MIT license.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ emcc hello.c -o hello.js
$ node hello.js
could not open file

I tried -s FORCE_FILESYSTEM=1 as this page suggests but it didn't do anything. It looks like NODEFS is what I want. How do I enable it? I tried -s NODEFS=1 but that threw an error.

Activity

  1. evanw commented on May 23, 2017

    @evanw
    ContributorAuthor

    Update: I got it to do something by doing what test_nodefs_rw.c does:

    EM_ASM(
      FS.mkdir('/hack');
      FS.mount(NODEFS, { root: '/' }, '/hack');
    );

    But that's undesirable because I don't want to have to add this the source code of the 3rd-party tool I'm using and because it's unreasonable for everyone to have to either prefix their paths with /hack or to heavily modify the tool to manually substitute /hack before every path-specific syscall.

  2. kripken commented on May 23, 2017

    @kripken
    Member

    I agree this could be much improved. There are some technical limitations that prevent mounting the root, see #2040, which we should fix. And I think that's a good idea to add a flag that mounts it for you, so you don't need to change source code for this to work.

  3. bzz commented on Aug 18, 2017

    @bzz

    Would be nice to have a flag indeed.

  4. changed the title [-]fopen is broken[/-] [+]native filesystem is not accessible without mounting on node.js[/+] on Aug 19, 2017
  5. IngwiePhoenix commented on Feb 12, 2019

    @IngwiePhoenix

    Has this been resolved? Throwing a EM_ASM statement in just to get file-access working seems pretty tedious in a way. I mean, it is not hard - but not what I want either. Basically, I want to tell emscripten to allow me to use the standard POSIX api to read files, list directories and the like. The program I am compiling actually supports self-testing by running through a list of unit tests. But I can not pass that to the program, since the path is not available unfortunately...

    So, is there a flag yet?

  6. kripken commented on Feb 12, 2019

    @kripken
    Member

    I think the NODERAWFS flag helps here, see settings.js.

  7. kkirby commented on Feb 25, 2019

    @kkirby

    @kripken Is correct. That worked for me.

  8. stale commented on Feb 26, 2020

    @stale

    This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions