Skip to content

Commit

Permalink
fix: create temporary files in same dir as end destination
Browse files Browse the repository at this point in the history
To ensure that config and state files are mutated atomically, updates
are done by creating a temporary file, writing the new content, and
doing an `os.rename` call to replace the old file with the temporary
file. However, until this commit, the temporary file was create in the
OS' default temp dir. In some environment (Codespaces), this
directory isn't on the same filesystem as the target file.

The solution is to create the temp file in the target directory. It
might end up leaving some artifacts if the process were to die before
cleaning it up, but that's better than failing on such a common case as
having more multiple filesystems.
  • Loading branch information
aybabtme authored and Antoine Grondin committed Jan 25, 2023
1 parent 336d26d commit 9612436
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ func WriteStateFile(path string, state *State) error {
if err != nil {
return fmt.Errorf("marshaling state file: %v", err)
}
newf, err := os.CreateTemp(os.TempDir(), "humanlog_statefile")

newf, err := os.CreateTemp(filepath.Dir(path), "humanlog_statefile")
if err != nil {
return fmt.Errorf("creating temporary file for statefile: %w", err)
}
Expand Down

0 comments on commit 9612436

Please sign in to comment.