Renaming minio-demo to erasure-demo

This commit is contained in:
Frederick F. Kautz IV
2014-11-30 13:13:57 -08:00
parent 0c16744e2c
commit 6382ee161e
4 changed files with 2 additions and 2 deletions

View File

@@ -0,0 +1,78 @@
package main
import (
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"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
}
// get input path
inputFilePath := c.Args().Get(0)
// get output path
outputFilePath := inputFilePath
if c.String("output") != "" {
outputFilePath = c.String("output")
}
protectionLevel := c.String("protection-level")
protectionLevelSplit := strings.Split(protectionLevel, ",")
if len(protectionLevelSplit) != 2 {
log.Fatal("Malformed input for protection-level")
}
k, err := strconv.Atoi(protectionLevelSplit[0])
if err != nil {
log.Fatal(err)
}
m, err := strconv.Atoi(protectionLevelSplit[1])
if err != nil {
log.Fatal(err)
}
// get chunks
chunks := make([][]byte, k+m)
for i := 0; i < k+m; i++ {
chunks[i], _ = ioutil.ReadFile(inputFilePath + "." + strconv.Itoa(i))
}
// 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.ParseEncoderParams(k, m, erasure.CAUCHY)
// decode data
decodedData, err := erasure.Decode(chunks, erasureParameters, length)
if err != nil {
log.Fatal(err)
}
// write decode data out
if _, err := os.Stat(outputFilePath); os.IsNotExist(err) {
ioutil.WriteFile(outputFilePath, decodedData, 0600)
} else {
log.Fatal("Output file already exists")
}
}

View File

@@ -0,0 +1,68 @@
package main
import (
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"github.com/codegangsta/cli"
"github.com/minio-io/minio/pkgs/erasure"
)
func encode(c *cli.Context) {
// check if minio-encode called without parameters
if len(c.Args()) != 1 {
cli.ShowCommandHelp(c, "encode")
return
}
// get input path
inputFilePath := c.Args().Get(0)
// get output path
outputFilePath := inputFilePath
if c.String("output") != "" {
outputFilePath = c.String("output")
}
protectionLevel := c.String("protection-level")
protectionLevelSplit := strings.Split(protectionLevel, ",")
if len(protectionLevelSplit) != 2 {
log.Fatal("Malformed input for protection-level")
}
k, err := strconv.Atoi(protectionLevelSplit[0])
if err != nil {
log.Fatal(err)
}
m, err := strconv.Atoi(protectionLevelSplit[1])
if err != nil {
log.Fatal(err)
}
// get file
inputFile, err := os.Open(inputFilePath)
if err != nil {
log.Fatal(err)
}
// read file
input, err := ioutil.ReadAll(inputFile)
if err != nil {
log.Fatal(err)
}
// set up encoder
erasureParameters, _ := erasure.ParseEncoderParams(k, m, erasure.CAUCHY)
// encode data
encodedData, length := erasure.Encode(input, erasureParameters)
// write encoded data out
for key, data := range encodedData {
ioutil.WriteFile(outputFilePath+"."+strconv.Itoa(key), data, 0600)
}
ioutil.WriteFile(outputFilePath+".length", []byte(strconv.Itoa(length)), 0600)
}

50
cmd/erasure-demo/main.go Normal file
View File

@@ -0,0 +1,50 @@
package main
import (
"os"
"github.com/codegangsta/cli"
)
func main() {
app := cli.NewApp()
app.Name = "erasure-demo"
app.Usage = "erasure encode a byte stream"
app.Commands = []cli.Command{
{
Name: "encode",
Usage: "erasure encode a byte stream",
Action: encode,
Flags: []cli.Flag{
cli.StringFlag{
Name: "output,o",
Value: "",
Usage: "Output file",
},
cli.StringFlag{
Name: "protection-level",
Value: "10,6",
Usage: "data,parity",
},
},
},
{
Name: "decode",
Usage: "erasure decode a byte stream",
Action: decode,
Flags: []cli.Flag{
cli.StringFlag{
Name: "output,o",
Value: "",
Usage: "Output file",
},
cli.StringFlag{
Name: "protection-level",
Value: "10,6",
Usage: "data,parity",
},
},
},
}
app.Run(os.Args)
}