Skip to content

Commit

Permalink
refactor: Split common code out of CopyBytes
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
eth-p committed Sep 14, 2024
1 parent 9205813 commit b4dd789
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
41 changes: 39 additions & 2 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,45 @@ func copyNextOrSkip(src, dest string, info os.FileInfo, opt Options) error {
// with considering existence of parent directory
// and file permission.
func fcopy(src, dest string, info os.FileInfo, opt Options) (err error) {
err, _ = opt.FileCopyMethod.fcopy(src, dest, info, opt)
return err
if err = os.MkdirAll(filepath.Dir(dest), os.ModePerm); err != nil {
return
}

// Use FileCopyMethod to do copy.
err, skipFile := opt.FileCopyMethod.fcopy(src, dest, info, opt)
if skipFile {
return nil
}

if err != nil {
return err
}

// Change file permissions.
chmodfunc, err := opt.PermissionControl(info, dest)
if err != nil {
return err
}

chmodfunc(&err)
if err != nil {
return err
}

// Preserve file ownership and times.
if opt.PreserveOwner {
if err := preserveOwner(src, dest, info); err != nil {
return err
}
}

if opt.PreserveTimes {
if err := preserveTimes(info, dest); err != nil {
return err
}
}

return
}

// dcopy is for a directory,
Expand Down
23 changes: 0 additions & 23 deletions copy_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"io"
"os"
"path/filepath"
)

// ErrUnsupportedCopyMethod is returned when the FileCopyMethod specified in
Expand All @@ -17,7 +16,6 @@ var ErrUnsupportedCopyMethod = errors.New(
// 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)
Expand All @@ -32,22 +30,12 @@ var CopyBytes = FileCopyMethod{
}
defer fclose(readcloser, &err)

if err = os.MkdirAll(filepath.Dir(dest), os.ModePerm); err != nil {
return
}

f, err := os.Create(dest)
if err != nil {
return
}
defer fclose(f, &err)

chmodfunc, err := opt.PermissionControl(info, dest)
if err != nil {
return err, false
}
chmodfunc(&err)

var buf []byte = nil
var w io.Writer = f
var r io.Reader = readcloser
Expand All @@ -72,17 +60,6 @@ var CopyBytes = FileCopyMethod{
err = f.Sync()
}

if opt.PreserveOwner {
if err := preserveOwner(src, dest, info); err != nil {
return err, false
}
}
if opt.PreserveTimes {
if err := preserveTimes(info, dest); err != nil {
return err, false
}
}

return
},
}

0 comments on commit b4dd789

Please sign in to comment.