-
Notifications
You must be signed in to change notification settings - Fork 3
/
copy.go
59 lines (49 loc) 路 1.2 KB
/
copy.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
54
55
56
57
58
59
package core
import (
"io"
"io/ioutil"
"os"
"path/filepath"
)
// writeAll will write the entire buffer to file or die trying
// i have no idea why go doesn't have this, it has ReadAll
func writeAll(f io.Writer, buf []byte) error {
for len(buf) > 0 {
n, err := f.Write(buf)
if err != nil {
return err
}
buf = buf[n:]
}
return nil
}
// CopyFile doesn't care if the file already exists, it just overwrites it.
// It does however, try to be at least a little bit atomic
func CopyFile(src, dst string) error {
_, name := filepath.Split(src)
tmpFile, err := ioutil.TempFile(os.TempDir(), name)
defer tmpFile.Close() //nolint (errcheck)
if err != nil {
return err
}
tmpFileName := tmpFile.Name()
defer os.Remove(tmpFileName) //nolint (errcheck)
srcFile, err := os.Open(src)
defer srcFile.Close() //nolint (errcheck)
if err != nil {
return err
}
for err == nil {
var n int
var buf [4 * 1024]byte
n, err = srcFile.Read(buf[:])
if err != nil && err != io.EOF {
return err
}
writeAll(tmpFile, buf[:n]) //nolint (errcheck)
}
// if we get here, tmpFile has a copy of src
tmpFile.Close() //nolint (errcheck)
srcFile.Close() //nolint (errcheck)
return os.Rename(tmpFileName, dst)
}