|
@@ -40,8 +40,10 @@ type SizeCounter struct {
|
|
|
Size int64
|
|
Size int64
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// CopyOption is an option for copying data.
|
|
|
type CopyOption func(*copyHandler)
|
|
type CopyOption func(*copyHandler)
|
|
|
|
|
|
|
|
|
|
+// IgnoreReaderError is a CopyOption that ignores errors from reader. Copy will continue in such case.
|
|
|
func IgnoreReaderError() CopyOption {
|
|
func IgnoreReaderError() CopyOption {
|
|
|
return func(handler *copyHandler) {
|
|
return func(handler *copyHandler) {
|
|
|
handler.onReadError = append(handler.onReadError, func(err error) error {
|
|
handler.onReadError = append(handler.onReadError, func(err error) error {
|
|
@@ -50,6 +52,7 @@ func IgnoreReaderError() CopyOption {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// IgnoreWriterError is a CopyOption that ignores errors from writer. Copy will continue in such case.
|
|
|
func IgnoreWriterError() CopyOption {
|
|
func IgnoreWriterError() CopyOption {
|
|
|
return func(handler *copyHandler) {
|
|
return func(handler *copyHandler) {
|
|
|
handler.onWriteError = append(handler.onWriteError, func(err error) error {
|
|
handler.onWriteError = append(handler.onWriteError, func(err error) error {
|
|
@@ -58,6 +61,7 @@ func IgnoreWriterError() CopyOption {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// UpdateActivity is a CopyOption to update activity on each data copy operation.
|
|
|
func UpdateActivity(timer signal.ActivityUpdater) CopyOption {
|
|
func UpdateActivity(timer signal.ActivityUpdater) CopyOption {
|
|
|
return func(handler *copyHandler) {
|
|
return func(handler *copyHandler) {
|
|
|
handler.onData = append(handler.onData, func(MultiBuffer) {
|
|
handler.onData = append(handler.onData, func(MultiBuffer) {
|
|
@@ -66,6 +70,7 @@ func UpdateActivity(timer signal.ActivityUpdater) CopyOption {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// CountSize is a CopyOption that sums the total size of data copied into the given SizeCounter.
|
|
|
func CountSize(sc *SizeCounter) CopyOption {
|
|
func CountSize(sc *SizeCounter) CopyOption {
|
|
|
return func(handler *copyHandler) {
|
|
return func(handler *copyHandler) {
|
|
|
handler.onData = append(handler.onData, func(b MultiBuffer) {
|
|
handler.onData = append(handler.onData, func(b MultiBuffer) {
|
|
@@ -97,8 +102,7 @@ func copyInternal(reader Reader, writer Writer, handler *copyHandler) error {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Copy dumps all payload from reader to writer or stops when an error occurs.
|
|
|
|
|
-// ActivityTimer gets updated as soon as there is a payload.
|
|
|
|
|
|
|
+// Copy dumps all payload from reader to writer or stops when an error occurs. It returns nil when EOF.
|
|
|
func Copy(reader Reader, writer Writer, options ...CopyOption) error {
|
|
func Copy(reader Reader, writer Writer, options ...CopyOption) error {
|
|
|
handler := new(copyHandler)
|
|
handler := new(copyHandler)
|
|
|
for _, option := range options {
|
|
for _, option := range options {
|