-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy.go
56 lines (48 loc) · 893 Bytes
/
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
package pathutil
import (
"io"
"os"
)
func (srcPath PathImpl) CopyFile(dst string) (p Path, err error) {
dstPath, err := New(dst)
if err != nil {
return nil, err
}
if dstPath.IsDir() {
dstPath, err = New(dst, srcPath.Basename())
if err != nil {
return nil, err
} else {
dst = dstPath.String()
}
}
originalFile, err := os.Open(srcPath.String())
if err != nil {
return nil, err
}
defer func() {
if errClose := originalFile.Close(); errClose != nil {
err = errClose
}
}()
newFile, err := os.Create(dst)
if err != nil {
return nil, err
}
defer func() {
if errClose := newFile.Close(); errClose != nil {
err = errClose
}
}()
_, err = io.Copy(newFile, originalFile)
if err != nil {
return nil, err
}
// Commit the file contents
// Flushes memory to disk
err = newFile.Sync()
if err != nil {
return nil, err
}
return New(dst)
}