Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New RFC: file_create_exclusive #1348

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions text/0000-file-create-exclusive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
- Feature Name: file_create_exclusive
- Start Date: 2015-10-30
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)

# Summary
[summary]: #summary

Atomically create a file is it does not exist, or fail.

# Motivation
[motivation]: #motivation

This exposes an OS operation available on at least Unix and Windows. It can be
emulated as a non-atomic operation.

My use case is to sequentially create files like `snapshot-1`, `snapshot-2`,
etc. without overwriting an existing file. I believe a more common usage for
this feature is to open a temporary file ensuring this does not conflict with
another file.

# Detailed design
[design]: #detailed-design

Add another function to `std::fs::OpenOptions`:

/// Sets the option for creating a file exclusively.
///
/// If this is set and "create" is set, the `open()` operation shall fail
/// if the file already exists.
///
/// The check for the existence of the file and the creation of the file
/// if it does not exist shall be atomic with respect to other threads
/// executing open() naming the same filename in the same directory with
/// "exclusive" and "create" options set.
fn exclusive(&mut self, excl: bool) -> &mut OpenOptions

On Unix this shall set the [`O_EXCL` flag](http://linux.die.net/man/3/open).
On Windows this shall use the [`CREATE_NEW` parameter](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx).

# Drawbacks
[drawbacks]: #drawbacks

I don't see any. This functionality should eventually be exposed.

# Alternatives
[alternatives]: #alternatives

I have no idea whether more extensive modifications to the file-open
functionality are under way.

# Unresolved questions
[unresolved]: #unresolved-questions

I have looked over the code, and implementation looks straightforward, so I do
not anticipate any issues. There may however be other opinions over how this
functionality should be exposed.

I am not certain what testing should be implemented.