Skip to content

fix(xmodem): truncate file on open instead of appending#10143

Open
benallfree wants to merge 9 commits intomeshtastic:developfrom
MeshEnvy:fix/xmodem-truncate-on-write
Open

fix(xmodem): truncate file on open instead of appending#10143
benallfree wants to merge 9 commits intomeshtastic:developfrom
MeshEnvy:fix/xmodem-truncate-on-write

Conversation

@benallfree
Copy link
Copy Markdown

This PR fixes what I perceive is a bug in the XModem module: subsequent file writes to the same path append data rather than truncating.

🤝 Attestations

  • I have tested that my proposed changes behave as described.
  • I have tested that my proposed changes do not cause any obvious regressions on the following devices:
    • Heltec (Lora32) V3
    • LilyGo T-Deck
    • LilyGo T-Beam
    • RAK WisBlock 4631
    • Seeed Studio T-1000E tracker card
    • Other (please specify below)

FILE_O_WRITE on nRF52 (Adafruit LittleFS) appends to existing files
rather than truncating. Remove the file before opening for write so
repeated XModem uploads always produce the correct file size.

Made-with: Cursor
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the XModem receive path to ensure a new transfer to an existing filename overwrites prior contents (truncate semantics) rather than appending, aligning expected file-transfer behavior across supported filesystems/platforms.

Changes:

  • Delete any pre-existing destination file before opening for write in the XModem receive flow.

Comment thread src/xmodem.cpp Outdated
Comment thread src/xmodem.cpp Outdated
@benallfree
Copy link
Copy Markdown
Author

@thebentern Copilot brought up a good point about preserving atomicity by writing to a tmp file and then doing a rename. I updated the PR to reflect that.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the XModem receive flow to avoid unintended appending when receiving a file to an existing path by writing to a temporary file and committing it at end-of-transmission.

Changes:

  • Add receive state (recvCommitPending) and a receive temp path buffer (recvTmpPath) to support temp-file based receives.
  • On receive start, build {filename}.tmp, remove any pre-existing temp file, and write incoming blocks to the temp file.
  • On EOT/CAN, close and either commit via renameFile() (with retry support) or delete the temp file.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/xmodem.h Adds new receive state and temp path storage to support safe receive/commit behavior.
src/xmodem.cpp Implements temp-file receive + EOT commit (rename) and cancel cleanup; improves filename copying/termination.

Comment thread src/xmodem.cpp
benallfree and others added 2 commits April 17, 2026 04:00
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@benallfree
Copy link
Copy Markdown
Author

@thebentern Sorry for the delay, for some reason I am not getting GH notifications on this PR.

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

Labels

bugfix Pull request that fixes bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants