Skip to content

Commit

Permalink
Merge pull request #38 from bpineau/atomic_changes
Browse files Browse the repository at this point in the history
Use a temp file + rename for atomic changes
  • Loading branch information
bpineau authored Apr 20, 2018
2 parents b59030b + 5f6a8a9 commit 8276571
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/recorder/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,22 @@ func (w *Listener) save(file string, data []byte) error {
w.actives[w.relativePath(file)] = true
w.activesLock.Unlock()

err = afero.WriteFile(appFs, file, data, 0600)
tmpf, err := afero.TempFile(appFs, "", "katafygio")
if err != nil {
return fmt.Errorf("failed to create a temporary file: %v", err)
}

_, err = tmpf.Write(data)
if err != nil {
return fmt.Errorf("failed to write to %s on disk: %v", file, err)
return fmt.Errorf("failed to write to %s on disk: %v", tmpf.Name(), err)
}

if err := tmpf.Close(); err != nil {
return fmt.Errorf("failed to close a temporary file: %v", err)
}

if err := appFs.Rename(tmpf.Name(), file); err != nil {
return fmt.Errorf("failed to rename %s to %s: %v", tmpf.Name(), file, err)
}

return nil
Expand Down

0 comments on commit 8276571

Please sign in to comment.