Skip to content

Commit

Permalink
Use a temp file + rename for atomic changes
Browse files Browse the repository at this point in the history
We were updating the files in place. This left a small time frame
where files may be commited while in the middle of a write. The
final, complete file would then be commited soon after, but we
would end up with an ugly, partial commit in the history.

Using a temporary file then a rename ensure git only see complete
files (rename is atomic on POSIX filesystems).
  • Loading branch information
bpineau committed Apr 20, 2018
1 parent b59030b commit c4ce52c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/recorder/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,24 @@ 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)
tmpfile, err := afero.TempFile(appFs, "", "katafygio")
if err != nil {
return fmt.Errorf("failed to create a temporary file: %v", err)
}

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

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

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

return nil
}

Expand Down

0 comments on commit c4ce52c

Please sign in to comment.