-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Description
Link to proposal: #53432 (comment)
Discussion
While working on a new API called File.OpenHandle
I've realized that FileStream
currently does not support handles that have been opened for appending (FILE_APPEND_DATA
on Windows and O_APPEND
on Unix).
Moreover, we don't use these flags for FileMode.Append
when we are opening a file from a path. Instead, we mimic the append by setting the file offset to the end of the file:
runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/WindowsFileStreamStrategy.cs
Lines 240 to 243 in 33b0f24
if (mode == FileMode.Append) | |
{ | |
_appendStart = _filePosition = Length; | |
} |
Lines 86 to 90 in 33b0f24
if (mode == FileMode.Append) | |
{ | |
// Jump to the end of the file if opened as Append. | |
_appendStart = SeekCore(_fileHandle, 0, SeekOrigin.End); | |
} |
Lines 85 to 88 in 33b0f24
if (mode == FileMode.Append) | |
{ | |
_appendStart = SeekCore(_fileHandle, 0, SeekOrigin.End); | |
} |
I did some quick web search and it turns out that it was possible with .NET Framework: https://stackoverflow.com/questions/1862309/how-can-i-do-an-atomic-write-append-in-c-or-how-do-i-get-files-opened-with-the
And some of our users (@nblumhardt) are missing it in .NET Core: https://nblumhardt.com/2016/08/atomic-shared-log-file-writes/
@stephentoub @ericstj @JeremyKuhne is there any chance that you remember why we have decided to implement it in such a way?