forked from lotusdblabs/lotusdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
82 lines (69 loc) · 1.36 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
74
75
76
77
78
79
80
81
82
package util
import (
"io"
"io/ioutil"
"os"
"path"
)
// PathExist check if the directory or file exists.
func PathExist(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
// CopyDir copy directory from src to dst.
func CopyDir(src string, dst string) error {
var (
err error
dir []os.FileInfo
srcInfo os.FileInfo
)
if srcInfo, err = os.Stat(src); err != nil {
return err
}
if err = os.MkdirAll(dst, srcInfo.Mode()); err != nil {
return err
}
if dir, err = ioutil.ReadDir(src); err != nil {
return err
}
for _, fd := range dir {
srcPath := path.Join(src, fd.Name())
dstPath := path.Join(dst, fd.Name())
if fd.IsDir() {
if err = CopyDir(srcPath, dstPath); err != nil {
return err
}
} else {
if err = CopyFile(srcPath, dstPath); err != nil {
return err
}
}
}
return nil
}
// CopyFile copy a single file.
func CopyFile(src, dst string) error {
var (
err error
srcFile *os.File
dstFie *os.File
srcInfo os.FileInfo
)
if srcFile, err = os.Open(src); err != nil {
return err
}
defer srcFile.Close()
if dstFie, err = os.Create(dst); err != nil {
return err
}
defer dstFie.Close()
if _, err = io.Copy(dstFie, srcFile); err != nil {
return err
}
if srcInfo, err = os.Stat(src); err != nil {
return err
}
return os.Chmod(dst, srcInfo.Mode())
}