Adding minio-decode

This commit is contained in:
Frederick F. Kautz IV 2014-11-23 19:28:07 -08:00
parent 4b97cf4b9d
commit 5a3eb47583
3 changed files with 98 additions and 1 deletions

View File

@ -11,6 +11,7 @@ test: build-erasure
install: build-erasure install: build-erasure
godep go install github.com/minio-io/minio/cmd/minio-encode godep go install github.com/minio-io/minio/cmd/minio-encode
godep go install github.com/minio-io/minio/cmd/minio-decode
save: save:
godep save ./... godep save ./...

95
cmd/minio-decode/main.go Normal file
View File

@ -0,0 +1,95 @@
package main
import (
"io/ioutil"
"log"
"os"
"strconv"
"github.com/codegangsta/cli"
"github.com/minio-io/minio/erasure"
)
func main() {
app := cli.NewApp()
app.Name = "minio-encode"
app.Usage = "erasure encode a byte stream"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "input,i",
Value: "",
Usage: "Input file",
},
cli.StringFlag{
Name: "output,o",
Value: "",
Usage: "Output file",
},
cli.IntFlag{
Name: "k",
Value: 10,
Usage: "k value of encoder parameters",
},
cli.IntFlag{
Name: "m",
Value: 5,
Usage: "m value of encoder parameters",
},
}
app.Action = func(c *cli.Context) {
// check if minio-encode called without parameters
if len(c.Args()) == 1 {
cli.ShowAppHelp(c)
}
// get input path
if c.String("input") == "" {
log.Fatal("No input specified")
}
inputFilePath := c.String("input")
// get output path
outputFilePath := inputFilePath
if c.String("output") != "" {
outputFilePath = c.String("output")
}
k := c.Int("k")
m := c.Int("m")
// get chunks
chunks := make([][]byte, k+m)
for i := 0; i < k+m; i++ {
var err error
chunks[i], err = ioutil.ReadFile(inputFilePath + "." + strconv.Itoa(i))
if err != nil {
log.Fatal(err)
}
}
// get length
lengthBytes, err := ioutil.ReadFile(inputFilePath + ".length")
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.ValidateParams(k, m, 8, erasure.CAUCHY)
encoder := erasure.NewEncoder(erasureParameters)
// decode data
decodedData, err := encoder.Decode(chunks, length)
if err != nil {
log.Fatal(err)
}
// write decode data out
ioutil.WriteFile(outputFilePath, decodedData, 0600)
}
app.Run(os.Args)
}

View File

@ -74,12 +74,13 @@ func main() {
encoder := erasure.NewEncoder(erasureParameters) encoder := erasure.NewEncoder(erasureParameters)
// encode data // encode data
encodedData, _ := encoder.Encode(input) encodedData, length := encoder.Encode(input)
// write encoded data out // write encoded data out
for key, data := range encodedData { for key, data := range encodedData {
ioutil.WriteFile(outputFilePath+"."+strconv.Itoa(key), data, 0600) ioutil.WriteFile(outputFilePath+"."+strconv.Itoa(key), data, 0600)
} }
ioutil.WriteFile(outputFilePath+".length", []byte(strconv.Itoa(length)), 0600)
} }
app.Run(os.Args) app.Run(os.Args)
} }