2014-11-30 01:48:37 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-12-02 00:06:36 -05:00
|
|
|
"bytes"
|
2014-11-30 01:48:37 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
2014-12-10 03:58:12 -05:00
|
|
|
"github.com/minio-io/minio/pkgs/checksum/crc32c"
|
2014-11-30 01:48:37 -05:00
|
|
|
"github.com/minio-io/minio/pkgs/erasure"
|
2014-12-02 00:06:36 -05:00
|
|
|
"github.com/minio-io/minio/pkgs/split"
|
2014-11-30 01:48:37 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func encode(c *cli.Context) {
|
|
|
|
// check if minio-encode called without parameters
|
|
|
|
if len(c.Args()) != 1 {
|
|
|
|
cli.ShowCommandHelp(c, "encode")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get file
|
2014-12-01 01:18:02 -05:00
|
|
|
inputFile, err := os.Open(config.input)
|
2014-12-02 00:06:36 -05:00
|
|
|
defer inputFile.Close()
|
2014-11-30 01:48:37 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// read file
|
|
|
|
input, err := ioutil.ReadAll(inputFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up encoder
|
2014-12-01 01:18:02 -05:00
|
|
|
erasureParameters, _ := erasure.ParseEncoderParams(config.k, config.m, erasure.CAUCHY)
|
2014-12-07 03:09:24 -05:00
|
|
|
|
|
|
|
// Init new encoder
|
|
|
|
encoder := erasure.NewEncoder(erasureParameters)
|
|
|
|
|
2014-11-30 01:48:37 -05:00
|
|
|
// encode data
|
2014-12-02 00:06:36 -05:00
|
|
|
if config.blockSize == 0 {
|
2014-12-07 03:09:24 -05:00
|
|
|
encodedData, length := encoder.Encode(input)
|
2014-12-02 00:06:36 -05:00
|
|
|
for key, data := range encodedData {
|
2014-12-10 03:58:12 -05:00
|
|
|
crc, err := crc32c.Crc32c(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
crcString := strconv.Itoa(int(crc))
|
|
|
|
ioutil.WriteFile(config.output+"."+strconv.Itoa(key)+".cksum", []byte(crcString), 0600)
|
2014-12-02 00:06:36 -05:00
|
|
|
ioutil.WriteFile(config.output+"."+strconv.Itoa(key), data, 0600)
|
|
|
|
ioutil.WriteFile(config.output+".length", []byte(strconv.Itoa(length)), 0600)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chunkCount := 0
|
2014-12-05 12:49:07 -05:00
|
|
|
splitChannel := make(chan split.SplitMessage)
|
2014-12-02 00:06:36 -05:00
|
|
|
inputReader := bytes.NewReader(input)
|
|
|
|
go split.SplitStream(inputReader, config.blockSize, splitChannel)
|
|
|
|
for chunk := range splitChannel {
|
|
|
|
if chunk.Err != nil {
|
|
|
|
log.Fatal(chunk.Err)
|
|
|
|
}
|
2014-12-07 03:09:24 -05:00
|
|
|
encodedData, length := encoder.Encode(chunk.Data)
|
2014-12-02 00:06:36 -05:00
|
|
|
for key, data := range encodedData {
|
2014-12-10 03:58:12 -05:00
|
|
|
crc, err := crc32c.Crc32c(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
crcString := strconv.Itoa(int(crc))
|
|
|
|
ioutil.WriteFile(config.output+"."+strconv.Itoa(chunkCount)+"."+strconv.Itoa(key)+".cksum", []byte(crcString), 0600)
|
2014-12-02 00:06:36 -05:00
|
|
|
ioutil.WriteFile(config.output+"."+strconv.Itoa(chunkCount)+"."+strconv.Itoa(key), data, 0600)
|
|
|
|
}
|
2014-12-10 03:58:12 -05:00
|
|
|
ioutil.WriteFile(config.output+"."+strconv.Itoa(chunkCount)+".length", []byte(strconv.Itoa(length)), 0600)
|
2014-12-02 00:06:36 -05:00
|
|
|
chunkCount += 1
|
|
|
|
}
|
2014-11-30 01:48:37 -05:00
|
|
|
}
|
|
|
|
}
|