-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.go
143 lines (127 loc) · 2.76 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package fshelper
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
)
type FlagsCopy uint
const (
CopyContent FlagsCopy = 1 << iota
CopyRecursive
CopyPreserveMode
CopyPreserveOwner
CopyParents
CopyVerbose
copyOneFile
)
const (
DefaultDirMode = 0755
DefaultFileMode = 0644
)
var ErrNotSupported = errors.New("not supported")
func Copy(flags FlagsCopy, src, dest string) error {
srcPrefix := string(os.PathSeparator)
if CopyParents&flags != 0 {
fullSrc, err := filepath.Abs(src)
if err != nil {
return err
}
src = fullSrc
} else {
if CopyContent&flags != 0 {
srcPrefix = src
} else {
srcPrefix = filepath.Dir(src)
}
}
if CopyRecursive&flags != 0 {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if path == src && CopyContent&flags != 0 {
return nil
}
if err != nil {
return err
}
if info.IsDir() {
return copyDirectory(srcPrefix, path, dest, info, flags)
} else {
return copyFile(srcPrefix, path, dest, info, flags)
}
})
}
info, err := os.Stat(src)
if err != nil {
return err
}
if info.IsDir() {
return errors.New("src is directory, CopyRecursive is required")
} else {
return copyFile(srcPrefix, src, dest, info, flags|copyOneFile)
}
}
func copyFile(srcPrefix, src, dest string, info os.FileInfo, flags FlagsCopy) (err error) {
relPart, err := filepath.Rel(srcPrefix, src)
if err != nil {
return err
}
_, destFileName := filepath.Split(dest)
if copyOneFile&flags == 0 || filepath.Dir(relPart) != "." || destFileName == "" {
dest = filepath.Join(dest, relPart)
}
if CopyVerbose&flags != 0 {
fmt.Printf("f: %s -> %s ", src, dest)
defer func() {
if err == nil {
fmt.Println(": OK!")
} else {
fmt.Printf(": %s\n", err)
}
}()
}
destFile, err := os.OpenFile(dest, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, DefaultFileMode)
if err != nil {
return err
}
defer destFile.Close()
if CopyPreserveMode&flags != 0 {
if err = os.Chmod(destFile.Name(), info.Mode()); err != nil {
return err
}
}
if CopyPreserveOwner&flags != 0 {
preserveOwner(destFile, info)
}
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
_, err = io.Copy(destFile, srcFile)
return err
}
func copyDirectory(srcPrefix, src, dest string, info os.FileInfo, flags FlagsCopy) (err error) {
destPart, err := filepath.Rel(srcPrefix, src)
if err != nil {
return err
}
dest = filepath.Join(dest, destPart)
if CopyVerbose&flags != 0 {
fmt.Printf("d: %s -> %s ", src, dest)
defer func() {
if err == nil {
fmt.Println(": OK!")
} else {
fmt.Printf(": %s\n", err)
}
}()
}
mode := os.ModeDir
if CopyPreserveMode&flags != 0 {
mode |= info.Mode()
} else {
mode |= DefaultDirMode
}
return os.MkdirAll(dest, mode)
}