-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.go
More file actions
248 lines (233 loc) · 6.88 KB
/
create.go
File metadata and controls
248 lines (233 loc) · 6.88 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"fmt"
"github.com/klauspost/reedsolomon"
"hash"
"hash/crc32"
"io"
"io/ioutil"
"os"
)
func createPresFile(inFilename string) {
presFilename := fmt.Sprint(inFilename, ".pres")
if _, err := os.Stat(presFilename); !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "'%s' already exists.\n", presFilename)
os.Exit(1)
}
var conf conf
conf.dataShardCnt = 100
conf.parityShardCnt = 3
var err error
conf.dataLen, err = getFilesize(inFilename)
if err != nil {
fmt.Fprintln(os.Stderr, "Error checking input filesize:", err.Error())
os.Exit(1)
}
if conf.dataLen == 0 {
fmt.Fprintln(os.Stderr, "The input file is empty.")
os.Exit(1)
}
conf.dataShardCnt = reduceShardCntIfNecessary(conf)
hashers := getShardsHashers(conf)
fmt.Fprintln(os.Stderr, "Calculating parity information and checksums.")
parityFilenames, err := makeParityFilesAndCalculateHashes(inFilename, conf, hashers)
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating parity files:", err.Error())
os.Exit(2)
}
fmt.Fprintf(os.Stderr, "Appending output to '%s'.\n", inFilename)
if err = copyOverData(inFilename, parityFilenames...); err != nil {
fmt.Fprintln(os.Stderr, "Error writing parity to output:", err.Error())
os.Exit(3)
}
err = writeMetadata(inFilename, conf, hashers)
if err != nil {
fmt.Fprintln(os.Stderr, "Error writing metadata:", err.Error())
os.Exit(3)
}
fmt.Fprintf(os.Stderr, "Renaming '%s' to '%s'.\n", inFilename, presFilename)
if err = os.Rename(inFilename, presFilename); err != nil {
fmt.Fprintln(os.Stderr, "Error renaming to *.pres:", err.Error())
os.Exit(4)
}
if err = removeFiles(parityFilenames); err != nil {
fmt.Fprintln(os.Stderr, "Error removing temporary files:", err.Error())
os.Exit(5)
}
}
// reduceShardCntIfNecessary returns a reduced dataShardCnt, if the
// input file is too small for the previously set dataShardCnt.
func reduceShardCntIfNecessary(conf conf) uint8 {
shardSize := calculateShardSize(conf.dataLen, conf.dataShardCnt)
return uint8((conf.dataLen + shardSize - 1) / shardSize)
}
func getShardsHashers(conf conf) []hash.Hash32 {
hashers := make([]hash.Hash32, conf.dataShardCnt+conf.parityShardCnt)
for i := 0; i < int(conf.dataShardCnt+conf.parityShardCnt); i += 1 {
hashers[i] = crc32.New(crc32.MakeTable(crc32.Castagnoli))
}
return hashers
}
func makeParityFilesAndCalculateHashes(inFilename string, conf conf, hashers []hash.Hash32) ([]string, error) {
dataInputs, err := getDataInputs(inFilename, conf.dataShardCnt)
if err != nil {
return nil, err
}
dataInputReaders, err := toDataInputReaders(dataInputs, conf.dataShardCnt, hashers)
if err != nil {
return nil, err
}
parityOutputs, err := getParityOutputs(conf.parityShardCnt)
if err != nil {
return nil, err
}
parityOutputWriters := getParityOutputWriters(parityOutputs, conf, hashers)
err = writeParityFiles(dataInputReaders, conf, parityOutputWriters)
if err != nil {
return nil, err
}
for i := range dataInputs {
err = dataInputs[i].Close()
if err != nil {
return nil, err
}
}
parityFilenames := make([]string, conf.parityShardCnt)
for i, parityOutput := range parityOutputs {
parityFilenames[i] = parityOutput.Name()
err = parityOutput.Close()
if err != nil {
return nil, err
}
}
return parityFilenames, nil
}
func copyOverData(destFilename string, srcFilenames ...string) error {
destFile, err := os.OpenFile(destFilename, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer destFile.Close()
for _, srcFilename := range srcFilenames {
srcFile, err := os.Open(srcFilename)
if err != nil {
return err
}
defer srcFile.Close()
if _, err = io.CopyBuffer(destFile, srcFile, nil); err != nil {
return err
}
}
return nil
}
func writeMetadata(inFilename string, conf conf, hashers []hash.Hash32) error {
destFile, err := os.OpenFile(inFilename, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer destFile.Close()
shardsHashes := make([]string, conf.dataShardCnt+conf.parityShardCnt)
for i := range hashers {
shardsHashes[i] = fmt.Sprint(hashers[i].Sum32())
}
conf.version = "1"
conf.shardCRC32Cs = shardsHashes
if _, err := fmt.Fprintln(destFile, "\n\n[conf]"); err != nil {
return err
}
if err = writeConf(destFile, conf); err != nil {
return err
}
if _, err := fmt.Fprintln(destFile, "\n[conf_copy_1]"); err != nil {
return err
}
if err = writeConf(destFile, conf); err != nil {
return err
}
if _, err := fmt.Fprintln(destFile, "\n[conf_copy_2]"); err != nil {
return err
}
return writeConf(destFile, conf)
}
func getDataInputs(inFilename string, dataShardCnt uint8) ([]*os.File, error) {
var err error
var shardSize int64
inputs := make([]*os.File, dataShardCnt)
for i := range inputs {
if inputs[i], err = os.Open(inFilename); err != nil {
return nil, err
}
if _, err = inputs[i].Seek(int64(i)*shardSize, 0); err != nil {
return nil, err
}
if i == 0 {
shardSize, err = getShardSize(inputs[0], dataShardCnt)
if err != nil {
return nil, err
}
}
}
return inputs, nil
}
func toDataInputReaders(dataInputs []*os.File, dataShardCnt uint8, shardHashers []hash.Hash32) ([]io.Reader, error) {
shardSize, err := getShardSize(dataInputs[0], dataShardCnt)
if err != nil {
return nil, err
}
dataLen, err := getDataLen(dataInputs[0])
if err != nil {
return nil, err
}
inputReaders := make([]io.Reader, dataShardCnt)
for i := range dataInputs {
inputReaders[i] = dataInputs[i]
inputReaders[i] = io.LimitReader(inputReaders[i], shardSize)
inputReaders[i] = io.TeeReader(inputReaders[i], shardHashers[i])
}
i := dataShardCnt - 1
inputReaders[i] = fillLastDataReader(inputReaders[i], dataShardCnt, dataLen)
return inputReaders, nil
}
func getParityOutputs(parityShardCnt uint8) ([]*os.File, error) {
var err error
outputs := make([]*os.File, parityShardCnt)
for i := range outputs {
outputs[i], err = ioutil.TempFile("", "pres_parity_file_*")
if err != nil {
return nil, err
}
}
return outputs, nil
}
func getParityOutputWriters(outputs []*os.File, conf conf, shardHashers []hash.Hash32) []io.Writer {
writers := make([]io.Writer, conf.parityShardCnt)
for i := range outputs {
writers[i] = outputs[i]
j := i + int(conf.dataShardCnt)
writers[i] = io.MultiWriter(writers[i], shardHashers[j])
}
return writers
}
func writeParityFiles(dataInputReaders []io.Reader, conf conf, parityOutputWriters []io.Writer) error {
enc, err := reedsolomon.NewStream(int(conf.dataShardCnt), int(conf.parityShardCnt))
if err != nil {
return err
}
err = enc.Encode(dataInputReaders, parityOutputWriters)
return err
}
func getShardSize(input *os.File, dataShardCnt uint8) (int64, error) {
fileSize, err := getDataLen(input)
if err != nil {
return -1, err
}
return calculateShardSize(fileSize, dataShardCnt), nil
}
func getFilesize(filename string) (int64, error) {
file, err := os.Open(filename)
if err != nil {
return -1, err
}
defer file.Close()
return getDataLen(file)
}