-
-
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.
Merge pull request #164 from eth-p/feat-copymethod-api
feat: Add FileCopyMethod option / API
- Loading branch information
Showing
4 changed files
with
96 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ on: | |
branches: [ main, develop ] | ||
pull_request: | ||
branches: [ main, develop ] | ||
workflow_dispatch: | ||
|
||
jobs: | ||
|
||
|
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,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 | ||
}, | ||
} |
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