blob: d9645b84f730e583495dbd944d7fd099c8c70c9e [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Jeff Gaston11b5c512017-10-12 12:19:14 -070015package zip
Colin Cross2fe66872015-03-30 17:20:39 -070016
17import (
Dan Willemsen017d8932016-08-04 15:43:03 -070018 "bytes"
19 "compress/flate"
Jeff Gastona2976952017-08-22 17:51:25 -070020 "errors"
Colin Cross2fe66872015-03-30 17:20:39 -070021 "fmt"
Dan Willemsen017d8932016-08-04 15:43:03 -070022 "hash/crc32"
Colin Cross2fe66872015-03-30 17:20:39 -070023 "io"
24 "io/ioutil"
Nan Zhang9067b042017-03-17 14:04:43 -070025 "log"
Colin Cross2fe66872015-03-30 17:20:39 -070026 "os"
27 "path/filepath"
Dan Willemsen017d8932016-08-04 15:43:03 -070028 "runtime/pprof"
29 "runtime/trace"
Jeff Gastona2976952017-08-22 17:51:25 -070030 "sort"
Colin Cross2fe66872015-03-30 17:20:39 -070031 "strings"
Dan Willemsen017d8932016-08-04 15:43:03 -070032 "sync"
Colin Cross2fe66872015-03-30 17:20:39 -070033 "time"
Nan Zhang674dd932018-01-26 18:30:36 -080034 "unicode"
Dan Willemsen017d8932016-08-04 15:43:03 -070035
Colin Crossf83c1502017-11-10 13:11:02 -080036 "github.com/google/blueprint/pathtools"
37
Jeff Gastona2976952017-08-22 17:51:25 -070038 "android/soong/jar"
Dan Willemsen017d8932016-08-04 15:43:03 -070039 "android/soong/third_party/zip"
Colin Cross2fe66872015-03-30 17:20:39 -070040)
41
Dan Willemsen017d8932016-08-04 15:43:03 -070042// Block size used during parallel compression of a single file.
43const parallelBlockSize = 1 * 1024 * 1024 // 1MB
44
45// Minimum file size to use parallel compression. It requires more
46// flate.Writer allocations, since we can't change the dictionary
47// during Reset
48const minParallelFileSize = parallelBlockSize * 6
49
50// Size of the ZIP compression window (32KB)
51const windowSize = 32 * 1024
52
53type nopCloser struct {
54 io.Writer
55}
56
57func (nopCloser) Close() error {
58 return nil
59}
60
Jeff Gastoncef50b92017-08-23 15:41:35 -070061type byteReaderCloser struct {
Colin Cross635acc92017-09-12 22:50:46 -070062 *bytes.Reader
Jeff Gastoncef50b92017-08-23 15:41:35 -070063 io.Closer
64}
65
Nan Zhang9067b042017-03-17 14:04:43 -070066type pathMapping struct {
67 dest, src string
Nan Zhangf281bd82017-04-25 16:47:45 -070068 zipMethod uint16
69}
70
71type uniqueSet map[string]bool
72
73func (u *uniqueSet) String() string {
74 return `""`
75}
76
77func (u *uniqueSet) Set(s string) error {
78 if _, found := (*u)[s]; found {
79 return fmt.Errorf("File %q was specified twice as a file to not deflate", s)
80 } else {
81 (*u)[s] = true
82 }
83
84 return nil
Colin Cross2fe66872015-03-30 17:20:39 -070085}
86
Jeff Gastonc3bdc972017-10-12 12:18:19 -070087type FileArg struct {
88 PathPrefixInZip, SourcePrefixToStrip string
89 SourceFiles []string
Colin Crossb7c69112018-09-18 16:51:43 -070090 JunkPaths bool
Jeff Gastonc3bdc972017-10-12 12:18:19 -070091 GlobDir string
92}
93
94type FileArgs []FileArg
95
96type ZipWriter struct {
Colin Crosse5580972017-08-30 17:40:21 -070097 time time.Time
98 createdFiles map[string]string
99 createdDirs map[string]string
100 directories bool
Colin Crosse19c7932015-04-24 15:08:38 -0700101
Dan Willemsen017d8932016-08-04 15:43:03 -0700102 errors chan error
103 writeOps chan chan *zipEntry
104
Jeff Gaston175f34c2017-08-17 21:43:21 -0700105 cpuRateLimiter *CPURateLimiter
106 memoryRateLimiter *MemoryRateLimiter
Dan Willemsen017d8932016-08-04 15:43:03 -0700107
108 compressorPool sync.Pool
109 compLevel int
Colin Crossd59dab92018-09-21 15:12:39 -0700110 followSymlinks pathtools.ShouldFollowSymlinks
Dan Willemsen017d8932016-08-04 15:43:03 -0700111}
112
113type zipEntry struct {
114 fh *zip.FileHeader
115
116 // List of delayed io.Reader
117 futureReaders chan chan io.Reader
Jeff Gaston175f34c2017-08-17 21:43:21 -0700118
119 // Only used for passing into the MemoryRateLimiter to ensure we
120 // release as much memory as much as we request
121 allocatedSize int64
Colin Cross2fe66872015-03-30 17:20:39 -0700122}
123
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700124type ZipArgs struct {
125 FileArgs FileArgs
126 OutputFilePath string
127 CpuProfileFilePath string
128 TraceFilePath string
129 EmulateJar bool
130 AddDirectoryEntriesToZip bool
131 CompressionLevel int
132 ManifestSourcePath string
133 NumParallelJobs int
134 NonDeflatedFiles map[string]bool
Colin Crossf83c1502017-11-10 13:11:02 -0800135 WriteIfChanged bool
Colin Crossd59dab92018-09-21 15:12:39 -0700136 StoreSymlinks bool
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700137}
Colin Cross2fe66872015-03-30 17:20:39 -0700138
Nan Zhang674dd932018-01-26 18:30:36 -0800139const NOQUOTE = '\x00'
140
141func ReadRespFile(bytes []byte) []string {
142 var args []string
143 var arg []rune
144
145 isEscaping := false
146 quotingStart := NOQUOTE
147 for _, c := range string(bytes) {
148 switch {
149 case isEscaping:
150 if quotingStart == '"' {
151 if !(c == '"' || c == '\\') {
152 // '\"' or '\\' will be escaped under double quoting.
153 arg = append(arg, '\\')
154 }
155 }
156 arg = append(arg, c)
157 isEscaping = false
158 case c == '\\' && quotingStart != '\'':
159 isEscaping = true
160 case quotingStart == NOQUOTE && (c == '\'' || c == '"'):
161 quotingStart = c
162 case quotingStart != NOQUOTE && c == quotingStart:
163 quotingStart = NOQUOTE
164 case quotingStart == NOQUOTE && unicode.IsSpace(c):
165 // Current character is a space outside quotes
166 if len(arg) != 0 {
167 args = append(args, string(arg))
168 }
169 arg = arg[:0]
170 default:
171 arg = append(arg, c)
172 }
173 }
174
175 if len(arg) != 0 {
176 args = append(args, string(arg))
177 }
178
179 return args
180}
181
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700182func Run(args ZipArgs) (err error) {
183 if args.CpuProfileFilePath != "" {
184 f, err := os.Create(args.CpuProfileFilePath)
Dan Willemsen017d8932016-08-04 15:43:03 -0700185 if err != nil {
186 fmt.Fprintln(os.Stderr, err.Error())
187 os.Exit(1)
188 }
189 defer f.Close()
190 pprof.StartCPUProfile(f)
191 defer pprof.StopCPUProfile()
192 }
193
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700194 if args.TraceFilePath != "" {
195 f, err := os.Create(args.TraceFilePath)
Dan Willemsen017d8932016-08-04 15:43:03 -0700196 if err != nil {
197 fmt.Fprintln(os.Stderr, err.Error())
198 os.Exit(1)
199 }
200 defer f.Close()
201 err = trace.Start(f)
202 if err != nil {
203 fmt.Fprintln(os.Stderr, err.Error())
204 os.Exit(1)
205 }
206 defer trace.Stop()
207 }
208
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700209 if args.OutputFilePath == "" {
210 return fmt.Errorf("output file path must be nonempty")
Colin Cross2fe66872015-03-30 17:20:39 -0700211 }
212
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700213 if args.EmulateJar {
214 args.AddDirectoryEntriesToZip = true
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700215 }
216
Colin Crossd59dab92018-09-21 15:12:39 -0700217 // Have Glob follow symlinks if they are not being stored as symlinks in the zip file.
218 followSymlinks := pathtools.ShouldFollowSymlinks(!args.StoreSymlinks)
219
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700220 w := &ZipWriter{
Colin Crossd59dab92018-09-21 15:12:39 -0700221 time: jar.DefaultTime,
222 createdDirs: make(map[string]string),
223 createdFiles: make(map[string]string),
224 directories: args.AddDirectoryEntriesToZip,
225 compLevel: args.CompressionLevel,
226 followSymlinks: followSymlinks,
Colin Cross2fe66872015-03-30 17:20:39 -0700227 }
Nan Zhang9067b042017-03-17 14:04:43 -0700228 pathMappings := []pathMapping{}
Nan Zhang9067b042017-03-17 14:04:43 -0700229
Colin Crossd3216292018-09-14 15:06:31 -0700230 noCompression := args.CompressionLevel == 0
231
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700232 for _, fa := range args.FileArgs {
Colin Cross08e28ab2018-09-18 17:05:15 -0700233 var srcs []string
234 for _, s := range fa.SourceFiles {
Colin Crossd59dab92018-09-21 15:12:39 -0700235 globbed, _, err := pathtools.Glob(s, nil, followSymlinks)
Colin Cross08e28ab2018-09-18 17:05:15 -0700236 if err != nil {
237 return err
238 }
239 srcs = append(srcs, globbed...)
240 }
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700241 if fa.GlobDir != "" {
Colin Crossd59dab92018-09-21 15:12:39 -0700242 globbed, _, err := pathtools.Glob(filepath.Join(fa.GlobDir, "**/*"), nil, followSymlinks)
Colin Cross08e28ab2018-09-18 17:05:15 -0700243 if err != nil {
244 return err
245 }
246 srcs = append(srcs, globbed...)
Colin Cross7b10cf12017-08-30 14:12:21 -0700247 }
248 for _, src := range srcs {
Colin Crossb7c69112018-09-18 16:51:43 -0700249 err := fillPathPairs(fa, src, &pathMappings, args.NonDeflatedFiles, noCompression)
Colin Crossd3216292018-09-14 15:06:31 -0700250 if err != nil {
Nan Zhang9067b042017-03-17 14:04:43 -0700251 log.Fatal(err)
252 }
253 }
254 }
255
Colin Crossf83c1502017-11-10 13:11:02 -0800256 buf := &bytes.Buffer{}
257 var out io.Writer = buf
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700258
Colin Crossf83c1502017-11-10 13:11:02 -0800259 if !args.WriteIfChanged {
260 f, err := os.Create(args.OutputFilePath)
261 if err != nil {
262 return err
263 }
264
265 defer f.Close()
266 defer func() {
267 if err != nil {
268 os.Remove(args.OutputFilePath)
269 }
270 }()
271
272 out = f
273 }
274
275 err = w.write(out, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.NumParallelJobs)
276 if err != nil {
277 return err
278 }
279
280 if args.WriteIfChanged {
281 err := pathtools.WriteFileIfChanged(args.OutputFilePath, buf.Bytes(), 0666)
282 if err != nil {
283 return err
284 }
285 }
286
287 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700288}
289
Colin Crossb7c69112018-09-18 16:51:43 -0700290func fillPathPairs(fa FileArg, src string, pathMappings *[]pathMapping,
Colin Crossd3216292018-09-14 15:06:31 -0700291 nonDeflatedFiles map[string]bool, noCompression bool) error {
292
Nan Zhang9067b042017-03-17 14:04:43 -0700293 src = strings.TrimSpace(src)
294 if src == "" {
295 return nil
296 }
297 src = filepath.Clean(src)
Colin Crossb7c69112018-09-18 16:51:43 -0700298 var dest string
299
300 if fa.JunkPaths {
301 dest = filepath.Base(src)
302 } else {
303 var err error
304 dest, err = filepath.Rel(fa.SourcePrefixToStrip, src)
305 if err != nil {
306 return err
307 }
Nan Zhang9067b042017-03-17 14:04:43 -0700308 }
Colin Crossb7c69112018-09-18 16:51:43 -0700309 dest = filepath.Join(fa.PathPrefixInZip, dest)
Nan Zhang9067b042017-03-17 14:04:43 -0700310
Nan Zhangf281bd82017-04-25 16:47:45 -0700311 zipMethod := zip.Deflate
Colin Crossd3216292018-09-14 15:06:31 -0700312 if _, found := nonDeflatedFiles[dest]; found || noCompression {
Nan Zhangf281bd82017-04-25 16:47:45 -0700313 zipMethod = zip.Store
314 }
315 *pathMappings = append(*pathMappings,
316 pathMapping{dest: dest, src: src, zipMethod: zipMethod})
Nan Zhang9067b042017-03-17 14:04:43 -0700317
318 return nil
319}
320
Jeff Gastona2976952017-08-22 17:51:25 -0700321func jarSort(mappings []pathMapping) {
322 less := func(i int, j int) (smaller bool) {
323 return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest)
324 }
325 sort.SliceStable(mappings, less)
326}
327
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700328type readerSeekerCloser interface {
329 io.Reader
330 io.ReaderAt
331 io.Closer
332 io.Seeker
333}
334
Colin Crossf83c1502017-11-10 13:11:02 -0800335func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string, emulateJar bool, parallelJobs int) error {
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 Gastonc3bdc972017-10-12 12:18:19 -0700352 z.cpuRateLimiter = NewCPURateLimiter(int64(parallelJobs))
Jeff Gaston175f34c2017-08-17 21:43:21 -0700353 z.memoryRateLimiter = NewMemoryRateLimiter(0)
354 defer func() {
355 z.cpuRateLimiter.Stop()
356 z.memoryRateLimiter.Stop()
357 }()
Jeff Gastona2976952017-08-22 17:51:25 -0700358
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700359 if manifest != "" && !emulateJar {
Colin Cross635acc92017-09-12 22:50:46 -0700360 return errors.New("must specify --jar when specifying a manifest via -m")
Jeff Gastona2976952017-08-22 17:51:25 -0700361 }
362
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700363 if emulateJar {
Colin Cross635acc92017-09-12 22:50:46 -0700364 // manifest may be empty, in which case addManifest will fill in a default
365 pathMappings = append(pathMappings, pathMapping{jar.ManifestFile, manifest, zip.Deflate})
366
Jeff Gastona2976952017-08-22 17:51:25 -0700367 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 Gastonc3bdc972017-10-12 12:18:19 -0700375 if emulateJar && ele.dest == jar.ManifestFile {
Jeff Gastoncef50b92017-08-23 15:41:35 -0700376 err = z.addManifest(ele.dest, ele.src, ele.zipMethod)
377 } else {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700378 err = z.addFile(ele.dest, ele.src, ele.zipMethod, emulateJar)
Jeff Gastoncef50b92017-08-23 15:41:35 -0700379 }
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
Colin Crossf83c1502017-11-10 13:11:02 -0800421 var err error
Dan Willemsen017d8932016-08-04 15:43:03 -0700422 if op.fh.Method == zip.Deflate {
423 currentWriter, err = zipw.CreateCompressedHeader(op.fh)
424 } else {
425 var zw io.Writer
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700426
427 op.fh.CompressedSize64 = op.fh.UncompressedSize64
428
429 zw, err = zipw.CreateHeaderAndroid(op.fh)
Dan Willemsen017d8932016-08-04 15:43:03 -0700430 currentWriter = nopCloser{zw}
431 }
432 if err != nil {
433 return err
434 }
435
436 currentReaders = op.futureReaders
437 if op.futureReaders == nil {
438 currentWriter.Close()
439 currentWriter = nil
440 }
Jeff Gaston175f34c2017-08-17 21:43:21 -0700441 z.memoryRateLimiter.Finish(op.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700442
443 case futureReader, ok := <-readersChan:
444 if !ok {
445 // Done with reading
446 currentWriter.Close()
447 currentWriter = nil
448 currentReaders = nil
449 }
450
451 currentReader = futureReader
452
453 case reader := <-currentReader:
Colin Crossf83c1502017-11-10 13:11:02 -0800454 _, err := io.Copy(currentWriter, reader)
Dan Willemsen017d8932016-08-04 15:43:03 -0700455 if err != nil {
456 return err
457 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700458
459 currentReader = nil
460
Colin Crossf83c1502017-11-10 13:11:02 -0800461 case err := <-z.errors:
Colin Cross2fe66872015-03-30 17:20:39 -0700462 return err
463 }
464 }
465
Dan Willemsen017d8932016-08-04 15:43:03 -0700466 // One last chance to catch an error
467 select {
Colin Crossf83c1502017-11-10 13:11:02 -0800468 case err := <-z.errors:
Dan Willemsen017d8932016-08-04 15:43:03 -0700469 return err
470 default:
471 zipw.Close()
472 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700473 }
Colin Cross2fe66872015-03-30 17:20:39 -0700474}
475
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700476// imports (possibly with compression) <src> into the zip at sub-path <dest>
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700477func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar bool) error {
Dan Willemsen017d8932016-08-04 15:43:03 -0700478 var fileSize int64
Dan Willemsen10462b32017-03-15 19:02:51 -0700479 var executable bool
Dan Willemsen017d8932016-08-04 15:43:03 -0700480
Colin Crossd59dab92018-09-21 15:12:39 -0700481 var s os.FileInfo
482 var err error
483 if z.followSymlinks {
484 s, err = os.Stat(src)
485 } else {
486 s, err = os.Lstat(src)
487 }
488
489 if err != nil {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700490 return err
491 } else if s.IsDir() {
Colin Cross957cc4e2015-04-24 15:10:32 -0700492 if z.directories {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700493 return z.writeDirectory(dest, src, emulateJar)
Colin Cross957cc4e2015-04-24 15:10:32 -0700494 }
495 return nil
Dan Willemsen017d8932016-08-04 15:43:03 -0700496 } else {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700497 if err := z.writeDirectory(filepath.Dir(dest), src, emulateJar); err != nil {
Colin Crosse5580972017-08-30 17:40:21 -0700498 return err
499 }
500
501 if prev, exists := z.createdDirs[dest]; exists {
502 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
503 }
504 if prev, exists := z.createdFiles[dest]; exists {
505 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
506 }
507
508 z.createdFiles[dest] = src
509
510 if s.Mode()&os.ModeSymlink != 0 {
511 return z.writeSymlink(dest, src)
512 } else if !s.Mode().IsRegular() {
513 return fmt.Errorf("%s is not a file, directory, or symlink", src)
514 }
515
Dan Willemsen017d8932016-08-04 15:43:03 -0700516 fileSize = s.Size()
Dan Willemsen10462b32017-03-15 19:02:51 -0700517 executable = s.Mode()&0100 != 0
Colin Cross957cc4e2015-04-24 15:10:32 -0700518 }
519
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700520 r, err := os.Open(src)
521 if err != nil {
522 return err
523 }
524
525 header := &zip.FileHeader{
526 Name: dest,
527 Method: method,
528 UncompressedSize64: uint64(fileSize),
529 }
530
531 if executable {
532 header.SetMode(0700)
533 }
534
535 return z.writeFileContents(header, r)
536}
537
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700538func (z *ZipWriter) addManifest(dest string, src string, method uint16) error {
Colin Crosse5580972017-08-30 17:40:21 -0700539 if prev, exists := z.createdDirs[dest]; exists {
540 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
541 }
542 if prev, exists := z.createdFiles[dest]; exists {
543 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
544 }
545
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700546 if err := z.writeDirectory(filepath.Dir(dest), src, true); err != nil {
Colin Cross635acc92017-09-12 22:50:46 -0700547 return err
Jeff Gastoncef50b92017-08-23 15:41:35 -0700548 }
549
Colin Cross635acc92017-09-12 22:50:46 -0700550 fh, buf, err := jar.ManifestFileContents(src)
551 if err != nil {
552 return err
Jeff Gastoncef50b92017-08-23 15:41:35 -0700553 }
554
Colin Cross635acc92017-09-12 22:50:46 -0700555 reader := &byteReaderCloser{bytes.NewReader(buf), ioutil.NopCloser(nil)}
556
557 return z.writeFileContents(fh, reader)
Jeff Gastoncef50b92017-08-23 15:41:35 -0700558}
559
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700560func (z *ZipWriter) writeFileContents(header *zip.FileHeader, r readerSeekerCloser) (err error) {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700561
562 header.SetModTime(z.time)
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 Gastonc3bdc972017-10-12 12:18:19 -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 Gastonc3bdc972017-10-12 12:18:19 -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
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700666func (z *ZipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700667 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 Gastonc3bdc972017-10-12 12:18:19 -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
Colin Crosse5580972017-08-30 17:40:21 -0700771// writeDirectory annotates that dir is a directory created for the src file or directory, and adds
772// the directory entry to the zip file if directories are enabled.
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700773func (z *ZipWriter) writeDirectory(dir string, src string, emulateJar bool) error {
Jeff Gaston2d174132017-08-15 18:05:56 -0700774 // clean the input
Colin Crosse5580972017-08-30 17:40:21 -0700775 dir = filepath.Clean(dir)
Jeff Gaston2d174132017-08-15 18:05:56 -0700776
777 // discover any uncreated directories in the path
778 zipDirs := []string{}
Colin Crosse5580972017-08-30 17:40:21 -0700779 for dir != "" && dir != "." {
780 if _, exists := z.createdDirs[dir]; exists {
781 break
782 }
Jeff Gaston2d174132017-08-15 18:05:56 -0700783
Colin Crosse5580972017-08-30 17:40:21 -0700784 if prev, exists := z.createdFiles[dir]; exists {
785 return fmt.Errorf("destination %q is both a directory %q and a file %q", dir, src, prev)
786 }
787
788 z.createdDirs[dir] = src
Jeff Gaston2d174132017-08-15 18:05:56 -0700789 // parent directories precede their children
Colin Crosse5580972017-08-30 17:40:21 -0700790 zipDirs = append([]string{dir}, zipDirs...)
Jeff Gaston2d174132017-08-15 18:05:56 -0700791
Colin Crosse5580972017-08-30 17:40:21 -0700792 dir = filepath.Dir(dir)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700793 }
794
Colin Crosse5580972017-08-30 17:40:21 -0700795 if z.directories {
796 // make a directory entry for each uncreated directory
797 for _, cleanDir := range zipDirs {
Colin Cross635acc92017-09-12 22:50:46 -0700798 var dirHeader *zip.FileHeader
Colin Crosse19c7932015-04-24 15:08:38 -0700799
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700800 if emulateJar && cleanDir+"/" == jar.MetaDir {
Colin Cross635acc92017-09-12 22:50:46 -0700801 dirHeader = jar.MetaDirFileHeader()
802 } else {
803 dirHeader = &zip.FileHeader{
804 Name: cleanDir + "/",
805 }
806 dirHeader.SetMode(0700 | os.ModeDir)
Colin Crosse5580972017-08-30 17:40:21 -0700807 }
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700808
Colin Cross635acc92017-09-12 22:50:46 -0700809 dirHeader.SetModTime(z.time)
810
Colin Crosse5580972017-08-30 17:40:21 -0700811 ze := make(chan *zipEntry, 1)
812 ze <- &zipEntry{
813 fh: dirHeader,
814 }
815 close(ze)
816 z.writeOps <- ze
Colin Crosse19c7932015-04-24 15:08:38 -0700817 }
Colin Crosse19c7932015-04-24 15:08:38 -0700818 }
819
820 return nil
821}
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700822
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700823func (z *ZipWriter) writeSymlink(rel, file string) error {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700824 fileHeader := &zip.FileHeader{
825 Name: rel,
826 }
827 fileHeader.SetModTime(z.time)
Colin Cross297d9bc2018-06-22 16:37:47 -0700828 fileHeader.SetMode(0777 | os.ModeSymlink)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700829
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700830 dest, err := os.Readlink(file)
831 if err != nil {
832 return err
833 }
834
Colin Cross297d9bc2018-06-22 16:37:47 -0700835 fileHeader.UncompressedSize64 = uint64(len(dest))
836 fileHeader.CRC32 = crc32.ChecksumIEEE([]byte(dest))
837
Dan Willemsen017d8932016-08-04 15:43:03 -0700838 ze := make(chan *zipEntry, 1)
839 futureReaders := make(chan chan io.Reader, 1)
840 futureReader := make(chan io.Reader, 1)
841 futureReaders <- futureReader
842 close(futureReaders)
843 futureReader <- bytes.NewBufferString(dest)
844 close(futureReader)
845
Dan Willemsen017d8932016-08-04 15:43:03 -0700846 ze <- &zipEntry{
847 fh: fileHeader,
848 futureReaders: futureReaders,
849 }
850 close(ze)
851 z.writeOps <- ze
852
853 return nil
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700854}
Colin Cross7b10cf12017-08-30 14:12:21 -0700855
856func recursiveGlobFiles(path string) []string {
857 var files []string
858 filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
859 if !info.IsDir() {
860 files = append(files, path)
861 }
862 return nil
863 })
864
865 return files
866}