-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcopy.go
55 lines (51 loc) · 1.12 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
package fileutils
import (
"io"
"io/ioutil"
"os"
"path/filepath"
)
// A Copier copies files.
// The operation of Copier's public functions are controled by its
// public fields. If none are set, the Copier behaves accoriding to
// the zero value rules of each public field.
type Copier struct {
}
// CopyFile copies the contents of src to dst atomically.
func (c *Copier) CopyFile(dst, src string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
tmp, err := ioutil.TempFile(filepath.Dir(dst), "copyfile")
if err != nil {
return err
}
_, err = io.Copy(tmp, in)
if err != nil {
tmp.Close()
os.Remove(tmp.Name())
return err
}
if err := tmp.Close(); err != nil {
os.Remove(tmp.Name())
return err
}
const perm = 0644
if err := os.Chmod(tmp.Name(), perm); err != nil {
os.Remove(tmp.Name())
return err
}
if err := os.Rename(tmp.Name(), dst); err != nil {
os.Remove(tmp.Name())
return err
}
return nil
}
// CopyFile is a convenience method that calls CopyFile on a Copier
// zero value.
func CopyFile(dst, src string) error {
var c Copier
return c.CopyFile(dst, src)
}