blob: f7dc9e0e8f7270d1f63cb9fa2524155d9f8ce89e [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
Colin Cross2fe66872015-03-30 17:20:39 -070060type fileArg struct {
Nan Zhangf281bd82017-04-25 16:47:45 -070061 pathPrefixInZip, sourcePrefixToStrip string
62 sourceFiles []string
Nan Zhang9067b042017-03-17 14:04:43 -070063}
64
65type 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
86type fileArgs []fileArg
87
Nan Zhangf281bd82017-04-25 16:47:45 -070088type file struct{}
89
90type listFiles struct{}
91
92func (f *file) String() string {
Colin Cross2fe66872015-03-30 17:20:39 -070093 return `""`
94}
95
Nan Zhangf281bd82017-04-25 16:47:45 -070096func (f *file) Set(s string) error {
Colin Cross2fe66872015-03-30 17:20:39 -070097 if *relativeRoot == "" {
Nan Zhang9067b042017-03-17 14:04:43 -070098 return fmt.Errorf("must pass -C before -f or -l")
Colin Cross2fe66872015-03-30 17:20:39 -070099 }
100
Nan Zhangf281bd82017-04-25 16:47:45 -0700101 fArgs = append(fArgs, fileArg{
102 pathPrefixInZip: filepath.Clean(*rootPrefix),
103 sourcePrefixToStrip: filepath.Clean(*relativeRoot),
104 sourceFiles: []string{s},
105 })
106
Colin Cross2fe66872015-03-30 17:20:39 -0700107 return nil
108}
109
Nan Zhangf281bd82017-04-25 16:47:45 -0700110func (l *listFiles) String() string {
111 return `""`
112}
113
114func (l *listFiles) Set(s string) error {
115 if *relativeRoot == "" {
116 return fmt.Errorf("must pass -C before -f or -l")
117 }
118
119 list, err := ioutil.ReadFile(s)
120 if err != nil {
121 return err
122 }
123
124 fArgs = append(fArgs, fileArg{
125 pathPrefixInZip: filepath.Clean(*rootPrefix),
126 sourcePrefixToStrip: filepath.Clean(*relativeRoot),
127 sourceFiles: strings.Split(string(list), "\n"),
128 })
129
130 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700131}
132
133var (
Dan Willemsen47ec28f2016-08-10 16:12:30 -0700134 out = flag.String("o", "", "file to write zip file to")
135 manifest = flag.String("m", "", "input jar manifest file name")
136 directories = flag.Bool("d", false, "include directories in zip")
Nan Zhang9067b042017-03-17 14:04:43 -0700137 rootPrefix = flag.String("P", "", "path prefix within the zip at which to place files")
Colin Cross2fe66872015-03-30 17:20:39 -0700138 relativeRoot = flag.String("C", "", "path to use as relative root of files in next -f or -l argument")
Dan Willemsen017d8932016-08-04 15:43:03 -0700139 parallelJobs = flag.Int("j", runtime.NumCPU(), "number of parallel threads to use")
140 compLevel = flag.Int("L", 5, "deflate compression level (0-9)")
Jeff Gastona2976952017-08-22 17:51:25 -0700141 emulateJar = flag.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'")
Nan Zhang9067b042017-03-17 14:04:43 -0700142
Nan Zhangf281bd82017-04-25 16:47:45 -0700143 fArgs fileArgs
144 nonDeflatedFiles = make(uniqueSet)
Dan Willemsen017d8932016-08-04 15:43:03 -0700145
146 cpuProfile = flag.String("cpuprofile", "", "write cpu profile to file")
147 traceFile = flag.String("trace", "", "write trace to file")
Colin Cross2fe66872015-03-30 17:20:39 -0700148)
149
150func init() {
Nan Zhangf281bd82017-04-25 16:47:45 -0700151 flag.Var(&listFiles{}, "l", "file containing list of .class files")
152 flag.Var(&file{}, "f", "file to include in zip")
153 flag.Var(&nonDeflatedFiles, "s", "file path to be stored within the zip without compression")
Colin Cross2fe66872015-03-30 17:20:39 -0700154}
155
156func usage() {
Dan Willemsen47ec28f2016-08-10 16:12:30 -0700157 fmt.Fprintf(os.Stderr, "usage: soong_zip -o zipfile [-m manifest] -C dir [-f|-l file]...\n")
Colin Cross2fe66872015-03-30 17:20:39 -0700158 flag.PrintDefaults()
159 os.Exit(2)
160}
161
Colin Crosse19c7932015-04-24 15:08:38 -0700162type zipWriter struct {
Colin Cross2fe66872015-03-30 17:20:39 -0700163 time time.Time
164 createdDirs map[string]bool
165 directories bool
Colin Crosse19c7932015-04-24 15:08:38 -0700166
Dan Willemsen017d8932016-08-04 15:43:03 -0700167 errors chan error
168 writeOps chan chan *zipEntry
169
Jeff Gaston175f34c2017-08-17 21:43:21 -0700170 cpuRateLimiter *CPURateLimiter
171 memoryRateLimiter *MemoryRateLimiter
Dan Willemsen017d8932016-08-04 15:43:03 -0700172
173 compressorPool sync.Pool
174 compLevel int
175}
176
177type zipEntry struct {
178 fh *zip.FileHeader
179
180 // List of delayed io.Reader
181 futureReaders chan chan io.Reader
Jeff Gaston175f34c2017-08-17 21:43:21 -0700182
183 // Only used for passing into the MemoryRateLimiter to ensure we
184 // release as much memory as much as we request
185 allocatedSize int64
Colin Cross2fe66872015-03-30 17:20:39 -0700186}
187
188func main() {
189 flag.Parse()
190
Dan Willemsen017d8932016-08-04 15:43:03 -0700191 if *cpuProfile != "" {
192 f, err := os.Create(*cpuProfile)
193 if err != nil {
194 fmt.Fprintln(os.Stderr, err.Error())
195 os.Exit(1)
196 }
197 defer f.Close()
198 pprof.StartCPUProfile(f)
199 defer pprof.StopCPUProfile()
200 }
201
202 if *traceFile != "" {
203 f, err := os.Create(*traceFile)
204 if err != nil {
205 fmt.Fprintln(os.Stderr, err.Error())
206 os.Exit(1)
207 }
208 defer f.Close()
209 err = trace.Start(f)
210 if err != nil {
211 fmt.Fprintln(os.Stderr, err.Error())
212 os.Exit(1)
213 }
214 defer trace.Stop()
215 }
216
Colin Cross2fe66872015-03-30 17:20:39 -0700217 if *out == "" {
218 fmt.Fprintf(os.Stderr, "error: -o is required\n")
219 usage()
220 }
221
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700222 if *emulateJar {
223 *directories = true
224 }
225
Colin Crosse19c7932015-04-24 15:08:38 -0700226 w := &zipWriter{
Dan Willemsen77a6b862016-08-04 20:38:47 -0700227 time: time.Date(2009, 1, 1, 0, 0, 0, 0, time.UTC),
Colin Cross2fe66872015-03-30 17:20:39 -0700228 createdDirs: make(map[string]bool),
229 directories: *directories,
Dan Willemsen017d8932016-08-04 15:43:03 -0700230 compLevel: *compLevel,
Colin Cross2fe66872015-03-30 17:20:39 -0700231 }
232
Nan Zhang9067b042017-03-17 14:04:43 -0700233 pathMappings := []pathMapping{}
234 set := make(map[string]string)
235
Nan Zhangf281bd82017-04-25 16:47:45 -0700236 for _, fa := range fArgs {
237 for _, src := range fa.sourceFiles {
238 if err := fillPathPairs(fa.pathPrefixInZip,
239 fa.sourcePrefixToStrip, src, set, &pathMappings); err != nil {
Nan Zhang9067b042017-03-17 14:04:43 -0700240 log.Fatal(err)
241 }
242 }
243 }
244
Nan Zhang9067b042017-03-17 14:04:43 -0700245 err := w.write(*out, pathMappings, *manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700246 if err != nil {
247 fmt.Fprintln(os.Stderr, err.Error())
248 os.Exit(1)
249 }
250}
251
Nan Zhang9067b042017-03-17 14:04:43 -0700252func fillPathPairs(prefix, rel, src string, set map[string]string, pathMappings *[]pathMapping) error {
253 src = strings.TrimSpace(src)
254 if src == "" {
255 return nil
256 }
257 src = filepath.Clean(src)
258 dest, err := filepath.Rel(rel, src)
259 if err != nil {
260 return err
261 }
262 dest = filepath.Join(prefix, dest)
263
264 if _, found := set[dest]; found {
265 return fmt.Errorf("found two file paths to be copied into dest path: %q,"+
266 " both [%q]%q and [%q]%q!",
267 dest, dest, src, dest, set[dest])
268 } else {
269 set[dest] = src
270 }
271
Nan Zhangf281bd82017-04-25 16:47:45 -0700272 zipMethod := zip.Deflate
273 if _, found := nonDeflatedFiles[dest]; found {
274 zipMethod = zip.Store
275 }
276 *pathMappings = append(*pathMappings,
277 pathMapping{dest: dest, src: src, zipMethod: zipMethod})
Nan Zhang9067b042017-03-17 14:04:43 -0700278
279 return nil
280}
281
Jeff Gastona2976952017-08-22 17:51:25 -0700282func jarSort(mappings []pathMapping) {
283 less := func(i int, j int) (smaller bool) {
284 return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest)
285 }
286 sort.SliceStable(mappings, less)
287}
288
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700289type readerSeekerCloser interface {
290 io.Reader
291 io.ReaderAt
292 io.Closer
293 io.Seeker
294}
295
Nan Zhang9067b042017-03-17 14:04:43 -0700296func (z *zipWriter) write(out string, pathMappings []pathMapping, manifest string) error {
Colin Cross2fe66872015-03-30 17:20:39 -0700297 f, err := os.Create(out)
298 if err != nil {
299 return err
300 }
301
302 defer f.Close()
303 defer func() {
304 if err != nil {
305 os.Remove(out)
306 }
307 }()
308
Dan Willemsen017d8932016-08-04 15:43:03 -0700309 z.errors = make(chan error)
310 defer close(z.errors)
Colin Cross2fe66872015-03-30 17:20:39 -0700311
Dan Willemsen017d8932016-08-04 15:43:03 -0700312 // This channel size can be essentially unlimited -- it's used as a fifo
313 // queue decouple the CPU and IO loads. Directories don't require any
314 // compression time, but still cost some IO. Similar with small files that
315 // can be very fast to compress. Some files that are more difficult to
316 // compress won't take a corresponding longer time writing out.
317 //
318 // The optimum size here depends on your CPU and IO characteristics, and
319 // the the layout of your zip file. 1000 was chosen mostly at random as
320 // something that worked reasonably well for a test file.
321 //
322 // The RateLimit object will put the upper bounds on the number of
323 // parallel compressions and outstanding buffers.
324 z.writeOps = make(chan chan *zipEntry, 1000)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700325 z.cpuRateLimiter = NewCPURateLimiter(int64(*parallelJobs))
326 z.memoryRateLimiter = NewMemoryRateLimiter(0)
327 defer func() {
328 z.cpuRateLimiter.Stop()
329 z.memoryRateLimiter.Stop()
330 }()
Jeff Gastona2976952017-08-22 17:51:25 -0700331
332 if manifest != "" {
333 if !*emulateJar {
334 return errors.New("must specify --jar when specifying a manifest via -m")
335 }
336 pathMappings = append(pathMappings, pathMapping{"META-INF/MANIFEST.MF", manifest, zip.Deflate})
337 }
338
339 if *emulateJar {
340 jarSort(pathMappings)
341 }
342
Dan Willemsen017d8932016-08-04 15:43:03 -0700343 go func() {
344 var err error
345 defer close(z.writeOps)
346
Nan Zhang9067b042017-03-17 14:04:43 -0700347 for _, ele := range pathMappings {
Nan Zhangf281bd82017-04-25 16:47:45 -0700348 err = z.writeFile(ele.dest, ele.src, ele.zipMethod)
Dan Willemsen017d8932016-08-04 15:43:03 -0700349 if err != nil {
350 z.errors <- err
351 return
352 }
353 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700354 }()
355
356 zipw := zip.NewWriter(f)
357
358 var currentWriteOpChan chan *zipEntry
359 var currentWriter io.WriteCloser
360 var currentReaders chan chan io.Reader
361 var currentReader chan io.Reader
362 var done bool
363
364 for !done {
365 var writeOpsChan chan chan *zipEntry
366 var writeOpChan chan *zipEntry
367 var readersChan chan chan io.Reader
368
369 if currentReader != nil {
370 // Only read and process errors
371 } else if currentReaders != nil {
372 readersChan = currentReaders
373 } else if currentWriteOpChan != nil {
374 writeOpChan = currentWriteOpChan
375 } else {
376 writeOpsChan = z.writeOps
377 }
378
379 select {
380 case writeOp, ok := <-writeOpsChan:
381 if !ok {
382 done = true
383 }
384
385 currentWriteOpChan = writeOp
386
387 case op := <-writeOpChan:
388 currentWriteOpChan = nil
389
390 if op.fh.Method == zip.Deflate {
391 currentWriter, err = zipw.CreateCompressedHeader(op.fh)
392 } else {
393 var zw io.Writer
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700394
395 op.fh.CompressedSize64 = op.fh.UncompressedSize64
396
397 zw, err = zipw.CreateHeaderAndroid(op.fh)
Dan Willemsen017d8932016-08-04 15:43:03 -0700398 currentWriter = nopCloser{zw}
399 }
400 if err != nil {
401 return err
402 }
403
404 currentReaders = op.futureReaders
405 if op.futureReaders == nil {
406 currentWriter.Close()
407 currentWriter = nil
408 }
Jeff Gaston175f34c2017-08-17 21:43:21 -0700409 z.memoryRateLimiter.Finish(op.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700410
411 case futureReader, ok := <-readersChan:
412 if !ok {
413 // Done with reading
414 currentWriter.Close()
415 currentWriter = nil
416 currentReaders = nil
417 }
418
419 currentReader = futureReader
420
421 case reader := <-currentReader:
Jeff Gaston175f34c2017-08-17 21:43:21 -0700422 _, err = io.Copy(currentWriter, reader)
Dan Willemsen017d8932016-08-04 15:43:03 -0700423 if err != nil {
424 return err
425 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700426
427 currentReader = nil
428
429 case err = <-z.errors:
Colin Cross2fe66872015-03-30 17:20:39 -0700430 return err
431 }
432 }
433
Dan Willemsen017d8932016-08-04 15:43:03 -0700434 // One last chance to catch an error
435 select {
436 case err = <-z.errors:
437 return err
438 default:
439 zipw.Close()
440 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700441 }
Colin Cross2fe66872015-03-30 17:20:39 -0700442}
443
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700444// imports (possibly with compression) <src> into the zip at sub-path <dest>
Nan Zhangf281bd82017-04-25 16:47:45 -0700445func (z *zipWriter) writeFile(dest, src string, method uint16) error {
Dan Willemsen017d8932016-08-04 15:43:03 -0700446 var fileSize int64
Dan Willemsen10462b32017-03-15 19:02:51 -0700447 var executable bool
Dan Willemsen017d8932016-08-04 15:43:03 -0700448
Nan Zhang9067b042017-03-17 14:04:43 -0700449 if s, err := os.Lstat(src); err != nil {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700450 return err
451 } else if s.IsDir() {
Colin Cross957cc4e2015-04-24 15:10:32 -0700452 if z.directories {
Nan Zhang9067b042017-03-17 14:04:43 -0700453 return z.writeDirectory(dest)
Colin Cross957cc4e2015-04-24 15:10:32 -0700454 }
455 return nil
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700456 } else if s.Mode()&os.ModeSymlink != 0 {
Nan Zhang9067b042017-03-17 14:04:43 -0700457 return z.writeSymlink(dest, src)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700458 } else if !s.Mode().IsRegular() {
Nan Zhang9067b042017-03-17 14:04:43 -0700459 return fmt.Errorf("%s is not a file, directory, or symlink", src)
Dan Willemsen017d8932016-08-04 15:43:03 -0700460 } else {
461 fileSize = s.Size()
Dan Willemsen10462b32017-03-15 19:02:51 -0700462 executable = s.Mode()&0100 != 0
Colin Cross957cc4e2015-04-24 15:10:32 -0700463 }
464
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700465 r, err := os.Open(src)
466 if err != nil {
467 return err
468 }
469
470 header := &zip.FileHeader{
471 Name: dest,
472 Method: method,
473 UncompressedSize64: uint64(fileSize),
474 }
475
476 if executable {
477 header.SetMode(0700)
478 }
479
480 return z.writeFileContents(header, r)
481}
482
483// writes the contents of r according to the specifications in header
484func (z *zipWriter) writeFileContents(header *zip.FileHeader, r readerSeekerCloser) (err error) {
485
486 header.SetModTime(z.time)
487
Colin Crosse19c7932015-04-24 15:08:38 -0700488 if z.directories {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700489 dest := header.Name
Nan Zhang9067b042017-03-17 14:04:43 -0700490 dir, _ := filepath.Split(dest)
Colin Crosse19c7932015-04-24 15:08:38 -0700491 err := z.writeDirectory(dir)
492 if err != nil {
493 return err
Colin Cross2fe66872015-03-30 17:20:39 -0700494 }
495 }
496
Dan Willemsen017d8932016-08-04 15:43:03 -0700497 compressChan := make(chan *zipEntry, 1)
498 z.writeOps <- compressChan
499
500 // Pre-fill a zipEntry, it will be sent in the compressChan once
501 // we're sure about the Method and CRC.
502 ze := &zipEntry{
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700503 fh: header,
Dan Willemsen10462b32017-03-15 19:02:51 -0700504 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700505
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700506 ze.allocatedSize = int64(header.UncompressedSize64)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700507 z.cpuRateLimiter.Request()
508 z.memoryRateLimiter.Request(ze.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700509
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700510 fileSize := int64(header.UncompressedSize64)
511 if fileSize == 0 {
512 fileSize = int64(header.UncompressedSize)
513 }
514
515 if header.Method == zip.Deflate && fileSize >= minParallelFileSize {
Dan Willemsen017d8932016-08-04 15:43:03 -0700516 wg := new(sync.WaitGroup)
517
518 // Allocate enough buffer to hold all readers. We'll limit
519 // this based on actual buffer sizes in RateLimit.
520 ze.futureReaders = make(chan chan io.Reader, (fileSize/parallelBlockSize)+1)
521
522 // Calculate the CRC in the background, since reading the entire
523 // file could take a while.
524 //
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700525 // We could split this up into chunks as well, but it's faster
Dan Willemsen017d8932016-08-04 15:43:03 -0700526 // than the compression. Due to the Go Zip API, we also need to
527 // know the result before we can begin writing the compressed
528 // data out to the zipfile.
529 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700530 go z.crcFile(r, ze, compressChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700531
532 for start := int64(0); start < fileSize; start += parallelBlockSize {
533 sr := io.NewSectionReader(r, start, parallelBlockSize)
534 resultChan := make(chan io.Reader, 1)
535 ze.futureReaders <- resultChan
536
Jeff Gaston175f34c2017-08-17 21:43:21 -0700537 z.cpuRateLimiter.Request()
Dan Willemsen017d8932016-08-04 15:43:03 -0700538
539 last := !(start+parallelBlockSize < fileSize)
540 var dict []byte
541 if start >= windowSize {
542 dict, err = ioutil.ReadAll(io.NewSectionReader(r, start-windowSize, windowSize))
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700543 if err != nil {
544 return err
545 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700546 }
547
548 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700549 go z.compressPartialFile(sr, dict, last, resultChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700550 }
551
552 close(ze.futureReaders)
553
554 // Close the file handle after all readers are done
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700555 go func(wg *sync.WaitGroup, closer io.Closer) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700556 wg.Wait()
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700557 closer.Close()
Dan Willemsen017d8932016-08-04 15:43:03 -0700558 }(wg, r)
559 } else {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700560 go func() {
561 z.compressWholeFile(ze, r, compressChan)
562 r.Close()
563 }()
Dan Willemsen017d8932016-08-04 15:43:03 -0700564 }
565
566 return nil
567}
568
Jeff Gaston175f34c2017-08-17 21:43:21 -0700569func (z *zipWriter) crcFile(r io.Reader, ze *zipEntry, resultChan chan *zipEntry, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700570 defer wg.Done()
Jeff Gaston175f34c2017-08-17 21:43:21 -0700571 defer z.cpuRateLimiter.Finish()
Dan Willemsen017d8932016-08-04 15:43:03 -0700572
573 crc := crc32.NewIEEE()
574 _, err := io.Copy(crc, r)
575 if err != nil {
576 z.errors <- err
577 return
578 }
579
580 ze.fh.CRC32 = crc.Sum32()
581 resultChan <- ze
582 close(resultChan)
583}
584
Jeff Gaston175f34c2017-08-17 21:43:21 -0700585func (z *zipWriter) compressPartialFile(r io.Reader, dict []byte, last bool, resultChan chan io.Reader, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700586 defer wg.Done()
587
588 result, err := z.compressBlock(r, dict, last)
589 if err != nil {
590 z.errors <- err
591 return
592 }
593
Jeff Gaston175f34c2017-08-17 21:43:21 -0700594 z.cpuRateLimiter.Finish()
595
Dan Willemsen017d8932016-08-04 15:43:03 -0700596 resultChan <- result
597}
598
599func (z *zipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) {
600 buf := new(bytes.Buffer)
601 var fw *flate.Writer
602 var err error
603 if len(dict) > 0 {
604 // There's no way to Reset a Writer with a new dictionary, so
605 // don't use the Pool
606 fw, err = flate.NewWriterDict(buf, z.compLevel, dict)
607 } else {
608 var ok bool
609 if fw, ok = z.compressorPool.Get().(*flate.Writer); ok {
610 fw.Reset(buf)
611 } else {
612 fw, err = flate.NewWriter(buf, z.compLevel)
613 }
614 defer z.compressorPool.Put(fw)
615 }
616 if err != nil {
617 return nil, err
618 }
619
620 _, err = io.Copy(fw, r)
621 if err != nil {
622 return nil, err
623 }
624 if last {
625 fw.Close()
626 } else {
627 fw.Flush()
628 }
629
630 return buf, nil
631}
632
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700633func (z *zipWriter) compressWholeFile(ze *zipEntry, r io.ReadSeeker, compressChan chan *zipEntry) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700634
Dan Willemsen017d8932016-08-04 15:43:03 -0700635 crc := crc32.NewIEEE()
Dan Willemsena8b55022017-03-15 21:49:26 -0700636 _, err := io.Copy(crc, r)
Colin Cross2fe66872015-03-30 17:20:39 -0700637 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700638 z.errors <- err
639 return
Colin Cross2fe66872015-03-30 17:20:39 -0700640 }
641
Dan Willemsena8b55022017-03-15 21:49:26 -0700642 ze.fh.CRC32 = crc.Sum32()
Colin Cross2fe66872015-03-30 17:20:39 -0700643
Dan Willemsen017d8932016-08-04 15:43:03 -0700644 _, err = r.Seek(0, 0)
Colin Cross2fe66872015-03-30 17:20:39 -0700645 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700646 z.errors <- err
647 return
Colin Cross2fe66872015-03-30 17:20:39 -0700648 }
649
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700650 readFile := func(reader io.ReadSeeker) ([]byte, error) {
651 _, err := reader.Seek(0, 0)
Nan Zhangf281bd82017-04-25 16:47:45 -0700652 if err != nil {
653 return nil, err
654 }
655
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700656 buf, err := ioutil.ReadAll(reader)
Nan Zhangf281bd82017-04-25 16:47:45 -0700657 if err != nil {
658 return nil, err
659 }
660
661 return buf, nil
662 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700663
Dan Willemsena8b55022017-03-15 21:49:26 -0700664 ze.futureReaders = make(chan chan io.Reader, 1)
Dan Willemsen017d8932016-08-04 15:43:03 -0700665 futureReader := make(chan io.Reader, 1)
666 ze.futureReaders <- futureReader
667 close(ze.futureReaders)
668
Nan Zhangf281bd82017-04-25 16:47:45 -0700669 if ze.fh.Method == zip.Deflate {
670 compressed, err := z.compressBlock(r, nil, true)
671 if err != nil {
672 z.errors <- err
673 return
674 }
675 if uint64(compressed.Len()) < ze.fh.UncompressedSize64 {
676 futureReader <- compressed
Nan Zhangf281bd82017-04-25 16:47:45 -0700677 } else {
678 buf, err := readFile(r)
679 if err != nil {
680 z.errors <- err
681 return
682 }
683 ze.fh.Method = zip.Store
684 futureReader <- bytes.NewReader(buf)
Nan Zhangf281bd82017-04-25 16:47:45 -0700685 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700686 } else {
Nan Zhangf281bd82017-04-25 16:47:45 -0700687 buf, err := readFile(r)
Dan Willemsen017d8932016-08-04 15:43:03 -0700688 if err != nil {
689 z.errors <- err
690 return
691 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700692 ze.fh.Method = zip.Store
693 futureReader <- bytes.NewReader(buf)
Dan Willemsen017d8932016-08-04 15:43:03 -0700694 }
Nan Zhangf281bd82017-04-25 16:47:45 -0700695
Jeff Gaston175f34c2017-08-17 21:43:21 -0700696 z.cpuRateLimiter.Finish()
697
Dan Willemsen017d8932016-08-04 15:43:03 -0700698 close(futureReader)
699
700 compressChan <- ze
701 close(compressChan)
Colin Cross2fe66872015-03-30 17:20:39 -0700702}
Colin Crosse19c7932015-04-24 15:08:38 -0700703
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700704func (z *zipWriter) addExtraField(zipHeader *zip.FileHeader, fieldHeader [2]byte, data []byte) {
705 // add the field header in little-endian order
706 zipHeader.Extra = append(zipHeader.Extra, fieldHeader[1], fieldHeader[0])
707
708 // specify the length of the data (in little-endian order)
709 dataLength := len(data)
710 lengthBytes := []byte{byte(dataLength % 256), byte(dataLength / 256)}
711 zipHeader.Extra = append(zipHeader.Extra, lengthBytes...)
712
713 // add the contents of the extra field
714 zipHeader.Extra = append(zipHeader.Extra, data...)
715}
716
Colin Crosse19c7932015-04-24 15:08:38 -0700717func (z *zipWriter) writeDirectory(dir string) error {
Jeff Gaston2d174132017-08-15 18:05:56 -0700718 // clean the input
719 cleanDir := filepath.Clean(dir)
720
721 // discover any uncreated directories in the path
722 zipDirs := []string{}
723 for cleanDir != "" && cleanDir != "." && !z.createdDirs[cleanDir] {
724
725 z.createdDirs[cleanDir] = true
726 // parent directories precede their children
727 zipDirs = append([]string{cleanDir}, zipDirs...)
728
729 cleanDir = filepath.Dir(cleanDir)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700730 }
731
Jeff Gaston2d174132017-08-15 18:05:56 -0700732 // make a directory entry for each uncreated directory
733 for _, cleanDir := range zipDirs {
Colin Crosse19c7932015-04-24 15:08:38 -0700734 dirHeader := &zip.FileHeader{
Jeff Gaston2d174132017-08-15 18:05:56 -0700735 Name: cleanDir + "/",
Colin Crosse19c7932015-04-24 15:08:38 -0700736 }
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700737 dirHeader.SetMode(0700 | os.ModeDir)
Colin Crosse19c7932015-04-24 15:08:38 -0700738 dirHeader.SetModTime(z.time)
739
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700740 if *emulateJar && dir == "META-INF/" {
741 // Jar files have a 0-length extra field with header "CAFE"
742 z.addExtraField(dirHeader, [2]byte{0xca, 0xfe}, []byte{})
743 }
744
Dan Willemsen017d8932016-08-04 15:43:03 -0700745 ze := make(chan *zipEntry, 1)
746 ze <- &zipEntry{
747 fh: dirHeader,
Colin Crosse19c7932015-04-24 15:08:38 -0700748 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700749 close(ze)
750 z.writeOps <- ze
Colin Crosse19c7932015-04-24 15:08:38 -0700751 }
752
753 return nil
754}
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700755
756func (z *zipWriter) writeSymlink(rel, file string) error {
757 if z.directories {
758 dir, _ := filepath.Split(rel)
759 if err := z.writeDirectory(dir); err != nil {
760 return err
761 }
762 }
763
764 fileHeader := &zip.FileHeader{
765 Name: rel,
766 }
767 fileHeader.SetModTime(z.time)
768 fileHeader.SetMode(0700 | os.ModeSymlink)
769
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700770 dest, err := os.Readlink(file)
771 if err != nil {
772 return err
773 }
774
Dan Willemsen017d8932016-08-04 15:43:03 -0700775 ze := make(chan *zipEntry, 1)
776 futureReaders := make(chan chan io.Reader, 1)
777 futureReader := make(chan io.Reader, 1)
778 futureReaders <- futureReader
779 close(futureReaders)
780 futureReader <- bytes.NewBufferString(dest)
781 close(futureReader)
782
Dan Willemsen017d8932016-08-04 15:43:03 -0700783 ze <- &zipEntry{
784 fh: fileHeader,
785 futureReaders: futureReaders,
786 }
787 close(ze)
788 z.writeOps <- ze
789
790 return nil
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700791}