blob: 2bcc9a5f4e39298dd0a69d3cf7909541f0da773c [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
Dan Willemsen017d8932016-08-04 15:43:03 -070018 "bytes"
19 "compress/flate"
Jeff Gastona2976952017-08-22 17:51:25 -070020 "errors"
Colin Cross2fe66872015-03-30 17:20:39 -070021 "flag"
22 "fmt"
Dan Willemsen017d8932016-08-04 15:43:03 -070023 "hash/crc32"
Colin Cross2fe66872015-03-30 17:20:39 -070024 "io"
25 "io/ioutil"
Nan Zhang9067b042017-03-17 14:04:43 -070026 "log"
Colin Cross2fe66872015-03-30 17:20:39 -070027 "os"
28 "path/filepath"
Dan Willemsen017d8932016-08-04 15:43:03 -070029 "runtime"
30 "runtime/pprof"
31 "runtime/trace"
Jeff Gastona2976952017-08-22 17:51:25 -070032 "sort"
Colin Cross2fe66872015-03-30 17:20:39 -070033 "strings"
Dan Willemsen017d8932016-08-04 15:43:03 -070034 "sync"
Colin Cross2fe66872015-03-30 17:20:39 -070035 "time"
Dan Willemsen017d8932016-08-04 15:43:03 -070036
Jeff Gastona2976952017-08-22 17:51:25 -070037 "android/soong/jar"
Dan Willemsen017d8932016-08-04 15:43:03 -070038 "android/soong/third_party/zip"
Colin Cross2fe66872015-03-30 17:20:39 -070039)
40
Dan Willemsen017d8932016-08-04 15:43:03 -070041// Block size used during parallel compression of a single file.
42const parallelBlockSize = 1 * 1024 * 1024 // 1MB
43
44// Minimum file size to use parallel compression. It requires more
45// flate.Writer allocations, since we can't change the dictionary
46// during Reset
47const minParallelFileSize = parallelBlockSize * 6
48
49// Size of the ZIP compression window (32KB)
50const windowSize = 32 * 1024
51
52type nopCloser struct {
53 io.Writer
54}
55
56func (nopCloser) Close() error {
57 return nil
58}
59
Jeff Gastoncef50b92017-08-23 15:41:35 -070060type byteReaderCloser struct {
Colin Cross635acc92017-09-12 22:50:46 -070061 *bytes.Reader
Jeff Gastoncef50b92017-08-23 15:41:35 -070062 io.Closer
63}
64
Nan Zhang9067b042017-03-17 14:04:43 -070065type 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
Nan Zhangf281bd82017-04-25 16:47:45 -070086type file struct{}
87
88type listFiles struct{}
89
Colin Cross7b10cf12017-08-30 14:12:21 -070090type dir struct{}
91
Nan Zhangf281bd82017-04-25 16:47:45 -070092func (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 == "" {
Colin Cross7b10cf12017-08-30 14:12:21 -070098 return fmt.Errorf("must pass -C before -f")
Colin Cross2fe66872015-03-30 17:20:39 -070099 }
100
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700101 fArgs = append(fArgs, FileArg{
102 PathPrefixInZip: filepath.Clean(*rootPrefix),
103 SourcePrefixToStrip: filepath.Clean(*relativeRoot),
104 SourceFiles: []string{s},
Nan Zhangf281bd82017-04-25 16:47:45 -0700105 })
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 == "" {
Colin Cross7b10cf12017-08-30 14:12:21 -0700116 return fmt.Errorf("must pass -C before -l")
Nan Zhangf281bd82017-04-25 16:47:45 -0700117 }
118
119 list, err := ioutil.ReadFile(s)
120 if err != nil {
121 return err
122 }
123
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700124 fArgs = append(fArgs, FileArg{
125 PathPrefixInZip: filepath.Clean(*rootPrefix),
126 SourcePrefixToStrip: filepath.Clean(*relativeRoot),
127 SourceFiles: strings.Split(string(list), "\n"),
Nan Zhangf281bd82017-04-25 16:47:45 -0700128 })
129
130 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700131}
132
Colin Cross7b10cf12017-08-30 14:12:21 -0700133func (d *dir) String() string {
134 return `""`
135}
136
137func (d *dir) Set(s string) error {
138 if *relativeRoot == "" {
139 return fmt.Errorf("must pass -C before -D")
140 }
141
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700142 fArgs = append(fArgs, FileArg{
143 PathPrefixInZip: filepath.Clean(*rootPrefix),
144 SourcePrefixToStrip: filepath.Clean(*relativeRoot),
145 GlobDir: filepath.Clean(s),
Colin Cross7b10cf12017-08-30 14:12:21 -0700146 })
147
148 return nil
149}
150
Colin Cross2fe66872015-03-30 17:20:39 -0700151var (
Dan Willemsen47ec28f2016-08-10 16:12:30 -0700152 out = flag.String("o", "", "file to write zip file to")
153 manifest = flag.String("m", "", "input jar manifest file name")
154 directories = flag.Bool("d", false, "include directories in zip")
Nan Zhang9067b042017-03-17 14:04:43 -0700155 rootPrefix = flag.String("P", "", "path prefix within the zip at which to place files")
Colin Cross7b10cf12017-08-30 14:12:21 -0700156 relativeRoot = flag.String("C", "", "path to use as relative root of files in following -f, -l, or -D arguments")
Dan Willemsen017d8932016-08-04 15:43:03 -0700157 parallelJobs = flag.Int("j", runtime.NumCPU(), "number of parallel threads to use")
158 compLevel = flag.Int("L", 5, "deflate compression level (0-9)")
Jeff Gastona2976952017-08-22 17:51:25 -0700159 emulateJar = flag.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'")
Nan Zhang9067b042017-03-17 14:04:43 -0700160
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700161 fArgs FileArgs
Nan Zhangf281bd82017-04-25 16:47:45 -0700162 nonDeflatedFiles = make(uniqueSet)
Dan Willemsen017d8932016-08-04 15:43:03 -0700163
164 cpuProfile = flag.String("cpuprofile", "", "write cpu profile to file")
165 traceFile = flag.String("trace", "", "write trace to file")
Colin Cross2fe66872015-03-30 17:20:39 -0700166)
167
168func init() {
Nan Zhangf281bd82017-04-25 16:47:45 -0700169 flag.Var(&listFiles{}, "l", "file containing list of .class files")
Colin Cross7b10cf12017-08-30 14:12:21 -0700170 flag.Var(&dir{}, "D", "directory to include in zip")
Nan Zhangf281bd82017-04-25 16:47:45 -0700171 flag.Var(&file{}, "f", "file to include in zip")
172 flag.Var(&nonDeflatedFiles, "s", "file path to be stored within the zip without compression")
Colin Cross2fe66872015-03-30 17:20:39 -0700173}
174
175func usage() {
Dan Willemsen47ec28f2016-08-10 16:12:30 -0700176 fmt.Fprintf(os.Stderr, "usage: soong_zip -o zipfile [-m manifest] -C dir [-f|-l file]...\n")
Colin Cross2fe66872015-03-30 17:20:39 -0700177 flag.PrintDefaults()
178 os.Exit(2)
179}
180
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700181func main() {
182 flag.Parse()
183
184 err := Run(ZipArgs{
185 FileArgs: fArgs,
186 OutputFilePath: *out,
187 CpuProfileFilePath: *cpuProfile,
188 TraceFilePath: *traceFile,
189 EmulateJar: *emulateJar,
190 AddDirectoryEntriesToZip: *directories,
191 CompressionLevel: *compLevel,
192 ManifestSourcePath: *manifest,
193 NumParallelJobs: *parallelJobs,
194 NonDeflatedFiles: nonDeflatedFiles,
195 })
196 if err != nil {
197 fmt.Fprintln(os.Stderr, err.Error())
198 os.Exit(1)
199 }
200}
201
202type FileArg struct {
203 PathPrefixInZip, SourcePrefixToStrip string
204 SourceFiles []string
205 GlobDir string
206}
207
208type FileArgs []FileArg
209
210type ZipWriter struct {
Colin Crosse5580972017-08-30 17:40:21 -0700211 time time.Time
212 createdFiles map[string]string
213 createdDirs map[string]string
214 directories bool
Colin Crosse19c7932015-04-24 15:08:38 -0700215
Dan Willemsen017d8932016-08-04 15:43:03 -0700216 errors chan error
217 writeOps chan chan *zipEntry
218
Jeff Gaston175f34c2017-08-17 21:43:21 -0700219 cpuRateLimiter *CPURateLimiter
220 memoryRateLimiter *MemoryRateLimiter
Dan Willemsen017d8932016-08-04 15:43:03 -0700221
222 compressorPool sync.Pool
223 compLevel int
224}
225
226type zipEntry struct {
227 fh *zip.FileHeader
228
229 // List of delayed io.Reader
230 futureReaders chan chan io.Reader
Jeff Gaston175f34c2017-08-17 21:43:21 -0700231
232 // Only used for passing into the MemoryRateLimiter to ensure we
233 // release as much memory as much as we request
234 allocatedSize int64
Colin Cross2fe66872015-03-30 17:20:39 -0700235}
236
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700237type ZipArgs struct {
238 FileArgs FileArgs
239 OutputFilePath string
240 CpuProfileFilePath string
241 TraceFilePath string
242 EmulateJar bool
243 AddDirectoryEntriesToZip bool
244 CompressionLevel int
245 ManifestSourcePath string
246 NumParallelJobs int
247 NonDeflatedFiles map[string]bool
248}
Colin Cross2fe66872015-03-30 17:20:39 -0700249
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700250func Run(args ZipArgs) (err error) {
251 if args.CpuProfileFilePath != "" {
252 f, err := os.Create(args.CpuProfileFilePath)
Dan Willemsen017d8932016-08-04 15:43:03 -0700253 if err != nil {
254 fmt.Fprintln(os.Stderr, err.Error())
255 os.Exit(1)
256 }
257 defer f.Close()
258 pprof.StartCPUProfile(f)
259 defer pprof.StopCPUProfile()
260 }
261
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700262 if args.TraceFilePath != "" {
263 f, err := os.Create(args.TraceFilePath)
Dan Willemsen017d8932016-08-04 15:43:03 -0700264 if err != nil {
265 fmt.Fprintln(os.Stderr, err.Error())
266 os.Exit(1)
267 }
268 defer f.Close()
269 err = trace.Start(f)
270 if err != nil {
271 fmt.Fprintln(os.Stderr, err.Error())
272 os.Exit(1)
273 }
274 defer trace.Stop()
275 }
276
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700277 if args.OutputFilePath == "" {
278 return fmt.Errorf("output file path must be nonempty")
Colin Cross2fe66872015-03-30 17:20:39 -0700279 }
280
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700281 if args.EmulateJar {
282 args.AddDirectoryEntriesToZip = true
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700283 }
284
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700285 w := &ZipWriter{
Colin Cross635acc92017-09-12 22:50:46 -0700286 time: jar.DefaultTime,
Colin Crosse5580972017-08-30 17:40:21 -0700287 createdDirs: make(map[string]string),
288 createdFiles: make(map[string]string),
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700289 directories: args.AddDirectoryEntriesToZip,
290 compLevel: args.CompressionLevel,
Colin Cross2fe66872015-03-30 17:20:39 -0700291 }
Nan Zhang9067b042017-03-17 14:04:43 -0700292 pathMappings := []pathMapping{}
Nan Zhang9067b042017-03-17 14:04:43 -0700293
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700294 for _, fa := range args.FileArgs {
295 srcs := fa.SourceFiles
296 if fa.GlobDir != "" {
297 srcs = append(srcs, recursiveGlobFiles(fa.GlobDir)...)
Colin Cross7b10cf12017-08-30 14:12:21 -0700298 }
299 for _, src := range srcs {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700300 if err := fillPathPairs(fa.PathPrefixInZip,
301 fa.SourcePrefixToStrip, src, &pathMappings, args.NonDeflatedFiles); err != nil {
Nan Zhang9067b042017-03-17 14:04:43 -0700302 log.Fatal(err)
303 }
304 }
305 }
306
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700307 return w.write(args.OutputFilePath, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.NumParallelJobs)
308
Colin Cross2fe66872015-03-30 17:20:39 -0700309}
310
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700311func fillPathPairs(prefix, rel, src string, pathMappings *[]pathMapping, nonDeflatedFiles map[string]bool) error {
Nan Zhang9067b042017-03-17 14:04:43 -0700312 src = strings.TrimSpace(src)
313 if src == "" {
314 return nil
315 }
316 src = filepath.Clean(src)
317 dest, err := filepath.Rel(rel, src)
318 if err != nil {
319 return err
320 }
321 dest = filepath.Join(prefix, dest)
322
Nan Zhangf281bd82017-04-25 16:47:45 -0700323 zipMethod := zip.Deflate
324 if _, found := nonDeflatedFiles[dest]; found {
325 zipMethod = zip.Store
326 }
327 *pathMappings = append(*pathMappings,
328 pathMapping{dest: dest, src: src, zipMethod: zipMethod})
Nan Zhang9067b042017-03-17 14:04:43 -0700329
330 return nil
331}
332
Jeff Gastona2976952017-08-22 17:51:25 -0700333func jarSort(mappings []pathMapping) {
334 less := func(i int, j int) (smaller bool) {
335 return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest)
336 }
337 sort.SliceStable(mappings, less)
338}
339
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700340type readerSeekerCloser interface {
341 io.Reader
342 io.ReaderAt
343 io.Closer
344 io.Seeker
345}
346
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700347func (z *ZipWriter) write(out string, pathMappings []pathMapping, manifest string, emulateJar bool, parallelJobs int) error {
Colin Cross2fe66872015-03-30 17:20:39 -0700348 f, err := os.Create(out)
349 if err != nil {
350 return err
351 }
352
353 defer f.Close()
354 defer func() {
355 if err != nil {
356 os.Remove(out)
357 }
358 }()
359
Dan Willemsen017d8932016-08-04 15:43:03 -0700360 z.errors = make(chan error)
361 defer close(z.errors)
Colin Cross2fe66872015-03-30 17:20:39 -0700362
Dan Willemsen017d8932016-08-04 15:43:03 -0700363 // This channel size can be essentially unlimited -- it's used as a fifo
364 // queue decouple the CPU and IO loads. Directories don't require any
365 // compression time, but still cost some IO. Similar with small files that
366 // can be very fast to compress. Some files that are more difficult to
367 // compress won't take a corresponding longer time writing out.
368 //
369 // The optimum size here depends on your CPU and IO characteristics, and
370 // the the layout of your zip file. 1000 was chosen mostly at random as
371 // something that worked reasonably well for a test file.
372 //
373 // The RateLimit object will put the upper bounds on the number of
374 // parallel compressions and outstanding buffers.
375 z.writeOps = make(chan chan *zipEntry, 1000)
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700376 z.cpuRateLimiter = NewCPURateLimiter(int64(parallelJobs))
Jeff Gaston175f34c2017-08-17 21:43:21 -0700377 z.memoryRateLimiter = NewMemoryRateLimiter(0)
378 defer func() {
379 z.cpuRateLimiter.Stop()
380 z.memoryRateLimiter.Stop()
381 }()
Jeff Gastona2976952017-08-22 17:51:25 -0700382
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700383 if manifest != "" && !emulateJar {
Colin Cross635acc92017-09-12 22:50:46 -0700384 return errors.New("must specify --jar when specifying a manifest via -m")
Jeff Gastona2976952017-08-22 17:51:25 -0700385 }
386
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700387 if emulateJar {
Colin Cross635acc92017-09-12 22:50:46 -0700388 // manifest may be empty, in which case addManifest will fill in a default
389 pathMappings = append(pathMappings, pathMapping{jar.ManifestFile, manifest, zip.Deflate})
390
Jeff Gastona2976952017-08-22 17:51:25 -0700391 jarSort(pathMappings)
392 }
393
Dan Willemsen017d8932016-08-04 15:43:03 -0700394 go func() {
395 var err error
396 defer close(z.writeOps)
397
Nan Zhang9067b042017-03-17 14:04:43 -0700398 for _, ele := range pathMappings {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700399 if emulateJar && ele.dest == jar.ManifestFile {
Jeff Gastoncef50b92017-08-23 15:41:35 -0700400 err = z.addManifest(ele.dest, ele.src, ele.zipMethod)
401 } else {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700402 err = z.addFile(ele.dest, ele.src, ele.zipMethod, emulateJar)
Jeff Gastoncef50b92017-08-23 15:41:35 -0700403 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700404 if err != nil {
405 z.errors <- err
406 return
407 }
408 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700409 }()
410
411 zipw := zip.NewWriter(f)
412
413 var currentWriteOpChan chan *zipEntry
414 var currentWriter io.WriteCloser
415 var currentReaders chan chan io.Reader
416 var currentReader chan io.Reader
417 var done bool
418
419 for !done {
420 var writeOpsChan chan chan *zipEntry
421 var writeOpChan chan *zipEntry
422 var readersChan chan chan io.Reader
423
424 if currentReader != nil {
425 // Only read and process errors
426 } else if currentReaders != nil {
427 readersChan = currentReaders
428 } else if currentWriteOpChan != nil {
429 writeOpChan = currentWriteOpChan
430 } else {
431 writeOpsChan = z.writeOps
432 }
433
434 select {
435 case writeOp, ok := <-writeOpsChan:
436 if !ok {
437 done = true
438 }
439
440 currentWriteOpChan = writeOp
441
442 case op := <-writeOpChan:
443 currentWriteOpChan = nil
444
445 if op.fh.Method == zip.Deflate {
446 currentWriter, err = zipw.CreateCompressedHeader(op.fh)
447 } else {
448 var zw io.Writer
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700449
450 op.fh.CompressedSize64 = op.fh.UncompressedSize64
451
452 zw, err = zipw.CreateHeaderAndroid(op.fh)
Dan Willemsen017d8932016-08-04 15:43:03 -0700453 currentWriter = nopCloser{zw}
454 }
455 if err != nil {
456 return err
457 }
458
459 currentReaders = op.futureReaders
460 if op.futureReaders == nil {
461 currentWriter.Close()
462 currentWriter = nil
463 }
Jeff Gaston175f34c2017-08-17 21:43:21 -0700464 z.memoryRateLimiter.Finish(op.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700465
466 case futureReader, ok := <-readersChan:
467 if !ok {
468 // Done with reading
469 currentWriter.Close()
470 currentWriter = nil
471 currentReaders = nil
472 }
473
474 currentReader = futureReader
475
476 case reader := <-currentReader:
Jeff Gaston175f34c2017-08-17 21:43:21 -0700477 _, err = io.Copy(currentWriter, reader)
Dan Willemsen017d8932016-08-04 15:43:03 -0700478 if err != nil {
479 return err
480 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700481
482 currentReader = nil
483
484 case err = <-z.errors:
Colin Cross2fe66872015-03-30 17:20:39 -0700485 return err
486 }
487 }
488
Dan Willemsen017d8932016-08-04 15:43:03 -0700489 // One last chance to catch an error
490 select {
491 case err = <-z.errors:
492 return err
493 default:
494 zipw.Close()
495 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700496 }
Colin Cross2fe66872015-03-30 17:20:39 -0700497}
498
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700499// imports (possibly with compression) <src> into the zip at sub-path <dest>
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700500func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar bool) error {
Dan Willemsen017d8932016-08-04 15:43:03 -0700501 var fileSize int64
Dan Willemsen10462b32017-03-15 19:02:51 -0700502 var executable bool
Dan Willemsen017d8932016-08-04 15:43:03 -0700503
Nan Zhang9067b042017-03-17 14:04:43 -0700504 if s, err := os.Lstat(src); err != nil {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700505 return err
506 } else if s.IsDir() {
Colin Cross957cc4e2015-04-24 15:10:32 -0700507 if z.directories {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700508 return z.writeDirectory(dest, src, emulateJar)
Colin Cross957cc4e2015-04-24 15:10:32 -0700509 }
510 return nil
Dan Willemsen017d8932016-08-04 15:43:03 -0700511 } else {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700512 if err := z.writeDirectory(filepath.Dir(dest), src, emulateJar); err != nil {
Colin Crosse5580972017-08-30 17:40:21 -0700513 return err
514 }
515
516 if prev, exists := z.createdDirs[dest]; exists {
517 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
518 }
519 if prev, exists := z.createdFiles[dest]; exists {
520 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
521 }
522
523 z.createdFiles[dest] = src
524
525 if s.Mode()&os.ModeSymlink != 0 {
526 return z.writeSymlink(dest, src)
527 } else if !s.Mode().IsRegular() {
528 return fmt.Errorf("%s is not a file, directory, or symlink", src)
529 }
530
Dan Willemsen017d8932016-08-04 15:43:03 -0700531 fileSize = s.Size()
Dan Willemsen10462b32017-03-15 19:02:51 -0700532 executable = s.Mode()&0100 != 0
Colin Cross957cc4e2015-04-24 15:10:32 -0700533 }
534
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700535 r, err := os.Open(src)
536 if err != nil {
537 return err
538 }
539
540 header := &zip.FileHeader{
541 Name: dest,
542 Method: method,
543 UncompressedSize64: uint64(fileSize),
544 }
545
546 if executable {
547 header.SetMode(0700)
548 }
549
550 return z.writeFileContents(header, r)
551}
552
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700553func (z *ZipWriter) addManifest(dest string, src string, method uint16) error {
Colin Crosse5580972017-08-30 17:40:21 -0700554 if prev, exists := z.createdDirs[dest]; exists {
555 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
556 }
557 if prev, exists := z.createdFiles[dest]; exists {
558 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
559 }
560
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700561 if err := z.writeDirectory(filepath.Dir(dest), src, true); err != nil {
Colin Cross635acc92017-09-12 22:50:46 -0700562 return err
Jeff Gastoncef50b92017-08-23 15:41:35 -0700563 }
564
Colin Cross635acc92017-09-12 22:50:46 -0700565 fh, buf, err := jar.ManifestFileContents(src)
566 if err != nil {
567 return err
Jeff Gastoncef50b92017-08-23 15:41:35 -0700568 }
569
Colin Cross635acc92017-09-12 22:50:46 -0700570 reader := &byteReaderCloser{bytes.NewReader(buf), ioutil.NopCloser(nil)}
571
572 return z.writeFileContents(fh, reader)
Jeff Gastoncef50b92017-08-23 15:41:35 -0700573}
574
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700575func (z *ZipWriter) writeFileContents(header *zip.FileHeader, r readerSeekerCloser) (err error) {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700576
577 header.SetModTime(z.time)
578
Dan Willemsen017d8932016-08-04 15:43:03 -0700579 compressChan := make(chan *zipEntry, 1)
580 z.writeOps <- compressChan
581
582 // Pre-fill a zipEntry, it will be sent in the compressChan once
583 // we're sure about the Method and CRC.
584 ze := &zipEntry{
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700585 fh: header,
Dan Willemsen10462b32017-03-15 19:02:51 -0700586 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700587
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700588 ze.allocatedSize = int64(header.UncompressedSize64)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700589 z.cpuRateLimiter.Request()
590 z.memoryRateLimiter.Request(ze.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700591
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700592 fileSize := int64(header.UncompressedSize64)
593 if fileSize == 0 {
594 fileSize = int64(header.UncompressedSize)
595 }
596
597 if header.Method == zip.Deflate && fileSize >= minParallelFileSize {
Dan Willemsen017d8932016-08-04 15:43:03 -0700598 wg := new(sync.WaitGroup)
599
600 // Allocate enough buffer to hold all readers. We'll limit
601 // this based on actual buffer sizes in RateLimit.
602 ze.futureReaders = make(chan chan io.Reader, (fileSize/parallelBlockSize)+1)
603
604 // Calculate the CRC in the background, since reading the entire
605 // file could take a while.
606 //
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700607 // We could split this up into chunks as well, but it's faster
Dan Willemsen017d8932016-08-04 15:43:03 -0700608 // than the compression. Due to the Go Zip API, we also need to
609 // know the result before we can begin writing the compressed
610 // data out to the zipfile.
611 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700612 go z.crcFile(r, ze, compressChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700613
614 for start := int64(0); start < fileSize; start += parallelBlockSize {
615 sr := io.NewSectionReader(r, start, parallelBlockSize)
616 resultChan := make(chan io.Reader, 1)
617 ze.futureReaders <- resultChan
618
Jeff Gaston175f34c2017-08-17 21:43:21 -0700619 z.cpuRateLimiter.Request()
Dan Willemsen017d8932016-08-04 15:43:03 -0700620
621 last := !(start+parallelBlockSize < fileSize)
622 var dict []byte
623 if start >= windowSize {
624 dict, err = ioutil.ReadAll(io.NewSectionReader(r, start-windowSize, windowSize))
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700625 if err != nil {
626 return err
627 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700628 }
629
630 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700631 go z.compressPartialFile(sr, dict, last, resultChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700632 }
633
634 close(ze.futureReaders)
635
636 // Close the file handle after all readers are done
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700637 go func(wg *sync.WaitGroup, closer io.Closer) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700638 wg.Wait()
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700639 closer.Close()
Dan Willemsen017d8932016-08-04 15:43:03 -0700640 }(wg, r)
641 } else {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700642 go func() {
643 z.compressWholeFile(ze, r, compressChan)
644 r.Close()
645 }()
Dan Willemsen017d8932016-08-04 15:43:03 -0700646 }
647
648 return nil
649}
650
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700651func (z *ZipWriter) crcFile(r io.Reader, ze *zipEntry, resultChan chan *zipEntry, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700652 defer wg.Done()
Jeff Gaston175f34c2017-08-17 21:43:21 -0700653 defer z.cpuRateLimiter.Finish()
Dan Willemsen017d8932016-08-04 15:43:03 -0700654
655 crc := crc32.NewIEEE()
656 _, err := io.Copy(crc, r)
657 if err != nil {
658 z.errors <- err
659 return
660 }
661
662 ze.fh.CRC32 = crc.Sum32()
663 resultChan <- ze
664 close(resultChan)
665}
666
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700667func (z *ZipWriter) compressPartialFile(r io.Reader, dict []byte, last bool, resultChan chan io.Reader, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700668 defer wg.Done()
669
670 result, err := z.compressBlock(r, dict, last)
671 if err != nil {
672 z.errors <- err
673 return
674 }
675
Jeff Gaston175f34c2017-08-17 21:43:21 -0700676 z.cpuRateLimiter.Finish()
677
Dan Willemsen017d8932016-08-04 15:43:03 -0700678 resultChan <- result
679}
680
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700681func (z *ZipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700682 buf := new(bytes.Buffer)
683 var fw *flate.Writer
684 var err error
685 if len(dict) > 0 {
686 // There's no way to Reset a Writer with a new dictionary, so
687 // don't use the Pool
688 fw, err = flate.NewWriterDict(buf, z.compLevel, dict)
689 } else {
690 var ok bool
691 if fw, ok = z.compressorPool.Get().(*flate.Writer); ok {
692 fw.Reset(buf)
693 } else {
694 fw, err = flate.NewWriter(buf, z.compLevel)
695 }
696 defer z.compressorPool.Put(fw)
697 }
698 if err != nil {
699 return nil, err
700 }
701
702 _, err = io.Copy(fw, r)
703 if err != nil {
704 return nil, err
705 }
706 if last {
707 fw.Close()
708 } else {
709 fw.Flush()
710 }
711
712 return buf, nil
713}
714
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700715func (z *ZipWriter) compressWholeFile(ze *zipEntry, r io.ReadSeeker, compressChan chan *zipEntry) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700716
Dan Willemsen017d8932016-08-04 15:43:03 -0700717 crc := crc32.NewIEEE()
Dan Willemsena8b55022017-03-15 21:49:26 -0700718 _, err := io.Copy(crc, r)
Colin Cross2fe66872015-03-30 17:20:39 -0700719 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700720 z.errors <- err
721 return
Colin Cross2fe66872015-03-30 17:20:39 -0700722 }
723
Dan Willemsena8b55022017-03-15 21:49:26 -0700724 ze.fh.CRC32 = crc.Sum32()
Colin Cross2fe66872015-03-30 17:20:39 -0700725
Dan Willemsen017d8932016-08-04 15:43:03 -0700726 _, err = r.Seek(0, 0)
Colin Cross2fe66872015-03-30 17:20:39 -0700727 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700728 z.errors <- err
729 return
Colin Cross2fe66872015-03-30 17:20:39 -0700730 }
731
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700732 readFile := func(reader io.ReadSeeker) ([]byte, error) {
733 _, err := reader.Seek(0, 0)
Nan Zhangf281bd82017-04-25 16:47:45 -0700734 if err != nil {
735 return nil, err
736 }
737
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700738 buf, err := ioutil.ReadAll(reader)
Nan Zhangf281bd82017-04-25 16:47:45 -0700739 if err != nil {
740 return nil, err
741 }
742
743 return buf, nil
744 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700745
Dan Willemsena8b55022017-03-15 21:49:26 -0700746 ze.futureReaders = make(chan chan io.Reader, 1)
Dan Willemsen017d8932016-08-04 15:43:03 -0700747 futureReader := make(chan io.Reader, 1)
748 ze.futureReaders <- futureReader
749 close(ze.futureReaders)
750
Nan Zhangf281bd82017-04-25 16:47:45 -0700751 if ze.fh.Method == zip.Deflate {
752 compressed, err := z.compressBlock(r, nil, true)
753 if err != nil {
754 z.errors <- err
755 return
756 }
757 if uint64(compressed.Len()) < ze.fh.UncompressedSize64 {
758 futureReader <- compressed
Nan Zhangf281bd82017-04-25 16:47:45 -0700759 } else {
760 buf, err := readFile(r)
761 if err != nil {
762 z.errors <- err
763 return
764 }
765 ze.fh.Method = zip.Store
766 futureReader <- bytes.NewReader(buf)
Nan Zhangf281bd82017-04-25 16:47:45 -0700767 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700768 } else {
Nan Zhangf281bd82017-04-25 16:47:45 -0700769 buf, err := readFile(r)
Dan Willemsen017d8932016-08-04 15:43:03 -0700770 if err != nil {
771 z.errors <- err
772 return
773 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700774 ze.fh.Method = zip.Store
775 futureReader <- bytes.NewReader(buf)
Dan Willemsen017d8932016-08-04 15:43:03 -0700776 }
Nan Zhangf281bd82017-04-25 16:47:45 -0700777
Jeff Gaston175f34c2017-08-17 21:43:21 -0700778 z.cpuRateLimiter.Finish()
779
Dan Willemsen017d8932016-08-04 15:43:03 -0700780 close(futureReader)
781
782 compressChan <- ze
783 close(compressChan)
Colin Cross2fe66872015-03-30 17:20:39 -0700784}
Colin Crosse19c7932015-04-24 15:08:38 -0700785
Colin Crosse5580972017-08-30 17:40:21 -0700786// writeDirectory annotates that dir is a directory created for the src file or directory, and adds
787// the directory entry to the zip file if directories are enabled.
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700788func (z *ZipWriter) writeDirectory(dir string, src string, emulateJar bool) error {
Jeff Gaston2d174132017-08-15 18:05:56 -0700789 // clean the input
Colin Crosse5580972017-08-30 17:40:21 -0700790 dir = filepath.Clean(dir)
Jeff Gaston2d174132017-08-15 18:05:56 -0700791
792 // discover any uncreated directories in the path
793 zipDirs := []string{}
Colin Crosse5580972017-08-30 17:40:21 -0700794 for dir != "" && dir != "." {
795 if _, exists := z.createdDirs[dir]; exists {
796 break
797 }
Jeff Gaston2d174132017-08-15 18:05:56 -0700798
Colin Crosse5580972017-08-30 17:40:21 -0700799 if prev, exists := z.createdFiles[dir]; exists {
800 return fmt.Errorf("destination %q is both a directory %q and a file %q", dir, src, prev)
801 }
802
803 z.createdDirs[dir] = src
Jeff Gaston2d174132017-08-15 18:05:56 -0700804 // parent directories precede their children
Colin Crosse5580972017-08-30 17:40:21 -0700805 zipDirs = append([]string{dir}, zipDirs...)
Jeff Gaston2d174132017-08-15 18:05:56 -0700806
Colin Crosse5580972017-08-30 17:40:21 -0700807 dir = filepath.Dir(dir)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700808 }
809
Colin Crosse5580972017-08-30 17:40:21 -0700810 if z.directories {
811 // make a directory entry for each uncreated directory
812 for _, cleanDir := range zipDirs {
Colin Cross635acc92017-09-12 22:50:46 -0700813 var dirHeader *zip.FileHeader
Colin Crosse19c7932015-04-24 15:08:38 -0700814
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700815 if emulateJar && cleanDir+"/" == jar.MetaDir {
Colin Cross635acc92017-09-12 22:50:46 -0700816 dirHeader = jar.MetaDirFileHeader()
817 } else {
818 dirHeader = &zip.FileHeader{
819 Name: cleanDir + "/",
820 }
821 dirHeader.SetMode(0700 | os.ModeDir)
Colin Crosse5580972017-08-30 17:40:21 -0700822 }
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700823
Colin Cross635acc92017-09-12 22:50:46 -0700824 dirHeader.SetModTime(z.time)
825
Colin Crosse5580972017-08-30 17:40:21 -0700826 ze := make(chan *zipEntry, 1)
827 ze <- &zipEntry{
828 fh: dirHeader,
829 }
830 close(ze)
831 z.writeOps <- ze
Colin Crosse19c7932015-04-24 15:08:38 -0700832 }
Colin Crosse19c7932015-04-24 15:08:38 -0700833 }
834
835 return nil
836}
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700837
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700838func (z *ZipWriter) writeSymlink(rel, file string) error {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700839 fileHeader := &zip.FileHeader{
840 Name: rel,
841 }
842 fileHeader.SetModTime(z.time)
843 fileHeader.SetMode(0700 | os.ModeSymlink)
844
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700845 dest, err := os.Readlink(file)
846 if err != nil {
847 return err
848 }
849
Dan Willemsen017d8932016-08-04 15:43:03 -0700850 ze := make(chan *zipEntry, 1)
851 futureReaders := make(chan chan io.Reader, 1)
852 futureReader := make(chan io.Reader, 1)
853 futureReaders <- futureReader
854 close(futureReaders)
855 futureReader <- bytes.NewBufferString(dest)
856 close(futureReader)
857
Dan Willemsen017d8932016-08-04 15:43:03 -0700858 ze <- &zipEntry{
859 fh: fileHeader,
860 futureReaders: futureReaders,
861 }
862 close(ze)
863 z.writeOps <- ze
864
865 return nil
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700866}
Colin Cross7b10cf12017-08-30 14:12:21 -0700867
868func recursiveGlobFiles(path string) []string {
869 var files []string
870 filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
871 if !info.IsDir() {
872 files = append(files, path)
873 }
874 return nil
875 })
876
877 return files
878}