Skip to content

Commit

Permalink
refactor: close file before renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLoch committed Jan 31, 2025
1 parent 7d43108 commit ce13751
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ func (f *fsStorage) Save(key, etag string, data []byte) error {
if err != nil {
return fmt.Errorf("creating file %q: %w", filePathTmp, err)
}
defer file.Close()
closeOnce := syncPkg.OnceValue(file.Close)
defer closeOnce()

var w io.Writer = file

Expand Down Expand Up @@ -123,6 +124,10 @@ func (f *fsStorage) Save(key, etag string, data []byte) error {
return fmt.Errorf("syncing file %q to stable storage: %w", filePathTmp, err)
}

if err := closeOnce(); err != nil {
return fmt.Errorf("closing file %q: %w", filePathTmp, err)
}

// Replaces an existing file; on unix-like systems that should be an atomic operation
if err := os.Rename(filePathTmp, filePath); err != nil {
return fmt.Errorf("renaming tmp file %q into actual file %q: %w", filePathTmp, filePath, err)
Expand Down

0 comments on commit ce13751

Please sign in to comment.