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-12-02 00:06:36 -05:00
|
|
|
|
|
|
|
// check if output file exists, fail if so
|
|
|
|
if _, err := os.Stat(config.output); !os.IsNotExist(err) {
|
|
|
|
log.Fatal("Output file exists")
|
2014-11-30 01:48:37 -05:00
|
|
|
}
|
|
|
|
|
2014-12-02 00:06:36 -05:00
|
|
|
// get list of files
|
|
|
|
var inputFiles []string
|
|
|
|
if _, err := os.Stat(config.input + ".length"); os.IsNotExist(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}
|
2014-11-30 01:48:37 -05:00
|
|
|
}
|
2014-12-02 00:06:36 -05:00
|
|
|
|
|
|
|
// open file to write
|
|
|
|
outputFile, err := os.OpenFile(config.output, os.O_CREATE|os.O_WRONLY, 0600)
|
|
|
|
defer outputFile.Close()
|
2014-11-30 01:48:37 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-02 00:06:36 -05:00
|
|
|
for _, inputFile := range inputFiles {
|
|
|
|
// get chunks
|
|
|
|
chunks := make([][]byte, k+m)
|
|
|
|
for i := 0; i < k+m; i++ {
|
|
|
|
chunks[i], _ = ioutil.ReadFile(inputFile + "." + strconv.Itoa(i))
|
|
|
|
}
|
2014-11-30 01:48:37 -05:00
|
|
|
|
2014-12-02 00:06:36 -05:00
|
|
|
// get length
|
|
|
|
lengthBytes, err := ioutil.ReadFile(inputFile + ".length")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
lengthString := string(lengthBytes)
|
|
|
|
length, err := strconv.Atoi(lengthString)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-11-30 01:48:37 -05:00
|
|
|
|
2014-12-02 00:06:36 -05:00
|
|
|
// set up encoder
|
|
|
|
erasureParameters, _ := erasure.ParseEncoderParams(k, m, erasure.CAUCHY)
|
|
|
|
|
2014-12-07 03:09:24 -05:00
|
|
|
// Get decoder
|
|
|
|
encoder := erasure.NewEncoder(erasureParameters)
|
|
|
|
|
2014-12-02 00:06:36 -05:00
|
|
|
// decode data
|
2014-12-07 03:09:24 -05:00
|
|
|
decodedData, err := encoder.Decode(chunks, length)
|
2014-12-02 00:06:36 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-12-07 03:09:24 -05:00
|
|
|
|
2014-12-02 00:06:36 -05:00
|
|
|
// append decoded data
|
|
|
|
length, err = outputFile.Write(decodedData)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-11-30 01:48:37 -05:00
|
|
|
}
|
|
|
|
}
|