[FAT32] fix corruption when appending to empty file #369
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.
When creating a new file, the start cluster in the directory entry is set to all zeroes. However, the context's state is set up so that the first byte written to the file causes the driver to allocate a new cluster.
If no byte is written to the file, this is never changed.
Prior to this change, when opening an existing file, the start cluster was trusted to point to the sector that the file's data began on. Unfortunately since this was the value $00000000, which is an alias for the root directory's start cluster, the next write would clobber the root directory and trash the filesystem entirely.
Rather than unconditionally trusting that the file has data and opening the first cluster, we now handle the case correctly when the opened file's size is 0.