2014-11-30 07:48:37 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-12-01 07:18:02 +01:00
|
|
|
"errors"
|
2014-11-30 07:48:37 +01:00
|
|
|
"os"
|
2014-12-01 07:18:02 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-11-30 07:48:37 +01:00
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
2014-12-02 06:06:36 +01:00
|
|
|
"github.com/minio-io/minio/pkgs/strbyteconv"
|
2014-11-30 07:48:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := cli.NewApp()
|
2014-11-30 22:13:57 +01:00
|
|
|
app.Name = "erasure-demo"
|
2014-11-30 07:48:37 +01:00
|
|
|
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",
|
2014-11-30 07:59:34 +01:00
|
|
|
Value: "10,6",
|
2014-11-30 07:48:37 +01:00
|
|
|
Usage: "data,parity",
|
|
|
|
},
|
2014-12-02 06:06:36 +01:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "block-size",
|
|
|
|
Value: "1M",
|
|
|
|
Usage: "Size of blocks. Examples: 1K, 1M, full",
|
|
|
|
},
|
2014-11-30 07:48:37 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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",
|
2014-11-30 07:59:34 +01:00
|
|
|
Value: "10,6",
|
2014-11-30 07:48:37 +01:00
|
|
|
Usage: "data,parity",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
app.Run(os.Args)
|
|
|
|
}
|
2014-12-01 07:18:02 +01:00
|
|
|
|
|
|
|
// config representing cli input
|
|
|
|
type inputConfig struct {
|
|
|
|
input string
|
|
|
|
output string
|
|
|
|
k int
|
|
|
|
m int
|
2014-12-02 06:06:36 +01:00
|
|
|
blockSize uint64
|
2014-12-01 07:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// parses input and returns an inputConfig with parsed input
|
|
|
|
func parseInput(c *cli.Context) (inputConfig, error) {
|
|
|
|
// 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 {
|
|
|
|
return inputConfig{}, errors.New("Malformed input for protection-level")
|
|
|
|
}
|
|
|
|
|
|
|
|
k, err := strconv.Atoi(protectionLevelSplit[0])
|
|
|
|
if err != nil {
|
|
|
|
return inputConfig{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := strconv.Atoi(protectionLevelSplit[1])
|
|
|
|
if err != nil {
|
|
|
|
return inputConfig{}, err
|
|
|
|
}
|
|
|
|
|
2014-12-02 06:06:36 +01:00
|
|
|
var blockSize uint64
|
|
|
|
blockSize = 0
|
|
|
|
if c.String("block-size") != "" {
|
|
|
|
if c.String("block-size") != "full" {
|
|
|
|
blockSize, err = strbyteconv.StringToBytes(c.String("block-size"))
|
|
|
|
if err != nil {
|
|
|
|
return inputConfig{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-01 07:18:02 +01:00
|
|
|
return inputConfig{
|
2014-12-02 06:06:36 +01:00
|
|
|
input: inputFilePath,
|
|
|
|
output: outputFilePath,
|
|
|
|
k: k,
|
|
|
|
m: m,
|
|
|
|
blockSize: blockSize,
|
2014-12-01 07:18:02 +01:00
|
|
|
}, nil
|
|
|
|
}
|