-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare.go
85 lines (71 loc) · 1.68 KB
/
prepare.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
82
83
84
85
package acp
import (
"context"
"fmt"
"io"
"os"
"sync"
"github.com/samuelncui/acp/mmap"
)
func (c *Copyer) prepare(ctx context.Context, indexed <-chan *baseJob) <-chan *writeJob {
chanLen := 32
if c.fromDevice.linear {
chanLen = 0
}
var wg sync.WaitGroup
ch := make(chan *writeJob, chanLen)
defer func() {
go wrap(ctx, func() {
defer close(ch)
wg.Wait()
})
}()
for idx := 0; idx < c.fromDevice.threads; idx++ {
wg.Add(1)
go wrap(ctx, func() {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case job, ok := <-indexed:
if !ok {
return
}
job.setStatus(jobStatusPreparing)
file, size, err := func(path string) (io.ReadCloser, int64, error) {
if c.fromDevice.linear {
file, err := os.Open(path)
if err != nil {
return nil, 0, fmt.Errorf("open src file fail, %w", err)
}
fileInfo, err := file.Stat()
if err != nil {
return nil, 0, fmt.Errorf("get src file stat fail, %w", err)
}
if fileInfo.Size() == 0 {
return nil, 0, fmt.Errorf("get src file, size is zero")
}
return file, fileInfo.Size(), nil
}
readerAt, err := mmap.Open(path)
if err != nil {
return nil, 0, fmt.Errorf("open src file by mmap fail, %w", err)
}
if readerAt.Len() == 0 {
return nil, 0, fmt.Errorf("get src file by mmap, size is zero")
}
return mmap.NewReader(readerAt), int64(readerAt.Len()), nil
}(job.path)
if err != nil {
c.reportError(job.path, "", err)
}
wj := newWriteJob(job, file, size, c.fromDevice.linear)
ch <- wj
wj.wait()
}
}
})
}
return ch
}