Repository navigation
fs: require existing parents in MemoryFS and reject rename into own subtree - #857
Merged
Merged
Conversation
…ubtree MemoryFS creating operations (OpenFile with O_CREATE, Symlink, Link, Rename) silently created missing parent directories, so `echo hi > /nope/dir/x` succeeded where bash fails with ENOENT. They now fail with ENOENT for a missing parent and ENOTDIR for a non-directory parent, and the shell renders ENOTDIR as "Not a directory". Rename of a directory into its own descendant deleted the subtree and then dereferenced a nil parent; it now returns EINVAL like OverlayFS. Link used the unresolved destination path and panicked when the parent was a symlinked directory; it now uses the resolved path. OverlayFS's private upper layer keeps implicit parent creation so copy-up into lower-only directories still works.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes two MemoryFS bugs: a runtime crash, and creating operations that didn't behave like bash.
1. Rename into own subtree panicked
MemoryFS.Rename("/a", "/a/b")deleted/a's subtree and then dereferenced the missing parent. Themvbuiltin guards against this, but any external command callingcommands.CommandFS.Renamecrashed the runtime. Rename now returnsEINVAL, the same as OverlayFS'sisDescendantPathguard. The check runs on resolved paths, so/alias/cwith/alias -> /ais caught too.While checking
Linkfor the same pattern I found a second panic.Linkbuilt itschildrenentry from the unresolved destination path, soln /f /symlinked-dir/xwrote into the symlink node's nilchildrenmap. It now uses the resolved path, likeSymlinkalready does.2. O_CREATE silently created missing parents
echo hi > /nope/dir/xreturned 0 and created/nope/dir. It now matches bash:Before this change, the first command printed
rc=0, and the second printed/tmp/f/x: Is a directorybecause ErrInvalid was mapped to "Is a directory".OpenFilewithO_CREATE(truncate, append, excl),Symlink,LinkandRenamenow fail withENOENTfor a missing parent andENOTDIRfor a non-directory parent. Rename follows bashmv/rename(2)and gets the same treatment.ENOTDIRto "Not a directory" (internal/shell/core.go).MkdirAll, initial-file seeding and the privatecloneSpecialFilehelper still create parents, because creating them is their job.OverlayFS
The overlay's upper layer is a MemoryFS, and copy-up writes upper paths whose parents may exist only in the lower layer. That upper layer now uses an unexported
implicitParentsmode that keeps the old behavior. The only change tofs/overlay.gois one line inNewOverlay, to stay clear of the in-flight OverlayFS symlink rework. Known gap: in overlay mode,echo hi > /missing/xstill succeeds. Adding a parent check against the merged view belongs with that rework; there's a hand-over note for it.Behavior change for library users
Code that called
fs.NewMemory().OpenFile("/a/b/c", O_CREATE…)withoutMkdirAllfirst now gets ENOENT. Several test helpers relied on the old behavior; they now callMkdirAllfirst. Incontrib/awk, five tests wrote> /data/xwithoutmkdir -p /data, so they hit exactly this bug.SPEC
Updated the MemoryFS notes in
SPEC.md(parent requirement, Rename EINVAL, overlay upper exception).Testing
fs/memory_create_test.gocovers ENOENT/ENOTDIR for each O_CREATE flavor, a symlinked or dangling parent, Symlink/Link/Rename into a missing parent, rename into own subtree (direct, through a symlink, and the/a→/absibling case), Link through a symlinked parent, and overlay copy-up into lower-only dirs. Run against the oldmemory.go, the rename and link tests panic and the O_CREATE test fails.internal/runtime/memoryfs_parents_test.gois end-to-end: redirect exit codes and stderr, plus a custom command callinginv.FS.Renameinto its own subdirectory.go testacross the workspace plus contrib modules (with the diff/ripgrep oracles set) passes, except two failures that also happen onmainwithout this change:scripts/bench-compareTestExpansionStressScenarioMatchesExpectedStdoutUnderBashandcontrib/nodejsTestNodeJSShebangViaEnvWorks.make conformance-test(full suite) passes.make lintpasses with 0 issues, and so does golangci-lint incontrib/awk.