-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add FileCopyMethod option / API
This will be used so we can have multiple implementations for copying a regular file. Currently, there is just CopyBytes, which is the old fcopy code. The FileCopyMethod struct is a struct with an un-exported member. This is done to make the type opaque so the user can't make their own copy function. There are still design questions left about passing around the `Options` struct and `os.FileInfo`, so I opted to do this so that can be figured out later. The type can be made into `type FileCopyMethod func(...)` later, and it won't be a breaking change. Or, instead, a `UserCopy(func (src, dest string) error) FileCopyMethod` function can be added.
- Loading branch information
Showing
3 changed files
with
102 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package copy | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// 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) { | ||
|
||
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 | ||
} | ||
return | ||
} | ||
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 | ||
} | ||
chmodfunc(&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 | ||
} | ||
|
||
if opt.Sync { | ||
err = f.Sync() | ||
} | ||
|
||
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 | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters