forked from wal-g/wal-g
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tarballs.go
105 lines (87 loc) · 2.49 KB
/
tarballs.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
package testtools
import (
"archive/tar"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"github.com/pierrec/lz4"
"github.com/wal-g/wal-g/internal"
"github.com/wal-g/wal-g/internal/crypto"
)
// FileTarBall represents a tarball that is
// written to disk.
type FileTarBall struct {
out string
number int
size int64
writeCloser io.WriteCloser
tarWriter *tar.Writer
}
// SetUp creates a new LZ4 writer, tar writer and file for
// writing bundled compressed bytes to.
func (tarBall *FileTarBall) SetUp(crypter crypto.Crypter, names ...string) {
if tarBall.tarWriter == nil {
name := filepath.Join(tarBall.out, "part_"+fmt.Sprintf("%0.3d", tarBall.number)+".tar.lz4")
file, err := os.Create(name)
if err != nil {
panic(err)
}
var writeCloser io.WriteCloser
if crypter != nil {
writeCloser, err = crypter.Encrypt(file)
if err != nil {
panic(err)
}
tarBall.writeCloser = &internal.CascadeWriteCloser{
WriteCloser: lz4.NewWriter(file),
Underlying: &internal.CascadeWriteCloser{WriteCloser: writeCloser, Underlying: file},
}
} else {
writeCloser = file
tarBall.writeCloser = &internal.CascadeWriteCloser{
WriteCloser: lz4.NewWriter(file),
Underlying: writeCloser,
}
}
tarBall.tarWriter = tar.NewWriter(tarBall.writeCloser)
}
}
// CloseTar closes the tar writer and file, flushing any
// unwritten data to the file before closing.
func (tarBall *FileTarBall) CloseTar() error {
err := tarBall.tarWriter.Close()
if err != nil {
return err
}
return tarBall.writeCloser.Close()
}
func (tarBall *FileTarBall) Size() int64 { return tarBall.size }
func (tarBall *FileTarBall) AddSize(i int64) { tarBall.size += i }
func (tarBall *FileTarBall) TarWriter() *tar.Writer { return tarBall.tarWriter }
func (tarBall *FileTarBall) AwaitUploads() {}
// BufferTarBall represents a tarball that is
// written to buffer.
type BufferTarBall struct {
number int
size int64
underlying *bytes.Buffer
tarWriter *tar.Writer
}
func (tarBall *BufferTarBall) SetUp(crypter crypto.Crypter, args ...string) {
tarBall.tarWriter = tar.NewWriter(tarBall.underlying)
}
func (tarBall *BufferTarBall) CloseTar() error {
return tarBall.tarWriter.Close()
}
func (tarBall *BufferTarBall) Size() int64 {
return tarBall.size
}
func (tarBall *BufferTarBall) AddSize(add int64) {
tarBall.size += add
}
func (tarBall *BufferTarBall) TarWriter() *tar.Writer {
return tarBall.tarWriter
}
func (tarBall *BufferTarBall) AwaitUploads() {}