Repository navigation
fix(SD): open(FILE_WRITE) must not truncate a file whose lookup failed - #94
Merged
fpistm merged 1 commit intoNov 20, 2026
Merged
Conversation
open() called exists() to decide whether to add FA_CREATE_ALWAYS, and exists() reports any f_stat() error as 'absent'. A transient read error (FR_DISK_ERR) on the directory sector therefore truncated an existing file. Use FA_OPEN_ALWAYS instead: create if absent, open untouched if present, and fail the open on a read error. Fixes stm32duino#93
fpistm
approved these changes
Nov 20, 2026
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.
Fixes #93.
SDClass::open()calledexists()to decide whether to addFA_CREATE_ALWAYS, andexists()returnsfalsefor anyf_stat()error. A transient read error (FR_DISK_ERR) on the directory sector therefore made an existing file look absent, andf_open()then truncated it.This uses
FA_OPEN_ALWAYSforFILE_WRITEinstead: the file is created if absent and opened untouched if present, and a read error makes the open fail instead of destroying data. It also drops one directory lookup per open.exists()itself is unchanged.Tested on an Adafruit Feather STM32F405 (SDIO, 24 MHz, STM32SD 1.5.0, FatFs 4.0.4).
disk_readwas wrapped to fail the next sector read once, thenSD.open(path, FILE_WRITE)was called on an existing file, 50 times per build:open()on error