| 1 |
//go:build ignore |
| 2 |
|
| 3 |
// Copyright 2015, Klaus Post, see LICENSE for details. |
| 4 |
// |
| 5 |
// Simple decoder example. |
| 6 |
// |
| 7 |
// The decoder reverses the process of "simple-encoder.go" |
| 8 |
// |
| 9 |
// To build an executable use: |
| 10 |
// |
| 11 |
// go build simple-decoder.go |
| 12 |
// |
| 13 |
// Simple Encoder/Decoder Shortcomings: |
| 14 |
// * If the file size of the input isn't divisible by the number of data shards |
| 15 |
// the output will contain extra zeroes |
| 16 |
// |
| 17 |
// * If the shard numbers isn't the same for the decoder as in the |
| 18 |
// encoder, invalid output will be generated. |
| 19 |
// |
| 20 |
// * If values have changed in a shard, it cannot be reconstructed. |
| 21 |
// |
| 22 |
// * If two shards have been swapped, reconstruction will always fail. |
| 23 |
// You need to supply the shards in the same order as they were given to you. |
| 24 |
// |
| 25 |
// The solution for this is to save a metadata file containing: |
| 26 |
// |
| 27 |
// * File size. |
| 28 |
// * The number of data/parity shards. |
| 29 |
// * HASH of each shard. |
| 30 |
// * Order of the shards. |
| 31 |
// |
| 32 |
// If you save these properties, you should abe able to detect file corruption |
| 33 |
// in a shard and be able to reconstruct your data if you have the needed number of shards left. |
| 34 |
|
| 35 |
package main |
| 36 |
|
| 37 |
import ( |
| 38 |
"flag" |
| 39 |
"fmt" |
| 40 |
"os" |
| 41 |
|
| 42 |
"github.com/klauspost/reedsolomon" |
| 43 |
) |
| 44 |
|
| 45 |
var dataShards = flag.Int("data", 4, "Number of shards to split the data into") |
| 46 |
var parShards = flag.Int("par", 2, "Number of parity shards") |
| 47 |
var outFile = flag.String("out", "", "Alternative output path/file") |
| 48 |
|
| 49 |
func init() { |
| 50 |
flag.Usage = func() { |
| 51 |
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) |
| 52 |
fmt.Fprintf(os.Stderr, " simple-decoder [-flags] basefile.ext\nDo not add the number to the filename.\n") |
| 53 |
fmt.Fprintf(os.Stderr, "Valid flags:\n") |
| 54 |
flag.PrintDefaults() |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func main() { |
| 59 |
// Parse flags |
| 60 |
flag.Parse() |
| 61 |
args := flag.Args() |
| 62 |
if len(args) != 1 { |
| 63 |
fmt.Fprintf(os.Stderr, "Error: No filenames given\n") |
| 64 |
flag.Usage() |
| 65 |
os.Exit(1) |
| 66 |
} |
| 67 |
fname := args[0] |
| 68 |
|
| 69 |
// Create matrix |
| 70 |
enc, err := reedsolomon.New(*dataShards, *parShards) |
| 71 |
checkErr(err) |
| 72 |
|
| 73 |
// Create shards and load the data. |
| 74 |
shards := make([][]byte, *dataShards+*parShards) |
| 75 |
for i := range shards { |
| 76 |
infn := fmt.Sprintf("%s.%d", fname, i) |
| 77 |
fmt.Println("Opening", infn) |
| 78 |
shards[i], err = os.ReadFile(infn) |
| 79 |
if err != nil { |
| 80 |
fmt.Println("Error reading file", err) |
| 81 |
shards[i] = nil |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// Verify the shards |
| 86 |
ok, err := enc.Verify(shards) |
| 87 |
if ok { |
| 88 |
fmt.Println("No reconstruction needed") |
| 89 |
} else { |
| 90 |
fmt.Println("Verification failed. Reconstructing data") |
| 91 |
err = enc.Reconstruct(shards) |
| 92 |
if err != nil { |
| 93 |
fmt.Println("Reconstruct failed -", err) |
| 94 |
os.Exit(1) |
| 95 |
} |
| 96 |
ok, err = enc.Verify(shards) |
| 97 |
if !ok { |
| 98 |
fmt.Println("Verification failed after reconstruction, data likely corrupted.") |
| 99 |
os.Exit(1) |
| 100 |
} |
| 101 |
checkErr(err) |
| 102 |
} |
| 103 |
|
| 104 |
// Join the shards and write them |
| 105 |
outfn := *outFile |
| 106 |
if outfn == "" { |
| 107 |
outfn = fname |
| 108 |
} |
| 109 |
|
| 110 |
fmt.Println("Writing data to", outfn) |
| 111 |
f, err := os.Create(outfn) |
| 112 |
checkErr(err) |
| 113 |
|
| 114 |
// We don't know the exact filesize. |
| 115 |
err = enc.Join(f, shards, len(shards[0])**dataShards) |
| 116 |
checkErr(err) |
| 117 |
} |
| 118 |
|
| 119 |
func checkErr(err error) { |
| 120 |
if err != nil { |
| 121 |
fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) |
| 122 |
os.Exit(2) |
| 123 |
} |
| 124 |
} |