-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshare.go
145 lines (125 loc) · 3.09 KB
/
share.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (C) 2016 Space Monkey, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gfbank
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
)
const (
stem = "data"
)
var (
// shares are in the form data.NNN
sharesRE = regexp.MustCompile(`^` + stem + `.(\d{3})$`)
)
type Share struct {
Number string
Data []byte
}
func SplitString(data string, n, m int) (shares []Share, err error) {
return SplitBytes([]byte(data), n, m)
}
func SplitBytes(data []byte, n, m int) (shares []Share, err error) {
dir, err := ioutil.TempDir("", "gfbank-")
if err != nil {
return nil, Error.Wrap(err)
}
defer func() {
os.RemoveAll(dir)
}()
err = ioutil.WriteFile(filepath.Join(dir, stem), data, 0600)
if err != nil {
return nil, Error.Wrap(err)
}
var stderr bytes.Buffer
cmd := exec.Command("gfsplit",
"-n", fmt.Sprint(n),
"-m", fmt.Sprint(m),
"data")
cmd.Stderr = &stderr
cmd.Dir = dir
err = cmd.Run()
if err != nil {
return nil, Error.New("gfsplit failed: %s", stderr.String())
}
entries, err := ioutil.ReadDir(dir)
if err != nil {
return nil, Error.Wrap(err)
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
name := entry.Name()
if name == stem {
continue
}
m := sharesRE.FindStringSubmatch(name)
if m == nil {
continue
}
data, err := ioutil.ReadFile(filepath.Join(dir, name))
if err != nil {
return nil, Error.New("unable to read share data on %q: %v",
name, err)
}
shares = append(shares, Share{
Number: m[1],
Data: data,
})
}
if len(shares) != m {
return nil, Error.New("expected %d shares; got %d", m, len(shares))
}
return shares, nil
}
func CombineString(shares []Share) (data string, err error) {
data_bytes, err := CombineBytes(shares)
return string(data_bytes), err
}
func CombineBytes(shares []Share) (data []byte, err error) {
dir, err := ioutil.TempDir("", "gfbank-")
if err != nil {
return nil, Error.Wrap(err)
}
defer func() {
os.RemoveAll(dir)
}()
args := []string{"-o", stem}
for _, share := range shares {
inputfile := fmt.Sprintf("%s.%s", stem, share.Number)
args = append(args, inputfile)
err = ioutil.WriteFile(
filepath.Join(dir, inputfile), share.Data, 0600)
if err != nil {
return nil, Error.Wrap(err)
}
}
cmd := exec.Command("gfcombine", args...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
return nil, Error.New("gfcombine failed: %q %v", string(out), err)
}
data, err = ioutil.ReadFile(filepath.Join(dir, stem))
if err != nil {
return nil, Error.New("unable to read combined data: %v", err)
}
return data, nil
}