Skip to content

Fix retrieval of mounted directory names - #23756

Open
guusw wants to merge 2 commits into
emscripten-core:mainfrom
guusw:fix-wasmfs-cwd
Open

guusw wants to merge 2 commits into
emscripten-core:mainfrom
guusw:fix-wasmfs-cwd

Conversation

@guusw

@guusw guusw commented Feb 25, 2025

Copy link
Copy Markdown
Contributor

As mentioned in #18112 I decided to PR my changes since I would like to use upstream emscripten for development where possible.

Basically retrieval of the CWD for mounted directories (e.g. fetchfs) will not return the correct name for the mounted directory, for example // instead of /http_fs/

@sbc100

sbc100 commented Feb 25, 2025

Copy link
Copy Markdown
Collaborator

Can you add a test for this? That might also help me understand the problem we are solving here.

@guusw

guusw commented Feb 26, 2025

Copy link
Copy Markdown
Contributor Author

Added a test, seems like it can be reproduced with just memory backend too

4.0.1:

WasmFS Mounted Directory CWD Test
Initial CWD: /
CWD after chdir("/tfs"): /
Aborted(Assertion failed: strcmp(buf, "/tfs") == 0, at: /Users/bakje.coffee/Projects/emscripten/test/wasmfs/wasmfs_mounted_cwd.c,36,main)
/Users/bakje.coffee/Projects/emscripten/out/test/wasmfs_mounted_cwd.js:684
  var e = new WebAssembly.RuntimeError(what);

After PR works fine

@guusw
guusw force-pushed the fix-wasmfs-cwd branch 2 times, most recently from f34bb14 to 816c7d1 Compare February 26, 2025 08:46
@sbc100

sbc100 commented Mar 8, 2025

Copy link
Copy Markdown
Collaborator

I recently updated test_unistd_unlink in test_core.py such this it skips under wasmfs due to this bug.. can you remove the skipTest there as part of this PR?

@tlively
tlively self-requested a review March 17, 2025 21:24
@tlively

tlively commented Mar 17, 2025

Copy link
Copy Markdown
Member

Part of the documented requirements for a backend opting into maintainsFileIdentity are that the backend needs to implement getName itself:

// When this is `true`, backends are responsible for:
//
// 1. Ensuring that all insert* and getChild calls returning a particular
// file return the same File object.
//
// 2. Clearing unlinked Files' parents in `removeChild` and `insertMove`.
//
// 3. Implementing `getName`, since it cannot be implemented in terms of the
// dcache.
virtual bool maintainsFileIdentity() { return false; }

It sounds like maybe we have backends that aren't doing this correctly? If so, it would be better to fix the backends to make sure the implementation is consistent with the documented intent.

@guusw

guusw commented Mar 21, 2025

Copy link
Copy Markdown
Contributor Author

It sounds like maybe we have backends that aren't doing this correctly? If so, it would be better to fix the backends to make sure the implementation is consistent with the documented intent.

Yeah memory backend does this:

std::string MemoryDirectory::getName(std::shared_ptr<File> file) {
  auto it =
    std::find_if(entries.begin(), entries.end(), [&](const auto& entry) {
      return entry.child == file;
    });
  if (it != entries.end()) {
    return it->name;
  }
  return "";
}

But the entries don't seem to contain the file that is requested in these paths, the mounted folder itself I assume?

guusw added 2 commits March 21, 2025 14:49
They are only stored in the dcache, so check there after checking the backend
@typedrat

typedrat commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

On the question of whether this belongs in the backends: I left a reproduction on 6.0.9 and an analysis on #18112, #18112 (comment)

@tlively tlively left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me

Comment on lines +211 to +214
auto string = getDir()->getName(file);
if (!string.empty()) {
return string;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add a comment about why this matters.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants