-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore.go
205 lines (195 loc) · 5.29 KB
/
restore.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
package main
import (
"errors"
"fmt"
"github.com/klauspost/reedsolomon"
"io"
"io/ioutil"
"os"
"strings"
)
const (
intact = true
damaged = false
)
func restoreData(inFilename string) {
outFilename, err := getDataOutFilename(inFilename)
if err != nil {
fmt.Fprintln(os.Stderr, "Error choosing output filename:", err.Error())
os.Exit(1)
}
if _, err := os.Stat(outFilename); !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "'%s' already exists.\n", outFilename)
os.Exit(1)
}
fmt.Fprintln(os.Stderr, "Checking shards for damage.")
conf, err := getConf(inFilename)
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading *.pres file:", err.Error())
os.Exit(2)
}
shardStates, err := getShardStates(inFilename, conf)
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading *.pres file:", err.Error())
os.Exit(2)
}
fmt.Fprintln(os.Stderr, "Restoring damaged shards.")
restoredShards, err := restore(inFilename, shardStates, conf)
if err != nil {
fmt.Fprintln(os.Stderr, "Error restoring damaged shards:", err.Error())
os.Exit(3)
}
fmt.Fprintln(os.Stderr, "Verifying restored data.")
err = verify(inFilename, restoredShards, conf)
if err != nil {
fmt.Fprintln(os.Stderr, "Error verifying restored shards:", err.Error())
os.Exit(4)
}
fmt.Fprintf(os.Stderr, "Writing '%s'.\n", outFilename)
err = writeOutput(inFilename, restoredShards, conf)
if err != nil {
fmt.Fprintln(os.Stderr, "Error writing output:", err.Error())
os.Exit(5)
}
if err = removeFiles(restoredShards); err != nil {
fmt.Fprintln(os.Stderr, "Error removing temporary files:", err.Error())
os.Exit(6)
}
}
func getConf(inFilename string) (conf, error) {
confs, err := readConfs(inFilename)
if err != nil {
var dummy conf
return dummy, err
}
correctConfs := getCorrectConfs(confs)
if len(correctConfs) == 0 {
var dummy conf
return dummy, errors.New("could not find unharmed conf block")
}
return correctConfs[0], nil
}
func getShardStates(inFilename string, conf conf) ([]bool, error) {
generatedHashes, err := generateHashes(inFilename, conf)
if err != nil {
return nil, err
}
shardStates := make([]bool, conf.dataShardCnt+conf.parityShardCnt)
for i, hash := range conf.shardCRC32Cs {
if hash == generatedHashes[i] {
shardStates[i] = intact
}
}
return shardStates, nil
}
func restore(inFilename string, shardStates []bool, conf conf) ([]string, error) {
readers, files, err := getShardReaders(inFilename, conf)
if err != nil {
return nil, err
}
defer func() {
for _, file := range files {
file.Close()
}
}()
i := conf.dataShardCnt - 1
readers[i] = fillLastDataReader(readers[i], conf.dataShardCnt, conf.dataLen)
outFilenames := make([]string, len(readers))
writers := make([]io.Writer, len(readers))
for i, shardState := range shardStates {
if shardState == damaged {
readers[i] = nil
outFile, err := ioutil.TempFile("", "pres_restored_shard_*")
if err != nil {
return nil, err
}
defer outFile.Close()
outFilenames[i] = outFile.Name()
writers[i] = outFile
}
}
dataShardCnt := int(conf.dataShardCnt)
parityShardCnt := int(conf.parityShardCnt)
enc, err := reedsolomon.NewStream(dataShardCnt, parityShardCnt)
if err != nil {
return nil, err
}
err = enc.Reconstruct(readers, writers)
return outFilenames, err
}
func verify(inFilename string, restoredShards []string, conf conf) error {
readers, files, err := getRestoredReaders(inFilename, restoredShards, conf)
if err != nil {
return err
}
defer func() {
for _, file := range files {
file.Close()
}
}()
dataShardCnt := int(conf.dataShardCnt)
parityShardCnt := int(conf.parityShardCnt)
enc, err := reedsolomon.NewStream(dataShardCnt, parityShardCnt)
if err != nil {
return err
}
isOK, err := enc.Verify(readers)
if !isOK {
return errors.New("parity shards contain wrong data")
}
return err
}
func writeOutput(inFilename string, restoredShards []string, conf conf) error {
readers, files, err := getRestoredReaders(inFilename, restoredShards, conf)
if err != nil {
return err
}
defer func() {
for _, file := range files {
file.Close()
}
}()
dataShardCnt := int(conf.dataShardCnt)
parityShardCnt := int(conf.parityShardCnt)
enc, err := reedsolomon.NewStream(dataShardCnt, parityShardCnt)
if err != nil {
return err
}
outFilename, err := getDataOutFilename(inFilename)
if err != nil {
return err
}
outFile, err := os.Create(outFilename)
if err != nil {
return err
}
defer outFile.Close()
var writer io.Writer = outFile
return enc.Join(writer, readers, conf.dataLen)
}
func getRestoredReaders(inFilename string, restoredShards []string, conf conf) ([]io.Reader, []*os.File, error) {
readers, files, err := getShardReaders(inFilename, conf)
if err != nil {
return nil, nil, err
}
i := conf.dataShardCnt - 1
readers[i] = fillLastDataReader(readers[i], conf.dataShardCnt, conf.dataLen)
for i, restoredShard := range restoredShards {
if restoredShard != "" {
file, err := os.Open(restoredShard)
if err != nil {
return nil, nil, err
}
files = append(files, file)
readers[i] = file
}
}
return readers, files, nil
}
func getDataOutFilename(inFilename string) (string, error) {
if !strings.HasSuffix(inFilename, ".pres") {
return "", errors.New("input file does not have .pres suffix")
}
outFilename := strings.TrimSuffix(inFilename, ".pres")
return outFilename, nil
}