-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.go
263 lines (213 loc) · 4.65 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package copy
import (
"context"
"fmt"
"hash"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
)
// Copy copies source file/folder to destination with given options.
func Copy(ctx context.Context, src, dst string, opts ...optFunc) error {
opt := defaultOptions()
for _, fn := range opts {
fn(opt)
}
src, err := resolvePath(src)
if err != nil {
return err
}
// Attempt to rename file/folder instead of copying and then removing.
// If call to rename was finished with an error, it will be ignored
// and copy algorithm will be used.
if opt.move {
if err := rename(src, dst, opt.hash); err == nil {
return os.RemoveAll(src)
}
// Reset hash if rename was unsuccessful, since it will be
// recalculated with copy if needed.
opt.hash.Reset()
}
return copy(ctx, src, dst, opt)
}
func rename(src, dst string, h hash.Hash) error {
info, err := os.Stat(src)
if err != nil {
return err
}
if !info.IsDir() {
if h != nil {
f, err := os.Open(src)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(h, f); err != nil {
return err
}
}
info, err = os.Stat(filepath.Dir(src))
if err != nil {
return err
}
}
if err := os.MkdirAll(filepath.Dir(dst), info.Mode()); err != nil {
return err
}
return os.Rename(src, dst)
}
func copy(ctx context.Context, src, dst string, opt *options) error {
srcInfo, err := os.Stat(src)
if err != nil {
return err
}
if srcInfo.IsDir() {
return copyFolder(ctx, src, dst, opt)
}
dstDir, f := path.Split(dst)
if f == "" {
f = path.Base(src)
}
info, err := os.Stat(path.Dir(src))
if err != nil {
return err
}
if err := os.MkdirAll(dstDir, info.Mode()); err != nil {
return err
}
dst = path.Join(dstDir, f)
if _, err := os.Stat(dst); os.IsNotExist(err) || opt.force {
return copyFile(ctx, src, dst, opt)
}
return nil
}
// copyFolder is a support function to copy folder and its' content.
func copyFolder(ctx context.Context, src, dst string, opt *options) error {
if !opt.contentOnly {
dst = path.Join(dst, path.Base(src))
}
if err := filepath.Walk(
src, func(root string, info fs.FileInfo, err error,
) error {
if err != nil {
return err
}
subDst := strings.ReplaceAll(root, src, dst)
if info.IsDir() {
select {
case <-ctx.Done():
return ctx.Err()
default:
return os.MkdirAll(subDst, info.Mode())
}
} else {
if _, err := os.Stat(subDst); os.IsNotExist(err) || opt.force {
return copyFile(ctx, root, subDst, opt)
}
}
return nil
}); err != nil {
return err
}
if opt.move {
return os.RemoveAll(src)
}
return nil
}
// copyFile is a support function to copy file content.
func copyFile(ctx context.Context, src, dst string, opt *options) error {
srcF, err := os.Open(src)
if err != nil {
return err
}
defer srcF.Close()
stat, err := os.Stat(src)
if err != nil {
return err
}
dstF, err := os.OpenFile(
dst,
os.O_RDWR|os.O_CREATE|os.O_TRUNC,
stat.Mode().Perm(),
)
if err != nil {
return err
}
defer dstF.Close()
var mw io.Writer
if opt.hash != nil {
mw = io.MultiWriter(dstF, opt.hash)
} else {
mw = dstF
}
if cErr := copyBytes(ctx, srcF, mw, opt.bufSize, opt.hash); cErr != nil {
if opt.revert {
if rErr := os.Remove(dst); rErr != nil {
return fmt.Errorf("%w: %s", cErr, rErr)
}
}
return cErr
}
if opt.move {
return os.Remove(src)
}
return nil
}
// copyBytes is a support function to copy bytes from [io.Reader] to [io.Writer] with given
// size buffer and hash.
func copyBytes(ctx context.Context, r io.Reader, w io.Writer, size int, h hash.Hash) error {
buf := make([]byte, size)
if h != nil {
w = io.MultiWriter(w, h)
}
srcReader := &readerWithContext{ctx, r}
dstWriter := &writerWithContext{ctx, w}
for {
b, err := srcReader.Read(buf)
if err != nil && err != io.EOF {
return err
}
if b == 0 {
return nil
}
if _, err := dstWriter.Write(buf[:b]); err != nil {
return err
}
}
}
// resolvePath resolves symlinks and relative paths.
func resolvePath(p string) (string, error) {
info, err := os.Lstat(p)
if err != nil {
return "", err
}
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
if p, err = filepath.EvalSymlinks(p); err != nil {
return "", err
}
}
return filepath.Abs(p)
}
type readerWithContext struct {
ctx context.Context
r io.Reader
}
func (r *readerWithContext) Read(b []byte) (int, error) {
if err := r.ctx.Err(); err != nil {
return 0, err
}
return r.r.Read(b)
}
type writerWithContext struct {
ctx context.Context
w io.Writer
}
func (w *writerWithContext) Write(b []byte) (int, error) {
if err := w.ctx.Err(); err != nil {
return 0, err
}
return w.w.Write(b)
}