-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharchiver.go
330 lines (267 loc) · 8.03 KB
/
archiver.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package pzip
import (
"archive/zip"
"bufio"
"context"
"fmt"
"hash/crc32"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"sync"
"unicode/utf8"
"github.com/ybirader/pzip/pool"
)
const (
defaultCompression = -1
zipVersion20 = 20
sequentialWrites = 1
)
const bufferSize = 32 * 1024
var bufferPool = sync.Pool{
New: func() any {
return bufio.NewReaderSize(nil, bufferSize)
},
}
type archiver struct {
xArchive *os.File
concurrency int
w *zip.Writer
fileProcessPool pool.WorkerPool[pool.File]
fileWriterPool pool.WorkerPool[pool.File]
chroot string
absoluteArchivePath string
}
// NewArchiver returns a new pzip archiver. The archiver can be configured by passing in a number of options.
// Available options include ArchiverConcurrency(n int). It returns an error if the archiver can't be created
// Close() should be called on the returned archiver when done
func NewArchiver(archive *os.File, options ...archiverOption) (*archiver, error) {
a := &archiver{
xArchive: archive,
w: zip.NewWriter(archive),
concurrency: runtime.GOMAXPROCS(0),
}
var err error
a.absoluteArchivePath, err = filepath.Abs(archive.Name())
if err != nil {
return nil, fmt.Errorf("absolute archive path %q: %w", archive.Name(), err)
}
fileProcessExecutor := func(file *pool.File) error {
err := a.compress(file)
if err != nil {
return fmt.Errorf("compress file %q: %w", file.Path, err)
}
a.fileWriterPool.Enqueue(file)
return nil
}
fileProcessPool, err := pool.NewFileWorkerPool(fileProcessExecutor, &pool.Config{Concurrency: a.concurrency, Capacity: 1})
if err != nil {
return nil, fmt.Errorf("new file process pool: %w", err)
}
a.fileProcessPool = fileProcessPool
fileWriterExecutor := func(file *pool.File) error {
err := a.archive(file)
if err != nil {
return fmt.Errorf("archive %q: %w", file.Path, err)
}
return nil
}
fileWriterPool, err := pool.NewFileWorkerPool(fileWriterExecutor, &pool.Config{Concurrency: sequentialWrites, Capacity: 1})
if err != nil {
return nil, fmt.Errorf("new file writer pool: %w", err)
}
a.fileWriterPool = fileWriterPool
for _, option := range options {
err = option(a)
if err != nil {
return nil, err
}
}
return a, nil
}
// Archive compresses and stores (archives) the files at the provides filePaths to
// the corresponding archive registered with the archiver. Archiving is canceled when the
// associated ctx is canceled. The first error that arises during archiving is returned.
func (a *archiver) Archive(ctx context.Context, filePaths []string) error {
a.fileProcessPool.Start(ctx)
a.fileWriterPool.Start(ctx)
for _, path := range filePaths {
info, err := os.Lstat(path)
if err != nil {
return fmt.Errorf("lstat %q: %w", path, err)
}
if info.IsDir() {
if err = a.archiveDir(path); err != nil {
return fmt.Errorf("archive dir %q: %w", path, err)
}
} else {
a.chroot = ""
file, err := pool.NewFile(path, info, "")
if err != nil {
return fmt.Errorf("new file %q: %w", path, err)
}
a.archiveFile(file)
}
}
if err := a.fileProcessPool.Close(); err != nil {
return fmt.Errorf("close file process pool: %w", err)
}
if err := a.fileWriterPool.Close(); err != nil {
return fmt.Errorf("close file writer pool: %w", err)
}
return nil
}
func (a *archiver) Close() error {
if err := a.w.Close(); err != nil {
return fmt.Errorf("close zip writer: %w", err)
}
return nil
}
func (a *archiver) archiveDir(root string) error {
if err := a.changeRoot(root); err != nil {
return fmt.Errorf("change root to %q: %w", root, err)
}
if err := a.walkDir(); err != nil {
return fmt.Errorf("walk directory: %w", err)
}
return nil
}
// archiveFile enqueues file for archiving if it doesn't match
// our output file.
func (a *archiver) archiveFile(file *pool.File) {
if file.Path == a.absoluteArchivePath {
// Don't archive the output file.
return
}
a.fileProcessPool.Enqueue(file)
}
func (a *archiver) changeRoot(root string) error {
absRoot, err := filepath.Abs(root)
if err != nil {
return fmt.Errorf("get absolute path of %q: %w", root, err)
}
a.chroot = absRoot
return nil
}
func (a *archiver) walkDir() error {
if err := filepath.Walk(a.chroot, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
file, err := pool.NewFile(path, info, a.chroot)
if err != nil {
return fmt.Errorf("new file %q: %w", path, err)
}
a.archiveFile(file)
return nil
}); err != nil {
return fmt.Errorf("walk directory %q: %w", a.chroot, err)
}
return nil
}
func (a *archiver) compress(file *pool.File) error {
if file.Info.IsDir() {
if err := a.populateHeader(file); err != nil {
return fmt.Errorf("populate header for %q: %w", file.Path, err)
}
return nil
}
hasher := crc32.NewIEEE()
if err := a.copy(io.MultiWriter(file.Compressor, hasher), file); err != nil {
return fmt.Errorf("copy %q: %w", file.Path, err)
}
if err := file.Compressor.Close(); err != nil {
return fmt.Errorf("close compressor for %q: %w", file.Path, err)
}
if err := a.populateHeader(file); err != nil {
return fmt.Errorf("populate header for %q: %w", file.Path, err)
}
file.Header.CRC32 = hasher.Sum32()
return nil
}
func (a *archiver) copy(w io.Writer, file *pool.File) error {
f, err := os.Open(file.Path)
if err != nil {
return fmt.Errorf("open %q: %w", file.Path, err)
}
defer f.Close()
buf := bufferPool.Get().(*bufio.Reader)
buf.Reset(f)
_, err = io.Copy(w, buf)
bufferPool.Put(buf)
if err != nil {
return fmt.Errorf("copy %q: %w", file.Path, err)
}
return nil
}
func (a *archiver) populateHeader(file *pool.File) error {
header := file.Header
utf8ValidName, utf8RequireName := detectUTF8(header.Name)
utf8ValidComment, utf8RequireComment := detectUTF8(header.Comment)
switch {
case header.NonUTF8:
header.Flags &^= 0x800
case (utf8RequireName || utf8RequireComment) && (utf8ValidName && utf8ValidComment):
header.Flags |= 0x800
}
header.CreatorVersion = header.CreatorVersion&0xff00 | zipVersion20
header.ReaderVersion = zipVersion20
// we store local times in header.Modified- other zip readers expect this
// we set extended timestamp (UTC) info as an Extra for compatibility
// we only set mod time, not time of last access or time of original creation
// https://libzip.org/specifications/extrafld.txt
if !header.Modified.IsZero() {
header.Extra = append(header.Extra, NewExtendedTimestampExtraField(header.Modified).Encode()...)
}
if file.Info.IsDir() {
header.Name += "/"
header.Method = zip.Store
header.Flags &^= 0x8 // won't write data descriptor (crc32, comp, uncomp)
header.UncompressedSize64 = 0
} else {
header.Method = zip.Deflate
header.Flags |= 0x8 // will write data descriptor (crc32, comp, uncomp)
header.CompressedSize64 = uint64(file.Written())
}
file.Header = header
return nil
}
func (a *archiver) archive(file *pool.File) error {
fileWriter, err := a.w.CreateRaw(file.Header)
if err != nil {
return fmt.Errorf("create raw for %q: %w", file.Path, err)
}
if _, err = io.Copy(fileWriter, file.CompressedData); err != nil {
return fmt.Errorf("write compressed data for %q: %w", file.Path, err)
}
if file.Overflowed() {
if _, err = file.Overflow.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("seek overflow for %q: %w", file.Path, err)
}
if _, err = io.Copy(fileWriter, file.Overflow); err != nil {
return fmt.Errorf("copy overflow for %q: %w", file.Path, err)
}
file.Overflow.Close()
if err = os.Remove(file.Overflow.Name()); err != nil {
return fmt.Errorf("remove overflow for %q: %w", file.Overflow.Name(), err)
}
}
pool.FilePool.Put(file)
return nil
}
// https://cs.opensource.google/go/go/+/refs/tags/go1.21.0:src/archive/zip/writer.go
func detectUTF8(s string) (valid, require bool) {
for i := 0; i < len(s); {
r, size := utf8.DecodeRuneInString(s[i:])
i += size
if r < 0x20 || r > 0x7d || r == 0x5c {
if !utf8.ValidRune(r) || (r == utf8.RuneError && size == 1) {
return false, false
}
require = true
}
}
return true, require
}