-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
copy_methods.go
65 lines (55 loc) · 1.35 KB
/
copy_methods.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
package copy
import (
"errors"
"io"
"os"
)
// ErrUnsupportedCopyMethod is returned when the FileCopyMethod specified in
// Options is not supported.
var ErrUnsupportedCopyMethod = errors.New(
"copy method not supported",
)
// CopyBytes copies the file contents by reading the source file into a buffer,
// then writing the buffer back to the destination file.
var CopyBytes = FileCopyMethod{
fcopy: func(src, dest string, info os.FileInfo, opt Options) (err error, skipFile bool) {
var readcloser io.ReadCloser
if opt.FS != nil {
readcloser, err = opt.FS.Open(src)
} else {
readcloser, err = os.Open(src)
}
if err != nil {
if os.IsNotExist(err) {
return nil, true
}
return
}
defer fclose(readcloser, &err)
f, err := os.Create(dest)
if err != nil {
return
}
defer fclose(f, &err)
var buf []byte = nil
var w io.Writer = f
var r io.Reader = readcloser
if opt.WrapReader != nil {
r = opt.WrapReader(r)
}
if opt.CopyBufferSize != 0 {
buf = make([]byte, opt.CopyBufferSize)
// Disable using `ReadFrom` by io.CopyBuffer.
// See https://github.com/otiai10/copy/pull/60#discussion_r627320811 for more details.
w = struct{ io.Writer }{f}
// r = struct{ io.Reader }{s}
}
if _, err = io.CopyBuffer(w, r, buf); err != nil {
return err, false
}
if opt.Sync {
err = f.Sync()
}
return
},
}