-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwriteatomically.go
53 lines (45 loc) · 1.04 KB
/
writeatomically.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"bufio"
"io"
"io/ioutil"
"os"
"path/filepath"
)
func tempDir(dest string) string {
tempdir := os.Getenv("TMPDIR")
if tempdir == "" {
// Convenient for development: decreases the chance that we
// cannot move files due to TMPDIR being on a different file
// system than dest.
tempdir = filepath.Dir(dest)
}
return tempdir
}
func writeAtomically(dest string, write func(w io.Writer) error) (err error) {
f, err := ioutil.TempFile(tempDir(dest), "atomic-")
if err != nil {
return err
}
defer func() {
// Clean up (best effort) in case we are returning with an error:
if err != nil {
// Prevent file descriptor leaks.
f.Close()
// Remove the tempfile to avoid filling up the file system.
os.Remove(f.Name())
}
}()
// Use a buffered writer to minimize write(2) syscalls.
bufw := bufio.NewWriter(f)
if err := write(bufw); err != nil {
return err
}
if err := bufw.Flush(); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return os.Rename(f.Name(), dest)
}