blob: b56d09570eb0db55683e2a0eb4d572b3f4fdfbce (
plain)
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
|
// Create a temporary file in the directory of the given destination
// file, then rename the temporary file over the destination file if
// writes to the temporary file complete without error.
//
// Used to atomically update files.
//
// Note: This is not guaranteed and depends on the sync semantics of the
// underlying filesystem.
package atomictemp
import (
"io"
"os"
"path/filepath"
)
// Create a temporary file in the directory of the given destination
// file, then rename the temporary file over the destination file if
// writes to the temporary file complete without error.
//
// Used to atomically update files.
//
// Note: This is not guaranteed and depends on the sync semantics of the
// underlying filesystem.
func Create(dstPath string, fn func(io.Writer) error) error {
// open temp output file
f, err := os.CreateTemp(filepath.Dir(dstPath), "")
if err != nil {
return err
}
// build absolute path to temporary file
// tmpPath := filepath.Join(filepath.Dir(dstPath), f.Name())
defer os.Remove(f.Name())
// invoke callback
if err = fn(f); err != nil {
return err
}
// close temp file
if err = f.Close(); err != nil {
return err
}
// rename to destination file, return result
return os.Rename(f.Name(), dstPath)
}
|