-
Notifications
You must be signed in to change notification settings - Fork 10
/
common.go
203 lines (175 loc) · 4.91 KB
/
common.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package dstore
import (
"compress/gzip"
"context"
"fmt"
"io"
"os"
"strings"
"github.com/klauspost/compress/zstd"
)
//
// Common Archive Store
//
type commonStore struct {
extension string
compressionType string
overwrite bool
compressedWriteCallback func(ctx context.Context, size int)
uncompressedWriteCallback func(ctx context.Context, size int)
compressedReadCallback func(ctx context.Context, size int)
uncompressedReadCallback func(ctx context.Context, size int)
}
func (c *commonStore) Overwrite() bool { return c.overwrite }
func (c *commonStore) SetOverwrite(in bool) { c.overwrite = in }
func (c *commonStore) pathWithExt(base string) string {
if c.extension != "" {
return base + "." + c.extension
}
return base
}
func commonWalkFrom(store Store, ctx context.Context, prefix, startingPoint string, f func(filename string) (err error)) error {
if startingPoint != "" && !strings.HasPrefix(startingPoint, prefix) {
return fmt.Errorf("starting point %q must start with prefix %q", startingPoint, prefix)
}
var gatePassed bool
return store.Walk(ctx, prefix, func(filename string) error {
if gatePassed {
return f(filename)
}
if filename >= startingPoint {
gatePassed = true
return f(filename)
}
return nil
})
}
func pushLocalFile(ctx context.Context, store Store, localFile, toBaseName string) (removeFunc func() error, err error) {
f, err := os.Open(localFile)
if err != nil {
return nil, fmt.Errorf("open file: %w", err)
}
defer f.Close()
objPath := store.ObjectPath(toBaseName)
err = store.WriteObject(ctx, toBaseName, f)
if err != nil {
return nil, fmt.Errorf("writing %q to storage %q: %w", localFile, objPath, err)
}
return func() error {
return os.Remove(localFile)
}, nil
}
func listFiles(ctx context.Context, store Store, prefix string, max int) (out []string, err error) {
var count int
err = store.Walk(ctx, prefix, func(filename string) error {
count++
if max >= 0 && count > max {
return StopIteration
}
out = append(out, filename)
return nil
})
if err != nil {
return nil, err
}
return
}
func (c *commonStore) compressedCopy(ctx context.Context, destination io.Writer, source io.Reader) error {
// Wrap the writer with the uncompressed write callback if it exists
if c.compressedWriteCallback != nil {
destination = &callbackWriter{w: destination, callback: c.compressedWriteCallback, ctx: ctx}
}
var dest io.Writer
switch c.compressionType {
case "gzip":
gw := gzip.NewWriter(destination)
if c.uncompressedWriteCallback != nil {
dest = &callbackWriter{w: gw, callback: c.uncompressedWriteCallback, ctx: ctx}
} else {
dest = gw
}
if _, err := io.Copy(dest, source); err != nil {
return err
}
if err := gw.Close(); err != nil {
return err
}
case "zstd":
zstdEncoder, err := zstd.NewWriter(destination)
if err != nil {
return err
}
if c.uncompressedWriteCallback != nil {
dest = &callbackWriter{w: zstdEncoder, callback: c.uncompressedWriteCallback, ctx: ctx}
} else {
dest = zstdEncoder
}
if _, err := io.Copy(dest, source); err != nil {
return err
}
if err := zstdEncoder.Close(); err != nil {
return err
}
default:
if c.uncompressedWriteCallback != nil {
dest = &callbackWriter{w: destination, callback: c.uncompressedWriteCallback, ctx: ctx}
} else {
dest = destination
}
if _, err := io.Copy(dest, source); err != nil {
return err
}
}
return nil
}
func (c *commonStore) uncompressedReader(ctx context.Context, reader io.ReadCloser) (out io.ReadCloser, err error) {
if c.compressedReadCallback != nil {
reader = &callbackReadCloser{rc: reader, callback: c.compressedReadCallback, ctx: ctx}
}
switch c.compressionType {
case "gzip":
gzipReader, err := NewGZipReadCloser(reader)
if err != nil {
return nil, fmt.Errorf("unable to create gzip reader: %w", err)
}
if c.uncompressedReadCallback != nil {
out = &callbackReadCloser{rc: gzipReader, callback: c.uncompressedReadCallback, ctx: ctx}
} else {
out = gzipReader
}
case "zstd":
zstdReader, err := zstd.NewReader(reader)
if err != nil {
return nil, fmt.Errorf("unable to create zstd reader: %w", err)
}
if c.uncompressedReadCallback != nil {
out = &callbackReadCloser{rc: zstdReader.IOReadCloser(), callback: c.uncompressedReadCallback, ctx: ctx}
} else {
out = zstdReader.IOReadCloser()
}
default:
if c.uncompressedReadCallback != nil {
out = &callbackReadCloser{rc: reader, callback: c.uncompressedReadCallback, ctx: ctx}
} else {
out = reader
}
}
return out, nil
}
func wrapReadCloser(orig io.ReadCloser, f func()) io.ReadCloser {
return &wrappedReadCloser{
orig: orig,
closeHook: f,
}
}
type wrappedReadCloser struct {
orig io.ReadCloser
closeHook func()
}
func (wrc *wrappedReadCloser) Close() error {
wrc.closeHook()
return wrc.orig.Close()
}
func (wrc *wrappedReadCloser) Read(p []byte) (n int, err error) {
return wrc.orig.Read(p)
}