blob: 4a02531eb4f1e4fcfafc133bb4a0c7da0b76386d [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
110}
111
112type zipEntry struct {
113 fh *zip.FileHeader
114
115 // List of delayed io.Reader
116 futureReaders chan chan io.Reader
Jeff Gaston175f34c2017-08-17 21:43:21 -0700117
118 // Only used for passing into the MemoryRateLimiter to ensure we
119 // release as much memory as much as we request
120 allocatedSize int64
Colin Cross2fe66872015-03-30 17:20:39 -0700121}
122
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700123type ZipArgs struct {
124 FileArgs FileArgs
125 OutputFilePath string
126 CpuProfileFilePath string
127 TraceFilePath string
128 EmulateJar bool
129 AddDirectoryEntriesToZip bool
130 CompressionLevel int
131 ManifestSourcePath string
132 NumParallelJobs int
133 NonDeflatedFiles map[string]bool
Colin Crossf83c1502017-11-10 13:11:02 -0800134 WriteIfChanged bool
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700135}
Colin Cross2fe66872015-03-30 17:20:39 -0700136
Nan Zhang674dd932018-01-26 18:30:36 -0800137const NOQUOTE = '\x00'
138
139func ReadRespFile(bytes []byte) []string {
140 var args []string
141 var arg []rune
142
143 isEscaping := false
144 quotingStart := NOQUOTE
145 for _, c := range string(bytes) {
146 switch {
147 case isEscaping:
148 if quotingStart == '"' {
149 if !(c == '"' || c == '\\') {
150 // '\"' or '\\' will be escaped under double quoting.
151 arg = append(arg, '\\')
152 }
153 }
154 arg = append(arg, c)
155 isEscaping = false
156 case c == '\\' && quotingStart != '\'':
157 isEscaping = true
158 case quotingStart == NOQUOTE && (c == '\'' || c == '"'):
159 quotingStart = c
160 case quotingStart != NOQUOTE && c == quotingStart:
161 quotingStart = NOQUOTE
162 case quotingStart == NOQUOTE && unicode.IsSpace(c):
163 // Current character is a space outside quotes
164 if len(arg) != 0 {
165 args = append(args, string(arg))
166 }
167 arg = arg[:0]
168 default:
169 arg = append(arg, c)
170 }
171 }
172
173 if len(arg) != 0 {
174 args = append(args, string(arg))
175 }
176
177 return args
178}
179
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700180func Run(args ZipArgs) (err error) {
181 if args.CpuProfileFilePath != "" {
182 f, err := os.Create(args.CpuProfileFilePath)
Dan Willemsen017d8932016-08-04 15:43:03 -0700183 if err != nil {
184 fmt.Fprintln(os.Stderr, err.Error())
185 os.Exit(1)
186 }
187 defer f.Close()
188 pprof.StartCPUProfile(f)
189 defer pprof.StopCPUProfile()
190 }
191
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700192 if args.TraceFilePath != "" {
193 f, err := os.Create(args.TraceFilePath)
Dan Willemsen017d8932016-08-04 15:43:03 -0700194 if err != nil {
195 fmt.Fprintln(os.Stderr, err.Error())
196 os.Exit(1)
197 }
198 defer f.Close()
199 err = trace.Start(f)
200 if err != nil {
201 fmt.Fprintln(os.Stderr, err.Error())
202 os.Exit(1)
203 }
204 defer trace.Stop()
205 }
206
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700207 if args.OutputFilePath == "" {
208 return fmt.Errorf("output file path must be nonempty")
Colin Cross2fe66872015-03-30 17:20:39 -0700209 }
210
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700211 if args.EmulateJar {
212 args.AddDirectoryEntriesToZip = true
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700213 }
214
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700215 w := &ZipWriter{
Colin Crossc7feeff2018-09-26 21:36:44 +0000216 time: jar.DefaultTime,
217 createdDirs: make(map[string]string),
218 createdFiles: make(map[string]string),
219 directories: args.AddDirectoryEntriesToZip,
220 compLevel: args.CompressionLevel,
Colin Cross2fe66872015-03-30 17:20:39 -0700221 }
Nan Zhang9067b042017-03-17 14:04:43 -0700222 pathMappings := []pathMapping{}
Nan Zhang9067b042017-03-17 14:04:43 -0700223
Colin Crossd3216292018-09-14 15:06:31 -0700224 noCompression := args.CompressionLevel == 0
225
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700226 for _, fa := range args.FileArgs {
Colin Cross08e28ab2018-09-18 17:05:15 -0700227 var srcs []string
228 for _, s := range fa.SourceFiles {
Colin Crossc7feeff2018-09-26 21:36:44 +0000229 globbed, _, err := pathtools.Glob(s, nil, pathtools.DontFollowSymlinks)
Colin Cross08e28ab2018-09-18 17:05:15 -0700230 if err != nil {
231 return err
232 }
233 srcs = append(srcs, globbed...)
234 }
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700235 if fa.GlobDir != "" {
Colin Crossc7feeff2018-09-26 21:36:44 +0000236 globbed, _, err := pathtools.Glob(filepath.Join(fa.GlobDir, "**/*"), nil, pathtools.DontFollowSymlinks)
Colin Cross08e28ab2018-09-18 17:05:15 -0700237 if err != nil {
238 return err
239 }
240 srcs = append(srcs, globbed...)
Colin Cross7b10cf12017-08-30 14:12:21 -0700241 }
242 for _, src := range srcs {
Colin Crossb7c69112018-09-18 16:51:43 -0700243 err := fillPathPairs(fa, src, &pathMappings, args.NonDeflatedFiles, noCompression)
Colin Crossd3216292018-09-14 15:06:31 -0700244 if err != nil {
Nan Zhang9067b042017-03-17 14:04:43 -0700245 log.Fatal(err)
246 }
247 }
248 }
249
Colin Crossf83c1502017-11-10 13:11:02 -0800250 buf := &bytes.Buffer{}
251 var out io.Writer = buf
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700252
Colin Crossf83c1502017-11-10 13:11:02 -0800253 if !args.WriteIfChanged {
254 f, err := os.Create(args.OutputFilePath)
255 if err != nil {
256 return err
257 }
258
259 defer f.Close()
260 defer func() {
261 if err != nil {
262 os.Remove(args.OutputFilePath)
263 }
264 }()
265
266 out = f
267 }
268
269 err = w.write(out, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.NumParallelJobs)
270 if err != nil {
271 return err
272 }
273
274 if args.WriteIfChanged {
275 err := pathtools.WriteFileIfChanged(args.OutputFilePath, buf.Bytes(), 0666)
276 if err != nil {
277 return err
278 }
279 }
280
281 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700282}
283
Colin Crossb7c69112018-09-18 16:51:43 -0700284func fillPathPairs(fa FileArg, src string, pathMappings *[]pathMapping,
Colin Crossd3216292018-09-14 15:06:31 -0700285 nonDeflatedFiles map[string]bool, noCompression bool) error {
286
Nan Zhang9067b042017-03-17 14:04:43 -0700287 src = strings.TrimSpace(src)
288 if src == "" {
289 return nil
290 }
291 src = filepath.Clean(src)
Colin Crossb7c69112018-09-18 16:51:43 -0700292 var dest string
293
294 if fa.JunkPaths {
295 dest = filepath.Base(src)
296 } else {
297 var err error
298 dest, err = filepath.Rel(fa.SourcePrefixToStrip, src)
299 if err != nil {
300 return err
301 }
Nan Zhang9067b042017-03-17 14:04:43 -0700302 }
Colin Crossb7c69112018-09-18 16:51:43 -0700303 dest = filepath.Join(fa.PathPrefixInZip, dest)
Nan Zhang9067b042017-03-17 14:04:43 -0700304
Nan Zhangf281bd82017-04-25 16:47:45 -0700305 zipMethod := zip.Deflate
Colin Crossd3216292018-09-14 15:06:31 -0700306 if _, found := nonDeflatedFiles[dest]; found || noCompression {
Nan Zhangf281bd82017-04-25 16:47:45 -0700307 zipMethod = zip.Store
308 }
309 *pathMappings = append(*pathMappings,
310 pathMapping{dest: dest, src: src, zipMethod: zipMethod})
Nan Zhang9067b042017-03-17 14:04:43 -0700311
312 return nil
313}
314
Jeff Gastona2976952017-08-22 17:51:25 -0700315func jarSort(mappings []pathMapping) {
316 less := func(i int, j int) (smaller bool) {
317 return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest)
318 }
319 sort.SliceStable(mappings, less)
320}
321
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700322type readerSeekerCloser interface {
323 io.Reader
324 io.ReaderAt
325 io.Closer
326 io.Seeker
327}
328
Colin Crossf83c1502017-11-10 13:11:02 -0800329func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string, emulateJar bool, parallelJobs int) error {
Dan Willemsen017d8932016-08-04 15:43:03 -0700330 z.errors = make(chan error)
331 defer close(z.errors)
Colin Cross2fe66872015-03-30 17:20:39 -0700332
Dan Willemsen017d8932016-08-04 15:43:03 -0700333 // This channel size can be essentially unlimited -- it's used as a fifo
334 // queue decouple the CPU and IO loads. Directories don't require any
335 // compression time, but still cost some IO. Similar with small files that
336 // can be very fast to compress. Some files that are more difficult to
337 // compress won't take a corresponding longer time writing out.
338 //
339 // The optimum size here depends on your CPU and IO characteristics, and
340 // the the layout of your zip file. 1000 was chosen mostly at random as
341 // something that worked reasonably well for a test file.
342 //
343 // The RateLimit object will put the upper bounds on the number of
344 // parallel compressions and outstanding buffers.
345 z.writeOps = make(chan chan *zipEntry, 1000)
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700346 z.cpuRateLimiter = NewCPURateLimiter(int64(parallelJobs))
Jeff Gaston175f34c2017-08-17 21:43:21 -0700347 z.memoryRateLimiter = NewMemoryRateLimiter(0)
348 defer func() {
349 z.cpuRateLimiter.Stop()
350 z.memoryRateLimiter.Stop()
351 }()
Jeff Gastona2976952017-08-22 17:51:25 -0700352
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700353 if manifest != "" && !emulateJar {
Colin Cross635acc92017-09-12 22:50:46 -0700354 return errors.New("must specify --jar when specifying a manifest via -m")
Jeff Gastona2976952017-08-22 17:51:25 -0700355 }
356
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700357 if emulateJar {
Colin Cross635acc92017-09-12 22:50:46 -0700358 // manifest may be empty, in which case addManifest will fill in a default
359 pathMappings = append(pathMappings, pathMapping{jar.ManifestFile, manifest, zip.Deflate})
360
Jeff Gastona2976952017-08-22 17:51:25 -0700361 jarSort(pathMappings)
362 }
363
Dan Willemsen017d8932016-08-04 15:43:03 -0700364 go func() {
365 var err error
366 defer close(z.writeOps)
367
Nan Zhang9067b042017-03-17 14:04:43 -0700368 for _, ele := range pathMappings {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700369 if emulateJar && ele.dest == jar.ManifestFile {
Jeff Gastoncef50b92017-08-23 15:41:35 -0700370 err = z.addManifest(ele.dest, ele.src, ele.zipMethod)
371 } else {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700372 err = z.addFile(ele.dest, ele.src, ele.zipMethod, emulateJar)
Jeff Gastoncef50b92017-08-23 15:41:35 -0700373 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700374 if err != nil {
375 z.errors <- err
376 return
377 }
378 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700379 }()
380
381 zipw := zip.NewWriter(f)
382
383 var currentWriteOpChan chan *zipEntry
384 var currentWriter io.WriteCloser
385 var currentReaders chan chan io.Reader
386 var currentReader chan io.Reader
387 var done bool
388
389 for !done {
390 var writeOpsChan chan chan *zipEntry
391 var writeOpChan chan *zipEntry
392 var readersChan chan chan io.Reader
393
394 if currentReader != nil {
395 // Only read and process errors
396 } else if currentReaders != nil {
397 readersChan = currentReaders
398 } else if currentWriteOpChan != nil {
399 writeOpChan = currentWriteOpChan
400 } else {
401 writeOpsChan = z.writeOps
402 }
403
404 select {
405 case writeOp, ok := <-writeOpsChan:
406 if !ok {
407 done = true
408 }
409
410 currentWriteOpChan = writeOp
411
412 case op := <-writeOpChan:
413 currentWriteOpChan = nil
414
Colin Crossf83c1502017-11-10 13:11:02 -0800415 var err error
Dan Willemsen017d8932016-08-04 15:43:03 -0700416 if op.fh.Method == zip.Deflate {
417 currentWriter, err = zipw.CreateCompressedHeader(op.fh)
418 } else {
419 var zw io.Writer
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700420
421 op.fh.CompressedSize64 = op.fh.UncompressedSize64
422
423 zw, err = zipw.CreateHeaderAndroid(op.fh)
Dan Willemsen017d8932016-08-04 15:43:03 -0700424 currentWriter = nopCloser{zw}
425 }
426 if err != nil {
427 return err
428 }
429
430 currentReaders = op.futureReaders
431 if op.futureReaders == nil {
432 currentWriter.Close()
433 currentWriter = nil
434 }
Jeff Gaston175f34c2017-08-17 21:43:21 -0700435 z.memoryRateLimiter.Finish(op.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700436
437 case futureReader, ok := <-readersChan:
438 if !ok {
439 // Done with reading
440 currentWriter.Close()
441 currentWriter = nil
442 currentReaders = nil
443 }
444
445 currentReader = futureReader
446
447 case reader := <-currentReader:
Colin Crossf83c1502017-11-10 13:11:02 -0800448 _, err := io.Copy(currentWriter, reader)
Dan Willemsen017d8932016-08-04 15:43:03 -0700449 if err != nil {
450 return err
451 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700452
453 currentReader = nil
454
Colin Crossf83c1502017-11-10 13:11:02 -0800455 case err := <-z.errors:
Colin Cross2fe66872015-03-30 17:20:39 -0700456 return err
457 }
458 }
459
Dan Willemsen017d8932016-08-04 15:43:03 -0700460 // One last chance to catch an error
461 select {
Colin Crossf83c1502017-11-10 13:11:02 -0800462 case err := <-z.errors:
Dan Willemsen017d8932016-08-04 15:43:03 -0700463 return err
464 default:
465 zipw.Close()
466 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700467 }
Colin Cross2fe66872015-03-30 17:20:39 -0700468}
469
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700470// imports (possibly with compression) <src> into the zip at sub-path <dest>
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700471func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar bool) error {
Dan Willemsen017d8932016-08-04 15:43:03 -0700472 var fileSize int64
Dan Willemsen10462b32017-03-15 19:02:51 -0700473 var executable bool
Dan Willemsen017d8932016-08-04 15:43:03 -0700474
Colin Crossc7feeff2018-09-26 21:36:44 +0000475 if s, err := os.Lstat(src); err != nil {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700476 return err
477 } else if s.IsDir() {
Colin Cross957cc4e2015-04-24 15:10:32 -0700478 if z.directories {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700479 return z.writeDirectory(dest, src, emulateJar)
Colin Cross957cc4e2015-04-24 15:10:32 -0700480 }
481 return nil
Dan Willemsen017d8932016-08-04 15:43:03 -0700482 } else {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700483 if err := z.writeDirectory(filepath.Dir(dest), src, emulateJar); err != nil {
Colin Crosse5580972017-08-30 17:40:21 -0700484 return err
485 }
486
487 if prev, exists := z.createdDirs[dest]; exists {
488 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
489 }
490 if prev, exists := z.createdFiles[dest]; exists {
491 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
492 }
493
494 z.createdFiles[dest] = src
495
496 if s.Mode()&os.ModeSymlink != 0 {
497 return z.writeSymlink(dest, src)
498 } else if !s.Mode().IsRegular() {
499 return fmt.Errorf("%s is not a file, directory, or symlink", src)
500 }
501
Dan Willemsen017d8932016-08-04 15:43:03 -0700502 fileSize = s.Size()
Dan Willemsen10462b32017-03-15 19:02:51 -0700503 executable = s.Mode()&0100 != 0
Colin Cross957cc4e2015-04-24 15:10:32 -0700504 }
505
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700506 r, err := os.Open(src)
507 if err != nil {
508 return err
509 }
510
511 header := &zip.FileHeader{
512 Name: dest,
513 Method: method,
514 UncompressedSize64: uint64(fileSize),
515 }
516
517 if executable {
518 header.SetMode(0700)
519 }
520
521 return z.writeFileContents(header, r)
522}
523
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700524func (z *ZipWriter) addManifest(dest string, src string, method uint16) error {
Colin Crosse5580972017-08-30 17:40:21 -0700525 if prev, exists := z.createdDirs[dest]; exists {
526 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
527 }
528 if prev, exists := z.createdFiles[dest]; exists {
529 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
530 }
531
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700532 if err := z.writeDirectory(filepath.Dir(dest), src, true); err != nil {
Colin Cross635acc92017-09-12 22:50:46 -0700533 return err
Jeff Gastoncef50b92017-08-23 15:41:35 -0700534 }
535
Colin Cross635acc92017-09-12 22:50:46 -0700536 fh, buf, err := jar.ManifestFileContents(src)
537 if err != nil {
538 return err
Jeff Gastoncef50b92017-08-23 15:41:35 -0700539 }
540
Colin Cross635acc92017-09-12 22:50:46 -0700541 reader := &byteReaderCloser{bytes.NewReader(buf), ioutil.NopCloser(nil)}
542
543 return z.writeFileContents(fh, reader)
Jeff Gastoncef50b92017-08-23 15:41:35 -0700544}
545
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700546func (z *ZipWriter) writeFileContents(header *zip.FileHeader, r readerSeekerCloser) (err error) {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700547
548 header.SetModTime(z.time)
549
Dan Willemsen017d8932016-08-04 15:43:03 -0700550 compressChan := make(chan *zipEntry, 1)
551 z.writeOps <- compressChan
552
553 // Pre-fill a zipEntry, it will be sent in the compressChan once
554 // we're sure about the Method and CRC.
555 ze := &zipEntry{
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700556 fh: header,
Dan Willemsen10462b32017-03-15 19:02:51 -0700557 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700558
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700559 ze.allocatedSize = int64(header.UncompressedSize64)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700560 z.cpuRateLimiter.Request()
561 z.memoryRateLimiter.Request(ze.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700562
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700563 fileSize := int64(header.UncompressedSize64)
564 if fileSize == 0 {
565 fileSize = int64(header.UncompressedSize)
566 }
567
568 if header.Method == zip.Deflate && fileSize >= minParallelFileSize {
Dan Willemsen017d8932016-08-04 15:43:03 -0700569 wg := new(sync.WaitGroup)
570
571 // Allocate enough buffer to hold all readers. We'll limit
572 // this based on actual buffer sizes in RateLimit.
573 ze.futureReaders = make(chan chan io.Reader, (fileSize/parallelBlockSize)+1)
574
575 // Calculate the CRC in the background, since reading the entire
576 // file could take a while.
577 //
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700578 // We could split this up into chunks as well, but it's faster
Dan Willemsen017d8932016-08-04 15:43:03 -0700579 // than the compression. Due to the Go Zip API, we also need to
580 // know the result before we can begin writing the compressed
581 // data out to the zipfile.
582 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700583 go z.crcFile(r, ze, compressChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700584
585 for start := int64(0); start < fileSize; start += parallelBlockSize {
586 sr := io.NewSectionReader(r, start, parallelBlockSize)
587 resultChan := make(chan io.Reader, 1)
588 ze.futureReaders <- resultChan
589
Jeff Gaston175f34c2017-08-17 21:43:21 -0700590 z.cpuRateLimiter.Request()
Dan Willemsen017d8932016-08-04 15:43:03 -0700591
592 last := !(start+parallelBlockSize < fileSize)
593 var dict []byte
594 if start >= windowSize {
595 dict, err = ioutil.ReadAll(io.NewSectionReader(r, start-windowSize, windowSize))
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700596 if err != nil {
597 return err
598 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700599 }
600
601 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700602 go z.compressPartialFile(sr, dict, last, resultChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700603 }
604
605 close(ze.futureReaders)
606
607 // Close the file handle after all readers are done
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700608 go func(wg *sync.WaitGroup, closer io.Closer) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700609 wg.Wait()
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700610 closer.Close()
Dan Willemsen017d8932016-08-04 15:43:03 -0700611 }(wg, r)
612 } else {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700613 go func() {
614 z.compressWholeFile(ze, r, compressChan)
615 r.Close()
616 }()
Dan Willemsen017d8932016-08-04 15:43:03 -0700617 }
618
619 return nil
620}
621
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700622func (z *ZipWriter) crcFile(r io.Reader, ze *zipEntry, resultChan chan *zipEntry, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700623 defer wg.Done()
Jeff Gaston175f34c2017-08-17 21:43:21 -0700624 defer z.cpuRateLimiter.Finish()
Dan Willemsen017d8932016-08-04 15:43:03 -0700625
626 crc := crc32.NewIEEE()
627 _, err := io.Copy(crc, r)
628 if err != nil {
629 z.errors <- err
630 return
631 }
632
633 ze.fh.CRC32 = crc.Sum32()
634 resultChan <- ze
635 close(resultChan)
636}
637
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700638func (z *ZipWriter) compressPartialFile(r io.Reader, dict []byte, last bool, resultChan chan io.Reader, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700639 defer wg.Done()
640
641 result, err := z.compressBlock(r, dict, last)
642 if err != nil {
643 z.errors <- err
644 return
645 }
646
Jeff Gaston175f34c2017-08-17 21:43:21 -0700647 z.cpuRateLimiter.Finish()
648
Dan Willemsen017d8932016-08-04 15:43:03 -0700649 resultChan <- result
650}
651
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700652func (z *ZipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700653 buf := new(bytes.Buffer)
654 var fw *flate.Writer
655 var err error
656 if len(dict) > 0 {
657 // There's no way to Reset a Writer with a new dictionary, so
658 // don't use the Pool
659 fw, err = flate.NewWriterDict(buf, z.compLevel, dict)
660 } else {
661 var ok bool
662 if fw, ok = z.compressorPool.Get().(*flate.Writer); ok {
663 fw.Reset(buf)
664 } else {
665 fw, err = flate.NewWriter(buf, z.compLevel)
666 }
667 defer z.compressorPool.Put(fw)
668 }
669 if err != nil {
670 return nil, err
671 }
672
673 _, err = io.Copy(fw, r)
674 if err != nil {
675 return nil, err
676 }
677 if last {
678 fw.Close()
679 } else {
680 fw.Flush()
681 }
682
683 return buf, nil
684}
685
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700686func (z *ZipWriter) compressWholeFile(ze *zipEntry, r io.ReadSeeker, compressChan chan *zipEntry) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700687
Dan Willemsen017d8932016-08-04 15:43:03 -0700688 crc := crc32.NewIEEE()
Dan Willemsena8b55022017-03-15 21:49:26 -0700689 _, err := io.Copy(crc, r)
Colin Cross2fe66872015-03-30 17:20:39 -0700690 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700691 z.errors <- err
692 return
Colin Cross2fe66872015-03-30 17:20:39 -0700693 }
694
Dan Willemsena8b55022017-03-15 21:49:26 -0700695 ze.fh.CRC32 = crc.Sum32()
Colin Cross2fe66872015-03-30 17:20:39 -0700696
Dan Willemsen017d8932016-08-04 15:43:03 -0700697 _, err = r.Seek(0, 0)
Colin Cross2fe66872015-03-30 17:20:39 -0700698 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700699 z.errors <- err
700 return
Colin Cross2fe66872015-03-30 17:20:39 -0700701 }
702
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700703 readFile := func(reader io.ReadSeeker) ([]byte, error) {
704 _, err := reader.Seek(0, 0)
Nan Zhangf281bd82017-04-25 16:47:45 -0700705 if err != nil {
706 return nil, err
707 }
708
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700709 buf, err := ioutil.ReadAll(reader)
Nan Zhangf281bd82017-04-25 16:47:45 -0700710 if err != nil {
711 return nil, err
712 }
713
714 return buf, nil
715 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700716
Dan Willemsena8b55022017-03-15 21:49:26 -0700717 ze.futureReaders = make(chan chan io.Reader, 1)
Dan Willemsen017d8932016-08-04 15:43:03 -0700718 futureReader := make(chan io.Reader, 1)
719 ze.futureReaders <- futureReader
720 close(ze.futureReaders)
721
Nan Zhangf281bd82017-04-25 16:47:45 -0700722 if ze.fh.Method == zip.Deflate {
723 compressed, err := z.compressBlock(r, nil, true)
724 if err != nil {
725 z.errors <- err
726 return
727 }
728 if uint64(compressed.Len()) < ze.fh.UncompressedSize64 {
729 futureReader <- compressed
Nan Zhangf281bd82017-04-25 16:47:45 -0700730 } else {
731 buf, err := readFile(r)
732 if err != nil {
733 z.errors <- err
734 return
735 }
736 ze.fh.Method = zip.Store
737 futureReader <- bytes.NewReader(buf)
Nan Zhangf281bd82017-04-25 16:47:45 -0700738 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700739 } else {
Nan Zhangf281bd82017-04-25 16:47:45 -0700740 buf, err := readFile(r)
Dan Willemsen017d8932016-08-04 15:43:03 -0700741 if err != nil {
742 z.errors <- err
743 return
744 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700745 ze.fh.Method = zip.Store
746 futureReader <- bytes.NewReader(buf)
Dan Willemsen017d8932016-08-04 15:43:03 -0700747 }
Nan Zhangf281bd82017-04-25 16:47:45 -0700748
Jeff Gaston175f34c2017-08-17 21:43:21 -0700749 z.cpuRateLimiter.Finish()
750
Dan Willemsen017d8932016-08-04 15:43:03 -0700751 close(futureReader)
752
753 compressChan <- ze
754 close(compressChan)
Colin Cross2fe66872015-03-30 17:20:39 -0700755}
Colin Crosse19c7932015-04-24 15:08:38 -0700756
Colin Crosse5580972017-08-30 17:40:21 -0700757// writeDirectory annotates that dir is a directory created for the src file or directory, and adds
758// the directory entry to the zip file if directories are enabled.
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700759func (z *ZipWriter) writeDirectory(dir string, src string, emulateJar bool) error {
Jeff Gaston2d174132017-08-15 18:05:56 -0700760 // clean the input
Colin Crosse5580972017-08-30 17:40:21 -0700761 dir = filepath.Clean(dir)
Jeff Gaston2d174132017-08-15 18:05:56 -0700762
763 // discover any uncreated directories in the path
764 zipDirs := []string{}
Colin Crosse5580972017-08-30 17:40:21 -0700765 for dir != "" && dir != "." {
766 if _, exists := z.createdDirs[dir]; exists {
767 break
768 }
Jeff Gaston2d174132017-08-15 18:05:56 -0700769
Colin Crosse5580972017-08-30 17:40:21 -0700770 if prev, exists := z.createdFiles[dir]; exists {
771 return fmt.Errorf("destination %q is both a directory %q and a file %q", dir, src, prev)
772 }
773
774 z.createdDirs[dir] = src
Jeff Gaston2d174132017-08-15 18:05:56 -0700775 // parent directories precede their children
Colin Crosse5580972017-08-30 17:40:21 -0700776 zipDirs = append([]string{dir}, zipDirs...)
Jeff Gaston2d174132017-08-15 18:05:56 -0700777
Colin Crosse5580972017-08-30 17:40:21 -0700778 dir = filepath.Dir(dir)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700779 }
780
Colin Crosse5580972017-08-30 17:40:21 -0700781 if z.directories {
782 // make a directory entry for each uncreated directory
783 for _, cleanDir := range zipDirs {
Colin Cross635acc92017-09-12 22:50:46 -0700784 var dirHeader *zip.FileHeader
Colin Crosse19c7932015-04-24 15:08:38 -0700785
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700786 if emulateJar && cleanDir+"/" == jar.MetaDir {
Colin Cross635acc92017-09-12 22:50:46 -0700787 dirHeader = jar.MetaDirFileHeader()
788 } else {
789 dirHeader = &zip.FileHeader{
790 Name: cleanDir + "/",
791 }
792 dirHeader.SetMode(0700 | os.ModeDir)
Colin Crosse5580972017-08-30 17:40:21 -0700793 }
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700794
Colin Cross635acc92017-09-12 22:50:46 -0700795 dirHeader.SetModTime(z.time)
796
Colin Crosse5580972017-08-30 17:40:21 -0700797 ze := make(chan *zipEntry, 1)
798 ze <- &zipEntry{
799 fh: dirHeader,
800 }
801 close(ze)
802 z.writeOps <- ze
Colin Crosse19c7932015-04-24 15:08:38 -0700803 }
Colin Crosse19c7932015-04-24 15:08:38 -0700804 }
805
806 return nil
807}
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700808
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700809func (z *ZipWriter) writeSymlink(rel, file string) error {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700810 fileHeader := &zip.FileHeader{
811 Name: rel,
812 }
813 fileHeader.SetModTime(z.time)
Colin Cross297d9bc2018-06-22 16:37:47 -0700814 fileHeader.SetMode(0777 | os.ModeSymlink)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700815
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700816 dest, err := os.Readlink(file)
817 if err != nil {
818 return err
819 }
820
Colin Cross297d9bc2018-06-22 16:37:47 -0700821 fileHeader.UncompressedSize64 = uint64(len(dest))
822 fileHeader.CRC32 = crc32.ChecksumIEEE([]byte(dest))
823
Dan Willemsen017d8932016-08-04 15:43:03 -0700824 ze := make(chan *zipEntry, 1)
825 futureReaders := make(chan chan io.Reader, 1)
826 futureReader := make(chan io.Reader, 1)
827 futureReaders <- futureReader
828 close(futureReaders)
829 futureReader <- bytes.NewBufferString(dest)
830 close(futureReader)
831
Dan Willemsen017d8932016-08-04 15:43:03 -0700832 ze <- &zipEntry{
833 fh: fileHeader,
834 futureReaders: futureReaders,
835 }
836 close(ze)
837 z.writeOps <- ze
838
839 return nil
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700840}
Colin Cross7b10cf12017-08-30 14:12:21 -0700841
842func recursiveGlobFiles(path string) []string {
843 var files []string
844 filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
845 if !info.IsDir() {
846 files = append(files, path)
847 }
848 return nil
849 })
850
851 return files
852}