blob: 78a77b83160738a08793b315861ac3fcb3a8dd54 [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 Cross2fe66872015-03-30 17:20:39 -0700193 time time.Time
194 createdDirs map[string]bool
195 directories bool
Colin Crosse19c7932015-04-24 15:08:38 -0700196
Dan Willemsen017d8932016-08-04 15:43:03 -0700197 errors chan error
198 writeOps chan chan *zipEntry
199
Jeff Gaston175f34c2017-08-17 21:43:21 -0700200 cpuRateLimiter *CPURateLimiter
201 memoryRateLimiter *MemoryRateLimiter
Dan Willemsen017d8932016-08-04 15:43:03 -0700202
203 compressorPool sync.Pool
204 compLevel int
205}
206
207type zipEntry struct {
208 fh *zip.FileHeader
209
210 // List of delayed io.Reader
211 futureReaders chan chan io.Reader
Jeff Gaston175f34c2017-08-17 21:43:21 -0700212
213 // Only used for passing into the MemoryRateLimiter to ensure we
214 // release as much memory as much as we request
215 allocatedSize int64
Colin Cross2fe66872015-03-30 17:20:39 -0700216}
217
218func main() {
219 flag.Parse()
220
Dan Willemsen017d8932016-08-04 15:43:03 -0700221 if *cpuProfile != "" {
222 f, err := os.Create(*cpuProfile)
223 if err != nil {
224 fmt.Fprintln(os.Stderr, err.Error())
225 os.Exit(1)
226 }
227 defer f.Close()
228 pprof.StartCPUProfile(f)
229 defer pprof.StopCPUProfile()
230 }
231
232 if *traceFile != "" {
233 f, err := os.Create(*traceFile)
234 if err != nil {
235 fmt.Fprintln(os.Stderr, err.Error())
236 os.Exit(1)
237 }
238 defer f.Close()
239 err = trace.Start(f)
240 if err != nil {
241 fmt.Fprintln(os.Stderr, err.Error())
242 os.Exit(1)
243 }
244 defer trace.Stop()
245 }
246
Colin Cross2fe66872015-03-30 17:20:39 -0700247 if *out == "" {
248 fmt.Fprintf(os.Stderr, "error: -o is required\n")
249 usage()
250 }
251
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700252 if *emulateJar {
253 *directories = true
254 }
255
Colin Crosse19c7932015-04-24 15:08:38 -0700256 w := &zipWriter{
Dan Willemsen77a6b862016-08-04 20:38:47 -0700257 time: time.Date(2009, 1, 1, 0, 0, 0, 0, time.UTC),
Colin Cross2fe66872015-03-30 17:20:39 -0700258 createdDirs: make(map[string]bool),
259 directories: *directories,
Dan Willemsen017d8932016-08-04 15:43:03 -0700260 compLevel: *compLevel,
Colin Cross2fe66872015-03-30 17:20:39 -0700261 }
262
Nan Zhang9067b042017-03-17 14:04:43 -0700263 pathMappings := []pathMapping{}
264 set := make(map[string]string)
265
Nan Zhangf281bd82017-04-25 16:47:45 -0700266 for _, fa := range fArgs {
Colin Cross7b10cf12017-08-30 14:12:21 -0700267 srcs := fa.sourceFiles
268 if fa.globDir != "" {
269 srcs = append(srcs, recursiveGlobFiles(fa.globDir)...)
270 }
271 for _, src := range srcs {
Nan Zhangf281bd82017-04-25 16:47:45 -0700272 if err := fillPathPairs(fa.pathPrefixInZip,
273 fa.sourcePrefixToStrip, src, set, &pathMappings); err != nil {
Nan Zhang9067b042017-03-17 14:04:43 -0700274 log.Fatal(err)
275 }
276 }
277 }
278
Nan Zhang9067b042017-03-17 14:04:43 -0700279 err := w.write(*out, pathMappings, *manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700280 if err != nil {
281 fmt.Fprintln(os.Stderr, err.Error())
282 os.Exit(1)
283 }
284}
285
Nan Zhang9067b042017-03-17 14:04:43 -0700286func fillPathPairs(prefix, rel, src string, set map[string]string, pathMappings *[]pathMapping) error {
287 src = strings.TrimSpace(src)
288 if src == "" {
289 return nil
290 }
291 src = filepath.Clean(src)
292 dest, err := filepath.Rel(rel, src)
293 if err != nil {
294 return err
295 }
296 dest = filepath.Join(prefix, dest)
297
298 if _, found := set[dest]; found {
299 return fmt.Errorf("found two file paths to be copied into dest path: %q,"+
300 " both [%q]%q and [%q]%q!",
301 dest, dest, src, dest, set[dest])
302 } else {
303 set[dest] = src
304 }
305
Nan Zhangf281bd82017-04-25 16:47:45 -0700306 zipMethod := zip.Deflate
307 if _, found := nonDeflatedFiles[dest]; found {
308 zipMethod = zip.Store
309 }
310 *pathMappings = append(*pathMappings,
311 pathMapping{dest: dest, src: src, zipMethod: zipMethod})
Nan Zhang9067b042017-03-17 14:04:43 -0700312
313 return nil
314}
315
Jeff Gastona2976952017-08-22 17:51:25 -0700316func jarSort(mappings []pathMapping) {
317 less := func(i int, j int) (smaller bool) {
318 return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest)
319 }
320 sort.SliceStable(mappings, less)
321}
322
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700323type readerSeekerCloser interface {
324 io.Reader
325 io.ReaderAt
326 io.Closer
327 io.Seeker
328}
329
Nan Zhang9067b042017-03-17 14:04:43 -0700330func (z *zipWriter) write(out string, pathMappings []pathMapping, manifest string) error {
Colin Cross2fe66872015-03-30 17:20:39 -0700331 f, err := os.Create(out)
332 if err != nil {
333 return err
334 }
335
336 defer f.Close()
337 defer func() {
338 if err != nil {
339 os.Remove(out)
340 }
341 }()
342
Dan Willemsen017d8932016-08-04 15:43:03 -0700343 z.errors = make(chan error)
344 defer close(z.errors)
Colin Cross2fe66872015-03-30 17:20:39 -0700345
Dan Willemsen017d8932016-08-04 15:43:03 -0700346 // This channel size can be essentially unlimited -- it's used as a fifo
347 // queue decouple the CPU and IO loads. Directories don't require any
348 // compression time, but still cost some IO. Similar with small files that
349 // can be very fast to compress. Some files that are more difficult to
350 // compress won't take a corresponding longer time writing out.
351 //
352 // The optimum size here depends on your CPU and IO characteristics, and
353 // the the layout of your zip file. 1000 was chosen mostly at random as
354 // something that worked reasonably well for a test file.
355 //
356 // The RateLimit object will put the upper bounds on the number of
357 // parallel compressions and outstanding buffers.
358 z.writeOps = make(chan chan *zipEntry, 1000)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700359 z.cpuRateLimiter = NewCPURateLimiter(int64(*parallelJobs))
360 z.memoryRateLimiter = NewMemoryRateLimiter(0)
361 defer func() {
362 z.cpuRateLimiter.Stop()
363 z.memoryRateLimiter.Stop()
364 }()
Jeff Gastona2976952017-08-22 17:51:25 -0700365
366 if manifest != "" {
367 if !*emulateJar {
368 return errors.New("must specify --jar when specifying a manifest via -m")
369 }
Jeff Gastoncef50b92017-08-23 15:41:35 -0700370 pathMappings = append(pathMappings, pathMapping{manifestDest, manifest, zip.Deflate})
Jeff Gastona2976952017-08-22 17:51:25 -0700371 }
372
373 if *emulateJar {
374 jarSort(pathMappings)
375 }
376
Dan Willemsen017d8932016-08-04 15:43:03 -0700377 go func() {
378 var err error
379 defer close(z.writeOps)
380
Nan Zhang9067b042017-03-17 14:04:43 -0700381 for _, ele := range pathMappings {
Jeff Gastoncef50b92017-08-23 15:41:35 -0700382 if *emulateJar && ele.dest == manifestDest {
383 err = z.addManifest(ele.dest, ele.src, ele.zipMethod)
384 } else {
385 err = z.addFile(ele.dest, ele.src, ele.zipMethod)
386 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700387 if err != nil {
388 z.errors <- err
389 return
390 }
391 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700392 }()
393
394 zipw := zip.NewWriter(f)
395
396 var currentWriteOpChan chan *zipEntry
397 var currentWriter io.WriteCloser
398 var currentReaders chan chan io.Reader
399 var currentReader chan io.Reader
400 var done bool
401
402 for !done {
403 var writeOpsChan chan chan *zipEntry
404 var writeOpChan chan *zipEntry
405 var readersChan chan chan io.Reader
406
407 if currentReader != nil {
408 // Only read and process errors
409 } else if currentReaders != nil {
410 readersChan = currentReaders
411 } else if currentWriteOpChan != nil {
412 writeOpChan = currentWriteOpChan
413 } else {
414 writeOpsChan = z.writeOps
415 }
416
417 select {
418 case writeOp, ok := <-writeOpsChan:
419 if !ok {
420 done = true
421 }
422
423 currentWriteOpChan = writeOp
424
425 case op := <-writeOpChan:
426 currentWriteOpChan = nil
427
428 if op.fh.Method == zip.Deflate {
429 currentWriter, err = zipw.CreateCompressedHeader(op.fh)
430 } else {
431 var zw io.Writer
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700432
433 op.fh.CompressedSize64 = op.fh.UncompressedSize64
434
435 zw, err = zipw.CreateHeaderAndroid(op.fh)
Dan Willemsen017d8932016-08-04 15:43:03 -0700436 currentWriter = nopCloser{zw}
437 }
438 if err != nil {
439 return err
440 }
441
442 currentReaders = op.futureReaders
443 if op.futureReaders == nil {
444 currentWriter.Close()
445 currentWriter = nil
446 }
Jeff Gaston175f34c2017-08-17 21:43:21 -0700447 z.memoryRateLimiter.Finish(op.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700448
449 case futureReader, ok := <-readersChan:
450 if !ok {
451 // Done with reading
452 currentWriter.Close()
453 currentWriter = nil
454 currentReaders = nil
455 }
456
457 currentReader = futureReader
458
459 case reader := <-currentReader:
Jeff Gaston175f34c2017-08-17 21:43:21 -0700460 _, err = io.Copy(currentWriter, reader)
Dan Willemsen017d8932016-08-04 15:43:03 -0700461 if err != nil {
462 return err
463 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700464
465 currentReader = nil
466
467 case err = <-z.errors:
Colin Cross2fe66872015-03-30 17:20:39 -0700468 return err
469 }
470 }
471
Dan Willemsen017d8932016-08-04 15:43:03 -0700472 // One last chance to catch an error
473 select {
474 case err = <-z.errors:
475 return err
476 default:
477 zipw.Close()
478 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700479 }
Colin Cross2fe66872015-03-30 17:20:39 -0700480}
481
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700482// imports (possibly with compression) <src> into the zip at sub-path <dest>
Jeff Gastoncef50b92017-08-23 15:41:35 -0700483func (z *zipWriter) addFile(dest, src string, method uint16) error {
Dan Willemsen017d8932016-08-04 15:43:03 -0700484 var fileSize int64
Dan Willemsen10462b32017-03-15 19:02:51 -0700485 var executable bool
Dan Willemsen017d8932016-08-04 15:43:03 -0700486
Nan Zhang9067b042017-03-17 14:04:43 -0700487 if s, err := os.Lstat(src); err != nil {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700488 return err
489 } else if s.IsDir() {
Colin Cross957cc4e2015-04-24 15:10:32 -0700490 if z.directories {
Nan Zhang9067b042017-03-17 14:04:43 -0700491 return z.writeDirectory(dest)
Colin Cross957cc4e2015-04-24 15:10:32 -0700492 }
493 return nil
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700494 } else if s.Mode()&os.ModeSymlink != 0 {
Nan Zhang9067b042017-03-17 14:04:43 -0700495 return z.writeSymlink(dest, src)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700496 } else if !s.Mode().IsRegular() {
Nan Zhang9067b042017-03-17 14:04:43 -0700497 return fmt.Errorf("%s is not a file, directory, or symlink", src)
Dan Willemsen017d8932016-08-04 15:43:03 -0700498 } else {
499 fileSize = s.Size()
Dan Willemsen10462b32017-03-15 19:02:51 -0700500 executable = s.Mode()&0100 != 0
Colin Cross957cc4e2015-04-24 15:10:32 -0700501 }
502
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700503 r, err := os.Open(src)
504 if err != nil {
505 return err
506 }
507
508 header := &zip.FileHeader{
509 Name: dest,
510 Method: method,
511 UncompressedSize64: uint64(fileSize),
512 }
513
514 if executable {
515 header.SetMode(0700)
516 }
517
518 return z.writeFileContents(header, r)
519}
520
521// writes the contents of r according to the specifications in header
Jeff Gastoncef50b92017-08-23 15:41:35 -0700522func (z *zipWriter) addManifest(dest string, src string, method uint16) error {
523 givenBytes, err := ioutil.ReadFile(src)
524 if err != nil {
525 return err
526 }
527
528 manifestMarker := []byte("Manifest-Version:")
529 header := append(manifestMarker, []byte(" 1.0\nCreated-By: soong_zip\n")...)
530
531 var finalBytes []byte
532 if !bytes.Contains(givenBytes, manifestMarker) {
533 finalBytes = append(append(header, givenBytes...), byte('\n'))
534 } else {
535 finalBytes = givenBytes
536 }
537
538 byteReader := bytes.NewReader(finalBytes)
539
540 reader := &byteReaderCloser{*byteReader, ioutil.NopCloser(nil)}
541
542 fileHeader := &zip.FileHeader{
543 Name: dest,
544 Method: zip.Store,
545 UncompressedSize64: uint64(byteReader.Len()),
546 }
547
548 return z.writeFileContents(fileHeader, reader)
549}
550
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700551func (z *zipWriter) writeFileContents(header *zip.FileHeader, r readerSeekerCloser) (err error) {
552
553 header.SetModTime(z.time)
554
Colin Crosse19c7932015-04-24 15:08:38 -0700555 if z.directories {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700556 dest := header.Name
Nan Zhang9067b042017-03-17 14:04:43 -0700557 dir, _ := filepath.Split(dest)
Colin Crosse19c7932015-04-24 15:08:38 -0700558 err := z.writeDirectory(dir)
559 if err != nil {
560 return err
Colin Cross2fe66872015-03-30 17:20:39 -0700561 }
562 }
563
Dan Willemsen017d8932016-08-04 15:43:03 -0700564 compressChan := make(chan *zipEntry, 1)
565 z.writeOps <- compressChan
566
567 // Pre-fill a zipEntry, it will be sent in the compressChan once
568 // we're sure about the Method and CRC.
569 ze := &zipEntry{
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700570 fh: header,
Dan Willemsen10462b32017-03-15 19:02:51 -0700571 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700572
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700573 ze.allocatedSize = int64(header.UncompressedSize64)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700574 z.cpuRateLimiter.Request()
575 z.memoryRateLimiter.Request(ze.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700576
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700577 fileSize := int64(header.UncompressedSize64)
578 if fileSize == 0 {
579 fileSize = int64(header.UncompressedSize)
580 }
581
582 if header.Method == zip.Deflate && fileSize >= minParallelFileSize {
Dan Willemsen017d8932016-08-04 15:43:03 -0700583 wg := new(sync.WaitGroup)
584
585 // Allocate enough buffer to hold all readers. We'll limit
586 // this based on actual buffer sizes in RateLimit.
587 ze.futureReaders = make(chan chan io.Reader, (fileSize/parallelBlockSize)+1)
588
589 // Calculate the CRC in the background, since reading the entire
590 // file could take a while.
591 //
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700592 // We could split this up into chunks as well, but it's faster
Dan Willemsen017d8932016-08-04 15:43:03 -0700593 // than the compression. Due to the Go Zip API, we also need to
594 // know the result before we can begin writing the compressed
595 // data out to the zipfile.
596 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700597 go z.crcFile(r, ze, compressChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700598
599 for start := int64(0); start < fileSize; start += parallelBlockSize {
600 sr := io.NewSectionReader(r, start, parallelBlockSize)
601 resultChan := make(chan io.Reader, 1)
602 ze.futureReaders <- resultChan
603
Jeff Gaston175f34c2017-08-17 21:43:21 -0700604 z.cpuRateLimiter.Request()
Dan Willemsen017d8932016-08-04 15:43:03 -0700605
606 last := !(start+parallelBlockSize < fileSize)
607 var dict []byte
608 if start >= windowSize {
609 dict, err = ioutil.ReadAll(io.NewSectionReader(r, start-windowSize, windowSize))
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700610 if err != nil {
611 return err
612 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700613 }
614
615 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700616 go z.compressPartialFile(sr, dict, last, resultChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700617 }
618
619 close(ze.futureReaders)
620
621 // Close the file handle after all readers are done
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700622 go func(wg *sync.WaitGroup, closer io.Closer) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700623 wg.Wait()
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700624 closer.Close()
Dan Willemsen017d8932016-08-04 15:43:03 -0700625 }(wg, r)
626 } else {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700627 go func() {
628 z.compressWholeFile(ze, r, compressChan)
629 r.Close()
630 }()
Dan Willemsen017d8932016-08-04 15:43:03 -0700631 }
632
633 return nil
634}
635
Jeff Gaston175f34c2017-08-17 21:43:21 -0700636func (z *zipWriter) crcFile(r io.Reader, ze *zipEntry, resultChan chan *zipEntry, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700637 defer wg.Done()
Jeff Gaston175f34c2017-08-17 21:43:21 -0700638 defer z.cpuRateLimiter.Finish()
Dan Willemsen017d8932016-08-04 15:43:03 -0700639
640 crc := crc32.NewIEEE()
641 _, err := io.Copy(crc, r)
642 if err != nil {
643 z.errors <- err
644 return
645 }
646
647 ze.fh.CRC32 = crc.Sum32()
648 resultChan <- ze
649 close(resultChan)
650}
651
Jeff Gaston175f34c2017-08-17 21:43:21 -0700652func (z *zipWriter) compressPartialFile(r io.Reader, dict []byte, last bool, resultChan chan io.Reader, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700653 defer wg.Done()
654
655 result, err := z.compressBlock(r, dict, last)
656 if err != nil {
657 z.errors <- err
658 return
659 }
660
Jeff Gaston175f34c2017-08-17 21:43:21 -0700661 z.cpuRateLimiter.Finish()
662
Dan Willemsen017d8932016-08-04 15:43:03 -0700663 resultChan <- result
664}
665
666func (z *zipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) {
667 buf := new(bytes.Buffer)
668 var fw *flate.Writer
669 var err error
670 if len(dict) > 0 {
671 // There's no way to Reset a Writer with a new dictionary, so
672 // don't use the Pool
673 fw, err = flate.NewWriterDict(buf, z.compLevel, dict)
674 } else {
675 var ok bool
676 if fw, ok = z.compressorPool.Get().(*flate.Writer); ok {
677 fw.Reset(buf)
678 } else {
679 fw, err = flate.NewWriter(buf, z.compLevel)
680 }
681 defer z.compressorPool.Put(fw)
682 }
683 if err != nil {
684 return nil, err
685 }
686
687 _, err = io.Copy(fw, r)
688 if err != nil {
689 return nil, err
690 }
691 if last {
692 fw.Close()
693 } else {
694 fw.Flush()
695 }
696
697 return buf, nil
698}
699
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700700func (z *zipWriter) compressWholeFile(ze *zipEntry, r io.ReadSeeker, compressChan chan *zipEntry) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700701
Dan Willemsen017d8932016-08-04 15:43:03 -0700702 crc := crc32.NewIEEE()
Dan Willemsena8b55022017-03-15 21:49:26 -0700703 _, err := io.Copy(crc, r)
Colin Cross2fe66872015-03-30 17:20:39 -0700704 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700705 z.errors <- err
706 return
Colin Cross2fe66872015-03-30 17:20:39 -0700707 }
708
Dan Willemsena8b55022017-03-15 21:49:26 -0700709 ze.fh.CRC32 = crc.Sum32()
Colin Cross2fe66872015-03-30 17:20:39 -0700710
Dan Willemsen017d8932016-08-04 15:43:03 -0700711 _, err = r.Seek(0, 0)
Colin Cross2fe66872015-03-30 17:20:39 -0700712 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700713 z.errors <- err
714 return
Colin Cross2fe66872015-03-30 17:20:39 -0700715 }
716
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700717 readFile := func(reader io.ReadSeeker) ([]byte, error) {
718 _, err := reader.Seek(0, 0)
Nan Zhangf281bd82017-04-25 16:47:45 -0700719 if err != nil {
720 return nil, err
721 }
722
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700723 buf, err := ioutil.ReadAll(reader)
Nan Zhangf281bd82017-04-25 16:47:45 -0700724 if err != nil {
725 return nil, err
726 }
727
728 return buf, nil
729 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700730
Dan Willemsena8b55022017-03-15 21:49:26 -0700731 ze.futureReaders = make(chan chan io.Reader, 1)
Dan Willemsen017d8932016-08-04 15:43:03 -0700732 futureReader := make(chan io.Reader, 1)
733 ze.futureReaders <- futureReader
734 close(ze.futureReaders)
735
Nan Zhangf281bd82017-04-25 16:47:45 -0700736 if ze.fh.Method == zip.Deflate {
737 compressed, err := z.compressBlock(r, nil, true)
738 if err != nil {
739 z.errors <- err
740 return
741 }
742 if uint64(compressed.Len()) < ze.fh.UncompressedSize64 {
743 futureReader <- compressed
Nan Zhangf281bd82017-04-25 16:47:45 -0700744 } else {
745 buf, err := readFile(r)
746 if err != nil {
747 z.errors <- err
748 return
749 }
750 ze.fh.Method = zip.Store
751 futureReader <- bytes.NewReader(buf)
Nan Zhangf281bd82017-04-25 16:47:45 -0700752 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700753 } else {
Nan Zhangf281bd82017-04-25 16:47:45 -0700754 buf, err := readFile(r)
Dan Willemsen017d8932016-08-04 15:43:03 -0700755 if err != nil {
756 z.errors <- err
757 return
758 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700759 ze.fh.Method = zip.Store
760 futureReader <- bytes.NewReader(buf)
Dan Willemsen017d8932016-08-04 15:43:03 -0700761 }
Nan Zhangf281bd82017-04-25 16:47:45 -0700762
Jeff Gaston175f34c2017-08-17 21:43:21 -0700763 z.cpuRateLimiter.Finish()
764
Dan Willemsen017d8932016-08-04 15:43:03 -0700765 close(futureReader)
766
767 compressChan <- ze
768 close(compressChan)
Colin Cross2fe66872015-03-30 17:20:39 -0700769}
Colin Crosse19c7932015-04-24 15:08:38 -0700770
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700771func (z *zipWriter) addExtraField(zipHeader *zip.FileHeader, fieldHeader [2]byte, data []byte) {
772 // add the field header in little-endian order
773 zipHeader.Extra = append(zipHeader.Extra, fieldHeader[1], fieldHeader[0])
774
775 // specify the length of the data (in little-endian order)
776 dataLength := len(data)
777 lengthBytes := []byte{byte(dataLength % 256), byte(dataLength / 256)}
778 zipHeader.Extra = append(zipHeader.Extra, lengthBytes...)
779
780 // add the contents of the extra field
781 zipHeader.Extra = append(zipHeader.Extra, data...)
782}
783
Colin Crosse19c7932015-04-24 15:08:38 -0700784func (z *zipWriter) writeDirectory(dir string) error {
Jeff Gaston2d174132017-08-15 18:05:56 -0700785 // clean the input
786 cleanDir := filepath.Clean(dir)
787
788 // discover any uncreated directories in the path
789 zipDirs := []string{}
790 for cleanDir != "" && cleanDir != "." && !z.createdDirs[cleanDir] {
791
792 z.createdDirs[cleanDir] = true
793 // parent directories precede their children
794 zipDirs = append([]string{cleanDir}, zipDirs...)
795
796 cleanDir = filepath.Dir(cleanDir)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700797 }
798
Jeff Gaston2d174132017-08-15 18:05:56 -0700799 // make a directory entry for each uncreated directory
800 for _, cleanDir := range zipDirs {
Colin Crosse19c7932015-04-24 15:08:38 -0700801 dirHeader := &zip.FileHeader{
Jeff Gaston2d174132017-08-15 18:05:56 -0700802 Name: cleanDir + "/",
Colin Crosse19c7932015-04-24 15:08:38 -0700803 }
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700804 dirHeader.SetMode(0700 | os.ModeDir)
Colin Crosse19c7932015-04-24 15:08:38 -0700805 dirHeader.SetModTime(z.time)
806
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700807 if *emulateJar && dir == "META-INF/" {
808 // Jar files have a 0-length extra field with header "CAFE"
809 z.addExtraField(dirHeader, [2]byte{0xca, 0xfe}, []byte{})
810 }
811
Dan Willemsen017d8932016-08-04 15:43:03 -0700812 ze := make(chan *zipEntry, 1)
813 ze <- &zipEntry{
814 fh: dirHeader,
Colin Crosse19c7932015-04-24 15:08:38 -0700815 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700816 close(ze)
817 z.writeOps <- ze
Colin Crosse19c7932015-04-24 15:08:38 -0700818 }
819
820 return nil
821}
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700822
823func (z *zipWriter) writeSymlink(rel, file string) error {
824 if z.directories {
825 dir, _ := filepath.Split(rel)
826 if err := z.writeDirectory(dir); err != nil {
827 return err
828 }
829 }
830
831 fileHeader := &zip.FileHeader{
832 Name: rel,
833 }
834 fileHeader.SetModTime(z.time)
835 fileHeader.SetMode(0700 | os.ModeSymlink)
836
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700837 dest, err := os.Readlink(file)
838 if err != nil {
839 return err
840 }
841
Dan Willemsen017d8932016-08-04 15:43:03 -0700842 ze := make(chan *zipEntry, 1)
843 futureReaders := make(chan chan io.Reader, 1)
844 futureReader := make(chan io.Reader, 1)
845 futureReaders <- futureReader
846 close(futureReaders)
847 futureReader <- bytes.NewBufferString(dest)
848 close(futureReader)
849
Dan Willemsen017d8932016-08-04 15:43:03 -0700850 ze <- &zipEntry{
851 fh: fileHeader,
852 futureReaders: futureReaders,
853 }
854 close(ze)
855 z.writeOps <- ze
856
857 return nil
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700858}
Colin Cross7b10cf12017-08-30 14:12:21 -0700859
860func recursiveGlobFiles(path string) []string {
861 var files []string
862 filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
863 if !info.IsDir() {
864 files = append(files, path)
865 }
866 return nil
867 })
868
869 return files
870}