-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.go
211 lines (203 loc) · 5.94 KB
/
verify.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"bufio"
"fmt"
"hash/crc32"
"io"
"os"
"regexp"
"strconv"
"strings"
)
func verifyPresFile(inFilename string) {
confs, err := readConfs(inFilename)
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading conf sections:", err.Error())
os.Exit(2)
}
correctConfs := getCorrectConfs(confs)
warned := false
if len(correctConfs) == 0 {
fmt.Println("Could not find unharmed conf block.")
os.Exit(2)
} else if len(correctConfs) < 3 {
fmt.Fprintln(os.Stderr, "WARNING: One conf block is damaged!")
warned = true
} else {
fmt.Fprintln(os.Stderr, "All conf blocks are intact.")
}
conf := correctConfs[0]
generatedHashes, err := generateHashes(inFilename, conf)
if err != nil {
fmt.Fprintln(os.Stderr, "Error calculating hashes:", err.Error())
os.Exit(3)
}
matchingHashes := countMatchingHashes(generatedHashes, conf.shardCRC32Cs)
var shardCnt uint8 = conf.dataShardCnt + conf.parityShardCnt
fmt.Fprintln(os.Stderr, matchingHashes, "out of", shardCnt,
"shards are intact.")
if matchingHashes < conf.dataShardCnt {
fmt.Println("Restoration impossible: not enought shards are intact.")
os.Exit(4)
} else if matchingHashes < shardCnt {
damagedShards := shardCnt - matchingHashes
fmt.Fprintln(os.Stderr, "WARNING:", damagedShards,
"shard(s) is/are damaged!")
warned = true
}
if warned {
fmt.Println("Restore data and newly create the *.pres file to remove",
"warnings.")
} else {
fmt.Println("No problems found.")
}
}
func readConfs(inFilename string) ([]conf, error) {
confs := make([]conf, 3)
inFile, err := os.Open(inFilename)
if err != nil {
return nil, err
}
defer inFile.Close()
fileSize, err := getDataLen(inFile)
if err != nil {
return nil, err
}
// Seek to a point where the metadata isn't far away (for performance):
if _, err = inFile.Seek(-min64(fileSize, 32e3), 2); err != nil {
return nil, err
}
inputReader := bufio.NewReader(inFile)
var confIndex int = -1
var line string
reShard := regexp.MustCompile(`^shard_[0-9]*_crc32c=.*`)
for err = nil; err == nil; line, err = inputReader.ReadString('\n') {
line = strings.TrimSpace(line)
switch line {
case "[conf]":
confIndex = 0
continue
case "[conf_copy_1]":
confIndex = 1
continue
case "[conf_copy_2]":
confIndex = 2
continue
}
if confIndex < 0 {
// There were probably damaged lines at the beginning of the metadata.
continue
}
switch {
case strings.HasPrefix(line, "version="):
confs[confIndex].version = strings.SplitAfterN(line, "=", 2)[1]
case strings.HasPrefix(line, "data_len="):
s := strings.SplitAfterN(line, "=", 2)[1]
confs[confIndex].dataLen, _ = strconv.ParseInt(s, 10, 64)
case strings.HasPrefix(line, "data_shard_cnt="):
s := strings.SplitAfterN(line, "=", 2)[1]
x, _ := strconv.ParseUint(s, 10, 8)
confs[confIndex].dataShardCnt = uint8(x)
case strings.HasPrefix(line, "parity_shard_cnt="):
s := strings.SplitAfterN(line, "=", 2)[1]
x, _ := strconv.ParseUint(s, 10, 8)
confs[confIndex].parityShardCnt = uint8(x)
shardCnt := confs[confIndex].dataShardCnt + uint8(x)
confs[confIndex].shardCRC32Cs = make([]string, shardCnt)
case reShard.Match([]byte(line)):
s := strings.SplitAfterN(line, "_", 3)[1]
s = strings.Trim(s, "_")
x, _ := strconv.ParseUint(s, 10, 8)
shardIndex := uint8(x - 1)
if int(shardIndex) >= len(confs[confIndex].shardCRC32Cs) {
// Something went wrong; just go on
continue
}
s = strings.SplitAfterN(line, "=", 2)[1]
confs[confIndex].shardCRC32Cs[shardIndex] = s
}
}
if err != nil && err != io.EOF {
return nil, err
}
return confs, nil
}
func getCorrectConfs(confs []conf) []conf {
correctConfs := make([]conf, 0, 3)
if confs[0].seemsOK() &&
(confs[0].equals(confs[1]) || confs[0].equals(confs[2])) {
correctConfs = append(correctConfs, confs[0])
}
if confs[1].seemsOK() &&
(confs[1].equals(confs[0]) || confs[1].equals(confs[2])) {
correctConfs = append(correctConfs, confs[1])
}
if confs[2].seemsOK() &&
(confs[2].equals(confs[1]) || confs[2].equals(confs[0])) {
correctConfs = append(correctConfs, confs[2])
}
return correctConfs
}
func generateHashes(inFilename string, conf conf) ([]string, error) {
readers, files, err := getShardReaders(inFilename, conf)
if err != nil {
return nil, err
}
defer func() {
for _, file := range files {
file.Close()
}
}()
return generateHashesFromReaders(readers, conf)
}
func countMatchingHashes(generatedHashes, storedHashes []string) uint8 {
var matchingHashes uint8
if len(generatedHashes) != len(storedHashes) {
return 0
}
for i := range generatedHashes {
if generatedHashes[i] == storedHashes[i] {
matchingHashes += 1
}
}
return matchingHashes
}
func getShardReaders(inFilename string, conf conf) ([]io.Reader, []*os.File, error) {
shardSize := calculateShardSize(conf.dataLen, conf.dataShardCnt)
files := make([]*os.File, conf.dataShardCnt+conf.parityShardCnt)
readers := make([]io.Reader, conf.dataShardCnt+conf.parityShardCnt)
for i := 0; i < int(conf.dataShardCnt+conf.parityShardCnt); i += 1 {
var err error
files[i], err = os.Open(inFilename)
if err != nil {
return nil, nil, err
}
offset := int64(i) * shardSize
if i >= int(conf.dataShardCnt) {
offset = conf.dataLen + int64(i-int(conf.dataShardCnt))*shardSize
}
if _, err = files[i].Seek(offset, 0); err != nil {
return nil, nil, err
}
readers[i] = files[i]
if i != int(conf.dataShardCnt-1) {
readers[i] = io.LimitReader(readers[i], shardSize)
} else {
size := conf.dataLen - (int64(i) * shardSize)
readers[i] = io.LimitReader(readers[i], size)
}
}
return readers, files, nil
}
func generateHashesFromReaders(readers []io.Reader, conf conf) ([]string, error) {
hashes := make([]string, len(readers))
hasher := crc32.New(crc32.MakeTable(crc32.Castagnoli))
for i := range readers {
if _, err := bufio.NewReader(readers[i]).WriteTo(hasher); err != nil {
return nil, err
}
hashes[i] = fmt.Sprint(hasher.Sum32())
hasher.Reset()
}
return hashes, nil
}