blob: a8df51e2c3955d8ce9a0804101328bef1bae1048 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Jeff Gaston11b5c512017-10-12 12:19:14 -070015package zip
Colin Cross2fe66872015-03-30 17:20:39 -070016
17import (
Dan Willemsen017d8932016-08-04 15:43:03 -070018 "bytes"
19 "compress/flate"
Jeff Gastona2976952017-08-22 17:51:25 -070020 "errors"
Colin Cross2fe66872015-03-30 17:20:39 -070021 "fmt"
Dan Willemsen017d8932016-08-04 15:43:03 -070022 "hash/crc32"
Colin Cross2fe66872015-03-30 17:20:39 -070023 "io"
24 "io/ioutil"
Nan Zhang9067b042017-03-17 14:04:43 -070025 "log"
Colin Cross2fe66872015-03-30 17:20:39 -070026 "os"
27 "path/filepath"
Dan Willemsen017d8932016-08-04 15:43:03 -070028 "runtime/pprof"
29 "runtime/trace"
Jeff Gastona2976952017-08-22 17:51:25 -070030 "sort"
Colin Cross2fe66872015-03-30 17:20:39 -070031 "strings"
Dan Willemsen017d8932016-08-04 15:43:03 -070032 "sync"
Colin Cross2fe66872015-03-30 17:20:39 -070033 "time"
Nan Zhang674dd932018-01-26 18:30:36 -080034 "unicode"
Dan Willemsen017d8932016-08-04 15:43:03 -070035
Colin Crossf83c1502017-11-10 13:11:02 -080036 "github.com/google/blueprint/pathtools"
37
Jeff Gastona2976952017-08-22 17:51:25 -070038 "android/soong/jar"
Dan Willemsen017d8932016-08-04 15:43:03 -070039 "android/soong/third_party/zip"
Colin Cross2fe66872015-03-30 17:20:39 -070040)
41
Dan Willemsen017d8932016-08-04 15:43:03 -070042// Block size used during parallel compression of a single file.
43const parallelBlockSize = 1 * 1024 * 1024 // 1MB
44
45// Minimum file size to use parallel compression. It requires more
46// flate.Writer allocations, since we can't change the dictionary
47// during Reset
48const minParallelFileSize = parallelBlockSize * 6
49
50// Size of the ZIP compression window (32KB)
51const windowSize = 32 * 1024
52
53type nopCloser struct {
54 io.Writer
55}
56
57func (nopCloser) Close() error {
58 return nil
59}
60
Jeff Gastoncef50b92017-08-23 15:41:35 -070061type byteReaderCloser struct {
Colin Cross635acc92017-09-12 22:50:46 -070062 *bytes.Reader
Jeff Gastoncef50b92017-08-23 15:41:35 -070063 io.Closer
64}
65
Nan Zhang9067b042017-03-17 14:04:43 -070066type pathMapping struct {
67 dest, src string
Nan Zhangf281bd82017-04-25 16:47:45 -070068 zipMethod uint16
69}
70
71type uniqueSet map[string]bool
72
73func (u *uniqueSet) String() string {
74 return `""`
75}
76
77func (u *uniqueSet) Set(s string) error {
78 if _, found := (*u)[s]; found {
79 return fmt.Errorf("File %q was specified twice as a file to not deflate", s)
80 } else {
81 (*u)[s] = true
82 }
83
84 return nil
Colin Cross2fe66872015-03-30 17:20:39 -070085}
86
Jeff Gastonc3bdc972017-10-12 12:18:19 -070087type FileArg struct {
88 PathPrefixInZip, SourcePrefixToStrip string
89 SourceFiles []string
90 GlobDir string
91}
92
93type FileArgs []FileArg
94
95type ZipWriter struct {
Colin Crosse5580972017-08-30 17:40:21 -070096 time time.Time
97 createdFiles map[string]string
98 createdDirs map[string]string
99 directories bool
Colin Crosse19c7932015-04-24 15:08:38 -0700100
Dan Willemsen017d8932016-08-04 15:43:03 -0700101 errors chan error
102 writeOps chan chan *zipEntry
103
Jeff Gaston175f34c2017-08-17 21:43:21 -0700104 cpuRateLimiter *CPURateLimiter
105 memoryRateLimiter *MemoryRateLimiter
Dan Willemsen017d8932016-08-04 15:43:03 -0700106
107 compressorPool sync.Pool
108 compLevel int
109}
110
111type zipEntry struct {
112 fh *zip.FileHeader
113
114 // List of delayed io.Reader
115 futureReaders chan chan io.Reader
Jeff Gaston175f34c2017-08-17 21:43:21 -0700116
117 // Only used for passing into the MemoryRateLimiter to ensure we
118 // release as much memory as much as we request
119 allocatedSize int64
Colin Cross2fe66872015-03-30 17:20:39 -0700120}
121
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700122type ZipArgs struct {
123 FileArgs FileArgs
124 OutputFilePath string
125 CpuProfileFilePath string
126 TraceFilePath string
127 EmulateJar bool
128 AddDirectoryEntriesToZip bool
129 CompressionLevel int
130 ManifestSourcePath string
131 NumParallelJobs int
132 NonDeflatedFiles map[string]bool
Colin Crossf83c1502017-11-10 13:11:02 -0800133 WriteIfChanged bool
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700134}
Colin Cross2fe66872015-03-30 17:20:39 -0700135
Nan Zhang674dd932018-01-26 18:30:36 -0800136const NOQUOTE = '\x00'
137
138func ReadRespFile(bytes []byte) []string {
139 var args []string
140 var arg []rune
141
142 isEscaping := false
143 quotingStart := NOQUOTE
144 for _, c := range string(bytes) {
145 switch {
146 case isEscaping:
147 if quotingStart == '"' {
148 if !(c == '"' || c == '\\') {
149 // '\"' or '\\' will be escaped under double quoting.
150 arg = append(arg, '\\')
151 }
152 }
153 arg = append(arg, c)
154 isEscaping = false
155 case c == '\\' && quotingStart != '\'':
156 isEscaping = true
157 case quotingStart == NOQUOTE && (c == '\'' || c == '"'):
158 quotingStart = c
159 case quotingStart != NOQUOTE && c == quotingStart:
160 quotingStart = NOQUOTE
161 case quotingStart == NOQUOTE && unicode.IsSpace(c):
162 // Current character is a space outside quotes
163 if len(arg) != 0 {
164 args = append(args, string(arg))
165 }
166 arg = arg[:0]
167 default:
168 arg = append(arg, c)
169 }
170 }
171
172 if len(arg) != 0 {
173 args = append(args, string(arg))
174 }
175
176 return args
177}
178
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700179func Run(args ZipArgs) (err error) {
180 if args.CpuProfileFilePath != "" {
181 f, err := os.Create(args.CpuProfileFilePath)
Dan Willemsen017d8932016-08-04 15:43:03 -0700182 if err != nil {
183 fmt.Fprintln(os.Stderr, err.Error())
184 os.Exit(1)
185 }
186 defer f.Close()
187 pprof.StartCPUProfile(f)
188 defer pprof.StopCPUProfile()
189 }
190
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700191 if args.TraceFilePath != "" {
192 f, err := os.Create(args.TraceFilePath)
Dan Willemsen017d8932016-08-04 15:43:03 -0700193 if err != nil {
194 fmt.Fprintln(os.Stderr, err.Error())
195 os.Exit(1)
196 }
197 defer f.Close()
198 err = trace.Start(f)
199 if err != nil {
200 fmt.Fprintln(os.Stderr, err.Error())
201 os.Exit(1)
202 }
203 defer trace.Stop()
204 }
205
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700206 if args.OutputFilePath == "" {
207 return fmt.Errorf("output file path must be nonempty")
Colin Cross2fe66872015-03-30 17:20:39 -0700208 }
209
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700210 if args.EmulateJar {
211 args.AddDirectoryEntriesToZip = true
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700212 }
213
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700214 w := &ZipWriter{
Colin Cross635acc92017-09-12 22:50:46 -0700215 time: jar.DefaultTime,
Colin Crosse5580972017-08-30 17:40:21 -0700216 createdDirs: make(map[string]string),
217 createdFiles: make(map[string]string),
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700218 directories: args.AddDirectoryEntriesToZip,
219 compLevel: args.CompressionLevel,
Colin Cross2fe66872015-03-30 17:20:39 -0700220 }
Nan Zhang9067b042017-03-17 14:04:43 -0700221 pathMappings := []pathMapping{}
Nan Zhang9067b042017-03-17 14:04:43 -0700222
Colin Crossd3216292018-09-14 15:06:31 -0700223 noCompression := args.CompressionLevel == 0
224
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700225 for _, fa := range args.FileArgs {
226 srcs := fa.SourceFiles
227 if fa.GlobDir != "" {
228 srcs = append(srcs, recursiveGlobFiles(fa.GlobDir)...)
Colin Cross7b10cf12017-08-30 14:12:21 -0700229 }
230 for _, src := range srcs {
Colin Crossd3216292018-09-14 15:06:31 -0700231 err := fillPathPairs(fa.PathPrefixInZip, fa.SourcePrefixToStrip, src,
232 &pathMappings, args.NonDeflatedFiles, noCompression)
233 if err != nil {
Nan Zhang9067b042017-03-17 14:04:43 -0700234 log.Fatal(err)
235 }
236 }
237 }
238
Colin Crossf83c1502017-11-10 13:11:02 -0800239 buf := &bytes.Buffer{}
240 var out io.Writer = buf
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700241
Colin Crossf83c1502017-11-10 13:11:02 -0800242 if !args.WriteIfChanged {
243 f, err := os.Create(args.OutputFilePath)
244 if err != nil {
245 return err
246 }
247
248 defer f.Close()
249 defer func() {
250 if err != nil {
251 os.Remove(args.OutputFilePath)
252 }
253 }()
254
255 out = f
256 }
257
258 err = w.write(out, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.NumParallelJobs)
259 if err != nil {
260 return err
261 }
262
263 if args.WriteIfChanged {
264 err := pathtools.WriteFileIfChanged(args.OutputFilePath, buf.Bytes(), 0666)
265 if err != nil {
266 return err
267 }
268 }
269
270 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700271}
272
Colin Crossd3216292018-09-14 15:06:31 -0700273func fillPathPairs(prefix, rel, src string, pathMappings *[]pathMapping,
274 nonDeflatedFiles map[string]bool, noCompression bool) error {
275
Nan Zhang9067b042017-03-17 14:04:43 -0700276 src = strings.TrimSpace(src)
277 if src == "" {
278 return nil
279 }
280 src = filepath.Clean(src)
281 dest, err := filepath.Rel(rel, src)
282 if err != nil {
283 return err
284 }
285 dest = filepath.Join(prefix, dest)
286
Nan Zhangf281bd82017-04-25 16:47:45 -0700287 zipMethod := zip.Deflate
Colin Crossd3216292018-09-14 15:06:31 -0700288 if _, found := nonDeflatedFiles[dest]; found || noCompression {
Nan Zhangf281bd82017-04-25 16:47:45 -0700289 zipMethod = zip.Store
290 }
291 *pathMappings = append(*pathMappings,
292 pathMapping{dest: dest, src: src, zipMethod: zipMethod})
Nan Zhang9067b042017-03-17 14:04:43 -0700293
294 return nil
295}
296
Jeff Gastona2976952017-08-22 17:51:25 -0700297func jarSort(mappings []pathMapping) {
298 less := func(i int, j int) (smaller bool) {
299 return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest)
300 }
301 sort.SliceStable(mappings, less)
302}
303
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700304type readerSeekerCloser interface {
305 io.Reader
306 io.ReaderAt
307 io.Closer
308 io.Seeker
309}
310
Colin Crossf83c1502017-11-10 13:11:02 -0800311func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string, emulateJar bool, parallelJobs int) error {
Dan Willemsen017d8932016-08-04 15:43:03 -0700312 z.errors = make(chan error)
313 defer close(z.errors)
Colin Cross2fe66872015-03-30 17:20:39 -0700314
Dan Willemsen017d8932016-08-04 15:43:03 -0700315 // This channel size can be essentially unlimited -- it's used as a fifo
316 // queue decouple the CPU and IO loads. Directories don't require any
317 // compression time, but still cost some IO. Similar with small files that
318 // can be very fast to compress. Some files that are more difficult to
319 // compress won't take a corresponding longer time writing out.
320 //
321 // The optimum size here depends on your CPU and IO characteristics, and
322 // the the layout of your zip file. 1000 was chosen mostly at random as
323 // something that worked reasonably well for a test file.
324 //
325 // The RateLimit object will put the upper bounds on the number of
326 // parallel compressions and outstanding buffers.
327 z.writeOps = make(chan chan *zipEntry, 1000)
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700328 z.cpuRateLimiter = NewCPURateLimiter(int64(parallelJobs))
Jeff Gaston175f34c2017-08-17 21:43:21 -0700329 z.memoryRateLimiter = NewMemoryRateLimiter(0)
330 defer func() {
331 z.cpuRateLimiter.Stop()
332 z.memoryRateLimiter.Stop()
333 }()
Jeff Gastona2976952017-08-22 17:51:25 -0700334
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700335 if manifest != "" && !emulateJar {
Colin Cross635acc92017-09-12 22:50:46 -0700336 return errors.New("must specify --jar when specifying a manifest via -m")
Jeff Gastona2976952017-08-22 17:51:25 -0700337 }
338
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700339 if emulateJar {
Colin Cross635acc92017-09-12 22:50:46 -0700340 // manifest may be empty, in which case addManifest will fill in a default
341 pathMappings = append(pathMappings, pathMapping{jar.ManifestFile, manifest, zip.Deflate})
342
Jeff Gastona2976952017-08-22 17:51:25 -0700343 jarSort(pathMappings)
344 }
345
Dan Willemsen017d8932016-08-04 15:43:03 -0700346 go func() {
347 var err error
348 defer close(z.writeOps)
349
Nan Zhang9067b042017-03-17 14:04:43 -0700350 for _, ele := range pathMappings {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700351 if emulateJar && ele.dest == jar.ManifestFile {
Jeff Gastoncef50b92017-08-23 15:41:35 -0700352 err = z.addManifest(ele.dest, ele.src, ele.zipMethod)
353 } else {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700354 err = z.addFile(ele.dest, ele.src, ele.zipMethod, emulateJar)
Jeff Gastoncef50b92017-08-23 15:41:35 -0700355 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700356 if err != nil {
357 z.errors <- err
358 return
359 }
360 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700361 }()
362
363 zipw := zip.NewWriter(f)
364
365 var currentWriteOpChan chan *zipEntry
366 var currentWriter io.WriteCloser
367 var currentReaders chan chan io.Reader
368 var currentReader chan io.Reader
369 var done bool
370
371 for !done {
372 var writeOpsChan chan chan *zipEntry
373 var writeOpChan chan *zipEntry
374 var readersChan chan chan io.Reader
375
376 if currentReader != nil {
377 // Only read and process errors
378 } else if currentReaders != nil {
379 readersChan = currentReaders
380 } else if currentWriteOpChan != nil {
381 writeOpChan = currentWriteOpChan
382 } else {
383 writeOpsChan = z.writeOps
384 }
385
386 select {
387 case writeOp, ok := <-writeOpsChan:
388 if !ok {
389 done = true
390 }
391
392 currentWriteOpChan = writeOp
393
394 case op := <-writeOpChan:
395 currentWriteOpChan = nil
396
Colin Crossf83c1502017-11-10 13:11:02 -0800397 var err error
Dan Willemsen017d8932016-08-04 15:43:03 -0700398 if op.fh.Method == zip.Deflate {
399 currentWriter, err = zipw.CreateCompressedHeader(op.fh)
400 } else {
401 var zw io.Writer
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700402
403 op.fh.CompressedSize64 = op.fh.UncompressedSize64
404
405 zw, err = zipw.CreateHeaderAndroid(op.fh)
Dan Willemsen017d8932016-08-04 15:43:03 -0700406 currentWriter = nopCloser{zw}
407 }
408 if err != nil {
409 return err
410 }
411
412 currentReaders = op.futureReaders
413 if op.futureReaders == nil {
414 currentWriter.Close()
415 currentWriter = nil
416 }
Jeff Gaston175f34c2017-08-17 21:43:21 -0700417 z.memoryRateLimiter.Finish(op.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700418
419 case futureReader, ok := <-readersChan:
420 if !ok {
421 // Done with reading
422 currentWriter.Close()
423 currentWriter = nil
424 currentReaders = nil
425 }
426
427 currentReader = futureReader
428
429 case reader := <-currentReader:
Colin Crossf83c1502017-11-10 13:11:02 -0800430 _, err := io.Copy(currentWriter, reader)
Dan Willemsen017d8932016-08-04 15:43:03 -0700431 if err != nil {
432 return err
433 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700434
435 currentReader = nil
436
Colin Crossf83c1502017-11-10 13:11:02 -0800437 case err := <-z.errors:
Colin Cross2fe66872015-03-30 17:20:39 -0700438 return err
439 }
440 }
441
Dan Willemsen017d8932016-08-04 15:43:03 -0700442 // One last chance to catch an error
443 select {
Colin Crossf83c1502017-11-10 13:11:02 -0800444 case err := <-z.errors:
Dan Willemsen017d8932016-08-04 15:43:03 -0700445 return err
446 default:
447 zipw.Close()
448 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700449 }
Colin Cross2fe66872015-03-30 17:20:39 -0700450}
451
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700452// imports (possibly with compression) <src> into the zip at sub-path <dest>
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700453func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar bool) error {
Dan Willemsen017d8932016-08-04 15:43:03 -0700454 var fileSize int64
Dan Willemsen10462b32017-03-15 19:02:51 -0700455 var executable bool
Dan Willemsen017d8932016-08-04 15:43:03 -0700456
Nan Zhang9067b042017-03-17 14:04:43 -0700457 if s, err := os.Lstat(src); err != nil {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700458 return err
459 } else if s.IsDir() {
Colin Cross957cc4e2015-04-24 15:10:32 -0700460 if z.directories {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700461 return z.writeDirectory(dest, src, emulateJar)
Colin Cross957cc4e2015-04-24 15:10:32 -0700462 }
463 return nil
Dan Willemsen017d8932016-08-04 15:43:03 -0700464 } else {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700465 if err := z.writeDirectory(filepath.Dir(dest), src, emulateJar); err != nil {
Colin Crosse5580972017-08-30 17:40:21 -0700466 return err
467 }
468
469 if prev, exists := z.createdDirs[dest]; exists {
470 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
471 }
472 if prev, exists := z.createdFiles[dest]; exists {
473 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
474 }
475
476 z.createdFiles[dest] = src
477
478 if s.Mode()&os.ModeSymlink != 0 {
479 return z.writeSymlink(dest, src)
480 } else if !s.Mode().IsRegular() {
481 return fmt.Errorf("%s is not a file, directory, or symlink", src)
482 }
483
Dan Willemsen017d8932016-08-04 15:43:03 -0700484 fileSize = s.Size()
Dan Willemsen10462b32017-03-15 19:02:51 -0700485 executable = s.Mode()&0100 != 0
Colin Cross957cc4e2015-04-24 15:10:32 -0700486 }
487
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700488 r, err := os.Open(src)
489 if err != nil {
490 return err
491 }
492
493 header := &zip.FileHeader{
494 Name: dest,
495 Method: method,
496 UncompressedSize64: uint64(fileSize),
497 }
498
499 if executable {
500 header.SetMode(0700)
501 }
502
503 return z.writeFileContents(header, r)
504}
505
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700506func (z *ZipWriter) addManifest(dest string, src string, method uint16) error {
Colin Crosse5580972017-08-30 17:40:21 -0700507 if prev, exists := z.createdDirs[dest]; exists {
508 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
509 }
510 if prev, exists := z.createdFiles[dest]; exists {
511 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
512 }
513
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700514 if err := z.writeDirectory(filepath.Dir(dest), src, true); err != nil {
Colin Cross635acc92017-09-12 22:50:46 -0700515 return err
Jeff Gastoncef50b92017-08-23 15:41:35 -0700516 }
517
Colin Cross635acc92017-09-12 22:50:46 -0700518 fh, buf, err := jar.ManifestFileContents(src)
519 if err != nil {
520 return err
Jeff Gastoncef50b92017-08-23 15:41:35 -0700521 }
522
Colin Cross635acc92017-09-12 22:50:46 -0700523 reader := &byteReaderCloser{bytes.NewReader(buf), ioutil.NopCloser(nil)}
524
525 return z.writeFileContents(fh, reader)
Jeff Gastoncef50b92017-08-23 15:41:35 -0700526}
527
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700528func (z *ZipWriter) writeFileContents(header *zip.FileHeader, r readerSeekerCloser) (err error) {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700529
530 header.SetModTime(z.time)
531
Dan Willemsen017d8932016-08-04 15:43:03 -0700532 compressChan := make(chan *zipEntry, 1)
533 z.writeOps <- compressChan
534
535 // Pre-fill a zipEntry, it will be sent in the compressChan once
536 // we're sure about the Method and CRC.
537 ze := &zipEntry{
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700538 fh: header,
Dan Willemsen10462b32017-03-15 19:02:51 -0700539 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700540
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700541 ze.allocatedSize = int64(header.UncompressedSize64)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700542 z.cpuRateLimiter.Request()
543 z.memoryRateLimiter.Request(ze.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700544
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700545 fileSize := int64(header.UncompressedSize64)
546 if fileSize == 0 {
547 fileSize = int64(header.UncompressedSize)
548 }
549
550 if header.Method == zip.Deflate && fileSize >= minParallelFileSize {
Dan Willemsen017d8932016-08-04 15:43:03 -0700551 wg := new(sync.WaitGroup)
552
553 // Allocate enough buffer to hold all readers. We'll limit
554 // this based on actual buffer sizes in RateLimit.
555 ze.futureReaders = make(chan chan io.Reader, (fileSize/parallelBlockSize)+1)
556
557 // Calculate the CRC in the background, since reading the entire
558 // file could take a while.
559 //
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700560 // We could split this up into chunks as well, but it's faster
Dan Willemsen017d8932016-08-04 15:43:03 -0700561 // than the compression. Due to the Go Zip API, we also need to
562 // know the result before we can begin writing the compressed
563 // data out to the zipfile.
564 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700565 go z.crcFile(r, ze, compressChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700566
567 for start := int64(0); start < fileSize; start += parallelBlockSize {
568 sr := io.NewSectionReader(r, start, parallelBlockSize)
569 resultChan := make(chan io.Reader, 1)
570 ze.futureReaders <- resultChan
571
Jeff Gaston175f34c2017-08-17 21:43:21 -0700572 z.cpuRateLimiter.Request()
Dan Willemsen017d8932016-08-04 15:43:03 -0700573
574 last := !(start+parallelBlockSize < fileSize)
575 var dict []byte
576 if start >= windowSize {
577 dict, err = ioutil.ReadAll(io.NewSectionReader(r, start-windowSize, windowSize))
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700578 if err != nil {
579 return err
580 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700581 }
582
583 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700584 go z.compressPartialFile(sr, dict, last, resultChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700585 }
586
587 close(ze.futureReaders)
588
589 // Close the file handle after all readers are done
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700590 go func(wg *sync.WaitGroup, closer io.Closer) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700591 wg.Wait()
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700592 closer.Close()
Dan Willemsen017d8932016-08-04 15:43:03 -0700593 }(wg, r)
594 } else {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700595 go func() {
596 z.compressWholeFile(ze, r, compressChan)
597 r.Close()
598 }()
Dan Willemsen017d8932016-08-04 15:43:03 -0700599 }
600
601 return nil
602}
603
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700604func (z *ZipWriter) crcFile(r io.Reader, ze *zipEntry, resultChan chan *zipEntry, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700605 defer wg.Done()
Jeff Gaston175f34c2017-08-17 21:43:21 -0700606 defer z.cpuRateLimiter.Finish()
Dan Willemsen017d8932016-08-04 15:43:03 -0700607
608 crc := crc32.NewIEEE()
609 _, err := io.Copy(crc, r)
610 if err != nil {
611 z.errors <- err
612 return
613 }
614
615 ze.fh.CRC32 = crc.Sum32()
616 resultChan <- ze
617 close(resultChan)
618}
619
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700620func (z *ZipWriter) compressPartialFile(r io.Reader, dict []byte, last bool, resultChan chan io.Reader, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700621 defer wg.Done()
622
623 result, err := z.compressBlock(r, dict, last)
624 if err != nil {
625 z.errors <- err
626 return
627 }
628
Jeff Gaston175f34c2017-08-17 21:43:21 -0700629 z.cpuRateLimiter.Finish()
630
Dan Willemsen017d8932016-08-04 15:43:03 -0700631 resultChan <- result
632}
633
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700634func (z *ZipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700635 buf := new(bytes.Buffer)
636 var fw *flate.Writer
637 var err error
638 if len(dict) > 0 {
639 // There's no way to Reset a Writer with a new dictionary, so
640 // don't use the Pool
641 fw, err = flate.NewWriterDict(buf, z.compLevel, dict)
642 } else {
643 var ok bool
644 if fw, ok = z.compressorPool.Get().(*flate.Writer); ok {
645 fw.Reset(buf)
646 } else {
647 fw, err = flate.NewWriter(buf, z.compLevel)
648 }
649 defer z.compressorPool.Put(fw)
650 }
651 if err != nil {
652 return nil, err
653 }
654
655 _, err = io.Copy(fw, r)
656 if err != nil {
657 return nil, err
658 }
659 if last {
660 fw.Close()
661 } else {
662 fw.Flush()
663 }
664
665 return buf, nil
666}
667
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700668func (z *ZipWriter) compressWholeFile(ze *zipEntry, r io.ReadSeeker, compressChan chan *zipEntry) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700669
Dan Willemsen017d8932016-08-04 15:43:03 -0700670 crc := crc32.NewIEEE()
Dan Willemsena8b55022017-03-15 21:49:26 -0700671 _, err := io.Copy(crc, r)
Colin Cross2fe66872015-03-30 17:20:39 -0700672 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700673 z.errors <- err
674 return
Colin Cross2fe66872015-03-30 17:20:39 -0700675 }
676
Dan Willemsena8b55022017-03-15 21:49:26 -0700677 ze.fh.CRC32 = crc.Sum32()
Colin Cross2fe66872015-03-30 17:20:39 -0700678
Dan Willemsen017d8932016-08-04 15:43:03 -0700679 _, err = r.Seek(0, 0)
Colin Cross2fe66872015-03-30 17:20:39 -0700680 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700681 z.errors <- err
682 return
Colin Cross2fe66872015-03-30 17:20:39 -0700683 }
684
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700685 readFile := func(reader io.ReadSeeker) ([]byte, error) {
686 _, err := reader.Seek(0, 0)
Nan Zhangf281bd82017-04-25 16:47:45 -0700687 if err != nil {
688 return nil, err
689 }
690
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700691 buf, err := ioutil.ReadAll(reader)
Nan Zhangf281bd82017-04-25 16:47:45 -0700692 if err != nil {
693 return nil, err
694 }
695
696 return buf, nil
697 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700698
Dan Willemsena8b55022017-03-15 21:49:26 -0700699 ze.futureReaders = make(chan chan io.Reader, 1)
Dan Willemsen017d8932016-08-04 15:43:03 -0700700 futureReader := make(chan io.Reader, 1)
701 ze.futureReaders <- futureReader
702 close(ze.futureReaders)
703
Nan Zhangf281bd82017-04-25 16:47:45 -0700704 if ze.fh.Method == zip.Deflate {
705 compressed, err := z.compressBlock(r, nil, true)
706 if err != nil {
707 z.errors <- err
708 return
709 }
710 if uint64(compressed.Len()) < ze.fh.UncompressedSize64 {
711 futureReader <- compressed
Nan Zhangf281bd82017-04-25 16:47:45 -0700712 } else {
713 buf, err := readFile(r)
714 if err != nil {
715 z.errors <- err
716 return
717 }
718 ze.fh.Method = zip.Store
719 futureReader <- bytes.NewReader(buf)
Nan Zhangf281bd82017-04-25 16:47:45 -0700720 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700721 } else {
Nan Zhangf281bd82017-04-25 16:47:45 -0700722 buf, err := readFile(r)
Dan Willemsen017d8932016-08-04 15:43:03 -0700723 if err != nil {
724 z.errors <- err
725 return
726 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700727 ze.fh.Method = zip.Store
728 futureReader <- bytes.NewReader(buf)
Dan Willemsen017d8932016-08-04 15:43:03 -0700729 }
Nan Zhangf281bd82017-04-25 16:47:45 -0700730
Jeff Gaston175f34c2017-08-17 21:43:21 -0700731 z.cpuRateLimiter.Finish()
732
Dan Willemsen017d8932016-08-04 15:43:03 -0700733 close(futureReader)
734
735 compressChan <- ze
736 close(compressChan)
Colin Cross2fe66872015-03-30 17:20:39 -0700737}
Colin Crosse19c7932015-04-24 15:08:38 -0700738
Colin Crosse5580972017-08-30 17:40:21 -0700739// writeDirectory annotates that dir is a directory created for the src file or directory, and adds
740// the directory entry to the zip file if directories are enabled.
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700741func (z *ZipWriter) writeDirectory(dir string, src string, emulateJar bool) error {
Jeff Gaston2d174132017-08-15 18:05:56 -0700742 // clean the input
Colin Crosse5580972017-08-30 17:40:21 -0700743 dir = filepath.Clean(dir)
Jeff Gaston2d174132017-08-15 18:05:56 -0700744
745 // discover any uncreated directories in the path
746 zipDirs := []string{}
Colin Crosse5580972017-08-30 17:40:21 -0700747 for dir != "" && dir != "." {
748 if _, exists := z.createdDirs[dir]; exists {
749 break
750 }
Jeff Gaston2d174132017-08-15 18:05:56 -0700751
Colin Crosse5580972017-08-30 17:40:21 -0700752 if prev, exists := z.createdFiles[dir]; exists {
753 return fmt.Errorf("destination %q is both a directory %q and a file %q", dir, src, prev)
754 }
755
756 z.createdDirs[dir] = src
Jeff Gaston2d174132017-08-15 18:05:56 -0700757 // parent directories precede their children
Colin Crosse5580972017-08-30 17:40:21 -0700758 zipDirs = append([]string{dir}, zipDirs...)
Jeff Gaston2d174132017-08-15 18:05:56 -0700759
Colin Crosse5580972017-08-30 17:40:21 -0700760 dir = filepath.Dir(dir)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700761 }
762
Colin Crosse5580972017-08-30 17:40:21 -0700763 if z.directories {
764 // make a directory entry for each uncreated directory
765 for _, cleanDir := range zipDirs {
Colin Cross635acc92017-09-12 22:50:46 -0700766 var dirHeader *zip.FileHeader
Colin Crosse19c7932015-04-24 15:08:38 -0700767
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700768 if emulateJar && cleanDir+"/" == jar.MetaDir {
Colin Cross635acc92017-09-12 22:50:46 -0700769 dirHeader = jar.MetaDirFileHeader()
770 } else {
771 dirHeader = &zip.FileHeader{
772 Name: cleanDir + "/",
773 }
774 dirHeader.SetMode(0700 | os.ModeDir)
Colin Crosse5580972017-08-30 17:40:21 -0700775 }
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700776
Colin Cross635acc92017-09-12 22:50:46 -0700777 dirHeader.SetModTime(z.time)
778
Colin Crosse5580972017-08-30 17:40:21 -0700779 ze := make(chan *zipEntry, 1)
780 ze <- &zipEntry{
781 fh: dirHeader,
782 }
783 close(ze)
784 z.writeOps <- ze
Colin Crosse19c7932015-04-24 15:08:38 -0700785 }
Colin Crosse19c7932015-04-24 15:08:38 -0700786 }
787
788 return nil
789}
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700790
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700791func (z *ZipWriter) writeSymlink(rel, file string) error {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700792 fileHeader := &zip.FileHeader{
793 Name: rel,
794 }
795 fileHeader.SetModTime(z.time)
Colin Cross297d9bc2018-06-22 16:37:47 -0700796 fileHeader.SetMode(0777 | os.ModeSymlink)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700797
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700798 dest, err := os.Readlink(file)
799 if err != nil {
800 return err
801 }
802
Colin Cross297d9bc2018-06-22 16:37:47 -0700803 fileHeader.UncompressedSize64 = uint64(len(dest))
804 fileHeader.CRC32 = crc32.ChecksumIEEE([]byte(dest))
805
Dan Willemsen017d8932016-08-04 15:43:03 -0700806 ze := make(chan *zipEntry, 1)
807 futureReaders := make(chan chan io.Reader, 1)
808 futureReader := make(chan io.Reader, 1)
809 futureReaders <- futureReader
810 close(futureReaders)
811 futureReader <- bytes.NewBufferString(dest)
812 close(futureReader)
813
Dan Willemsen017d8932016-08-04 15:43:03 -0700814 ze <- &zipEntry{
815 fh: fileHeader,
816 futureReaders: futureReaders,
817 }
818 close(ze)
819 z.writeOps <- ze
820
821 return nil
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700822}
Colin Cross7b10cf12017-08-30 14:12:21 -0700823
824func recursiveGlobFiles(path string) []string {
825 var files []string
826 filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
827 if !info.IsDir() {
828 files = append(files, path)
829 }
830 return nil
831 })
832
833 return files
834}