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

Add atomic file saving #44

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Conversation

nyanpasu64
Copy link

Fixes #43.

I wrote my own atomic file saving module (common::atomic_save). It was adapted from the atomicwrites crate, but with an added flag to disable directory fsync (atomicwrites always performs directory fsync). I chose to not fsync the directory after writing the file, because it's unnecessary to prevent corruption and hurts performance (to some degree I don't know). Note that my code depends on nix and does not compile on Windows (though I assume handlr is only meant for Unix platforms?).

Both atomicwrites and my fork create a temporary directory within the folder (~/.config/) to write the temporary file to. I'm not sure why this is done instead of creating a temporary file directly in ~/.config/, but I didn't change it.

Note that atomicwrites doesn't use renameat2. There's an open bug report at untitaker/rust-atomicwrites#28, where some people have suggested that renameat2 is better suited for atomic saving. This is suboptimal and I haven't tried fixing it, but I don't think it's an issue for atomically saving mimeapps.list.

Unfortunately this branch has "unused enum case" warnings because I moved atomicwrites from a crate to a module, and didn't use all enum cases in this program. The warnings would not be present if the code was located in a library crate. I'm not clear on the exact details of what causes the warning to happen or not, but there's some discussion on Reddit /r/rust.

Maybe this code should be upstreamed to atomicwrites. However it's an API-breaking change (requiring a new semver version) which they may or may not accept, so even if I did, you'd have to wait for atomicwrites to be updated.

Adapted from the atomicwrites crate, but with directory fsync disabled
for performance.
leiserfg pushed a commit to leiserfg/handlr that referenced this pull request Mar 8, 2025
Bumps [h2](https://github.com/hyperium/h2) from 0.3.22 to 0.3.24.
- [Release notes](https://github.com/hyperium/h2/releases)
- [Changelog](https://github.com/hyperium/h2/blob/v0.3.24/CHANGELOG.md)
- [Commits](hyperium/h2@v0.3.22...v0.3.24)

---
updated-dependencies:
- dependency-name: h2
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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 this pull request may close these issues.

Should write to mimeapps.list atomically to prevent intermediate states or corruption
1 participant