corlib: FileMode.Create must truncate in BufferedFileStream - #2499
Merged
bfiete merged 1 commit intoSep 7, 2026
Merged
Conversation
System.IO.FileStream derives from BufferedFileStream, and BufferedFileStream carries its own copy of the FileMode to BfpFileCreateKind mapping. That copy has drifted from UnbufferedFileStream's: Create sets CreateAlways without the Truncate flag, so opening an existing file with FileMode.Create keeps its contents and a shorter write leaves the tail of the previous one behind. FileMode.Create is documented as overwriting the file, and every caller of FileStream gets the buffered class, so the working spelling is the one nobody reaches. Truncate had drifted the other way, to CreateAlways, which creates a file that is documented to fail when it does not exist. Both cases now read as UnbufferedFileStream has them. The tests use working-directory relative names rather than Path.GetTempPath, because BfpFile_GetTempPath is NOT_IMPL on POSIX.
Collaborator
|
Did you test on Linux only? The tests failed on Windows - which was actually a platform bug, not a bug with your implementation. Fixed now. |
Contributor
Author
|
Oh yes, sorry I only tested on Linux. I'll remember to check windows next time. |
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.
System.IO.FileStream derives from BufferedFileStream, and BufferedFileStream carries its own copy of the FileMode to BfpFileCreateKind mapping. That copy has drifted from UnbufferedFileStream's: Create sets CreateAlways without the Truncate flag, so opening an existing file with FileMode.Create keeps its contents and a shorter write leaves the tail of the previous one behind. FileMode.Create is documented as overwriting the file, and every caller of FileStream gets the buffered class, so the working spelling is the one nobody reaches.
Truncate had drifted the other way, to CreateAlways, which creates a file that is documented to fail when it does not exist. Both cases now read as UnbufferedFileStream has them.
The tests use working-directory relative names rather than Path.GetTempPath, because BfpFile_GetTempPath is NOT_IMPL on POSIX.