Debian Codegraph @

examples/stream-decoder.go

raw
1 //go:build ignore
2
3 // Copyright 2015, Klaus Post, see LICENSE for details.
4 //
5 // Stream decoder example.
6 //
7 // The decoder reverses the process of "stream-encoder.go"
8 //
9 // To build an executable use:
10 //
11 // go build stream-decoder.go
12 //
13 // Simple Encoder/Decoder Shortcomings:
14 // * If the file size of the input isn't dividable 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 "io"
41 "os"
42
43 "github.com/klauspost/reedsolomon"
44 )
45
46 var dataShards = flag.Int("data", 4, "Number of shards to split the data into")
47 var parShards = flag.Int("par", 2, "Number of parity shards")
48 var outFile = flag.String("out", "", "Alternative output path/file")
49
50 func init() {
51 flag.Usage = func() {
52 fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
53 fmt.Fprintf(os.Stderr, " %s [-flags] basefile.ext\nDo not add the number to the filename.\n", os.Args[0])
54 fmt.Fprintf(os.Stderr, "Valid flags:\n")
55 flag.PrintDefaults()
56 }
57 }
58
59 func main() {
60 // Parse flags
61 flag.Parse()
62 args := flag.Args()
63 if len(args) != 1 {
64 fmt.Fprintf(os.Stderr, "Error: No filenames given\n")
65 flag.Usage()
66 os.Exit(1)
67 }
68 fname := args[0]
69
70 // Create matrix
71 enc, err := reedsolomon.NewStream(*dataShards, *parShards)
72 checkErr(err)
73
74 // Open the inputs
75 shards, size, err := openInput(*dataShards, *parShards, fname)
76 checkErr(err)
77
78 // Verify the shards
79 ok, err := enc.Verify(shards)
80 if ok {
81 fmt.Println("No reconstruction needed")
82 } else {
83 fmt.Println("Verification failed. Reconstructing data")
84 shards, size, err = openInput(*dataShards, *parShards, fname)
85 checkErr(err)
86 // Create out destination writers
87 out := make([]io.Writer, len(shards))
88 for i := range out {
89 if shards[i] == nil {
90 outfn := fmt.Sprintf("%s.%d", fname, i)
91 fmt.Println("Creating", outfn)
92 out[i], err = os.Create(outfn)
93 checkErr(err)
94 }
95 }
96 err = enc.Reconstruct(shards, out)
97 if err != nil {
98 fmt.Println("Reconstruct failed -", err)
99 os.Exit(1)
100 }
101 // Close output.
102 for i := range out {
103 if out[i] != nil {
104 err := out[i].(*os.File).Close()
105 checkErr(err)
106 }
107 }
108 shards, size, err = openInput(*dataShards, *parShards, fname)
109 ok, err = enc.Verify(shards)
110 if !ok {
111 fmt.Println("Verification failed after reconstruction, data likely corrupted:", err)
112 os.Exit(1)
113 }
114 checkErr(err)
115 }
116
117 // Join the shards and write them
118 outfn := *outFile
119 if outfn == "" {
120 outfn = fname
121 }
122
123 fmt.Println("Writing data to", outfn)
124 f, err := os.Create(outfn)
125 checkErr(err)
126
127 shards, size, err = openInput(*dataShards, *parShards, fname)
128 checkErr(err)
129
130 // We don't know the exact filesize.
131 err = enc.Join(f, shards, int64(*dataShards)*size)
132 checkErr(err)
133 }
134
135 func openInput(dataShards, parShards int, fname string) (r []io.Reader, size int64, err error) {
136 // Create shards and load the data.
137 shards := make([]io.Reader, dataShards+parShards)
138 for i := range shards {
139 infn := fmt.Sprintf("%s.%d", fname, i)
140 fmt.Println("Opening", infn)
141 f, err := os.Open(infn)
142 if err != nil {
143 fmt.Println("Error reading file", err)
144 shards[i] = nil
145 continue
146 } else {
147 shards[i] = f
148 }
149 stat, err := f.Stat()
150 checkErr(err)
151 if stat.Size() > 0 {
152 size = stat.Size()
153 } else {
154 shards[i] = nil
155 }
156 }
157 return shards, size, nil
158 }
159
160 func checkErr(err error) {
161 if err != nil {
162 fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
163 os.Exit(2)
164 }
165 }