/
copy.go
67 lines (53 loc) · 1.29 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
package pwr
import (
"io"
"sync"
"github.com/itchio/headway/counter"
"github.com/itchio/headway/state"
"github.com/itchio/lake/tlc"
"github.com/itchio/lake"
)
// CopyContainer copies from one container to the other. Combined with fspool
// and blockpool, it can be used to split a container into blocks or join it back
// into regular files.
func CopyContainer(container *tlc.Container, outPool lake.WritablePool, inPool lake.Pool, consumer *state.Consumer) error {
copyFile := func(byteOffset int64, fileIndex int64) error {
r, err := inPool.GetReader(fileIndex)
if err != nil {
return err
}
w, err := outPool.GetWriter(fileIndex)
if err != nil {
return err
}
var closeOnce sync.Once
defer closeOnce.Do(func() {
w.Close()
})
cw := counter.NewWriterCallback(func(count int64) {
alpha := float64(byteOffset+count) / float64(container.Size)
consumer.Progress(alpha)
}, w)
_, err = io.Copy(cw, r)
if err != nil {
return err
}
closeOnce.Do(func() {
err = w.Close()
})
if err != nil {
return err
}
return nil
}
byteOffset := int64(0)
for fileIndex, f := range container.Files {
consumer.ProgressLabel(f.Path)
err := copyFile(byteOffset, int64(fileIndex))
if err != nil {
return err
}
byteOffset += f.Size
}
return nil
}