Skip to content

corlib: FileMode.Create must truncate in BufferedFileStream - #2499

Merged
bfiete merged 1 commit into
beefytech:masterfrom
jayrulez:fix/filestream-create-does-not-truncate
Sep 7, 2026
Merged

corlib: FileMode.Create must truncate in BufferedFileStream#2499
bfiete merged 1 commit into
beefytech:masterfrom
jayrulez:fix/filestream-create-does-not-truncate

Conversation

@jayrulez

@jayrulez jayrulez commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

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.

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.
@bfiete
bfiete merged commit e858cc2 into beefytech:master Sep 7, 2026
7 checks passed
@bfiete

bfiete commented Sep 7, 2026

Copy link
Copy Markdown
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.

@jayrulez

jayrulez commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

Oh yes, sorry I only tested on Linux. I'll remember to check windows next time.

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.

2 participants