Skip to content

Update the ".." entry when a directory is moved to a new parent - #99

Open
schnitst wants to merge 1 commit into
eclipse-threadx:masterfrom
BerninaInternationalAG:fix-directory-rename-dotdot-update
Open

Update the ".." entry when a directory is moved to a new parent#99
schnitst wants to merge 1 commit into
eclipse-threadx:masterfrom
BerninaInternationalAG:fix-directory-rename-dotdot-update

Conversation

@schnitst

@schnitst schnitst commented Sep 3, 2026

Copy link
Copy Markdown

Problem

When fx_directory_rename moves a directory to a different parent directory, it relinks the entry in both parents but never updates the '..' entry inside the moved directory itself. '..' keeps the starting cluster of the old parent, so any '..' path component inside the moved directory resolves to the old parent - and, once that cluster is reused, to unrelated data. fx_media_check cannot detect this because it skips the first two entries of every sub-directory.
Details and reproduction: #98.

Fix
After the relink completes, read the moved directory's '..' entry (entry 1 of its first cluster) and rewrite its starting cluster in place when the parent changed - cluster 0 when the new parent is the root, matching the convention fx_directory_create writes. Design points:

  • The write happens inside the existing fault-tolerant transaction, so a move stays all-or-nothing; the update is placed after the relink, so every non-fault-tolerant crash window is no worse than today's behavior.
  • A rename within one parent detects the unchanged cluster and ends without the additional write.
  • The entry is only rewritten if it actually reads back as .., so non-conformant media are left untouched (the rename still succeeds).
  • Exactly one entry needs updating regardless of subtree size: children of the moved directory reference its own starting cluster, which a move does not change.

Tests
filex_directory_rename_test.c gains a section covering every parent transition - between sub-directories, into the root, out of the root, and renames within one parent - verifying both path resolution through '..' and the on-media entry itself, and pinning the directory-entry write count (one additional write for a move, none for an in-place rename). The section runs on a freshly formatted media after the existing flow, so the error-injection steps calibrated against the original directory layout are unaffected.

When a directory is renamed into a different parent directory,
fx_directory_rename relinks the entry in both parents but never
updates the ".." entry inside the moved directory, which keeps
pointing at the old parent's starting cluster. A ".." path component
inside the moved directory then resolves to the old parent and, once
that cluster is reused, to an unrelated place. fx_media_check does
not detect this, as it skips the first two entries of every
sub-directory.

Read the moved directory's ".." entry after the relink and rewrite
its starting cluster in place when the parent changed (0 when the new
parent is the root). The write happens inside the existing fault
tolerant transaction; a rename within one parent ends without the
additional write. The regression test extension covers every parent
transition - between sub-directories, into and out of the root - and
pins the write count: one additional directory entry write for a move,
none for a rename within one parent.

Fixes Issue eclipse-threadx#98

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@fdesbiens
fdesbiens requested a review from rahmanih September 3, 2026 15:29
@fdesbiens

Copy link
Copy Markdown
Contributor

Thank you for submitting this fix, @schnitst. @rahmanih: Please review.

@rahmanih rahmanih left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM 💯

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.

3 participants