Skip to content

fix: Correct rename, link-count and zero-length write edge cases - #32

Merged
krodak merged 4 commits into
swiftwasm:mainfrom
PassiveLogic:fix/filesystem-namespace-edge-cases
Oct 7, 2026
Merged

krodak merged 4 commits into
swiftwasm:mainfrom
PassiveLogic:fix/filesystem-namespace-edge-cases

Conversation

@krodak

@krodak krodak commented Oct 7, 2026

Copy link
Copy Markdown
Member

Summary

Four small fixes in useMemoryFS. Each has its own commit and a regression test that fails on main:

  • Renaming a directory into its own subtree ("a" to "a/b/c") returned success and detached the directory, losing it and everything in it. It now returns EINVAL, as POSIX rename does.
  • fd_pwrite and fd_write with zero bytes at an offset past EOF grew the file to that offset. A zero-length write now leaves the file unchanged.
  • Hard links to symlinks and device nodes did not update the link count, so stat kept reporting nlink 1. Every non-directory node now has a link count, updated on link, unlink and replacing rename.
  • Renaming a file onto another hard link to it removed the source name and lowered the link count. POSIX says that rename succeeds and does nothing else.

These came up while separating a storage-backend layer from the memory filesystem in our fork. I'll propose that layer separately. The directory-rename and zero-length-write fixes are adapted from Pete Grayson's work there, and those commits credit him.

Test Plan

  • Each new test failed on main before its fix.
  • Each commit builds and passes npm test on its own.
  • npm run build and npm test: 187 passed (182 existing, 5 new), no failures or skips.
  • npx prettier --check src/features/fd.ts test/fd.test.mjs and git diff --check.

Krzysztof Rodak and others added 4 commits October 7, 2026 12:53
path_rename("a", "a/b/c") detached "a" from the tree and returned success,
so the directory and everything in it were lost. Return EINVAL instead,
as POSIX rename does.

Co-authored-by: Peter Grayson <pete@jpgrayson.net>
fd_pwrite and fd_write with no bytes at an offset past EOF grew the file
to that offset. POSIX says a zero-length write to a regular file has no
other results, so return 0 bytes written and leave the file as it is.

Co-authored-by: Peter Grayson <pete@jpgrayson.net>
path_link accepts any node but a directory, but only regular files kept a
link count. A hard link to a symlink or to /dev/null left stat reporting
nlink 1. Give every non-directory node a link count, and update it on
link, unlink and replacing rename.
path_rename("f", "g"), with "g" a hard link to "f", removed "f" and lowered
the link count. POSIX says rename succeeds and does nothing else when both
names link the same file.
@krodak
krodak requested a review from kateinoigakukun October 7, 2026 11:08
@krodak krodak self-assigned this Oct 7, 2026
@kateinoigakukun

Copy link
Copy Markdown
Member

I think the latest wasi-testsuite covers more cases so it might be helpful to catch other issues as well

@krodak
krodak merged commit caee52b into swiftwasm:main Oct 7, 2026
1 check passed
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.

2 participants