Skip to content

WasmFS: Current directory ignores custom backend #18112

Open

Description

@mere-human

getcwd()returns empty string for directories that use custom backends.

Version of emscripten/emsdk:

emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.25-git (8647224628dfc10dec6e5c6f3621d625a5f16eda)
clang version 16.0.0 (https://github.com/llvm/llvm-project 2234f582c91e8511443f3e71b6e06abe3e47db4b)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/user/src/emsdk/upstream/bin

Failing code:

#include <cassert>
#include <dirent.h>
#include <emscripten/wasmfs.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cerrno>

int main() {
  const char *dir_path = "/subdir";

#ifdef WASMFS
  const char *url = "http://localhost:8080";
  printf("url: %s\n", url);
  backend_t backend = wasmfs_create_fetch_backend(url);
  assert(backend);
  int res = wasmfs_create_directory(dir_path, 0777, backend);
  if (errno) perror("wasmfs_create_directory");
  assert(errno == 0);
  assert(res == 0);
#else
  int res = mkdir(dir_path, 0777);
  if (errno) perror("mkdir");
  assert(errno == 0);
  assert(res == 0);
#endif

  char buf[100];
  assert(getcwd(buf, sizeof(buf)));
  printf("cwd: %s\n", buf);

  printf("chdir: %s\n", dir_path);
  chdir(dir_path);

  assert(getcwd(buf, sizeof(buf)));
  printf("cwd: %s\n", buf);

  return 0;
}

Full link command and output with -v appended:

EXE=common
rm -rf build
mkdir -p build
FLAG_WASMFS="-DWASMFS -sWASMFS=1"
em++ -v -g $(echo $FLAG_WASMFS) -pthread -sPTHREAD_POOL_SIZE=2 -std=c++11 main.cpp -o build/$EXE.html
ret=$?
if [ $ret -eq 0 ]; then
    echo "build ok"
    emrun build/$EXE.html --browser chrome --port 8080
fi;

Build output:

Click to expand
"/Users/user/src/emsdk/upstream/bin/clang" --version
"/Users/user/src/emsdk/upstream/bin/clang++" -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -D__EMSCRIPTEN_SHARED_MEMORY__=1 -DEMSCRIPTEN -I/Users/user/src/emsdk/upstream/emscripten/cache/sysroot/include/SDL --sysroot=/Users/user/src/emsdk/upstream/emscripten/cache/sysroot -Xclang -iwithsysroot/include/compat -v -g3 -DWASMFS -pthread -std=c++11 -matomics -mbulk-memory main.cpp -c -o /var/folders/c_/szdjrjm12g5_9vh8ybcndhmm0000gp/T/emscripten_temp_83imqb2s/main_0.o
clang version 16.0.0 (https://github.com/llvm/llvm-project 2234f582c91e8511443f3e71b6e06abe3e47db4b)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/user/src/emsdk/upstream/bin
(in-process)
"/Users/user/src/emsdk/upstream/bin/clang-16" -cc1 -triple wasm32-unknown-emscripten -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -main-file-name main.cpp -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-feature +atomics -target-feature +bulk-memory -target-feature +mutable-globals -target-feature +sign-ext -target-cpu generic -target-feature +atomics -target-feature +bulk-memory -mllvm -treat-scalable-fixed-error-as-warning -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -v -fcoverage-compilation-dir=/Users/user/src/emdemo/common -resource-dir /Users/user/src/emsdk/upstream/lib/clang/16.0.0 -D __EMSCRIPTEN_SHARED_MEMORY__=1 -D EMSCRIPTEN -I /Users/user/src/emsdk/upstream/emscripten/cache/sysroot/include/SDL -D WASMFS -isysroot /Users/user/src/emsdk/upstream/emscripten/cache/sysroot -internal-isystem /Users/user/src/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1 -internal-isystem /Users/user/src/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1 -internal-isystem /Users/user/src/emsdk/upstream/lib/clang/16.0.0/include -internal-isystem /Users/user/src/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten -internal-isystem /Users/user/src/emsdk/upstream/emscripten/cache/sysroot/include -std=c++11 -fdeprecated-macro -fdebug-compilation-dir=/Users/user/src/emdemo/common -ferror-limit 19 -fvisibility=default -pthread -fgnuc-version=4.2.1 -fcxx-exceptions -fignore-exceptions -fexceptions -fcolor-diagnostics -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /var/folders/c_/szdjrjm12g5_9vh8ybcndhmm0000gp/T/emscripten_temp_83imqb2s/main_0.o -x c++ main.cpp
clang -cc1 version 16.0.0 based upon LLVM 16.0.0git default target x86_64-apple-darwin21.6.0
ignoring nonexistent directory "/Users/user/src/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1"
ignoring nonexistent directory "/Users/user/src/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
/Users/user/src/emsdk/upstream/emscripten/cache/sysroot/include/SDL
/Users/user/src/emsdk/upstream/emscripten/cache/sysroot/include/compat
/Users/user/src/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1
/Users/user/src/emsdk/upstream/lib/clang/16.0.0/include
/Users/user/src/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
"/Users/user/src/emsdk/upstream/bin/wasm-ld" -o build/common.wasm /var/folders/c_/szdjrjm12g5_9vh8ybcndhmm0000gp/T/emscripten_temp_83imqb2s/main_0.o -L/Users/user/src/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten /Users/user/src/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/crtbegin.o --whole-archive -lwasmfs-mt-debug --no-whole-archive -lGL-mt -lal -lhtml5 -lstubs-debug -lnoexit -lc-mt-debug -ldlmalloc-mt -lcompiler_rt-mt -lc++-mt-noexcept -lc++abi-debug-mt-noexcept -lsockets-mt -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --import-memory --shared-memory --export-if-defined=main --export-if-defined=_emscripten_thread_init --export-if-defined=_emscripten_thread_exit --export-if-defined=_emscripten_thread_crashed --export-if-defined=_emscripten_tls_init --export-if-defined=pthread_self --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=__main_argc_argv --export-if-defined=fflush --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_init --export=_wasmfs_read_file --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=__errno_location --export=emscripten_dispatch_to_thread_ --export=_emscripten_thread_free_data --export=emscripten_main_browser_thread_id --export=emscripten_main_thread_process_queued_calls --export=emscripten_run_in_main_runtime_thread_js --export=emscripten_stack_set_limits --export=emscripten_proxy_finish --export=__get_temp_ret --export=__set_temp_ret --export=malloc --export=free --export-table -z stack-size=5242880 --initial-memory=16777216 --no-entry --max-memory=16777216 --global-base=1024
"/Users/user/src/emsdk/upstream/bin/wasm-emscripten-finalize" -g --dyncalls-i64 --pass-arg=legalize-js-interface-exported-helpers --dwarf build/common.wasm -o build/common.wasm --detect-features
"/Users/user/src/emsdk/node/14.18.2_64bit/bin/node" /Users/user/src/emsdk/upstream/emscripten/src/compiler.js /var/folders/c_/szdjrjm12g5_9vh8ybcndhmm0000gp/T/tmpwpzuw4xu.json
"/Users/user/src/emsdk/upstream/bin/llvm-objcopy" build/common.wasm build/common.wasm --remove-section=producers
"/Users/user/src/emsdk/node/14.18.2_64bit/bin/node" /Users/user/src/emsdk/upstream/emscripten/tools/preprocessor.js /var/folders/c_/szdjrjm12g5_9vh8ybcndhmm0000gp/T/emscripten_temp_83imqb2s/settings.js worker.js --expandMacros
"/Users/user/src/emsdk/node/14.18.2_64bit/bin/node" /Users/user/src/emsdk/upstream/emscripten/tools/preprocessor.js /var/folders/c_/szdjrjm12g5_9vh8ybcndhmm0000gp/T/emscripten_temp_83imqb2s/settings.js shell.html
build ok
objc[19348]: Class WebSwapCGLLayer is implemented in both /System/Library/Frameworks/WebKit.framework/Versions/A/Frameworks/WebCore.framework/Versions/A/Frameworks/libANGLE-shared.dylib (0x7ffb53be4ec8) and /Applications/Google Chrome.app/Contents/Frameworks/Google Chrome Framework.framework/Versions/106.0.5249.119/Libraries/libGLESv2.dylib (0x116d8a668). One of the two will be used. Which one is undefined.

Program output:

url: http://localhost:8080
cwd: /
chdir: /subdir
cwd: /

Expected output: (when building without WASMFS):

cwd: /
chdir: /subdir
cwd: /subdir

Activity

  1. mere-human commented on Oct 28, 2022

    @mere-human
    ContributorAuthor

    Details:
    When calling wasmfs_create_directory() it in turn calls mountChild() but not the insertDirectory().

    bool mounted = lockedParent.mountChild(childName, created);

    Which leads to a child entry being added to the dcache.
    if (kind == DCacheKind::Mount || !getDir()->maintainsFileIdentity()) {

    Then this entry is not found via getcwd() because MemoryDirectory::maintainsFileIdentity() returns true which skips the dcache.
    std::string Directory::Handle::getName(std::shared_ptr<File> file) {

  2. added this to the WasmFS feature parity milestone on Oct 28, 2022
  3. fredericrous commented on Apr 20, 2024

    @fredericrous

    👋 I would like to kindly bring attention to this issue as it is blocking wasm-git to persist to opfs.
    I downloaded emscripten and was able to build it but I don't know what to do from there 😂😅

  4. tlively commented on Apr 22, 2024

    @tlively
    Member

    Hmm, the reference to MemoryDirectory::maintainsFileIdentity() above suggests that this would only be an issue when using the case-insensitive virtual backend, i.e. wasmfs_create_icase_backend. @fredericrous, are you using that virtual backend?

  5. fredericrous commented on Apr 23, 2024

    @fredericrous

    few months ago when I tried to test and I read emscripten code, I was under the impression that even with the correct compilation parameters, the opfs backend was never actually called. The virtual backend was always selected. @petersalomonsen would you mind helping with your reproduction workflow? 🙏

  6. petersalomonsen commented on Apr 24, 2024

    @petersalomonsen
    Contributor

    few months ago when I tried to test and I read emscripten code, I was under the impression that even with the correct compilation parameters, the opfs backend was never actually called. The virtual backend was always selected. @petersalomonsen would you mind helping with your reproduction workflow? 🙏

    I'm not sure if I'm using the API the correct way here. With the FS.mount, I'm not able to write the file, and if removing the FS.mount I'm able to write it, and list the directory contents, but not read the file.

    I'm compiling with -sWASMFS and -lopfs.js.

    FS.mkdir(`/test`);
    FS.mount(lg.OPFS, {}, `/test`);
    FS.writeFile('/test/test.txt', "Hello");
    console.log(FS.readdir('/test'));
    console.log(FS.readFile('/test'));
    

    So not getting very basic things to work, but maybe I'm using it the wrong way?

  7. tlively commented on Apr 24, 2024

    @tlively
    Member

    One problem is that you're passing a directory to the FS.readFile call there. Another is that you shouldn't need the lg. before OPFS, although perhaps you're putting the OPFS object there yourself.

    This sample program works for me:

    #include <emscripten/wasmfs.h>
    #include <emscripten/emscripten.h>
    
    int main() {
      EM_ASM({
          FS.mkdir('/test');
          FS.mount(OPFS, {}, '/test');
          FS.writeFile('/test/test.txt', "Hello");
          console.log(FS.readdir('/test'));
          console.log(new TextDecoder().decode(FS.readFile('/test/test.txt')));
        });
    }
    

    built with em++ test.cpp -o test.html -pthread -sWASMFS -lopfs.js -sFORCE_FILESYSTEM -sPROXY_TO_PTHREAD

  8. petersalomonsen commented on Apr 25, 2024

    @petersalomonsen
    Contributor

    Thanks, and sorry, the readFile was my mistake. Is it so that threads (and then SharedArrayBuffer) are required for WASMFS to work?

  9. tlively commented on Apr 25, 2024

    @tlively
    Member

    Yes, the OPFS backend in particular requires either threads or ASYNCIFY=2 (i.e. JSPI, which is currently under an origin trial in Chrome) because it needs to provide a synchronous interface to the underlying asynchronous APIs for opening files and doing other operations.

  10. guusw commented on Feb 11, 2025

    @guusw
    Contributor

    It's very simple to reproduce this, as mounting anything will cause it's name to not be retrieved correctly

    auto fetchBackend = wasmfs_create_fetch_backend(baseUrl.c_str());
    int r = wasmfs_create_directory("/tfs", 0777, fetchBackend);
    
    chdir("/tfs");
    
    char buf[1024];
    getcwd(buf, sizeof(buf));
    
    // buf will be "/" (wrong)
    // If you use any sub-paths it will look like
    // chdir("/tfs/subdir")
    // getcwd => "//subdir"

    Inside getcwd, the top-most MemoryDirectory will try to retrieve the name of the mounted directory since it's gives maintainsFileIdentity() == true

    however the name is only available inside the MemoryDirectory's dcache

    This should be very simple to fix, but I'm not sure what's the best way, maybe something like this in file.cpp

    diff --git a/system/lib/wasmfs/file.cpp b/system/lib/wasmfs/file.cpp
    index c16af931c..5a73ad25b 100644
    --- a/system/lib/wasmfs/file.cpp
    +++ b/system/lib/wasmfs/file.cpp
    @@ -208,7 +208,10 @@ int Directory::Handle::removeChild(const std::string& name) {
    
     std::string Directory::Handle::getName(std::shared_ptr<File> file) {
       if (getDir()->maintainsFileIdentity()) {
    -    return getDir()->getName(file);
    +    auto string = getDir()->getName(file);
    +    if (!string.empty()) {
    +      return string;
    +    }
       }
       auto& dcache = getDir()->dcache;
       for (auto it = dcache.begin(); it != dcache.end(); ++it) {

    This seems to work but I have no idea how to test the stability of this, or if it's maybe better to add the directory to the actual parent when mounting (would have to be removed when unmounting)

  11. typedrat commented on Oct 1, 2026

    @typedrat
    Contributor

    This still reproduces on 6.0.9. As the wasmfs_mounted_cwd.c test in #23756 already shows, it needs nothing but the memory backend: any directory created with wasmfs_create_directory() under the (memory) root loses its name in getcwd(). The test case below is the same idea with a subdirectory added, which shows the //sub form too.

    Version of emscripten/emsdk:

    emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 6.0.9-git
    clang version 22.1.8
    Target: wasm32-unknown-emscripten
    Thread model: posix
    InstalledDir: /nix/store/a0h9ym1ai9yawkmymx2wp0vnh64hsv0r-clang-22.1.8/bin
    

    This is the 6.0.9 release tag as packaged by nixpkgs. nixpkgs builds it against its own LLVM 22.1.8, so there is no commit hash. Node.js v24.20.0, x86_64 Linux.

    Test case (getcwd_mount.c):

    #include <emscripten/wasmfs.h>
    #include <stdio.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    static void show(const char *dir) {
      char buf[256];
      if (chdir(dir) != 0) {
        perror("chdir");
        return;
      }
      printf("chdir(\"%s\") -> getcwd() = \"%s\"\n", dir,
             getcwd(buf, sizeof buf) ? buf : "(error)");
    }
    
    int main(void) {
      /* A directory backed by a second memory backend: a mount point in the
         root's memory directory. */
      wasmfs_create_directory("/mnt", 0777, wasmfs_create_memory_backend());
      mkdir("/mnt/sub", 0777);
      /* An ordinary directory, for comparison. */
      mkdir("/plain", 0777);
      mkdir("/plain/sub", 0777);
    
      show("/plain/sub");
      show("/mnt");
      show("/mnt/sub");
      return 0;
    }

    Failing command line in full (the same at -O2):

    $ emcc -O0 -sWASMFS getcwd_mount.c -o getcwd_mount.js && node getcwd_mount.js
    chdir("/plain/sub") -> getcwd() = "/plain/sub"
    chdir("/mnt") -> getcwd() = "/"
    chdir("/mnt/sub") -> getcwd() = "//sub"
    

    Expected "/mnt" and "/mnt/sub".

    Full compile and link output with -v appended (cache and scratch paths shortened)
     /nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/bin/clang -target wasm32-unknown-emscripten -fignore-exceptions -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=$EM_CACHE/sysroot -resource-dir=/nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/lib/clang/22/ -idirafter$EM_CACHE/sysroot/include -iwithsysroot/include/c++/v1 -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -v -O0 -c getcwd_mount.c -o /tmp/emscripten_temp_9lvlc7x5/getcwd_mount.o
    clang version 22.1.8
    Target: wasm32-unknown-emscripten
    Thread model: posix
    InstalledDir: /nix/store/a0h9ym1ai9yawkmymx2wp0vnh64hsv0r-clang-22.1.8/bin
     (in-process)
     "/nix/store/a0h9ym1ai9yawkmymx2wp0vnh64hsv0r-clang-22.1.8/bin/clang-22" -cc1 -triple wasm32-unknown-emscripten -O0 -emit-obj -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name getcwd_mount.c -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -fvisibility=hidden -debugger-tuning=gdb -fdebug-compilation-dir=. -v -fcoverage-compilation-dir=. -resource-dir /nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/lib/clang/22/ -idirafter $EM_CACHE/sysroot/include -iwithsysroot /include/c++/v1 -isysroot $EM_CACHE/sysroot -internal-isystem /nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/lib/clang/22/include -internal-isystem $EM_CACHE/sysroot/include/wasm32-emscripten -internal-isystem $EM_CACHE/sysroot/include -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fignore-exceptions -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /tmp/emscripten_temp_9lvlc7x5/getcwd_mount.o -x c getcwd_mount.c
    clang -cc1 version 22.1.8 based upon LLVM 22.1.8 default target x86_64-unknown-linux-gnu
    ignoring nonexistent directory "$EM_CACHE/sysroot/include/wasm32-emscripten"
    ignoring duplicate directory "$EM_CACHE/sysroot/include"
    #include "..." search starts here:
    #include <...> search starts here:
     $EM_CACHE/sysroot/include/c++/v1
     $EM_CACHE/sysroot/include/fakesdl
     $EM_CACHE/sysroot/include/compat
     /nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/lib/clang/22/include
     $EM_CACHE/sysroot/include
    End of search list.
     /nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/bin/clang --version
     /nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/bin/wasm-ld -o getcwd_mount.wasm /tmp/tmp6s2p_n7slibemscripten_js_symbols.so -Bstatic --strip-debug --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=wasmfs_flush --export=_emscripten_stack_alloc --export=__wasm_call_ctors --export=_emscripten_stack_restore --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=main --export-if-defined=__main_argc_argv --export-if-defined=fflush --export-table -z stack-size=65536 --no-growable-memory --initial-heap=16777216 --no-entry --stack-first --table-base=1 /tmp/emscripten_temp_9lvlc7x5/getcwd_mount.o -L$EM_CACHE/sysroot/lib/wasm32-emscripten -L/nix/store/03iyrnrsxnbc64y6r62irmlbnbap5ya2-emscripten-6.0.9/share/emscripten/src/lib -lGL-getprocaddr -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc-debug -lclang_rt.builtins -lc++-debug-noexcept -lc++abi-debug-noexcept -lsockets -lwasmfs_no_fs -lwasmfs-debug -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr
     /nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/bin/llvm-objcopy getcwd_mount.wasm getcwd_mount.wasm '--remove-section=llvm.func_attr.annotate.*' --remove-section=producers --remove-section=name '--remove-section=.debug*'
    

    On the question in #23756 (fix the backends, or Directory::Handle?):

    • A mount point is never one of its parent backend's entries. mountChild puts it only in the parent's dcache, as DCacheKind::Mount (syscalls.cpp#L658, file.cpp#L43).
    • So no backend can name it, including one that implements getName as file.h asks.
    • MemoryDirectory::getName searches its own entries, correctly, and returns "" (memory_backend.cpp#L86-L95).
    • Directory::Handle::getName returns that "" without looking at the dcache, because the memory directory maintains file identity (file.cpp#L209-L220).
    • Mounts are a Directory::Handle-level concept, so that seems the right place to resolve their names.

    Checking the dcache first is enough. For identity-maintaining directories the dcache holds only mounts, so the extra scan is short:

    --- a/system/lib/wasmfs/file.cpp
    +++ b/system/lib/wasmfs/file.cpp
    @@ -207,15 +207,17 @@
     }
    
     std::string Directory::Handle::getName(std::shared_ptr<File> file) {
    -  if (getDir()->maintainsFileIdentity()) {
    -    return getDir()->getName(file);
    -  }
    +  // Mount points are always in the dcache, even in directories that maintain
    +  // file identity themselves, so look there first.
       auto& dcache = getDir()->dcache;
       for (auto it = dcache.begin(); it != dcache.end(); ++it) {
         if (it->second.file == file) {
           return it->first;
         }
       }
    +  if (getDir()->maintainsFileIdentity()) {
    +    return getDir()->getName(file);
    +  }
       return "";
     }
    

    With this applied to 6.0.9, the test case prints:

    chdir("/plain/sub") -> getcwd() = "/plain/sub"
    chdir("/mnt") -> getcwd() = "/mnt"
    chdir("/mnt/sub") -> getcwd() = "/mnt/sub"
    

    #23756 gets the same result by falling back to the dcache when the backend returns "". Either way works. The dcache-first order just doesn't depend on the backend returning "" for a file it doesn't know.

    getcwd() isn't the only caller. emscripten.cpp#L47 builds a FILE*'s path the same way, and the fix covers both.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    • Status
      No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions