forked from folbricht/desync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifyindex.go
83 lines (72 loc) · 1.84 KB
/
verifyindex.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
package desync
import (
"context"
"crypto/sha512"
"fmt"
"io"
"os"
"golang.org/x/sync/errgroup"
)
// VerifyIndex re-calculates the checksums of a blob comparing it to a given index.
// Fails if the index does not match the blob.
func VerifyIndex(ctx context.Context, name string, idx Index, n int, pb ProgressBar) error {
in := make(chan IndexChunk)
g, ctx := errgroup.WithContext(ctx)
// Setup and start the progressbar if any
if pb != nil {
pb.SetTotal(len(idx.Chunks))
pb.Start()
defer pb.Finish()
}
stat, err := os.Stat(name)
if err != nil {
return err
}
if stat.Size() != int64(idx.Length()) {
return fmt.Errorf("index size (%d) does not match file size (%d)", idx.Length(), stat.Size())
}
// Start the workers, each having its own filehandle to read concurrently
for i := 0; i < n; i++ {
f, err := os.Open(name)
if err != nil {
return fmt.Errorf("unable to open file %s, %s", name, err)
}
defer f.Close()
g.Go(func() error {
for c := range in {
// Update progress bar if any
if pb != nil {
pb.Increment()
}
// Position the filehandle to the place where the chunk is meant to come
// from within the file
if _, err := f.Seek(int64(c.Start), io.SeekStart); err != nil {
return err
}
// Read the whole (uncompressed) chunk into memory
b := make([]byte, c.Size)
if _, err := io.ReadFull(f, b); err != nil {
return err
}
// Calculate this chunks checksum and compare to what it's supposed to be
// according to the index
sum := sha512.Sum512_256(b)
if sum != c.ID {
return fmt.Errorf("checksum does not match chunk %s", c.ID)
}
}
return nil
})
}
// Feed the workers, stop if there are any errors
loop:
for _, c := range idx.Chunks {
select {
case <-ctx.Done():
break loop
case in <- c:
}
}
close(in)
return g.Wait()
}