-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.go
81 lines (73 loc) · 1.27 KB
/
copy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package cmdio
import (
"io"
"golang.org/x/sync/errgroup"
)
// Copy copies the output of each stream into the input of the next stream.
// When output is finished copying from one stream, the receiving stream is
// closed if it is an [io.Closer].
func Copy(
dst io.Writer, src io.Reader, mid ...io.ReadWriter,
) (written int64, err error) {
var (
g errgroup.Group
r io.Reader
w io.Writer
count = make(chan int64)
total = make(chan int64)
)
go func() {
var written int64
for n := range count {
written += n
}
total <- written
}()
for i := -1; i < len(mid); i++ {
if i < 0 {
r = src
} else {
r = mid[i]
}
if i == len(mid)-1 {
w = dst
} else {
w = mid[i+1]
}
i := i
w := w
r := r
g.Go(func() (err error) {
defer func() {
if c, ok := w.(io.Closer); ok {
err1 := c.Close()
if err == nil {
err = err1
}
}
}()
if n, err := io.Copy(w, r); err != nil {
return copyError{err, i + 1}
} else {
count <- n
}
return nil
})
}
err = g.Wait()
close(count)
return <-total, err
}
type copyError struct {
err error
off int
}
func (e copyError) Error() string {
return e.err.Error()
}
func (e copyError) Unwrap() error {
return e.err
}
func (e copyError) Offset() int {
return e.off
}