forked from wal-g/wal-g
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
73 lines (60 loc) · 1.42 KB
/
gen.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
package testtools
import (
"archive/tar"
"io"
"math/rand"
"strconv"
"sync/atomic"
)
var counter int32
// StrideByteReader allows for customizable "strides" of
// random bytes. Creates an infinite stream.
type StrideByteReader struct {
stride int
counter int
randBytes []byte
}
// NewStrideByteReader creates a new random byte
// stride generator with a seed of 0.
func NewStrideByteReader(s int) *StrideByteReader {
sb := StrideByteReader{
stride: s,
randBytes: make([]byte, s),
}
rand.Seed(0)
// rand.Seed(time.Now().UTC().UnixNano())
rand.Read(sb.randBytes)
return &sb
}
// Read creates randomly generated bytes of 'stride' length.
func (sb *StrideByteReader) Read(p []byte) (int, error) {
l := len(sb.randBytes)
n := 0
for start := 0; start < len(p); n = copy(p[start:], sb.randBytes[sb.counter:]) {
sb.counter = (sb.counter + n) % l
start += n
}
return len(p), nil
}
// CreateTar creates a new tarball from the passed in reader
// and writes to a destination writer.
func CreateTar(w io.Writer, r *io.LimitedReader) {
// defer TimeTrack(time.Now(), "CREATE TAR")
tmp := atomic.AddInt32(&counter, 1)
_ = tmp
tw := tar.NewWriter(w)
hdr := &tar.Header{
Name: strconv.Itoa(int(counter)),
Size: int64(r.N),
Mode: 0600,
}
if err := tw.WriteHeader(hdr); err != nil {
panic(err)
}
if _, err := io.Copy(tw, r); err != nil {
panic(err)
}
if err := tw.Close(); err != nil {
panic(err)
}
}