-
Notifications
You must be signed in to change notification settings - Fork 1
/
filejob.go
85 lines (70 loc) · 1.21 KB
/
filejob.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 main
import (
"hash"
"hash/fnv"
"io"
"os"
)
type fileJob struct {
Fpath string
Info os.FileInfo
Err *myError
Chksum uint64
IoLen int64 // Actual amount of bytes read for stats
}
func (f *fileJob) Stat() {
if f.Info == nil {
f.Info, _ = os.Stat(f.Fpath)
f.Err = NewError(code_NOT_FOUND, f, "could not stat file")
}
}
func (f *fileJob) CalculateChecksum(
h hash.Hash64,
data []byte,
tr *ReadThrottler) {
var err error
if f.Err != nil {
return
}
f.Stat()
if f.Err != nil {
return
}
var file *os.File
file, err = os.Open(f.Fpath)
f.Err = WrapError(err)
if f.Err != nil {
return
}
defer file.Close()
h.Reset()
tr.SetReader(file)
for {
count, err := tr.Read(data)
if err != nil && err != io.EOF {
f.Err = WrapError(err)
return
}
if count == 0 {
break
}
h.Write(data[:count])
f.IoLen += int64(count)
}
f.Chksum = h.Sum64()
}
func Calculator(in, out chan *fileJob, buffSize int64, rate float64) {
defer func() { out <- nil }()
h := fnv.New64()
data := make([]byte, buffSize)
t := new(Throttler)
t.Start(rate)
reader := NewReadThrottler(t)
for f := range in {
if f == nil {
return
}
f.CalculateChecksum(h, data, reader)
out <- f
}
}