emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

NODEFS: soft link problems

Open ericmandel opened this issue 7 years ago • 1 comments

Under NODEFS, it appears that soft links in the local file system work if you mount "." but not "/". It's hard to show this in general, but consider the following specific case of one "real" file and four soft links, connected in the various ways one can make a soft link:

[~/tmp] eric% pwd
/Users/eric/tmp
[~/tmp] eric% ls -l stat.c *.ln*
-rw-r--r--  1 eric  staff  1430 May 31 13:47 stat.c
lrwxr-xr-x  1 eric  staff    22 May 31 13:12 stat.ln1 -> /Users/eric/tmp/stat.c 
lrwxr-xr-x  1 eric  staff    13 May 31 13:13 stat.ln2 -> ../tmp/stat.c
lrwxr-xr-x  1 eric  staff     8 May 31 13:13 stat.ln3 -> ./stat.c
lrwxr-xr-x  1 eric  staff     6 May 31 13:13 stat.ln4 -> stat.c

Now consider simple C code compiled using Emscripten that mounts "/" onto /slash and "." onto /dot and then performs a stat on each of the four files (sorry the directory is hard-wired):

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define BASEDIR "/Users/eric/tmp"

int dostat(char *fname){
  struct stat buf;
  if( stat(fname, &buf) < 0 ){
    perror("ERROR: stat");
    return -1;
  }
  printf("  %s: %d\n", fname, (int)buf.st_size);
  return 0;
}

int main(int argc, char **argv){
  char *mnt1 = "/slash";
  char *loc1 = "/";
  char *mnt2 = "/dot";
  char *loc2 = ".";
  char *basedir = BASEDIR;
  char tbuf[1024];
  if( argc > 1 ){
    basedir = argv[1];
  }
  EM_ASM(({
	var mnt = UTF8ToString($0);
	var loc = UTF8ToString($1);
	FS.mkdir(mnt);
	FS.mount(NODEFS, {root: loc}, mnt);
  }), mnt1, loc1);
  EM_ASM(({
	var mnt = UTF8ToString($0);
	var loc = UTF8ToString($1);
	FS.mkdir(mnt);
	FS.mount(NODEFS, {root: loc}, mnt);
  }), mnt2, loc2);

  printf("Emscripten-compiled C code:\n");
  printf("/dot mounts '.'\n");
  dostat("/dot/stat.c");
  dostat("/dot/stat.ln1");
  dostat("/dot/stat.ln2");
  dostat("/dot/stat.ln3");
  dostat("/dot/stat.ln4");

  printf("/slash mounts '/'\n");
  snprintf(tbuf, 1023, "/slash%s/stat.c", basedir);
  dostat(tbuf);
  snprintf(tbuf, 1023, "/slash%s/stat.ln1", basedir);
  dostat(tbuf);
  snprintf(tbuf, 1023, "/slash%s/stat.ln2", basedir);
  dostat(tbuf);
  snprintf(tbuf, 1023, "/slash%s/stat.ln3", basedir);
  dostat(tbuf);
  snprintf(tbuf, 1023, "/slash%s/stat.ln4", basedir);

  dostat(tbuf);
  return 0;
}

Finally, as a check, a --pre-js file called pre.js that performs similar calls using the native Node.js:

// must match stat.c
var basedir = "/Users/eric/tmp/";

var Module;
if( typeof Module !== "object" ){ Module = {}; }
Module["onRuntimeInitialized"] = function(){
    fs = require("fs");
    var dostat = function(fname){
        var stats;
        var size = 0;
        if( fs.existsSync(fname) ){
	    stats = fs.statSync(fname);
	    if( stats ){
		console.log("  %s: %s", fname, stats.size);
	    }
	} else {
	    console.log("  ERROR: fname %s does not exist", fname);
	}
    };
    console.log("Node.js:")
    dostat("stat.c");
    dostat("stat.ln1");
    dostat("stat.ln2");
    dostat("stat.ln3");
    dostat("stat.ln4");
    dostat(basedir + "stat.c");
    dostat(basedir + "stat.ln1");
    dostat(basedir + "stat.ln2");
    dostat(basedir + "stat.ln3");
    dostat(basedir + "stat.ln4");
    console.log(" ");
};

Compile the C code using Emscripten, etc:

emcc -O0 --pre-js pre.js stat.c

If you run the resulting a.out.js file under node, you can see that the Node calls all work, and the C calls work within the file system /dot (where "." is mounted). Actually this is a bit surprising in itself, since two of the links (stat.lnk1 and stat.lnk2) take you outside the mounted directory ...

Unfortunately, no links are not found under /slash (where "/" is mounted):

eric% node a.out.js
Node.js:
  stat.c: 1430
  stat.ln1: 1430
  stat.ln2: 1430
  stat.ln3: 1430
  stat.ln4: 1430
  /Users/eric/tmp/stat.c: 1430
  /Users/eric/tmp/stat.ln1: 1430
  /Users/eric/tmp/stat.ln2: 1430
  /Users/eric/tmp/stat.ln3: 1430
  /Users/eric/tmp/stat.ln4: 1430
 
Emscripten-compiled C code:
/dot mounts '.'
  /dot/stat.c: 1430
  /dot/stat.ln1: 1430
  /dot/stat.ln2: 1430
  /dot/stat.ln3: 1430
  /dot/stat.ln4: 1430
/slash mounts '/'
  /slash/Users/eric/tmp/stat.c: 1430
ERROR: stat: No such file or directory
ERROR: stat: No such file or directory
ERROR: stat: No such file or directory
ERROR: stat: No such file or directory

I believe the problem can be traced to the NODEFS readlink routine in library_nodefs.js, around line 218:

      readlink: function(node) {
        var path = NODEFS.realPath(node);
        try {
          path = fs.readlinkSync(path);
==>          path = NODEJS_PATH.relative(NODEJS_PATH.resolve(node.mount.opts.root), path);
          return path;
        } catch (e) {
          if (!e.code) throw e;
          throw new FS.ErrnoError(ERRNO_CODES[e.code]);
        }
      },

I don't know the original intention in using the NODEJS.PATH.relative() call, but I am guessing that it assumes that the mount point is ... what? maybe "."? Because If I change the code:

readlink:function (node) {
          var path = NODEFS.realPath(node);
          try {
            path = fs.readlinkSync(path);
	    // path = NODEJS_PATH.relative(NODEJS_PATH.resolve(node.mount.opts.root), path);
	    if( node.mount.mountpoint.charAt(0) === "/" ){
		if( path.charAt(0) === "/" ){
		    path = node.mount.mountpoint + path;
		}
	    } else {
		path = NODEJS_PATH.relative(NODEJS_PATH.resolve(node.mount.opts.root), path);
	    }
            return path;
          } catch (e) {
            if (!e.code) throw e;
            throw new FS.ErrnoError(ERRNO_CODES[e.code]);
          }

I get much better results:

[~/tmp] eric% node a.out.js
Node.js:
  stat.c: 1430
  stat.ln1: 1430
  stat.ln2: 1430
  stat.ln3: 1430
  stat.ln4: 1430
  /Users/eric/tmp/stat.c: 1430
  /Users/eric/tmp/stat.ln1: 1430
  /Users/eric/tmp/stat.ln2: 1430
  /Users/eric/tmp/stat.ln3: 1430
  /Users/eric/tmp/stat.ln4: 1430
 
Emscripten-compiled C code:
/dot mounts '.'
  /dot/stat.c: 1430
ERROR: stat: No such file or directory
ERROR: stat: No such file or directory
  /dot/stat.ln3: 1430
  /dot/stat.ln4: 1430
/slash mounts '/'
  /slash/Users/eric/tmp/stat.c: 1430
  /slash/Users/eric/tmp/stat.ln1: 1430
  /slash/Users/eric/tmp/stat.ln2: 1430
  /slash/Users/eric/tmp/stat.ln3: 1430
  /slash/Users/eric/tmp/stat.ln4: 1430

Here, we even see the two expected errors when checking the link files under /dot, where the soft links are pointing to a path outside the mounted directory.

Dealing with soft links looks pretty complicated and I suspect there are more use cases that I have not considered, so I am not making a PR from the code change above ... but we might need one. How would you like to proceed?

P.S. I attach stat.c (renamed to stat.txt) and pre.js (renamed to pre.txt).

stat.txt pre.txt

ericmandel avatar May 31 '19 18:05 ericmandel

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.

stale[bot] avatar May 30 '20 19:05 stale[bot]