Skip to content

Allow for atomic appends to end of file #53432

@adamsitnik

Description

@adamsitnik

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:

if (mode == FileMode.Append)
{
_appendStart = _filePosition = Length;
}

if (mode == FileMode.Append)
{
// Jump to the end of the file if opened as Append.
_appendStart = SeekCore(_fileHandle, 0, SeekOrigin.End);
}

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?

cc @jozkee @carlossanlop

Metadata

Metadata

Assignees

Labels

api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.IO

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions