blob: ae176dff3d454375bed328a5bec12fa2c2f115aa [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
15package main
16
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 "flag"
22 "fmt"
Dan Willemsen017d8932016-08-04 15:43:03 -070023 "hash/crc32"
Colin Cross2fe66872015-03-30 17:20:39 -070024 "io"
25 "io/ioutil"
Nan Zhang9067b042017-03-17 14:04:43 -070026 "log"
Colin Cross2fe66872015-03-30 17:20:39 -070027 "os"
28 "path/filepath"
Dan Willemsen017d8932016-08-04 15:43:03 -070029 "runtime"
30 "runtime/pprof"
31 "runtime/trace"
Jeff Gastona2976952017-08-22 17:51:25 -070032 "sort"
Colin Cross2fe66872015-03-30 17:20:39 -070033 "strings"
Dan Willemsen017d8932016-08-04 15:43:03 -070034 "sync"
Colin Cross2fe66872015-03-30 17:20:39 -070035 "time"
Dan Willemsen017d8932016-08-04 15:43:03 -070036
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 {
61 bytes.Reader
62 io.Closer
63}
64
65// the file path in the zip at which a Java manifest file gets written
66const manifestDest = "META-INF/MANIFEST.MF"
67
Colin Cross2fe66872015-03-30 17:20:39 -070068type fileArg struct {
Nan Zhangf281bd82017-04-25 16:47:45 -070069 pathPrefixInZip, sourcePrefixToStrip string
70 sourceFiles []string
Colin Cross7b10cf12017-08-30 14:12:21 -070071 globDir string
Nan Zhang9067b042017-03-17 14:04:43 -070072}
73
74type pathMapping struct {
75 dest, src string
Nan Zhangf281bd82017-04-25 16:47:45 -070076 zipMethod uint16
77}
78
79type uniqueSet map[string]bool
80
81func (u *uniqueSet) String() string {
82 return `""`
83}
84
85func (u *uniqueSet) Set(s string) error {
86 if _, found := (*u)[s]; found {
87 return fmt.Errorf("File %q was specified twice as a file to not deflate", s)
88 } else {
89 (*u)[s] = true
90 }
91
92 return nil
Colin Cross2fe66872015-03-30 17:20:39 -070093}
94
95type fileArgs []fileArg
96
Nan Zhangf281bd82017-04-25 16:47:45 -070097type file struct{}
98
99type listFiles struct{}
100
Colin Cross7b10cf12017-08-30 14:12:21 -0700101type dir struct{}
102
Nan Zhangf281bd82017-04-25 16:47:45 -0700103func (f *file) String() string {
Colin Cross2fe66872015-03-30 17:20:39 -0700104 return `""`
105}
106
Nan Zhangf281bd82017-04-25 16:47:45 -0700107func (f *file) Set(s string) error {
Colin Cross2fe66872015-03-30 17:20:39 -0700108 if *relativeRoot == "" {
Colin Cross7b10cf12017-08-30 14:12:21 -0700109 return fmt.Errorf("must pass -C before -f")
Colin Cross2fe66872015-03-30 17:20:39 -0700110 }
111
Nan Zhangf281bd82017-04-25 16:47:45 -0700112 fArgs = append(fArgs, fileArg{
113 pathPrefixInZip: filepath.Clean(*rootPrefix),
114 sourcePrefixToStrip: filepath.Clean(*relativeRoot),
115 sourceFiles: []string{s},
116 })
117
Colin Cross2fe66872015-03-30 17:20:39 -0700118 return nil
119}
120
Nan Zhangf281bd82017-04-25 16:47:45 -0700121func (l *listFiles) String() string {
122 return `""`
123}
124
125func (l *listFiles) Set(s string) error {
126 if *relativeRoot == "" {
Colin Cross7b10cf12017-08-30 14:12:21 -0700127 return fmt.Errorf("must pass -C before -l")
Nan Zhangf281bd82017-04-25 16:47:45 -0700128 }
129
130 list, err := ioutil.ReadFile(s)
131 if err != nil {
132 return err
133 }
134
135 fArgs = append(fArgs, fileArg{
136 pathPrefixInZip: filepath.Clean(*rootPrefix),
137 sourcePrefixToStrip: filepath.Clean(*relativeRoot),
138 sourceFiles: strings.Split(string(list), "\n"),
139 })
140
141 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700142}
143
Colin Cross7b10cf12017-08-30 14:12:21 -0700144func (d *dir) String() string {
145 return `""`
146}
147
148func (d *dir) Set(s string) error {
149 if *relativeRoot == "" {
150 return fmt.Errorf("must pass -C before -D")
151 }
152
153 fArgs = append(fArgs, fileArg{
154 pathPrefixInZip: filepath.Clean(*rootPrefix),
155 sourcePrefixToStrip: filepath.Clean(*relativeRoot),
156 globDir: filepath.Clean(s),
157 })
158
159 return nil
160}
161
Colin Cross2fe66872015-03-30 17:20:39 -0700162var (
Dan Willemsen47ec28f2016-08-10 16:12:30 -0700163 out = flag.String("o", "", "file to write zip file to")
164 manifest = flag.String("m", "", "input jar manifest file name")
165 directories = flag.Bool("d", false, "include directories in zip")
Nan Zhang9067b042017-03-17 14:04:43 -0700166 rootPrefix = flag.String("P", "", "path prefix within the zip at which to place files")
Colin Cross7b10cf12017-08-30 14:12:21 -0700167 relativeRoot = flag.String("C", "", "path to use as relative root of files in following -f, -l, or -D arguments")
Dan Willemsen017d8932016-08-04 15:43:03 -0700168 parallelJobs = flag.Int("j", runtime.NumCPU(), "number of parallel threads to use")
169 compLevel = flag.Int("L", 5, "deflate compression level (0-9)")
Jeff Gastona2976952017-08-22 17:51:25 -0700170 emulateJar = flag.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'")
Nan Zhang9067b042017-03-17 14:04:43 -0700171
Nan Zhangf281bd82017-04-25 16:47:45 -0700172 fArgs fileArgs
173 nonDeflatedFiles = make(uniqueSet)
Dan Willemsen017d8932016-08-04 15:43:03 -0700174
175 cpuProfile = flag.String("cpuprofile", "", "write cpu profile to file")
176 traceFile = flag.String("trace", "", "write trace to file")
Colin Cross2fe66872015-03-30 17:20:39 -0700177)
178
179func init() {
Nan Zhangf281bd82017-04-25 16:47:45 -0700180 flag.Var(&listFiles{}, "l", "file containing list of .class files")
Colin Cross7b10cf12017-08-30 14:12:21 -0700181 flag.Var(&dir{}, "D", "directory to include in zip")
Nan Zhangf281bd82017-04-25 16:47:45 -0700182 flag.Var(&file{}, "f", "file to include in zip")
183 flag.Var(&nonDeflatedFiles, "s", "file path to be stored within the zip without compression")
Colin Cross2fe66872015-03-30 17:20:39 -0700184}
185
186func usage() {
Dan Willemsen47ec28f2016-08-10 16:12:30 -0700187 fmt.Fprintf(os.Stderr, "usage: soong_zip -o zipfile [-m manifest] -C dir [-f|-l file]...\n")
Colin Cross2fe66872015-03-30 17:20:39 -0700188 flag.PrintDefaults()
189 os.Exit(2)
190}
191
Colin Crosse19c7932015-04-24 15:08:38 -0700192type zipWriter struct {
Colin Crosse5580972017-08-30 17:40:21 -0700193 time time.Time
194 createdFiles map[string]string
195 createdDirs map[string]string
196 directories bool
Colin Crosse19c7932015-04-24 15:08:38 -0700197
Dan Willemsen017d8932016-08-04 15:43:03 -0700198 errors chan error
199 writeOps chan chan *zipEntry
200
Jeff Gaston175f34c2017-08-17 21:43:21 -0700201 cpuRateLimiter *CPURateLimiter
202 memoryRateLimiter *MemoryRateLimiter
Dan Willemsen017d8932016-08-04 15:43:03 -0700203
204 compressorPool sync.Pool
205 compLevel int
206}
207
208type zipEntry struct {
209 fh *zip.FileHeader
210
211 // List of delayed io.Reader
212 futureReaders chan chan io.Reader
Jeff Gaston175f34c2017-08-17 21:43:21 -0700213
214 // Only used for passing into the MemoryRateLimiter to ensure we
215 // release as much memory as much as we request
216 allocatedSize int64
Colin Cross2fe66872015-03-30 17:20:39 -0700217}
218
219func main() {
220 flag.Parse()
221
Dan Willemsen017d8932016-08-04 15:43:03 -0700222 if *cpuProfile != "" {
223 f, err := os.Create(*cpuProfile)
224 if err != nil {
225 fmt.Fprintln(os.Stderr, err.Error())
226 os.Exit(1)
227 }
228 defer f.Close()
229 pprof.StartCPUProfile(f)
230 defer pprof.StopCPUProfile()
231 }
232
233 if *traceFile != "" {
234 f, err := os.Create(*traceFile)
235 if err != nil {
236 fmt.Fprintln(os.Stderr, err.Error())
237 os.Exit(1)
238 }
239 defer f.Close()
240 err = trace.Start(f)
241 if err != nil {
242 fmt.Fprintln(os.Stderr, err.Error())
243 os.Exit(1)
244 }
245 defer trace.Stop()
246 }
247
Colin Cross2fe66872015-03-30 17:20:39 -0700248 if *out == "" {
249 fmt.Fprintf(os.Stderr, "error: -o is required\n")
250 usage()
251 }
252
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700253 if *emulateJar {
254 *directories = true
255 }
256
Colin Crosse19c7932015-04-24 15:08:38 -0700257 w := &zipWriter{
Colin Crosse5580972017-08-30 17:40:21 -0700258 time: time.Date(2009, 1, 1, 0, 0, 0, 0, time.UTC),
259 createdDirs: make(map[string]string),
260 createdFiles: make(map[string]string),
261 directories: *directories,
262 compLevel: *compLevel,
Colin Cross2fe66872015-03-30 17:20:39 -0700263 }
264
Nan Zhang9067b042017-03-17 14:04:43 -0700265 pathMappings := []pathMapping{}
Nan Zhang9067b042017-03-17 14:04:43 -0700266
Nan Zhangf281bd82017-04-25 16:47:45 -0700267 for _, fa := range fArgs {
Colin Cross7b10cf12017-08-30 14:12:21 -0700268 srcs := fa.sourceFiles
269 if fa.globDir != "" {
270 srcs = append(srcs, recursiveGlobFiles(fa.globDir)...)
271 }
272 for _, src := range srcs {
Nan Zhangf281bd82017-04-25 16:47:45 -0700273 if err := fillPathPairs(fa.pathPrefixInZip,
Colin Crosse5580972017-08-30 17:40:21 -0700274 fa.sourcePrefixToStrip, src, &pathMappings); err != nil {
Nan Zhang9067b042017-03-17 14:04:43 -0700275 log.Fatal(err)
276 }
277 }
278 }
279
Nan Zhang9067b042017-03-17 14:04:43 -0700280 err := w.write(*out, pathMappings, *manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700281 if err != nil {
282 fmt.Fprintln(os.Stderr, err.Error())
283 os.Exit(1)
284 }
285}
286
Colin Crosse5580972017-08-30 17:40:21 -0700287func fillPathPairs(prefix, rel, src string, pathMappings *[]pathMapping) error {
Nan Zhang9067b042017-03-17 14:04:43 -0700288 src = strings.TrimSpace(src)
289 if src == "" {
290 return nil
291 }
292 src = filepath.Clean(src)
293 dest, err := filepath.Rel(rel, src)
294 if err != nil {
295 return err
296 }
297 dest = filepath.Join(prefix, dest)
298
Nan Zhangf281bd82017-04-25 16:47:45 -0700299 zipMethod := zip.Deflate
300 if _, found := nonDeflatedFiles[dest]; found {
301 zipMethod = zip.Store
302 }
303 *pathMappings = append(*pathMappings,
304 pathMapping{dest: dest, src: src, zipMethod: zipMethod})
Nan Zhang9067b042017-03-17 14:04:43 -0700305
306 return nil
307}
308
Jeff Gastona2976952017-08-22 17:51:25 -0700309func jarSort(mappings []pathMapping) {
310 less := func(i int, j int) (smaller bool) {
311 return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest)
312 }
313 sort.SliceStable(mappings, less)
314}
315
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700316type readerSeekerCloser interface {
317 io.Reader
318 io.ReaderAt
319 io.Closer
320 io.Seeker
321}
322
Nan Zhang9067b042017-03-17 14:04:43 -0700323func (z *zipWriter) write(out string, pathMappings []pathMapping, manifest string) error {
Colin Cross2fe66872015-03-30 17:20:39 -0700324 f, err := os.Create(out)
325 if err != nil {
326 return err
327 }
328
329 defer f.Close()
330 defer func() {
331 if err != nil {
332 os.Remove(out)
333 }
334 }()
335
Dan Willemsen017d8932016-08-04 15:43:03 -0700336 z.errors = make(chan error)
337 defer close(z.errors)
Colin Cross2fe66872015-03-30 17:20:39 -0700338
Dan Willemsen017d8932016-08-04 15:43:03 -0700339 // This channel size can be essentially unlimited -- it's used as a fifo
340 // queue decouple the CPU and IO loads. Directories don't require any
341 // compression time, but still cost some IO. Similar with small files that
342 // can be very fast to compress. Some files that are more difficult to
343 // compress won't take a corresponding longer time writing out.
344 //
345 // The optimum size here depends on your CPU and IO characteristics, and
346 // the the layout of your zip file. 1000 was chosen mostly at random as
347 // something that worked reasonably well for a test file.
348 //
349 // The RateLimit object will put the upper bounds on the number of
350 // parallel compressions and outstanding buffers.
351 z.writeOps = make(chan chan *zipEntry, 1000)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700352 z.cpuRateLimiter = NewCPURateLimiter(int64(*parallelJobs))
353 z.memoryRateLimiter = NewMemoryRateLimiter(0)
354 defer func() {
355 z.cpuRateLimiter.Stop()
356 z.memoryRateLimiter.Stop()
357 }()
Jeff Gastona2976952017-08-22 17:51:25 -0700358
359 if manifest != "" {
360 if !*emulateJar {
361 return errors.New("must specify --jar when specifying a manifest via -m")
362 }
Jeff Gastoncef50b92017-08-23 15:41:35 -0700363 pathMappings = append(pathMappings, pathMapping{manifestDest, manifest, zip.Deflate})
Jeff Gastona2976952017-08-22 17:51:25 -0700364 }
365
366 if *emulateJar {
367 jarSort(pathMappings)
368 }
369
Dan Willemsen017d8932016-08-04 15:43:03 -0700370 go func() {
371 var err error
372 defer close(z.writeOps)
373
Nan Zhang9067b042017-03-17 14:04:43 -0700374 for _, ele := range pathMappings {
Jeff Gastoncef50b92017-08-23 15:41:35 -0700375 if *emulateJar && ele.dest == manifestDest {
376 err = z.addManifest(ele.dest, ele.src, ele.zipMethod)
377 } else {
378 err = z.addFile(ele.dest, ele.src, ele.zipMethod)
379 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700380 if err != nil {
381 z.errors <- err
382 return
383 }
384 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700385 }()
386
387 zipw := zip.NewWriter(f)
388
389 var currentWriteOpChan chan *zipEntry
390 var currentWriter io.WriteCloser
391 var currentReaders chan chan io.Reader
392 var currentReader chan io.Reader
393 var done bool
394
395 for !done {
396 var writeOpsChan chan chan *zipEntry
397 var writeOpChan chan *zipEntry
398 var readersChan chan chan io.Reader
399
400 if currentReader != nil {
401 // Only read and process errors
402 } else if currentReaders != nil {
403 readersChan = currentReaders
404 } else if currentWriteOpChan != nil {
405 writeOpChan = currentWriteOpChan
406 } else {
407 writeOpsChan = z.writeOps
408 }
409
410 select {
411 case writeOp, ok := <-writeOpsChan:
412 if !ok {
413 done = true
414 }
415
416 currentWriteOpChan = writeOp
417
418 case op := <-writeOpChan:
419 currentWriteOpChan = nil
420
421 if op.fh.Method == zip.Deflate {
422 currentWriter, err = zipw.CreateCompressedHeader(op.fh)
423 } else {
424 var zw io.Writer
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700425
426 op.fh.CompressedSize64 = op.fh.UncompressedSize64
427
428 zw, err = zipw.CreateHeaderAndroid(op.fh)
Dan Willemsen017d8932016-08-04 15:43:03 -0700429 currentWriter = nopCloser{zw}
430 }
431 if err != nil {
432 return err
433 }
434
435 currentReaders = op.futureReaders
436 if op.futureReaders == nil {
437 currentWriter.Close()
438 currentWriter = nil
439 }
Jeff Gaston175f34c2017-08-17 21:43:21 -0700440 z.memoryRateLimiter.Finish(op.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700441
442 case futureReader, ok := <-readersChan:
443 if !ok {
444 // Done with reading
445 currentWriter.Close()
446 currentWriter = nil
447 currentReaders = nil
448 }
449
450 currentReader = futureReader
451
452 case reader := <-currentReader:
Jeff Gaston175f34c2017-08-17 21:43:21 -0700453 _, err = io.Copy(currentWriter, reader)
Dan Willemsen017d8932016-08-04 15:43:03 -0700454 if err != nil {
455 return err
456 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700457
458 currentReader = nil
459
460 case err = <-z.errors:
Colin Cross2fe66872015-03-30 17:20:39 -0700461 return err
462 }
463 }
464
Dan Willemsen017d8932016-08-04 15:43:03 -0700465 // One last chance to catch an error
466 select {
467 case err = <-z.errors:
468 return err
469 default:
470 zipw.Close()
471 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700472 }
Colin Cross2fe66872015-03-30 17:20:39 -0700473}
474
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700475// imports (possibly with compression) <src> into the zip at sub-path <dest>
Jeff Gastoncef50b92017-08-23 15:41:35 -0700476func (z *zipWriter) addFile(dest, src string, method uint16) error {
Dan Willemsen017d8932016-08-04 15:43:03 -0700477 var fileSize int64
Dan Willemsen10462b32017-03-15 19:02:51 -0700478 var executable bool
Dan Willemsen017d8932016-08-04 15:43:03 -0700479
Nan Zhang9067b042017-03-17 14:04:43 -0700480 if s, err := os.Lstat(src); err != nil {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700481 return err
482 } else if s.IsDir() {
Colin Cross957cc4e2015-04-24 15:10:32 -0700483 if z.directories {
Colin Crosse5580972017-08-30 17:40:21 -0700484 return z.writeDirectory(dest, src)
Colin Cross957cc4e2015-04-24 15:10:32 -0700485 }
486 return nil
Dan Willemsen017d8932016-08-04 15:43:03 -0700487 } else {
Colin Crosse5580972017-08-30 17:40:21 -0700488 if err := z.writeDirectory(filepath.Dir(dest), src); err != nil {
489 return err
490 }
491
492 if prev, exists := z.createdDirs[dest]; exists {
493 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
494 }
495 if prev, exists := z.createdFiles[dest]; exists {
496 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
497 }
498
499 z.createdFiles[dest] = src
500
501 if s.Mode()&os.ModeSymlink != 0 {
502 return z.writeSymlink(dest, src)
503 } else if !s.Mode().IsRegular() {
504 return fmt.Errorf("%s is not a file, directory, or symlink", src)
505 }
506
Dan Willemsen017d8932016-08-04 15:43:03 -0700507 fileSize = s.Size()
Dan Willemsen10462b32017-03-15 19:02:51 -0700508 executable = s.Mode()&0100 != 0
Colin Cross957cc4e2015-04-24 15:10:32 -0700509 }
510
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700511 r, err := os.Open(src)
512 if err != nil {
513 return err
514 }
515
516 header := &zip.FileHeader{
517 Name: dest,
518 Method: method,
519 UncompressedSize64: uint64(fileSize),
520 }
521
522 if executable {
523 header.SetMode(0700)
524 }
525
526 return z.writeFileContents(header, r)
527}
528
Jeff Gastoncef50b92017-08-23 15:41:35 -0700529func (z *zipWriter) addManifest(dest string, src string, method uint16) error {
530 givenBytes, err := ioutil.ReadFile(src)
531 if err != nil {
532 return err
533 }
534
Colin Crosse5580972017-08-30 17:40:21 -0700535 if prev, exists := z.createdDirs[dest]; exists {
536 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
537 }
538 if prev, exists := z.createdFiles[dest]; exists {
539 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
540 }
541
Jeff Gastoncef50b92017-08-23 15:41:35 -0700542 manifestMarker := []byte("Manifest-Version:")
543 header := append(manifestMarker, []byte(" 1.0\nCreated-By: soong_zip\n")...)
544
545 var finalBytes []byte
546 if !bytes.Contains(givenBytes, manifestMarker) {
547 finalBytes = append(append(header, givenBytes...), byte('\n'))
548 } else {
549 finalBytes = givenBytes
550 }
551
552 byteReader := bytes.NewReader(finalBytes)
553
554 reader := &byteReaderCloser{*byteReader, ioutil.NopCloser(nil)}
555
556 fileHeader := &zip.FileHeader{
557 Name: dest,
558 Method: zip.Store,
559 UncompressedSize64: uint64(byteReader.Len()),
560 }
561
562 return z.writeFileContents(fileHeader, reader)
563}
564
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700565func (z *zipWriter) writeFileContents(header *zip.FileHeader, r readerSeekerCloser) (err error) {
566
567 header.SetModTime(z.time)
568
Dan Willemsen017d8932016-08-04 15:43:03 -0700569 compressChan := make(chan *zipEntry, 1)
570 z.writeOps <- compressChan
571
572 // Pre-fill a zipEntry, it will be sent in the compressChan once
573 // we're sure about the Method and CRC.
574 ze := &zipEntry{
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700575 fh: header,
Dan Willemsen10462b32017-03-15 19:02:51 -0700576 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700577
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700578 ze.allocatedSize = int64(header.UncompressedSize64)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700579 z.cpuRateLimiter.Request()
580 z.memoryRateLimiter.Request(ze.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700581
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700582 fileSize := int64(header.UncompressedSize64)
583 if fileSize == 0 {
584 fileSize = int64(header.UncompressedSize)
585 }
586
587 if header.Method == zip.Deflate && fileSize >= minParallelFileSize {
Dan Willemsen017d8932016-08-04 15:43:03 -0700588 wg := new(sync.WaitGroup)
589
590 // Allocate enough buffer to hold all readers. We'll limit
591 // this based on actual buffer sizes in RateLimit.
592 ze.futureReaders = make(chan chan io.Reader, (fileSize/parallelBlockSize)+1)
593
594 // Calculate the CRC in the background, since reading the entire
595 // file could take a while.
596 //
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700597 // We could split this up into chunks as well, but it's faster
Dan Willemsen017d8932016-08-04 15:43:03 -0700598 // than the compression. Due to the Go Zip API, we also need to
599 // know the result before we can begin writing the compressed
600 // data out to the zipfile.
601 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700602 go z.crcFile(r, ze, compressChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700603
604 for start := int64(0); start < fileSize; start += parallelBlockSize {
605 sr := io.NewSectionReader(r, start, parallelBlockSize)
606 resultChan := make(chan io.Reader, 1)
607 ze.futureReaders <- resultChan
608
Jeff Gaston175f34c2017-08-17 21:43:21 -0700609 z.cpuRateLimiter.Request()
Dan Willemsen017d8932016-08-04 15:43:03 -0700610
611 last := !(start+parallelBlockSize < fileSize)
612 var dict []byte
613 if start >= windowSize {
614 dict, err = ioutil.ReadAll(io.NewSectionReader(r, start-windowSize, windowSize))
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700615 if err != nil {
616 return err
617 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700618 }
619
620 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700621 go z.compressPartialFile(sr, dict, last, resultChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700622 }
623
624 close(ze.futureReaders)
625
626 // Close the file handle after all readers are done
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700627 go func(wg *sync.WaitGroup, closer io.Closer) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700628 wg.Wait()
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700629 closer.Close()
Dan Willemsen017d8932016-08-04 15:43:03 -0700630 }(wg, r)
631 } else {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700632 go func() {
633 z.compressWholeFile(ze, r, compressChan)
634 r.Close()
635 }()
Dan Willemsen017d8932016-08-04 15:43:03 -0700636 }
637
638 return nil
639}
640
Jeff Gaston175f34c2017-08-17 21:43:21 -0700641func (z *zipWriter) crcFile(r io.Reader, ze *zipEntry, resultChan chan *zipEntry, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700642 defer wg.Done()
Jeff Gaston175f34c2017-08-17 21:43:21 -0700643 defer z.cpuRateLimiter.Finish()
Dan Willemsen017d8932016-08-04 15:43:03 -0700644
645 crc := crc32.NewIEEE()
646 _, err := io.Copy(crc, r)
647 if err != nil {
648 z.errors <- err
649 return
650 }
651
652 ze.fh.CRC32 = crc.Sum32()
653 resultChan <- ze
654 close(resultChan)
655}
656
Jeff Gaston175f34c2017-08-17 21:43:21 -0700657func (z *zipWriter) compressPartialFile(r io.Reader, dict []byte, last bool, resultChan chan io.Reader, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700658 defer wg.Done()
659
660 result, err := z.compressBlock(r, dict, last)
661 if err != nil {
662 z.errors <- err
663 return
664 }
665
Jeff Gaston175f34c2017-08-17 21:43:21 -0700666 z.cpuRateLimiter.Finish()
667
Dan Willemsen017d8932016-08-04 15:43:03 -0700668 resultChan <- result
669}
670
671func (z *zipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) {
672 buf := new(bytes.Buffer)
673 var fw *flate.Writer
674 var err error
675 if len(dict) > 0 {
676 // There's no way to Reset a Writer with a new dictionary, so
677 // don't use the Pool
678 fw, err = flate.NewWriterDict(buf, z.compLevel, dict)
679 } else {
680 var ok bool
681 if fw, ok = z.compressorPool.Get().(*flate.Writer); ok {
682 fw.Reset(buf)
683 } else {
684 fw, err = flate.NewWriter(buf, z.compLevel)
685 }
686 defer z.compressorPool.Put(fw)
687 }
688 if err != nil {
689 return nil, err
690 }
691
692 _, err = io.Copy(fw, r)
693 if err != nil {
694 return nil, err
695 }
696 if last {
697 fw.Close()
698 } else {
699 fw.Flush()
700 }
701
702 return buf, nil
703}
704
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700705func (z *zipWriter) compressWholeFile(ze *zipEntry, r io.ReadSeeker, compressChan chan *zipEntry) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700706
Dan Willemsen017d8932016-08-04 15:43:03 -0700707 crc := crc32.NewIEEE()
Dan Willemsena8b55022017-03-15 21:49:26 -0700708 _, err := io.Copy(crc, r)
Colin Cross2fe66872015-03-30 17:20:39 -0700709 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700710 z.errors <- err
711 return
Colin Cross2fe66872015-03-30 17:20:39 -0700712 }
713
Dan Willemsena8b55022017-03-15 21:49:26 -0700714 ze.fh.CRC32 = crc.Sum32()
Colin Cross2fe66872015-03-30 17:20:39 -0700715
Dan Willemsen017d8932016-08-04 15:43:03 -0700716 _, err = r.Seek(0, 0)
Colin Cross2fe66872015-03-30 17:20:39 -0700717 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700718 z.errors <- err
719 return
Colin Cross2fe66872015-03-30 17:20:39 -0700720 }
721
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700722 readFile := func(reader io.ReadSeeker) ([]byte, error) {
723 _, err := reader.Seek(0, 0)
Nan Zhangf281bd82017-04-25 16:47:45 -0700724 if err != nil {
725 return nil, err
726 }
727
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700728 buf, err := ioutil.ReadAll(reader)
Nan Zhangf281bd82017-04-25 16:47:45 -0700729 if err != nil {
730 return nil, err
731 }
732
733 return buf, nil
734 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700735
Dan Willemsena8b55022017-03-15 21:49:26 -0700736 ze.futureReaders = make(chan chan io.Reader, 1)
Dan Willemsen017d8932016-08-04 15:43:03 -0700737 futureReader := make(chan io.Reader, 1)
738 ze.futureReaders <- futureReader
739 close(ze.futureReaders)
740
Nan Zhangf281bd82017-04-25 16:47:45 -0700741 if ze.fh.Method == zip.Deflate {
742 compressed, err := z.compressBlock(r, nil, true)
743 if err != nil {
744 z.errors <- err
745 return
746 }
747 if uint64(compressed.Len()) < ze.fh.UncompressedSize64 {
748 futureReader <- compressed
Nan Zhangf281bd82017-04-25 16:47:45 -0700749 } else {
750 buf, err := readFile(r)
751 if err != nil {
752 z.errors <- err
753 return
754 }
755 ze.fh.Method = zip.Store
756 futureReader <- bytes.NewReader(buf)
Nan Zhangf281bd82017-04-25 16:47:45 -0700757 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700758 } else {
Nan Zhangf281bd82017-04-25 16:47:45 -0700759 buf, err := readFile(r)
Dan Willemsen017d8932016-08-04 15:43:03 -0700760 if err != nil {
761 z.errors <- err
762 return
763 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700764 ze.fh.Method = zip.Store
765 futureReader <- bytes.NewReader(buf)
Dan Willemsen017d8932016-08-04 15:43:03 -0700766 }
Nan Zhangf281bd82017-04-25 16:47:45 -0700767
Jeff Gaston175f34c2017-08-17 21:43:21 -0700768 z.cpuRateLimiter.Finish()
769
Dan Willemsen017d8932016-08-04 15:43:03 -0700770 close(futureReader)
771
772 compressChan <- ze
773 close(compressChan)
Colin Cross2fe66872015-03-30 17:20:39 -0700774}
Colin Crosse19c7932015-04-24 15:08:38 -0700775
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700776func (z *zipWriter) addExtraField(zipHeader *zip.FileHeader, fieldHeader [2]byte, data []byte) {
777 // add the field header in little-endian order
778 zipHeader.Extra = append(zipHeader.Extra, fieldHeader[1], fieldHeader[0])
779
780 // specify the length of the data (in little-endian order)
781 dataLength := len(data)
782 lengthBytes := []byte{byte(dataLength % 256), byte(dataLength / 256)}
783 zipHeader.Extra = append(zipHeader.Extra, lengthBytes...)
784
785 // add the contents of the extra field
786 zipHeader.Extra = append(zipHeader.Extra, data...)
787}
788
Colin Crosse5580972017-08-30 17:40:21 -0700789// writeDirectory annotates that dir is a directory created for the src file or directory, and adds
790// the directory entry to the zip file if directories are enabled.
791func (z *zipWriter) writeDirectory(dir, src string) error {
Jeff Gaston2d174132017-08-15 18:05:56 -0700792 // clean the input
Colin Crosse5580972017-08-30 17:40:21 -0700793 dir = filepath.Clean(dir)
Jeff Gaston2d174132017-08-15 18:05:56 -0700794
795 // discover any uncreated directories in the path
796 zipDirs := []string{}
Colin Crosse5580972017-08-30 17:40:21 -0700797 for dir != "" && dir != "." {
798 if _, exists := z.createdDirs[dir]; exists {
799 break
800 }
Jeff Gaston2d174132017-08-15 18:05:56 -0700801
Colin Crosse5580972017-08-30 17:40:21 -0700802 if prev, exists := z.createdFiles[dir]; exists {
803 return fmt.Errorf("destination %q is both a directory %q and a file %q", dir, src, prev)
804 }
805
806 z.createdDirs[dir] = src
Jeff Gaston2d174132017-08-15 18:05:56 -0700807 // parent directories precede their children
Colin Crosse5580972017-08-30 17:40:21 -0700808 zipDirs = append([]string{dir}, zipDirs...)
Jeff Gaston2d174132017-08-15 18:05:56 -0700809
Colin Crosse5580972017-08-30 17:40:21 -0700810 dir = filepath.Dir(dir)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700811 }
812
Colin Crosse5580972017-08-30 17:40:21 -0700813 if z.directories {
814 // make a directory entry for each uncreated directory
815 for _, cleanDir := range zipDirs {
816 dirHeader := &zip.FileHeader{
817 Name: cleanDir + "/",
818 }
819 dirHeader.SetMode(0700 | os.ModeDir)
820 dirHeader.SetModTime(z.time)
Colin Crosse19c7932015-04-24 15:08:38 -0700821
Colin Crosse5580972017-08-30 17:40:21 -0700822 if *emulateJar && dir == "META-INF/" {
823 // Jar files have a 0-length extra field with header "CAFE"
824 z.addExtraField(dirHeader, [2]byte{0xca, 0xfe}, []byte{})
825 }
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700826
Colin Crosse5580972017-08-30 17:40:21 -0700827 ze := make(chan *zipEntry, 1)
828 ze <- &zipEntry{
829 fh: dirHeader,
830 }
831 close(ze)
832 z.writeOps <- ze
Colin Crosse19c7932015-04-24 15:08:38 -0700833 }
Colin Crosse19c7932015-04-24 15:08:38 -0700834 }
835
836 return nil
837}
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700838
839func (z *zipWriter) writeSymlink(rel, file string) error {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700840 fileHeader := &zip.FileHeader{
841 Name: rel,
842 }
843 fileHeader.SetModTime(z.time)
844 fileHeader.SetMode(0700 | os.ModeSymlink)
845
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700846 dest, err := os.Readlink(file)
847 if err != nil {
848 return err
849 }
850
Dan Willemsen017d8932016-08-04 15:43:03 -0700851 ze := make(chan *zipEntry, 1)
852 futureReaders := make(chan chan io.Reader, 1)
853 futureReader := make(chan io.Reader, 1)
854 futureReaders <- futureReader
855 close(futureReaders)
856 futureReader <- bytes.NewBufferString(dest)
857 close(futureReader)
858
Dan Willemsen017d8932016-08-04 15:43:03 -0700859 ze <- &zipEntry{
860 fh: fileHeader,
861 futureReaders: futureReaders,
862 }
863 close(ze)
864 z.writeOps <- ze
865
866 return nil
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700867}
Colin Cross7b10cf12017-08-30 14:12:21 -0700868
869func recursiveGlobFiles(path string) []string {
870 var files []string
871 filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
872 if !info.IsDir() {
873 files = append(files, path)
874 }
875 return nil
876 })
877
878 return files
879}