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

Recipe for non clobbering atomic persist? #333

Closed
Ravencentric opened this issue Feb 21, 2025 · 2 comments · Fixed by #334
Closed

Recipe for non clobbering atomic persist? #333

Ravencentric opened this issue Feb 21, 2025 · 2 comments · Fixed by #334

Comments

@Ravencentric
Copy link

Ravencentric commented Feb 21, 2025

NamedTempFile has two methods for persisting, persist() and persist_noclobber(). The former is atomic while the latter is not as stated by the documentation.

Now I'm no expert here, which is why I'm grateful for this crate so thank you for your work, but I was wondering how would I go about achieving non clobbering atomic persist? A naive implementation would look like the following pseudocode:

if !overwrite && path.exists() { /* error */ };
tempfile.persist()

But I'm guessing that the above is susceptible to race conditions.

If the answer is that persist_noclobber is already the best that's possible, then that's okay too. Feel free to close this issue in that case.

@Stebalien
Copy link
Owner

It's "atomic" on modern Linux (assuming a modern filesystem, etc.) and on Windows, but not on macos or the various BSDs (or anything else).

By "not atomic", I mean that the old name for the temporary file might persist in the filesystem (the rename isn't atomic). Any files at the target path will never be overwritten.

Does #334 help clarify this?

@Ravencentric
Copy link
Author

Yep! That clarifies it. Thank you.

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 a pull request may close this issue.

2 participants