Skip to content

Commit b4dd789

Browse files
committed
refactor: Split common code out of CopyBytes
Permission changes, ownership preservation, and access/modify/creation time presevation is something all implementations need. I brought it out of the CopyBytes implementation so it can be shared when more implementations are added later. A small note: The "ignore if source file deleted" check had to be brought out of `CopyBytes`. The way it worked before is it returned no error, which meant `fcopy` would try and modify the destination file (which was never created). Instead, I check for the error explicitly in `fcopy`.
1 parent 9205813 commit b4dd789

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

copy.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,45 @@ func copyNextOrSkip(src, dest string, info os.FileInfo, opt Options) error {
8686
// with considering existence of parent directory
8787
// and file permission.
8888
func fcopy(src, dest string, info os.FileInfo, opt Options) (err error) {
89-
err, _ = opt.FileCopyMethod.fcopy(src, dest, info, opt)
90-
return err
89+
if err = os.MkdirAll(filepath.Dir(dest), os.ModePerm); err != nil {
90+
return
91+
}
92+
93+
// Use FileCopyMethod to do copy.
94+
err, skipFile := opt.FileCopyMethod.fcopy(src, dest, info, opt)
95+
if skipFile {
96+
return nil
97+
}
98+
99+
if err != nil {
100+
return err
101+
}
102+
103+
// Change file permissions.
104+
chmodfunc, err := opt.PermissionControl(info, dest)
105+
if err != nil {
106+
return err
107+
}
108+
109+
chmodfunc(&err)
110+
if err != nil {
111+
return err
112+
}
113+
114+
// Preserve file ownership and times.
115+
if opt.PreserveOwner {
116+
if err := preserveOwner(src, dest, info); err != nil {
117+
return err
118+
}
119+
}
120+
121+
if opt.PreserveTimes {
122+
if err := preserveTimes(info, dest); err != nil {
123+
return err
124+
}
125+
}
126+
127+
return
91128
}
92129

93130
// dcopy is for a directory,

copy_methods.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"io"
66
"os"
7-
"path/filepath"
87
)
98

109
// ErrUnsupportedCopyMethod is returned when the FileCopyMethod specified in
@@ -17,7 +16,6 @@ var ErrUnsupportedCopyMethod = errors.New(
1716
// then writing the buffer back to the destination file.
1817
var CopyBytes = FileCopyMethod{
1918
fcopy: func(src, dest string, info os.FileInfo, opt Options) (err error, skipFile bool) {
20-
2119
var readcloser io.ReadCloser
2220
if opt.FS != nil {
2321
readcloser, err = opt.FS.Open(src)
@@ -32,22 +30,12 @@ var CopyBytes = FileCopyMethod{
3230
}
3331
defer fclose(readcloser, &err)
3432

35-
if err = os.MkdirAll(filepath.Dir(dest), os.ModePerm); err != nil {
36-
return
37-
}
38-
3933
f, err := os.Create(dest)
4034
if err != nil {
4135
return
4236
}
4337
defer fclose(f, &err)
4438

45-
chmodfunc, err := opt.PermissionControl(info, dest)
46-
if err != nil {
47-
return err, false
48-
}
49-
chmodfunc(&err)
50-
5139
var buf []byte = nil
5240
var w io.Writer = f
5341
var r io.Reader = readcloser
@@ -72,17 +60,6 @@ var CopyBytes = FileCopyMethod{
7260
err = f.Sync()
7361
}
7462

75-
if opt.PreserveOwner {
76-
if err := preserveOwner(src, dest, info); err != nil {
77-
return err, false
78-
}
79-
}
80-
if opt.PreserveTimes {
81-
if err := preserveTimes(info, dest); err != nil {
82-
return err, false
83-
}
84-
}
85-
8663
return
8764
},
8865
}

0 commit comments

Comments
 (0)