blob: c878a0cce086eedc00d337e350287185ca9915ae [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"
Dan Willemsen017d8932016-08-04 15:43:03 -070034
Colin Crossf83c1502017-11-10 13:11:02 -080035 "github.com/google/blueprint/pathtools"
36
Jeff Gastona2976952017-08-22 17:51:25 -070037 "android/soong/jar"
Dan Willemsen017d8932016-08-04 15:43:03 -070038 "android/soong/third_party/zip"
Colin Cross2fe66872015-03-30 17:20:39 -070039)
40
Dan Willemsen017d8932016-08-04 15:43:03 -070041// Block size used during parallel compression of a single file.
42const parallelBlockSize = 1 * 1024 * 1024 // 1MB
43
44// Minimum file size to use parallel compression. It requires more
45// flate.Writer allocations, since we can't change the dictionary
46// during Reset
47const minParallelFileSize = parallelBlockSize * 6
48
49// Size of the ZIP compression window (32KB)
50const windowSize = 32 * 1024
51
52type nopCloser struct {
53 io.Writer
54}
55
56func (nopCloser) Close() error {
57 return nil
58}
59
Jeff Gastoncef50b92017-08-23 15:41:35 -070060type byteReaderCloser struct {
Colin Cross635acc92017-09-12 22:50:46 -070061 *bytes.Reader
Jeff Gastoncef50b92017-08-23 15:41:35 -070062 io.Closer
63}
64
Nan Zhang9067b042017-03-17 14:04:43 -070065type pathMapping struct {
66 dest, src string
Nan Zhangf281bd82017-04-25 16:47:45 -070067 zipMethod uint16
68}
69
70type uniqueSet map[string]bool
71
72func (u *uniqueSet) String() string {
73 return `""`
74}
75
76func (u *uniqueSet) Set(s string) error {
77 if _, found := (*u)[s]; found {
78 return fmt.Errorf("File %q was specified twice as a file to not deflate", s)
79 } else {
80 (*u)[s] = true
81 }
82
83 return nil
Colin Cross2fe66872015-03-30 17:20:39 -070084}
85
Jeff Gastonc3bdc972017-10-12 12:18:19 -070086type FileArg struct {
87 PathPrefixInZip, SourcePrefixToStrip string
88 SourceFiles []string
89 GlobDir string
90}
91
92type FileArgs []FileArg
93
94type ZipWriter struct {
Colin Crosse5580972017-08-30 17:40:21 -070095 time time.Time
96 createdFiles map[string]string
97 createdDirs map[string]string
98 directories bool
Colin Crosse19c7932015-04-24 15:08:38 -070099
Dan Willemsen017d8932016-08-04 15:43:03 -0700100 errors chan error
101 writeOps chan chan *zipEntry
102
Jeff Gaston175f34c2017-08-17 21:43:21 -0700103 cpuRateLimiter *CPURateLimiter
104 memoryRateLimiter *MemoryRateLimiter
Dan Willemsen017d8932016-08-04 15:43:03 -0700105
106 compressorPool sync.Pool
107 compLevel int
108}
109
110type zipEntry struct {
111 fh *zip.FileHeader
112
113 // List of delayed io.Reader
114 futureReaders chan chan io.Reader
Jeff Gaston175f34c2017-08-17 21:43:21 -0700115
116 // Only used for passing into the MemoryRateLimiter to ensure we
117 // release as much memory as much as we request
118 allocatedSize int64
Colin Cross2fe66872015-03-30 17:20:39 -0700119}
120
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700121type ZipArgs struct {
122 FileArgs FileArgs
123 OutputFilePath string
124 CpuProfileFilePath string
125 TraceFilePath string
126 EmulateJar bool
127 AddDirectoryEntriesToZip bool
128 CompressionLevel int
129 ManifestSourcePath string
130 NumParallelJobs int
131 NonDeflatedFiles map[string]bool
Colin Crossf83c1502017-11-10 13:11:02 -0800132 WriteIfChanged bool
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700133}
Colin Cross2fe66872015-03-30 17:20:39 -0700134
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700135func Run(args ZipArgs) (err error) {
136 if args.CpuProfileFilePath != "" {
137 f, err := os.Create(args.CpuProfileFilePath)
Dan Willemsen017d8932016-08-04 15:43:03 -0700138 if err != nil {
139 fmt.Fprintln(os.Stderr, err.Error())
140 os.Exit(1)
141 }
142 defer f.Close()
143 pprof.StartCPUProfile(f)
144 defer pprof.StopCPUProfile()
145 }
146
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700147 if args.TraceFilePath != "" {
148 f, err := os.Create(args.TraceFilePath)
Dan Willemsen017d8932016-08-04 15:43:03 -0700149 if err != nil {
150 fmt.Fprintln(os.Stderr, err.Error())
151 os.Exit(1)
152 }
153 defer f.Close()
154 err = trace.Start(f)
155 if err != nil {
156 fmt.Fprintln(os.Stderr, err.Error())
157 os.Exit(1)
158 }
159 defer trace.Stop()
160 }
161
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700162 if args.OutputFilePath == "" {
163 return fmt.Errorf("output file path must be nonempty")
Colin Cross2fe66872015-03-30 17:20:39 -0700164 }
165
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700166 if args.EmulateJar {
167 args.AddDirectoryEntriesToZip = true
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700168 }
169
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700170 w := &ZipWriter{
Colin Cross635acc92017-09-12 22:50:46 -0700171 time: jar.DefaultTime,
Colin Crosse5580972017-08-30 17:40:21 -0700172 createdDirs: make(map[string]string),
173 createdFiles: make(map[string]string),
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700174 directories: args.AddDirectoryEntriesToZip,
175 compLevel: args.CompressionLevel,
Colin Cross2fe66872015-03-30 17:20:39 -0700176 }
Nan Zhang9067b042017-03-17 14:04:43 -0700177 pathMappings := []pathMapping{}
Nan Zhang9067b042017-03-17 14:04:43 -0700178
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700179 for _, fa := range args.FileArgs {
180 srcs := fa.SourceFiles
181 if fa.GlobDir != "" {
182 srcs = append(srcs, recursiveGlobFiles(fa.GlobDir)...)
Colin Cross7b10cf12017-08-30 14:12:21 -0700183 }
184 for _, src := range srcs {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700185 if err := fillPathPairs(fa.PathPrefixInZip,
186 fa.SourcePrefixToStrip, src, &pathMappings, args.NonDeflatedFiles); err != nil {
Nan Zhang9067b042017-03-17 14:04:43 -0700187 log.Fatal(err)
188 }
189 }
190 }
191
Colin Crossf83c1502017-11-10 13:11:02 -0800192 buf := &bytes.Buffer{}
193 var out io.Writer = buf
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700194
Colin Crossf83c1502017-11-10 13:11:02 -0800195 if !args.WriteIfChanged {
196 f, err := os.Create(args.OutputFilePath)
197 if err != nil {
198 return err
199 }
200
201 defer f.Close()
202 defer func() {
203 if err != nil {
204 os.Remove(args.OutputFilePath)
205 }
206 }()
207
208 out = f
209 }
210
211 err = w.write(out, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.NumParallelJobs)
212 if err != nil {
213 return err
214 }
215
216 if args.WriteIfChanged {
217 err := pathtools.WriteFileIfChanged(args.OutputFilePath, buf.Bytes(), 0666)
218 if err != nil {
219 return err
220 }
221 }
222
223 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700224}
225
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700226func fillPathPairs(prefix, rel, src string, pathMappings *[]pathMapping, nonDeflatedFiles map[string]bool) error {
Nan Zhang9067b042017-03-17 14:04:43 -0700227 src = strings.TrimSpace(src)
228 if src == "" {
229 return nil
230 }
231 src = filepath.Clean(src)
232 dest, err := filepath.Rel(rel, src)
233 if err != nil {
234 return err
235 }
236 dest = filepath.Join(prefix, dest)
237
Nan Zhangf281bd82017-04-25 16:47:45 -0700238 zipMethod := zip.Deflate
239 if _, found := nonDeflatedFiles[dest]; found {
240 zipMethod = zip.Store
241 }
242 *pathMappings = append(*pathMappings,
243 pathMapping{dest: dest, src: src, zipMethod: zipMethod})
Nan Zhang9067b042017-03-17 14:04:43 -0700244
245 return nil
246}
247
Jeff Gastona2976952017-08-22 17:51:25 -0700248func jarSort(mappings []pathMapping) {
249 less := func(i int, j int) (smaller bool) {
250 return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest)
251 }
252 sort.SliceStable(mappings, less)
253}
254
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700255type readerSeekerCloser interface {
256 io.Reader
257 io.ReaderAt
258 io.Closer
259 io.Seeker
260}
261
Colin Crossf83c1502017-11-10 13:11:02 -0800262func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string, emulateJar bool, parallelJobs int) error {
Dan Willemsen017d8932016-08-04 15:43:03 -0700263 z.errors = make(chan error)
264 defer close(z.errors)
Colin Cross2fe66872015-03-30 17:20:39 -0700265
Dan Willemsen017d8932016-08-04 15:43:03 -0700266 // This channel size can be essentially unlimited -- it's used as a fifo
267 // queue decouple the CPU and IO loads. Directories don't require any
268 // compression time, but still cost some IO. Similar with small files that
269 // can be very fast to compress. Some files that are more difficult to
270 // compress won't take a corresponding longer time writing out.
271 //
272 // The optimum size here depends on your CPU and IO characteristics, and
273 // the the layout of your zip file. 1000 was chosen mostly at random as
274 // something that worked reasonably well for a test file.
275 //
276 // The RateLimit object will put the upper bounds on the number of
277 // parallel compressions and outstanding buffers.
278 z.writeOps = make(chan chan *zipEntry, 1000)
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700279 z.cpuRateLimiter = NewCPURateLimiter(int64(parallelJobs))
Jeff Gaston175f34c2017-08-17 21:43:21 -0700280 z.memoryRateLimiter = NewMemoryRateLimiter(0)
281 defer func() {
282 z.cpuRateLimiter.Stop()
283 z.memoryRateLimiter.Stop()
284 }()
Jeff Gastona2976952017-08-22 17:51:25 -0700285
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700286 if manifest != "" && !emulateJar {
Colin Cross635acc92017-09-12 22:50:46 -0700287 return errors.New("must specify --jar when specifying a manifest via -m")
Jeff Gastona2976952017-08-22 17:51:25 -0700288 }
289
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700290 if emulateJar {
Colin Cross635acc92017-09-12 22:50:46 -0700291 // manifest may be empty, in which case addManifest will fill in a default
292 pathMappings = append(pathMappings, pathMapping{jar.ManifestFile, manifest, zip.Deflate})
293
Jeff Gastona2976952017-08-22 17:51:25 -0700294 jarSort(pathMappings)
295 }
296
Dan Willemsen017d8932016-08-04 15:43:03 -0700297 go func() {
298 var err error
299 defer close(z.writeOps)
300
Nan Zhang9067b042017-03-17 14:04:43 -0700301 for _, ele := range pathMappings {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700302 if emulateJar && ele.dest == jar.ManifestFile {
Jeff Gastoncef50b92017-08-23 15:41:35 -0700303 err = z.addManifest(ele.dest, ele.src, ele.zipMethod)
304 } else {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700305 err = z.addFile(ele.dest, ele.src, ele.zipMethod, emulateJar)
Jeff Gastoncef50b92017-08-23 15:41:35 -0700306 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700307 if err != nil {
308 z.errors <- err
309 return
310 }
311 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700312 }()
313
314 zipw := zip.NewWriter(f)
315
316 var currentWriteOpChan chan *zipEntry
317 var currentWriter io.WriteCloser
318 var currentReaders chan chan io.Reader
319 var currentReader chan io.Reader
320 var done bool
321
322 for !done {
323 var writeOpsChan chan chan *zipEntry
324 var writeOpChan chan *zipEntry
325 var readersChan chan chan io.Reader
326
327 if currentReader != nil {
328 // Only read and process errors
329 } else if currentReaders != nil {
330 readersChan = currentReaders
331 } else if currentWriteOpChan != nil {
332 writeOpChan = currentWriteOpChan
333 } else {
334 writeOpsChan = z.writeOps
335 }
336
337 select {
338 case writeOp, ok := <-writeOpsChan:
339 if !ok {
340 done = true
341 }
342
343 currentWriteOpChan = writeOp
344
345 case op := <-writeOpChan:
346 currentWriteOpChan = nil
347
Colin Crossf83c1502017-11-10 13:11:02 -0800348 var err error
Dan Willemsen017d8932016-08-04 15:43:03 -0700349 if op.fh.Method == zip.Deflate {
350 currentWriter, err = zipw.CreateCompressedHeader(op.fh)
351 } else {
352 var zw io.Writer
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700353
354 op.fh.CompressedSize64 = op.fh.UncompressedSize64
355
356 zw, err = zipw.CreateHeaderAndroid(op.fh)
Dan Willemsen017d8932016-08-04 15:43:03 -0700357 currentWriter = nopCloser{zw}
358 }
359 if err != nil {
360 return err
361 }
362
363 currentReaders = op.futureReaders
364 if op.futureReaders == nil {
365 currentWriter.Close()
366 currentWriter = nil
367 }
Jeff Gaston175f34c2017-08-17 21:43:21 -0700368 z.memoryRateLimiter.Finish(op.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700369
370 case futureReader, ok := <-readersChan:
371 if !ok {
372 // Done with reading
373 currentWriter.Close()
374 currentWriter = nil
375 currentReaders = nil
376 }
377
378 currentReader = futureReader
379
380 case reader := <-currentReader:
Colin Crossf83c1502017-11-10 13:11:02 -0800381 _, err := io.Copy(currentWriter, reader)
Dan Willemsen017d8932016-08-04 15:43:03 -0700382 if err != nil {
383 return err
384 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700385
386 currentReader = nil
387
Colin Crossf83c1502017-11-10 13:11:02 -0800388 case err := <-z.errors:
Colin Cross2fe66872015-03-30 17:20:39 -0700389 return err
390 }
391 }
392
Dan Willemsen017d8932016-08-04 15:43:03 -0700393 // One last chance to catch an error
394 select {
Colin Crossf83c1502017-11-10 13:11:02 -0800395 case err := <-z.errors:
Dan Willemsen017d8932016-08-04 15:43:03 -0700396 return err
397 default:
398 zipw.Close()
399 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700400 }
Colin Cross2fe66872015-03-30 17:20:39 -0700401}
402
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700403// imports (possibly with compression) <src> into the zip at sub-path <dest>
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700404func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar bool) error {
Dan Willemsen017d8932016-08-04 15:43:03 -0700405 var fileSize int64
Dan Willemsen10462b32017-03-15 19:02:51 -0700406 var executable bool
Dan Willemsen017d8932016-08-04 15:43:03 -0700407
Nan Zhang9067b042017-03-17 14:04:43 -0700408 if s, err := os.Lstat(src); err != nil {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700409 return err
410 } else if s.IsDir() {
Colin Cross957cc4e2015-04-24 15:10:32 -0700411 if z.directories {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700412 return z.writeDirectory(dest, src, emulateJar)
Colin Cross957cc4e2015-04-24 15:10:32 -0700413 }
414 return nil
Dan Willemsen017d8932016-08-04 15:43:03 -0700415 } else {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700416 if err := z.writeDirectory(filepath.Dir(dest), src, emulateJar); err != nil {
Colin Crosse5580972017-08-30 17:40:21 -0700417 return err
418 }
419
420 if prev, exists := z.createdDirs[dest]; exists {
421 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
422 }
423 if prev, exists := z.createdFiles[dest]; exists {
424 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
425 }
426
427 z.createdFiles[dest] = src
428
429 if s.Mode()&os.ModeSymlink != 0 {
430 return z.writeSymlink(dest, src)
431 } else if !s.Mode().IsRegular() {
432 return fmt.Errorf("%s is not a file, directory, or symlink", src)
433 }
434
Dan Willemsen017d8932016-08-04 15:43:03 -0700435 fileSize = s.Size()
Dan Willemsen10462b32017-03-15 19:02:51 -0700436 executable = s.Mode()&0100 != 0
Colin Cross957cc4e2015-04-24 15:10:32 -0700437 }
438
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700439 r, err := os.Open(src)
440 if err != nil {
441 return err
442 }
443
444 header := &zip.FileHeader{
445 Name: dest,
446 Method: method,
447 UncompressedSize64: uint64(fileSize),
448 }
449
450 if executable {
451 header.SetMode(0700)
452 }
453
454 return z.writeFileContents(header, r)
455}
456
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700457func (z *ZipWriter) addManifest(dest string, src string, method uint16) error {
Colin Crosse5580972017-08-30 17:40:21 -0700458 if prev, exists := z.createdDirs[dest]; exists {
459 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
460 }
461 if prev, exists := z.createdFiles[dest]; exists {
462 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
463 }
464
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700465 if err := z.writeDirectory(filepath.Dir(dest), src, true); err != nil {
Colin Cross635acc92017-09-12 22:50:46 -0700466 return err
Jeff Gastoncef50b92017-08-23 15:41:35 -0700467 }
468
Colin Cross635acc92017-09-12 22:50:46 -0700469 fh, buf, err := jar.ManifestFileContents(src)
470 if err != nil {
471 return err
Jeff Gastoncef50b92017-08-23 15:41:35 -0700472 }
473
Colin Cross635acc92017-09-12 22:50:46 -0700474 reader := &byteReaderCloser{bytes.NewReader(buf), ioutil.NopCloser(nil)}
475
476 return z.writeFileContents(fh, reader)
Jeff Gastoncef50b92017-08-23 15:41:35 -0700477}
478
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700479func (z *ZipWriter) writeFileContents(header *zip.FileHeader, r readerSeekerCloser) (err error) {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700480
481 header.SetModTime(z.time)
482
Dan Willemsen017d8932016-08-04 15:43:03 -0700483 compressChan := make(chan *zipEntry, 1)
484 z.writeOps <- compressChan
485
486 // Pre-fill a zipEntry, it will be sent in the compressChan once
487 // we're sure about the Method and CRC.
488 ze := &zipEntry{
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700489 fh: header,
Dan Willemsen10462b32017-03-15 19:02:51 -0700490 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700491
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700492 ze.allocatedSize = int64(header.UncompressedSize64)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700493 z.cpuRateLimiter.Request()
494 z.memoryRateLimiter.Request(ze.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700495
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700496 fileSize := int64(header.UncompressedSize64)
497 if fileSize == 0 {
498 fileSize = int64(header.UncompressedSize)
499 }
500
501 if header.Method == zip.Deflate && fileSize >= minParallelFileSize {
Dan Willemsen017d8932016-08-04 15:43:03 -0700502 wg := new(sync.WaitGroup)
503
504 // Allocate enough buffer to hold all readers. We'll limit
505 // this based on actual buffer sizes in RateLimit.
506 ze.futureReaders = make(chan chan io.Reader, (fileSize/parallelBlockSize)+1)
507
508 // Calculate the CRC in the background, since reading the entire
509 // file could take a while.
510 //
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700511 // We could split this up into chunks as well, but it's faster
Dan Willemsen017d8932016-08-04 15:43:03 -0700512 // than the compression. Due to the Go Zip API, we also need to
513 // know the result before we can begin writing the compressed
514 // data out to the zipfile.
515 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700516 go z.crcFile(r, ze, compressChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700517
518 for start := int64(0); start < fileSize; start += parallelBlockSize {
519 sr := io.NewSectionReader(r, start, parallelBlockSize)
520 resultChan := make(chan io.Reader, 1)
521 ze.futureReaders <- resultChan
522
Jeff Gaston175f34c2017-08-17 21:43:21 -0700523 z.cpuRateLimiter.Request()
Dan Willemsen017d8932016-08-04 15:43:03 -0700524
525 last := !(start+parallelBlockSize < fileSize)
526 var dict []byte
527 if start >= windowSize {
528 dict, err = ioutil.ReadAll(io.NewSectionReader(r, start-windowSize, windowSize))
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700529 if err != nil {
530 return err
531 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700532 }
533
534 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700535 go z.compressPartialFile(sr, dict, last, resultChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700536 }
537
538 close(ze.futureReaders)
539
540 // Close the file handle after all readers are done
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700541 go func(wg *sync.WaitGroup, closer io.Closer) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700542 wg.Wait()
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700543 closer.Close()
Dan Willemsen017d8932016-08-04 15:43:03 -0700544 }(wg, r)
545 } else {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700546 go func() {
547 z.compressWholeFile(ze, r, compressChan)
548 r.Close()
549 }()
Dan Willemsen017d8932016-08-04 15:43:03 -0700550 }
551
552 return nil
553}
554
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700555func (z *ZipWriter) crcFile(r io.Reader, ze *zipEntry, resultChan chan *zipEntry, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700556 defer wg.Done()
Jeff Gaston175f34c2017-08-17 21:43:21 -0700557 defer z.cpuRateLimiter.Finish()
Dan Willemsen017d8932016-08-04 15:43:03 -0700558
559 crc := crc32.NewIEEE()
560 _, err := io.Copy(crc, r)
561 if err != nil {
562 z.errors <- err
563 return
564 }
565
566 ze.fh.CRC32 = crc.Sum32()
567 resultChan <- ze
568 close(resultChan)
569}
570
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700571func (z *ZipWriter) compressPartialFile(r io.Reader, dict []byte, last bool, resultChan chan io.Reader, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700572 defer wg.Done()
573
574 result, err := z.compressBlock(r, dict, last)
575 if err != nil {
576 z.errors <- err
577 return
578 }
579
Jeff Gaston175f34c2017-08-17 21:43:21 -0700580 z.cpuRateLimiter.Finish()
581
Dan Willemsen017d8932016-08-04 15:43:03 -0700582 resultChan <- result
583}
584
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700585func (z *ZipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700586 buf := new(bytes.Buffer)
587 var fw *flate.Writer
588 var err error
589 if len(dict) > 0 {
590 // There's no way to Reset a Writer with a new dictionary, so
591 // don't use the Pool
592 fw, err = flate.NewWriterDict(buf, z.compLevel, dict)
593 } else {
594 var ok bool
595 if fw, ok = z.compressorPool.Get().(*flate.Writer); ok {
596 fw.Reset(buf)
597 } else {
598 fw, err = flate.NewWriter(buf, z.compLevel)
599 }
600 defer z.compressorPool.Put(fw)
601 }
602 if err != nil {
603 return nil, err
604 }
605
606 _, err = io.Copy(fw, r)
607 if err != nil {
608 return nil, err
609 }
610 if last {
611 fw.Close()
612 } else {
613 fw.Flush()
614 }
615
616 return buf, nil
617}
618
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700619func (z *ZipWriter) compressWholeFile(ze *zipEntry, r io.ReadSeeker, compressChan chan *zipEntry) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700620
Dan Willemsen017d8932016-08-04 15:43:03 -0700621 crc := crc32.NewIEEE()
Dan Willemsena8b55022017-03-15 21:49:26 -0700622 _, err := io.Copy(crc, r)
Colin Cross2fe66872015-03-30 17:20:39 -0700623 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700624 z.errors <- err
625 return
Colin Cross2fe66872015-03-30 17:20:39 -0700626 }
627
Dan Willemsena8b55022017-03-15 21:49:26 -0700628 ze.fh.CRC32 = crc.Sum32()
Colin Cross2fe66872015-03-30 17:20:39 -0700629
Dan Willemsen017d8932016-08-04 15:43:03 -0700630 _, err = r.Seek(0, 0)
Colin Cross2fe66872015-03-30 17:20:39 -0700631 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700632 z.errors <- err
633 return
Colin Cross2fe66872015-03-30 17:20:39 -0700634 }
635
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700636 readFile := func(reader io.ReadSeeker) ([]byte, error) {
637 _, err := reader.Seek(0, 0)
Nan Zhangf281bd82017-04-25 16:47:45 -0700638 if err != nil {
639 return nil, err
640 }
641
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700642 buf, err := ioutil.ReadAll(reader)
Nan Zhangf281bd82017-04-25 16:47:45 -0700643 if err != nil {
644 return nil, err
645 }
646
647 return buf, nil
648 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700649
Dan Willemsena8b55022017-03-15 21:49:26 -0700650 ze.futureReaders = make(chan chan io.Reader, 1)
Dan Willemsen017d8932016-08-04 15:43:03 -0700651 futureReader := make(chan io.Reader, 1)
652 ze.futureReaders <- futureReader
653 close(ze.futureReaders)
654
Nan Zhangf281bd82017-04-25 16:47:45 -0700655 if ze.fh.Method == zip.Deflate {
656 compressed, err := z.compressBlock(r, nil, true)
657 if err != nil {
658 z.errors <- err
659 return
660 }
661 if uint64(compressed.Len()) < ze.fh.UncompressedSize64 {
662 futureReader <- compressed
Nan Zhangf281bd82017-04-25 16:47:45 -0700663 } else {
664 buf, err := readFile(r)
665 if err != nil {
666 z.errors <- err
667 return
668 }
669 ze.fh.Method = zip.Store
670 futureReader <- bytes.NewReader(buf)
Nan Zhangf281bd82017-04-25 16:47:45 -0700671 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700672 } else {
Nan Zhangf281bd82017-04-25 16:47:45 -0700673 buf, err := readFile(r)
Dan Willemsen017d8932016-08-04 15:43:03 -0700674 if err != nil {
675 z.errors <- err
676 return
677 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700678 ze.fh.Method = zip.Store
679 futureReader <- bytes.NewReader(buf)
Dan Willemsen017d8932016-08-04 15:43:03 -0700680 }
Nan Zhangf281bd82017-04-25 16:47:45 -0700681
Jeff Gaston175f34c2017-08-17 21:43:21 -0700682 z.cpuRateLimiter.Finish()
683
Dan Willemsen017d8932016-08-04 15:43:03 -0700684 close(futureReader)
685
686 compressChan <- ze
687 close(compressChan)
Colin Cross2fe66872015-03-30 17:20:39 -0700688}
Colin Crosse19c7932015-04-24 15:08:38 -0700689
Colin Crosse5580972017-08-30 17:40:21 -0700690// writeDirectory annotates that dir is a directory created for the src file or directory, and adds
691// the directory entry to the zip file if directories are enabled.
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700692func (z *ZipWriter) writeDirectory(dir string, src string, emulateJar bool) error {
Jeff Gaston2d174132017-08-15 18:05:56 -0700693 // clean the input
Colin Crosse5580972017-08-30 17:40:21 -0700694 dir = filepath.Clean(dir)
Jeff Gaston2d174132017-08-15 18:05:56 -0700695
696 // discover any uncreated directories in the path
697 zipDirs := []string{}
Colin Crosse5580972017-08-30 17:40:21 -0700698 for dir != "" && dir != "." {
699 if _, exists := z.createdDirs[dir]; exists {
700 break
701 }
Jeff Gaston2d174132017-08-15 18:05:56 -0700702
Colin Crosse5580972017-08-30 17:40:21 -0700703 if prev, exists := z.createdFiles[dir]; exists {
704 return fmt.Errorf("destination %q is both a directory %q and a file %q", dir, src, prev)
705 }
706
707 z.createdDirs[dir] = src
Jeff Gaston2d174132017-08-15 18:05:56 -0700708 // parent directories precede their children
Colin Crosse5580972017-08-30 17:40:21 -0700709 zipDirs = append([]string{dir}, zipDirs...)
Jeff Gaston2d174132017-08-15 18:05:56 -0700710
Colin Crosse5580972017-08-30 17:40:21 -0700711 dir = filepath.Dir(dir)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700712 }
713
Colin Crosse5580972017-08-30 17:40:21 -0700714 if z.directories {
715 // make a directory entry for each uncreated directory
716 for _, cleanDir := range zipDirs {
Colin Cross635acc92017-09-12 22:50:46 -0700717 var dirHeader *zip.FileHeader
Colin Crosse19c7932015-04-24 15:08:38 -0700718
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700719 if emulateJar && cleanDir+"/" == jar.MetaDir {
Colin Cross635acc92017-09-12 22:50:46 -0700720 dirHeader = jar.MetaDirFileHeader()
721 } else {
722 dirHeader = &zip.FileHeader{
723 Name: cleanDir + "/",
724 }
725 dirHeader.SetMode(0700 | os.ModeDir)
Colin Crosse5580972017-08-30 17:40:21 -0700726 }
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700727
Colin Cross635acc92017-09-12 22:50:46 -0700728 dirHeader.SetModTime(z.time)
729
Colin Crosse5580972017-08-30 17:40:21 -0700730 ze := make(chan *zipEntry, 1)
731 ze <- &zipEntry{
732 fh: dirHeader,
733 }
734 close(ze)
735 z.writeOps <- ze
Colin Crosse19c7932015-04-24 15:08:38 -0700736 }
Colin Crosse19c7932015-04-24 15:08:38 -0700737 }
738
739 return nil
740}
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700741
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700742func (z *ZipWriter) writeSymlink(rel, file string) error {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700743 fileHeader := &zip.FileHeader{
744 Name: rel,
745 }
746 fileHeader.SetModTime(z.time)
747 fileHeader.SetMode(0700 | os.ModeSymlink)
748
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700749 dest, err := os.Readlink(file)
750 if err != nil {
751 return err
752 }
753
Dan Willemsen017d8932016-08-04 15:43:03 -0700754 ze := make(chan *zipEntry, 1)
755 futureReaders := make(chan chan io.Reader, 1)
756 futureReader := make(chan io.Reader, 1)
757 futureReaders <- futureReader
758 close(futureReaders)
759 futureReader <- bytes.NewBufferString(dest)
760 close(futureReader)
761
Dan Willemsen017d8932016-08-04 15:43:03 -0700762 ze <- &zipEntry{
763 fh: fileHeader,
764 futureReaders: futureReaders,
765 }
766 close(ze)
767 z.writeOps <- ze
768
769 return nil
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700770}
Colin Cross7b10cf12017-08-30 14:12:21 -0700771
772func recursiveGlobFiles(path string) []string {
773 var files []string
774 filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
775 if !info.IsDir() {
776 files = append(files, path)
777 }
778 return nil
779 })
780
781 return files
782}