manuelroemer/Files

Add support for atomically creating and opening a file

Closed this issue · 2 comments

New Feature Proposal

Description

The StorageFile.CreateAsync method currently doesn't open a Stream. This is done on purpose to prevent situations where a Stream is not disposed. However, in certain situations, one wants to atomically create and open the file, e.g. for creating lock files. Files should therefore provide both options.

Discussion

The naming is open, but I'm leaning towards StorageFile.CreateAndOpenAsync(CreationCollisionOption, CancellationToken).

This change also makes sense together with #9, since #9 might add new parameters to the OpenAsync method. This parameter should then be mirrored.

The new method can also be implemented without introducing a breaking change by making it virtual instead of abstract. The default implementation in StorageFile could then call CreateAsync and OpenAsync in order. This is not atomic, but perhaps the best possible implementation for certain file systems.

Closing for now. See this comment for the reasons.

Additional info:
I initially thought of this feature for the purpose of atomically acquiring a file lock. With FileShare being discarded for now, file locks are basically gone. Therefore, this feature is also less pressing.

With that being said, it might also make sense in other, non file locking related scenarios. If anyone has a need for this feature, please comment. Adding it is still possible (just less pressing for myself right now).

I've now reached a point where I want to have support for a method like this. Even though atomicity cannot be guaranteed, having it is incredibly convenient in most situations. Therefore reopening this issue.