Skip to content

Commit

Permalink
Merge pull request #24 from terrastruct/atomic-write
Browse files Browse the repository at this point in the history
add atomic write
  • Loading branch information
alixander authored Oct 5, 2024
2 parents b88c69b + 2ba1ac9 commit 44c011a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions xmain/xmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,39 @@ func (ms *State) WritePath(fp string, p []byte) error {
return os.WriteFile(fp, p, 0644)
}

func (ms *State) AtomicWritePath(fp string, p []byte) error {
if fp == "-" {
return ms.WritePath(fp, p)
}

dir := filepath.Dir(fp)
base := filepath.Base(fp)
tempFile, err := os.CreateTemp(dir, "tmp-"+base+"-")
if err != nil {
return err
}
defer func() {
// Clean up temporary file if it still exists and there's an error
if err != nil {
os.Remove(tempFile.Name())
}
}()

if _, err = tempFile.Write(p); err != nil {
return err
}

if err = tempFile.Close(); err != nil {
return err
}

if err = os.Rename(tempFile.Name(), fp); err != nil {
return err
}

return nil
}

// AbsPath joins the PWD with fp to give the absolute path to fp.
func (ms *State) AbsPath(fp string) string {
if fp == "-" || filepath.IsAbs(fp) {
Expand Down

0 comments on commit 44c011a

Please sign in to comment.