|
|
@ -24,36 +24,68 @@ func decode(c *cli.Context) { |
|
|
|
|
|
|
|
|
|
|
|
k := config.k |
|
|
|
k := config.k |
|
|
|
m := config.m |
|
|
|
m := config.m |
|
|
|
// get chunks
|
|
|
|
|
|
|
|
chunks := make([][]byte, k+m) |
|
|
|
// check if output file exists, fail if so
|
|
|
|
for i := 0; i < k+m; i++ { |
|
|
|
if _, err := os.Stat(config.output); !os.IsNotExist(err) { |
|
|
|
chunks[i], _ = ioutil.ReadFile(config.input + "." + strconv.Itoa(i)) |
|
|
|
log.Fatal("Output file exists") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// get length
|
|
|
|
// get list of files
|
|
|
|
lengthBytes, err := ioutil.ReadFile(config.input + ".length") |
|
|
|
var inputFiles []string |
|
|
|
if err != nil { |
|
|
|
if _, err := os.Stat(config.input + ".length"); os.IsNotExist(err) { |
|
|
|
log.Fatal(err) |
|
|
|
err = nil |
|
|
|
|
|
|
|
chunkCount := 0 |
|
|
|
|
|
|
|
for !os.IsNotExist(err) { |
|
|
|
|
|
|
|
_, err = os.Stat(config.input + "." + strconv.Itoa(chunkCount) + ".length") |
|
|
|
|
|
|
|
chunkCount += 1 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
chunkCount = chunkCount - 1 |
|
|
|
|
|
|
|
inputFiles = make([]string, chunkCount) |
|
|
|
|
|
|
|
for i := 0; i < chunkCount; i++ { |
|
|
|
|
|
|
|
inputFiles[i] = config.input + "." + strconv.Itoa(i) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
inputFiles = []string{config.input} |
|
|
|
} |
|
|
|
} |
|
|
|
lengthString := string(lengthBytes) |
|
|
|
|
|
|
|
length, err := strconv.Atoi(lengthString) |
|
|
|
// open file to write
|
|
|
|
|
|
|
|
outputFile, err := os.OpenFile(config.output, os.O_CREATE|os.O_WRONLY, 0600) |
|
|
|
|
|
|
|
defer outputFile.Close() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Fatal(err) |
|
|
|
log.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// set up encoder
|
|
|
|
for _, inputFile := range inputFiles { |
|
|
|
erasureParameters, _ := erasure.ParseEncoderParams(k, m, erasure.CAUCHY) |
|
|
|
// get chunks
|
|
|
|
|
|
|
|
chunks := make([][]byte, k+m) |
|
|
|
|
|
|
|
for i := 0; i < k+m; i++ { |
|
|
|
|
|
|
|
chunks[i], _ = ioutil.ReadFile(inputFile + "." + strconv.Itoa(i)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// decode data
|
|
|
|
// get length
|
|
|
|
decodedData, err := erasure.Decode(chunks, erasureParameters, length) |
|
|
|
lengthBytes, err := ioutil.ReadFile(inputFile + ".length") |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Fatal(err) |
|
|
|
log.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
lengthString := string(lengthBytes) |
|
|
|
|
|
|
|
length, err := strconv.Atoi(lengthString) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// write decode data out
|
|
|
|
// set up encoder
|
|
|
|
if _, err := os.Stat(config.output); os.IsNotExist(err) { |
|
|
|
erasureParameters, _ := erasure.ParseEncoderParams(k, m, erasure.CAUCHY) |
|
|
|
ioutil.WriteFile(config.output, decodedData, 0600) |
|
|
|
|
|
|
|
} else { |
|
|
|
// decode data
|
|
|
|
log.Fatal("Output file already exists") |
|
|
|
decodedData, err := erasure.Decode(chunks, erasureParameters, length) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// append decoded data
|
|
|
|
|
|
|
|
length, err = outputFile.Write(decodedData) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|