Skip to content

fix(fs): rmdir() removed nothing, it called unlink() - #77

Merged
caveman99 merged 1 commit into
masterfrom
fix-rmdir-uses-unlink
Aug 13, 2026
Merged

fix(fs): rmdir() removed nothing, it called unlink()#77
caveman99 merged 1 commit into
masterfrom
fix-rmdir-uses-unlink

Conversation

@caveman99

@caveman99 caveman99 commented Aug 13, 2026

Copy link
Copy Markdown
Member

VFSImpl::rmdir() built the native path and then passed it to unlink(), which cannot remove a directory. Measured on both hosts:

unlink(empty dir) rmdir(empty dir)
Linux (glibc) -1, EISDIR, directory still exists 0, gone
Windows (UCRT64) -1, EACCES, directory still exists 0, gone

So the call always failed. FSCom.rmdir() has never removed a directory on any portduino platform, and rmDir() leaves the whole tree behind.

Parity with the other backends

This file is a port of arduino-esp32's libraries/FS/src/vfs_api.cpp, which uses ::rmdir() at the same spot:

  // Let rmdir() handle the error if directory doesn't exist
  auto rc = ::rmdir(temp);

The nRF52 Adafruit_LittleFS::rmdir() backend calls lfs_remove(), which removes an empty directory and returns LFS_ERR_NOTEMPTY otherwise. Both platforms therefore implement "remove an empty directory". portduino was the only backend that did not, so firmware written against the Arduino FS API behaved differently there.

The call is qualified as ::rmdir because an unqualified rmdir() inside VFSImpl::rmdir resolves to the member function and recurses. That is the likely reason unlink() was reached for originally.

Verification

Against the meshtastic firmware native test suite on Windows, test_fscommon_getfiles previously passed only against a clean tree and failed on every later run: the directories its depth-limit case creates survived setUp()'s rmDir(), so getFiles() reported a depth truncation and the "not limited" case failed.

With this change, seeding the stale tree first, the suite passes with the stale tree present, passes on two consecutive runs, and leaves zero entries behind each time.

Linux does not currently show the failure because each suite there runs in its own scratch $HOME, so leftovers never survive into a later run, and within a single run the directory-creating case happens to run after the directory-sensitive one. The underlying defect is the same on both platforms.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed directory removal so directories can be deleted correctly.
    • Prevented directory deletion from incorrectly using file removal behavior.

VFSImpl::rmdir() built the native path and then passed it to unlink(),
which cannot remove a directory: EISDIR on Linux, EACCES on Windows.
The call always failed, so FSCom.rmdir() never removed a directory on
any portduino platform and rmDir() left the whole directory tree behind.

arduino-esp32, which this file is ported from, uses ::rmdir() here, and
the nRF52 Adafruit_LittleFS backend uses lfs_remove(), which removes an
empty directory. Both platforms therefore implement "remove an empty
directory" and portduino was the only backend that did not, so firmware
written against the Arduino FS API behaved differently there.

Qualified as ::rmdir because an unqualified call inside VFSImpl::rmdir
resolves to the member function and recurses. That is the likely reason
unlink() was reached for originally.

Verified on Windows against the meshtastic firmware test suite:
test_fscommon_getfiles previously passed only against a clean tree and
failed on every later run, because the directories its depth-limit case
creates survived setUp()'s rmDir() and made getFiles() report a depth
truncation. With this change the suite passes with a stale tree seeded,
passes on consecutive runs, and leaves no directories behind.
@coderabbitai

coderabbitai Bot commented Aug 13, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2039a821-07f9-4956-a2ad-bcf0c4225f58

📥 Commits

Reviewing files that changed from the base of the PR and between b50a292 and ec7b23e.

📒 Files selected for processing (1)
  • cores/portduino/FS/vfs_api.cpp

📝 Walkthrough

Walkthrough

VFSImpl::rmdir now removes directories through the global ::rmdir function instead of unlink.

Changes

VFS directory removal

Layer / File(s) Summary
Correct directory removal call
cores/portduino/FS/vfs_api.cpp
VFSImpl::rmdir now calls global ::rmdir for directory removal.

Estimated code review effort: 1 (Trivial) | ~2 minutes

Mergeability Score: ⚪ Minimal · up to ec7b2

This localized change makes directory removal work correctly without altering other filesystem behavior; no actionable merge-blocking risk remains after normal checks and review.

Poem

I’m a rabbit with folders to tend,
::rmdir now helps them end.
No unlink in the burrow today,
Directories leave the proper way.
Hop, hop—clean paths away!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely identifies the rmdir() bug and the incorrect unlink() call.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@caveman99
caveman99 merged commit 35a9b68 into master Aug 13, 2026
2 checks passed
@caveman99
caveman99 deleted the fix-rmdir-uses-unlink branch August 13, 2026 14:18
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.

1 participant