-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
73 lines (63 loc) · 1.33 KB
/
file.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"errors"
"fmt"
"io"
"os"
)
// see https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file
// safely copy file with checks that destination does not exists
func copyFileSafe(src, dst string) (err error) {
dstExists, _ := fileExists(dst)
if dstExists {
return fmt.Errorf("destination file %s already exists", dst)
}
err = copyFileContents(src, dst)
return
}
func copyFileContents(src, dst string) (err error) {
// check source file
srcStat, err := os.Stat(src)
if err != nil {
return
}
if !srcStat.Mode().IsRegular() {
return fmt.Errorf("non-regular source file %s", src)
}
srcFile, err := os.Open(src)
if err != nil {
return
}
defer srcFile.Close()
dstFile, err := os.Create(dst)
if err != nil {
return
}
defer func() {
closeDstErr := dstFile.Close()
if err == nil {
err = closeDstErr // if no error, try propagate close error if any
}
}()
if _, err = io.Copy(dstFile, srcFile); err != nil {
return
}
err = dstFile.Sync()
return
}
func fileExists(name string) (bool, error) {
_, err := os.Stat(name)
if err == nil {
// file apparently exists
return true, nil
} else {
// got error, let's see
if errors.Is(err, os.ErrNotExist) {
// file not exists, so no actual error here
return false, nil
} else {
// other error
return false, err
}
}
}