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

filecache: use rename for an atomic write #1816

Merged
merged 1 commit into from
Oct 25, 2023

Conversation

valpackett
Copy link
Contributor

This prevents e.g. getting killed during writing from leaving incomplete/corrupt data in the cache directory.

@@ -69,12 +69,28 @@ func (fc *fileCache) Add(key Key, content io.Reader) (err error) {
fc.mux.Lock()
defer fc.mux.Unlock()

file, err := os.Create(fc.path(key))
// Use rename for an atomic write (on Unix, at least; Windows needs special direct syscall code)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since https://go.dev/cl/6140 I'd say os.Rename is "atomic enough" on Windows (using MoveFileEx), so I don't think we really need this comment.

This prevents e.g. getting killed during writing from leaving incomplete/corrupt
data in the cache directory.

Signed-off-by: Val Packett <val@packett.cool>
@mathetake mathetake merged commit 6e61e20 into tetratelabs:main Oct 25, 2023
53 checks passed
@valpackett valpackett deleted the fcache branch October 25, 2023 08:25
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.

3 participants