| 1 |
//go:build ignore |
| 2 |
|
| 3 |
// Copyright 2015, Klaus Post, see LICENSE for details. |
| 4 |
// |
| 5 |
// Simple encoder example |
| 6 |
// |
| 7 |
// The encoder encodes a simple file into a number of shards |
| 8 |
// To reverse the process see "simpledecoder.go" |
| 9 |
// |
| 10 |
// To build an executable use: |
| 11 |
// |
| 12 |
// go build simple-decoder.go |
| 13 |
// |
| 14 |
// Simple Encoder/Decoder Shortcomings: |
| 15 |
// * If the file size of the input isn't divisible by the number of data shards |
| 16 |
// the output will contain extra zeroes |
| 17 |
// |
| 18 |
// * If the shard numbers isn't the same for the decoder as in the |
| 19 |
// encoder, invalid output will be generated. |
| 20 |
// |
| 21 |
// * If values have changed in a shard, it cannot be reconstructed. |
| 22 |
// |
| 23 |
// * If two shards have been swapped, reconstruction will always fail. |
| 24 |
// You need to supply the shards in the same order as they were given to you. |
| 25 |
// |
| 26 |
// The solution for this is to save a metadata file containing: |
| 27 |
// |
| 28 |
// * File size. |
| 29 |
// * The number of data/parity shards. |
| 30 |
// * HASH of each shard. |
| 31 |
// * Order of the shards. |
| 32 |
// |
| 33 |
// If you save these properties, you should abe able to detect file corruption |
| 34 |
// in a shard and be able to reconstruct your data if you have the needed number of shards left. |
| 35 |
|
| 36 |
package main |
| 37 |
|
| 38 |
import ( |
| 39 |
"flag" |
| 40 |
"fmt" |
| 41 |
"os" |
| 42 |
"path/filepath" |
| 43 |
|
| 44 |
"github.com/klauspost/reedsolomon" |
| 45 |
) |
| 46 |
|
| 47 |
var dataShards = flag.Int("data", 4, "Number of shards to split the data into, must be below 257.") |
| 48 |
var parShards = flag.Int("par", 2, "Number of parity shards") |
| 49 |
var outDir = flag.String("out", "", "Alternative output directory") |
| 50 |
|
| 51 |
func init() { |
| 52 |
flag.Usage = func() { |
| 53 |
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) |
| 54 |
fmt.Fprintf(os.Stderr, " simple-encoder [-flags] filename.ext\n\n") |
| 55 |
fmt.Fprintf(os.Stderr, "Valid flags:\n") |
| 56 |
flag.PrintDefaults() |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func main() { |
| 61 |
// Parse command line parameters. |
| 62 |
flag.Parse() |
| 63 |
args := flag.Args() |
| 64 |
if len(args) != 1 { |
| 65 |
fmt.Fprintf(os.Stderr, "Error: No input filename given\n") |
| 66 |
flag.Usage() |
| 67 |
os.Exit(1) |
| 68 |
} |
| 69 |
if (*dataShards + *parShards) > 256 { |
| 70 |
fmt.Fprintf(os.Stderr, "Error: sum of data and parity shards cannot exceed 256\n") |
| 71 |
os.Exit(1) |
| 72 |
} |
| 73 |
fname := args[0] |
| 74 |
|
| 75 |
// Create encoding matrix. |
| 76 |
enc, err := reedsolomon.New(*dataShards, *parShards) |
| 77 |
checkErr(err) |
| 78 |
|
| 79 |
fmt.Println("Opening", fname) |
| 80 |
b, err := os.ReadFile(fname) |
| 81 |
checkErr(err) |
| 82 |
|
| 83 |
// Split the file into equally sized shards. |
| 84 |
shards, err := enc.Split(b) |
| 85 |
checkErr(err) |
| 86 |
fmt.Printf("File split into %d data+parity shards with %d bytes/shard.\n", len(shards), len(shards[0])) |
| 87 |
|
| 88 |
// Encode parity |
| 89 |
err = enc.Encode(shards) |
| 90 |
checkErr(err) |
| 91 |
|
| 92 |
// Write out the resulting files. |
| 93 |
dir, file := filepath.Split(fname) |
| 94 |
if *outDir != "" { |
| 95 |
dir = *outDir |
| 96 |
} |
| 97 |
for i, shard := range shards { |
| 98 |
outfn := fmt.Sprintf("%s.%d", file, i) |
| 99 |
|
| 100 |
fmt.Println("Writing to", outfn) |
| 101 |
err = os.WriteFile(filepath.Join(dir, outfn), shard, 0644) |
| 102 |
checkErr(err) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func checkErr(err error) { |
| 107 |
if err != nil { |
| 108 |
fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) |
| 109 |
os.Exit(2) |
| 110 |
} |
| 111 |
} |