2019-01-11 15:35:26 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package sync2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2019-06-05 16:03:11 +01:00
|
|
|
|
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2019-01-11 15:35:26 +00:00
|
|
|
)
|
|
|
|
|
2019-06-05 16:03:11 +01:00
|
|
|
var mon = monkit.Package()
|
|
|
|
|
2019-01-11 15:35:26 +00:00
|
|
|
type readerFunc func(p []byte) (n int, err error)
|
|
|
|
|
|
|
|
func (rf readerFunc) Read(p []byte) (n int, err error) { return rf(p) }
|
|
|
|
|
|
|
|
// Copy implements copying with cancellation
|
|
|
|
func Copy(ctx context.Context, dst io.Writer, src io.Reader) (written int64, err error) {
|
2019-06-05 16:03:11 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-01-11 15:35:26 +00:00
|
|
|
written, err = io.Copy(dst, readerFunc(func(p []byte) (int, error) {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return 0, ctx.Err()
|
|
|
|
default:
|
|
|
|
return src.Read(p)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
return written, err
|
|
|
|
}
|