atomicfile: use ioutil.TempFile, sync

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
reviewable/pr560/r1
Brad Fitzpatrick 4 years ago committed by Brad Fitzpatrick
parent c3736250a4
commit bcbd41102c

@ -9,20 +9,36 @@
package atomicfile // import "tailscale.com/atomicfile" package atomicfile // import "tailscale.com/atomicfile"
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
) )
// WriteFile writes data to filename+some suffix, then renames it // WriteFile writes data to filename+some suffix, then renames it
// into filename. // into filename.
func WriteFile(filename string, data []byte, perm os.FileMode) error { func WriteFile(filename string, data []byte, perm os.FileMode) (err error) {
tmpname := filename + ".new.tmp" f, err := ioutil.TempFile(filepath.Dir(filename), filepath.Base(filename)+".tmp")
if err := ioutil.WriteFile(tmpname, data, perm); err != nil { if err != nil {
return fmt.Errorf("%#v: %v", tmpname, err) return err
} }
if err := os.Rename(tmpname, filename); err != nil { tmpName := f.Name()
return fmt.Errorf("%#v->%#v: %v", tmpname, filename, err) defer func() {
if err != nil {
f.Close()
os.Remove(tmpName)
} }
return nil }()
if _, err := f.Write(data); err != nil {
return err
}
if err := f.Chmod(perm); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return os.Rename(tmpName, filename)
} }

Loading…
Cancel
Save