2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-04-17 14:39:14 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-05-30 16:27:09 +01:00
|
|
|
"context"
|
2018-04-17 14:39:14 +01:00
|
|
|
"crypto/sha256"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/vivint/infectious"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/eestream"
|
2018-10-18 12:10:29 +01:00
|
|
|
"storj.io/storj/pkg/encryption"
|
2018-10-19 14:38:13 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-04-17 14:39:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-09-27 12:52:18 +01:00
|
|
|
erasureShareSize = flag.Int("erasure_share_size", 4*1024, "block size of pieces")
|
|
|
|
key = flag.String("key", "a key", "the secret key")
|
|
|
|
rsk = flag.Int("required", 20, "rs required")
|
|
|
|
rsn = flag.Int("total", 40, "rs total")
|
2018-04-17 14:39:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
if flag.Arg(0) == "" {
|
|
|
|
fmt.Printf("usage: cat data | %s <targetdir>\n", os.Args[0])
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
err := Main()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 15:55:26 +01:00
|
|
|
// Main is the exported CLI executable function
|
2018-04-17 14:39:14 +01:00
|
|
|
func Main() error {
|
|
|
|
err := os.MkdirAll(flag.Arg(0), 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fc, err := infectious.NewFEC(*rsk, *rsn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-27 12:52:18 +01:00
|
|
|
es := eestream.NewRSScheme(fc, *erasureShareSize)
|
2018-07-03 09:35:01 +01:00
|
|
|
rs, err := eestream.NewRedundancyStrategy(es, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-19 14:38:13 +01:00
|
|
|
encKey := storj.Key(sha256.Sum256([]byte(*key)))
|
|
|
|
var firstNonce storj.Nonce
|
|
|
|
encrypter, err := encryption.NewEncrypter(storj.AESGCM, &encKey, &firstNonce, es.StripeSize())
|
2018-04-17 14:39:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
readers, err := eestream.EncodeReader(context.Background(),
|
2018-10-18 12:10:29 +01:00
|
|
|
encryption.TransformReader(eestream.PadReader(os.Stdin,
|
2019-02-05 10:54:25 +00:00
|
|
|
encrypter.InBlockSize()), encrypter, 0), rs)
|
2018-05-30 16:27:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-17 14:39:14 +01:00
|
|
|
errs := make(chan error, len(readers))
|
|
|
|
for i := range readers {
|
|
|
|
go func(i int) {
|
2018-09-28 19:08:44 +01:00
|
|
|
pieceFile := filepath.Join(flag.Arg(0), fmt.Sprintf("%d.piece", i))
|
|
|
|
fh, err := os.Create(pieceFile)
|
2018-04-17 14:39:14 +01:00
|
|
|
if err != nil {
|
|
|
|
errs <- err
|
|
|
|
return
|
|
|
|
}
|
2018-09-28 19:08:44 +01:00
|
|
|
|
|
|
|
defer printError(fh.Close)
|
2019-02-05 10:54:25 +00:00
|
|
|
defer printError(readers[i].Close)
|
2018-09-28 19:08:44 +01:00
|
|
|
|
2018-04-17 14:39:14 +01:00
|
|
|
_, err = io.Copy(fh, readers[i])
|
|
|
|
errs <- err
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
for range readers {
|
|
|
|
err := <-errs
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-28 19:08:44 +01:00
|
|
|
|
|
|
|
func printError(fn func() error) {
|
|
|
|
err := fn()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|