Skip to content

Commit

Permalink
fix atomic write bug when temp dir is on another partition
Browse files Browse the repository at this point in the history
  • Loading branch information
edlitmus committed May 25, 2018
1 parent f80e556 commit 3ca1077
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion sls/sls.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func atomicWrite(fullPath string, buffer bytes.Buffer) (int, error) {
err = permErr
}
if err == nil {
err = os.Rename(f.Name(), fullPath)
err = copyFile(f.Name(), fullPath)
}
if err != nil {
return byteCount, err
Expand All @@ -196,6 +196,37 @@ func atomicWrite(fullPath string, buffer bytes.Buffer) (int, error) {
return byteCount, err
}

func copyFile(src string, dst string) error {
srcStat, err := os.Stat(src)
if err != nil {
return err
}

fsrc, err := os.Open(src)
if err != nil {
return err
}

fdst, err := os.Create(dst)
if err != nil {
return err
}

size, err := io.Copy(fdst, fsrc)
if err != nil {
return err
}
if size != srcStat.Size() {
return fmt.Errorf("%s: %d/%d copied", src, size, srcStat.Size())
}

err = fsrc.Close()
if err != nil {
return fdst.Close()
}
return err
}

// FormatBuffer returns a formatted .sls buffer with the gpg renderer line
func (s *Sls) FormatBuffer(action string) (bytes.Buffer, error) {
var buffer bytes.Buffer
Expand Down

0 comments on commit 3ca1077

Please sign in to comment.