Debian Codegraph @

examples/stream-encoder.go

raw
1 //go:build ignore
2
3 // Copyright 2015, Klaus Post, see LICENSE for details.
4 //
5 // Simple stream encoder example
6 //
7 // The encoder encodes a single file into a number of shards
8 // To reverse the process see "stream-decoder.go"
9 //
10 // To build an executable use:
11 //
12 // go build stream-encoder.go
13 //
14 // Simple Encoder/Decoder Shortcomings:
15 // * If the file size of the input isn't dividable 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 "io"
45
46 "github.com/klauspost/reedsolomon"
47 )
48
49 var dataShards = flag.Int("data", 4, "Number of shards to split the data into, must be below 257.")
50 var parShards = flag.Int("par", 2, "Number of parity shards")
51 var outDir = flag.String("out", "", "Alternative output directory")
52
53 func init() {
54 flag.Usage = func() {
55 fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
56 fmt.Fprintf(os.Stderr, " %s [-flags] filename.ext\n\n", os.Args[0])
57 fmt.Fprintf(os.Stderr, "Valid flags:\n")
58 flag.PrintDefaults()
59 }
60 }
61
62 func main() {
63 // Parse command line parameters.
64 flag.Parse()
65 args := flag.Args()
66 if len(args) != 1 {
67 fmt.Fprintf(os.Stderr, "Error: No input filename given\n")
68 flag.Usage()
69 os.Exit(1)
70 }
71 if (*dataShards + *parShards) > 256 {
72 fmt.Fprintf(os.Stderr, "Error: sum of data and parity shards cannot exceed 256\n")
73 os.Exit(1)
74 }
75 fname := args[0]
76
77 // Create encoding matrix.
78 enc, err := reedsolomon.NewStream(*dataShards, *parShards)
79 checkErr(err)
80
81 fmt.Println("Opening", fname)
82 f, err := os.Open(fname)
83 checkErr(err)
84
85 instat, err := f.Stat()
86 checkErr(err)
87
88 shards := *dataShards + *parShards
89 out := make([]*os.File, shards)
90
91 // Create the resulting files.
92 dir, file := filepath.Split(fname)
93 if *outDir != "" {
94 dir = *outDir
95 }
96 for i := range out {
97 outfn := fmt.Sprintf("%s.%d", file, i)
98 fmt.Println("Creating", outfn)
99 out[i], err = os.Create(filepath.Join(dir, outfn))
100 checkErr(err)
101 }
102
103 // Split into files.
104 data := make([]io.Writer, *dataShards)
105 for i := range data {
106 data[i] = out[i]
107 }
108 // Do the split
109 err = enc.Split(f, data, instat.Size())
110 checkErr(err)
111
112 // Close and re-open the files.
113 input := make([]io.Reader, *dataShards)
114
115 for i := range data {
116 out[i].Close()
117 f, err := os.Open(out[i].Name())
118 checkErr(err)
119 input[i] = f
120 defer f.Close()
121 }
122
123 // Create parity output writers
124 parity := make([]io.Writer, *parShards)
125 for i := range parity {
126 parity[i] = out[*dataShards+i]
127 defer out[*dataShards+i].Close()
128 }
129
130 // Encode parity
131 err = enc.Encode(input, parity)
132 checkErr(err)
133 fmt.Printf("File split into %d data + %d parity shards.\n", *dataShards, *parShards)
134
135 }
136
137 func checkErr(err error) {
138 if err != nil {
139 fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
140 os.Exit(2)
141 }
142 }