2014-11-30 01:48:37 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
"github.com/minio-io/minio/pkgs/erasure"
|
|
|
|
)
|
|
|
|
|
|
|
|
func decode(c *cli.Context) {
|
|
|
|
// check if minio-encode called without parameters
|
|
|
|
if len(c.Args()) != 1 {
|
|
|
|
cli.ShowCommandHelp(c, "decode")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-01 01:18:02 -05:00
|
|
|
config, err := parseInput(c)
|
2014-11-30 01:48:37 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-01 01:18:02 -05:00
|
|
|
k := config.k
|
|
|
|
m := config.m
|
2014-11-30 01:48:37 -05:00
|
|
|
// get chunks
|
|
|
|
chunks := make([][]byte, k+m)
|
|
|
|
for i := 0; i < k+m; i++ {
|
2014-12-01 01:18:02 -05:00
|
|
|
chunks[i], _ = ioutil.ReadFile(config.input + "." + strconv.Itoa(i))
|
2014-11-30 01:48:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// get length
|
2014-12-01 01:18:02 -05:00
|
|
|
lengthBytes, err := ioutil.ReadFile(config.input + ".length")
|
2014-11-30 01:48:37 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
lengthString := string(lengthBytes)
|
|
|
|
length, err := strconv.Atoi(lengthString)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up encoder
|
|
|
|
erasureParameters, _ := erasure.ParseEncoderParams(k, m, erasure.CAUCHY)
|
|
|
|
|
|
|
|
// decode data
|
|
|
|
decodedData, err := erasure.Decode(chunks, erasureParameters, length)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// write decode data out
|
2014-12-01 01:18:02 -05:00
|
|
|
if _, err := os.Stat(config.output); os.IsNotExist(err) {
|
|
|
|
ioutil.WriteFile(config.output, decodedData, 0600)
|
2014-11-30 01:48:37 -05:00
|
|
|
} else {
|
|
|
|
log.Fatal("Output file already exists")
|
|
|
|
}
|
|
|
|
}
|