github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/soong/zip/zip.go (about) 1 // 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 15 package zip 16 17 import ( 18 "bytes" 19 "compress/flate" 20 "errors" 21 "fmt" 22 "hash/crc32" 23 "io" 24 "io/ioutil" 25 "log" 26 "os" 27 "path/filepath" 28 "runtime/pprof" 29 "runtime/trace" 30 "sort" 31 "strings" 32 "sync" 33 "time" 34 "unicode" 35 36 "github.com/google/blueprint/pathtools" 37 38 "android/soong/jar" 39 "android/soong/third_party/zip" 40 ) 41 42 // Block size used during parallel compression of a single file. 43 const 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 48 const minParallelFileSize = parallelBlockSize * 6 49 50 // Size of the ZIP compression window (32KB) 51 const windowSize = 32 * 1024 52 53 type nopCloser struct { 54 io.Writer 55 } 56 57 func (nopCloser) Close() error { 58 return nil 59 } 60 61 type byteReaderCloser struct { 62 *bytes.Reader 63 io.Closer 64 } 65 66 type pathMapping struct { 67 dest, src string 68 zipMethod uint16 69 } 70 71 type uniqueSet map[string]bool 72 73 func (u *uniqueSet) String() string { 74 return `""` 75 } 76 77 func (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 85 } 86 87 type FileArg struct { 88 PathPrefixInZip, SourcePrefixToStrip string 89 SourceFiles []string 90 GlobDir string 91 } 92 93 type FileArgs []FileArg 94 95 type ZipWriter struct { 96 time time.Time 97 createdFiles map[string]string 98 createdDirs map[string]string 99 directories bool 100 101 errors chan error 102 writeOps chan chan *zipEntry 103 104 cpuRateLimiter *CPURateLimiter 105 memoryRateLimiter *MemoryRateLimiter 106 107 compressorPool sync.Pool 108 compLevel int 109 } 110 111 type zipEntry struct { 112 fh *zip.FileHeader 113 114 // List of delayed io.Reader 115 futureReaders chan chan io.Reader 116 117 // Only used for passing into the MemoryRateLimiter to ensure we 118 // release as much memory as much as we request 119 allocatedSize int64 120 } 121 122 type ZipArgs struct { 123 FileArgs FileArgs 124 OutputFilePath string 125 CpuProfileFilePath string 126 TraceFilePath string 127 EmulateJar bool 128 AddDirectoryEntriesToZip bool 129 CompressionLevel int 130 ManifestSourcePath string 131 NumParallelJobs int 132 NonDeflatedFiles map[string]bool 133 WriteIfChanged bool 134 } 135 136 const NOQUOTE = '\x00' 137 138 func ReadRespFile(bytes []byte) []string { 139 var args []string 140 var arg []rune 141 142 isEscaping := false 143 quotingStart := NOQUOTE 144 for _, c := range string(bytes) { 145 switch { 146 case isEscaping: 147 if quotingStart == '"' { 148 if !(c == '"' || c == '\\') { 149 // '\"' or '\\' will be escaped under double quoting. 150 arg = append(arg, '\\') 151 } 152 } 153 arg = append(arg, c) 154 isEscaping = false 155 case c == '\\' && quotingStart != '\'': 156 isEscaping = true 157 case quotingStart == NOQUOTE && (c == '\'' || c == '"'): 158 quotingStart = c 159 case quotingStart != NOQUOTE && c == quotingStart: 160 quotingStart = NOQUOTE 161 case quotingStart == NOQUOTE && unicode.IsSpace(c): 162 // Current character is a space outside quotes 163 if len(arg) != 0 { 164 args = append(args, string(arg)) 165 } 166 arg = arg[:0] 167 default: 168 arg = append(arg, c) 169 } 170 } 171 172 if len(arg) != 0 { 173 args = append(args, string(arg)) 174 } 175 176 return args 177 } 178 179 func Run(args ZipArgs) (err error) { 180 if args.CpuProfileFilePath != "" { 181 f, err := os.Create(args.CpuProfileFilePath) 182 if err != nil { 183 fmt.Fprintln(os.Stderr, err.Error()) 184 os.Exit(1) 185 } 186 defer f.Close() 187 pprof.StartCPUProfile(f) 188 defer pprof.StopCPUProfile() 189 } 190 191 if args.TraceFilePath != "" { 192 f, err := os.Create(args.TraceFilePath) 193 if err != nil { 194 fmt.Fprintln(os.Stderr, err.Error()) 195 os.Exit(1) 196 } 197 defer f.Close() 198 err = trace.Start(f) 199 if err != nil { 200 fmt.Fprintln(os.Stderr, err.Error()) 201 os.Exit(1) 202 } 203 defer trace.Stop() 204 } 205 206 if args.OutputFilePath == "" { 207 return fmt.Errorf("output file path must be nonempty") 208 } 209 210 if args.EmulateJar { 211 args.AddDirectoryEntriesToZip = true 212 } 213 214 w := &ZipWriter{ 215 time: jar.DefaultTime, 216 createdDirs: make(map[string]string), 217 createdFiles: make(map[string]string), 218 directories: args.AddDirectoryEntriesToZip, 219 compLevel: args.CompressionLevel, 220 } 221 pathMappings := []pathMapping{} 222 223 for _, fa := range args.FileArgs { 224 srcs := fa.SourceFiles 225 if fa.GlobDir != "" { 226 srcs = append(srcs, recursiveGlobFiles(fa.GlobDir)...) 227 } 228 for _, src := range srcs { 229 if err := fillPathPairs(fa.PathPrefixInZip, 230 fa.SourcePrefixToStrip, src, &pathMappings, args.NonDeflatedFiles); err != nil { 231 log.Fatal(err) 232 } 233 } 234 } 235 236 buf := &bytes.Buffer{} 237 var out io.Writer = buf 238 239 if !args.WriteIfChanged { 240 f, err := os.Create(args.OutputFilePath) 241 if err != nil { 242 return err 243 } 244 245 defer f.Close() 246 defer func() { 247 if err != nil { 248 os.Remove(args.OutputFilePath) 249 } 250 }() 251 252 out = f 253 } 254 255 err = w.write(out, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.NumParallelJobs) 256 if err != nil { 257 return err 258 } 259 260 if args.WriteIfChanged { 261 err := pathtools.WriteFileIfChanged(args.OutputFilePath, buf.Bytes(), 0666) 262 if err != nil { 263 return err 264 } 265 } 266 267 return nil 268 } 269 270 func fillPathPairs(prefix, rel, src string, pathMappings *[]pathMapping, nonDeflatedFiles map[string]bool) error { 271 src = strings.TrimSpace(src) 272 if src == "" { 273 return nil 274 } 275 src = filepath.Clean(src) 276 dest, err := filepath.Rel(rel, src) 277 if err != nil { 278 return err 279 } 280 dest = filepath.Join(prefix, dest) 281 282 zipMethod := zip.Deflate 283 if _, found := nonDeflatedFiles[dest]; found { 284 zipMethod = zip.Store 285 } 286 *pathMappings = append(*pathMappings, 287 pathMapping{dest: dest, src: src, zipMethod: zipMethod}) 288 289 return nil 290 } 291 292 func jarSort(mappings []pathMapping) { 293 less := func(i int, j int) (smaller bool) { 294 return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest) 295 } 296 sort.SliceStable(mappings, less) 297 } 298 299 type readerSeekerCloser interface { 300 io.Reader 301 io.ReaderAt 302 io.Closer 303 io.Seeker 304 } 305 306 func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string, emulateJar bool, parallelJobs int) error { 307 z.errors = make(chan error) 308 defer close(z.errors) 309 310 // This channel size can be essentially unlimited -- it's used as a fifo 311 // queue decouple the CPU and IO loads. Directories don't require any 312 // compression time, but still cost some IO. Similar with small files that 313 // can be very fast to compress. Some files that are more difficult to 314 // compress won't take a corresponding longer time writing out. 315 // 316 // The optimum size here depends on your CPU and IO characteristics, and 317 // the the layout of your zip file. 1000 was chosen mostly at random as 318 // something that worked reasonably well for a test file. 319 // 320 // The RateLimit object will put the upper bounds on the number of 321 // parallel compressions and outstanding buffers. 322 z.writeOps = make(chan chan *zipEntry, 1000) 323 z.cpuRateLimiter = NewCPURateLimiter(int64(parallelJobs)) 324 z.memoryRateLimiter = NewMemoryRateLimiter(0) 325 defer func() { 326 z.cpuRateLimiter.Stop() 327 z.memoryRateLimiter.Stop() 328 }() 329 330 if manifest != "" && !emulateJar { 331 return errors.New("must specify --jar when specifying a manifest via -m") 332 } 333 334 if emulateJar { 335 // manifest may be empty, in which case addManifest will fill in a default 336 pathMappings = append(pathMappings, pathMapping{jar.ManifestFile, manifest, zip.Deflate}) 337 338 jarSort(pathMappings) 339 } 340 341 go func() { 342 var err error 343 defer close(z.writeOps) 344 345 for _, ele := range pathMappings { 346 if emulateJar && ele.dest == jar.ManifestFile { 347 err = z.addManifest(ele.dest, ele.src, ele.zipMethod) 348 } else { 349 err = z.addFile(ele.dest, ele.src, ele.zipMethod, emulateJar) 350 } 351 if err != nil { 352 z.errors <- err 353 return 354 } 355 } 356 }() 357 358 zipw := zip.NewWriter(f) 359 360 var currentWriteOpChan chan *zipEntry 361 var currentWriter io.WriteCloser 362 var currentReaders chan chan io.Reader 363 var currentReader chan io.Reader 364 var done bool 365 366 for !done { 367 var writeOpsChan chan chan *zipEntry 368 var writeOpChan chan *zipEntry 369 var readersChan chan chan io.Reader 370 371 if currentReader != nil { 372 // Only read and process errors 373 } else if currentReaders != nil { 374 readersChan = currentReaders 375 } else if currentWriteOpChan != nil { 376 writeOpChan = currentWriteOpChan 377 } else { 378 writeOpsChan = z.writeOps 379 } 380 381 select { 382 case writeOp, ok := <-writeOpsChan: 383 if !ok { 384 done = true 385 } 386 387 currentWriteOpChan = writeOp 388 389 case op := <-writeOpChan: 390 currentWriteOpChan = nil 391 392 var err error 393 if op.fh.Method == zip.Deflate { 394 currentWriter, err = zipw.CreateCompressedHeader(op.fh) 395 } else { 396 var zw io.Writer 397 398 op.fh.CompressedSize64 = op.fh.UncompressedSize64 399 400 zw, err = zipw.CreateHeaderAndroid(op.fh) 401 currentWriter = nopCloser{zw} 402 } 403 if err != nil { 404 return err 405 } 406 407 currentReaders = op.futureReaders 408 if op.futureReaders == nil { 409 currentWriter.Close() 410 currentWriter = nil 411 } 412 z.memoryRateLimiter.Finish(op.allocatedSize) 413 414 case futureReader, ok := <-readersChan: 415 if !ok { 416 // Done with reading 417 currentWriter.Close() 418 currentWriter = nil 419 currentReaders = nil 420 } 421 422 currentReader = futureReader 423 424 case reader := <-currentReader: 425 _, err := io.Copy(currentWriter, reader) 426 if err != nil { 427 return err 428 } 429 430 currentReader = nil 431 432 case err := <-z.errors: 433 return err 434 } 435 } 436 437 // One last chance to catch an error 438 select { 439 case err := <-z.errors: 440 return err 441 default: 442 zipw.Close() 443 return nil 444 } 445 } 446 447 // imports (possibly with compression) <src> into the zip at sub-path <dest> 448 func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar bool) error { 449 var fileSize int64 450 var executable bool 451 452 if s, err := os.Lstat(src); err != nil { 453 return err 454 } else if s.IsDir() { 455 if z.directories { 456 return z.writeDirectory(dest, src, emulateJar) 457 } 458 return nil 459 } else { 460 if err := z.writeDirectory(filepath.Dir(dest), src, emulateJar); err != nil { 461 return err 462 } 463 464 if prev, exists := z.createdDirs[dest]; exists { 465 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src) 466 } 467 if prev, exists := z.createdFiles[dest]; exists { 468 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src) 469 } 470 471 z.createdFiles[dest] = src 472 473 if s.Mode()&os.ModeSymlink != 0 { 474 return z.writeSymlink(dest, src) 475 } else if !s.Mode().IsRegular() { 476 return fmt.Errorf("%s is not a file, directory, or symlink", src) 477 } 478 479 fileSize = s.Size() 480 executable = s.Mode()&0100 != 0 481 } 482 483 r, err := os.Open(src) 484 if err != nil { 485 return err 486 } 487 488 header := &zip.FileHeader{ 489 Name: dest, 490 Method: method, 491 UncompressedSize64: uint64(fileSize), 492 } 493 494 if executable { 495 header.SetMode(0700) 496 } 497 498 return z.writeFileContents(header, r) 499 } 500 501 func (z *ZipWriter) addManifest(dest string, src string, method uint16) error { 502 if prev, exists := z.createdDirs[dest]; exists { 503 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src) 504 } 505 if prev, exists := z.createdFiles[dest]; exists { 506 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src) 507 } 508 509 if err := z.writeDirectory(filepath.Dir(dest), src, true); err != nil { 510 return err 511 } 512 513 fh, buf, err := jar.ManifestFileContents(src) 514 if err != nil { 515 return err 516 } 517 518 reader := &byteReaderCloser{bytes.NewReader(buf), ioutil.NopCloser(nil)} 519 520 return z.writeFileContents(fh, reader) 521 } 522 523 func (z *ZipWriter) writeFileContents(header *zip.FileHeader, r readerSeekerCloser) (err error) { 524 525 header.SetModTime(z.time) 526 527 compressChan := make(chan *zipEntry, 1) 528 z.writeOps <- compressChan 529 530 // Pre-fill a zipEntry, it will be sent in the compressChan once 531 // we're sure about the Method and CRC. 532 ze := &zipEntry{ 533 fh: header, 534 } 535 536 ze.allocatedSize = int64(header.UncompressedSize64) 537 z.cpuRateLimiter.Request() 538 z.memoryRateLimiter.Request(ze.allocatedSize) 539 540 fileSize := int64(header.UncompressedSize64) 541 if fileSize == 0 { 542 fileSize = int64(header.UncompressedSize) 543 } 544 545 if header.Method == zip.Deflate && fileSize >= minParallelFileSize { 546 wg := new(sync.WaitGroup) 547 548 // Allocate enough buffer to hold all readers. We'll limit 549 // this based on actual buffer sizes in RateLimit. 550 ze.futureReaders = make(chan chan io.Reader, (fileSize/parallelBlockSize)+1) 551 552 // Calculate the CRC in the background, since reading the entire 553 // file could take a while. 554 // 555 // We could split this up into chunks as well, but it's faster 556 // than the compression. Due to the Go Zip API, we also need to 557 // know the result before we can begin writing the compressed 558 // data out to the zipfile. 559 wg.Add(1) 560 go z.crcFile(r, ze, compressChan, wg) 561 562 for start := int64(0); start < fileSize; start += parallelBlockSize { 563 sr := io.NewSectionReader(r, start, parallelBlockSize) 564 resultChan := make(chan io.Reader, 1) 565 ze.futureReaders <- resultChan 566 567 z.cpuRateLimiter.Request() 568 569 last := !(start+parallelBlockSize < fileSize) 570 var dict []byte 571 if start >= windowSize { 572 dict, err = ioutil.ReadAll(io.NewSectionReader(r, start-windowSize, windowSize)) 573 if err != nil { 574 return err 575 } 576 } 577 578 wg.Add(1) 579 go z.compressPartialFile(sr, dict, last, resultChan, wg) 580 } 581 582 close(ze.futureReaders) 583 584 // Close the file handle after all readers are done 585 go func(wg *sync.WaitGroup, closer io.Closer) { 586 wg.Wait() 587 closer.Close() 588 }(wg, r) 589 } else { 590 go func() { 591 z.compressWholeFile(ze, r, compressChan) 592 r.Close() 593 }() 594 } 595 596 return nil 597 } 598 599 func (z *ZipWriter) crcFile(r io.Reader, ze *zipEntry, resultChan chan *zipEntry, wg *sync.WaitGroup) { 600 defer wg.Done() 601 defer z.cpuRateLimiter.Finish() 602 603 crc := crc32.NewIEEE() 604 _, err := io.Copy(crc, r) 605 if err != nil { 606 z.errors <- err 607 return 608 } 609 610 ze.fh.CRC32 = crc.Sum32() 611 resultChan <- ze 612 close(resultChan) 613 } 614 615 func (z *ZipWriter) compressPartialFile(r io.Reader, dict []byte, last bool, resultChan chan io.Reader, wg *sync.WaitGroup) { 616 defer wg.Done() 617 618 result, err := z.compressBlock(r, dict, last) 619 if err != nil { 620 z.errors <- err 621 return 622 } 623 624 z.cpuRateLimiter.Finish() 625 626 resultChan <- result 627 } 628 629 func (z *ZipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) { 630 buf := new(bytes.Buffer) 631 var fw *flate.Writer 632 var err error 633 if len(dict) > 0 { 634 // There's no way to Reset a Writer with a new dictionary, so 635 // don't use the Pool 636 fw, err = flate.NewWriterDict(buf, z.compLevel, dict) 637 } else { 638 var ok bool 639 if fw, ok = z.compressorPool.Get().(*flate.Writer); ok { 640 fw.Reset(buf) 641 } else { 642 fw, err = flate.NewWriter(buf, z.compLevel) 643 } 644 defer z.compressorPool.Put(fw) 645 } 646 if err != nil { 647 return nil, err 648 } 649 650 _, err = io.Copy(fw, r) 651 if err != nil { 652 return nil, err 653 } 654 if last { 655 fw.Close() 656 } else { 657 fw.Flush() 658 } 659 660 return buf, nil 661 } 662 663 func (z *ZipWriter) compressWholeFile(ze *zipEntry, r io.ReadSeeker, compressChan chan *zipEntry) { 664 665 crc := crc32.NewIEEE() 666 _, err := io.Copy(crc, r) 667 if err != nil { 668 z.errors <- err 669 return 670 } 671 672 ze.fh.CRC32 = crc.Sum32() 673 674 _, err = r.Seek(0, 0) 675 if err != nil { 676 z.errors <- err 677 return 678 } 679 680 readFile := func(reader io.ReadSeeker) ([]byte, error) { 681 _, err := reader.Seek(0, 0) 682 if err != nil { 683 return nil, err 684 } 685 686 buf, err := ioutil.ReadAll(reader) 687 if err != nil { 688 return nil, err 689 } 690 691 return buf, nil 692 } 693 694 ze.futureReaders = make(chan chan io.Reader, 1) 695 futureReader := make(chan io.Reader, 1) 696 ze.futureReaders <- futureReader 697 close(ze.futureReaders) 698 699 if ze.fh.Method == zip.Deflate { 700 compressed, err := z.compressBlock(r, nil, true) 701 if err != nil { 702 z.errors <- err 703 return 704 } 705 if uint64(compressed.Len()) < ze.fh.UncompressedSize64 { 706 futureReader <- compressed 707 } else { 708 buf, err := readFile(r) 709 if err != nil { 710 z.errors <- err 711 return 712 } 713 ze.fh.Method = zip.Store 714 futureReader <- bytes.NewReader(buf) 715 } 716 } else { 717 buf, err := readFile(r) 718 if err != nil { 719 z.errors <- err 720 return 721 } 722 ze.fh.Method = zip.Store 723 futureReader <- bytes.NewReader(buf) 724 } 725 726 z.cpuRateLimiter.Finish() 727 728 close(futureReader) 729 730 compressChan <- ze 731 close(compressChan) 732 } 733 734 // writeDirectory annotates that dir is a directory created for the src file or directory, and adds 735 // the directory entry to the zip file if directories are enabled. 736 func (z *ZipWriter) writeDirectory(dir string, src string, emulateJar bool) error { 737 // clean the input 738 dir = filepath.Clean(dir) 739 740 // discover any uncreated directories in the path 741 zipDirs := []string{} 742 for dir != "" && dir != "." { 743 if _, exists := z.createdDirs[dir]; exists { 744 break 745 } 746 747 if prev, exists := z.createdFiles[dir]; exists { 748 return fmt.Errorf("destination %q is both a directory %q and a file %q", dir, src, prev) 749 } 750 751 z.createdDirs[dir] = src 752 // parent directories precede their children 753 zipDirs = append([]string{dir}, zipDirs...) 754 755 dir = filepath.Dir(dir) 756 } 757 758 if z.directories { 759 // make a directory entry for each uncreated directory 760 for _, cleanDir := range zipDirs { 761 var dirHeader *zip.FileHeader 762 763 if emulateJar && cleanDir+"/" == jar.MetaDir { 764 dirHeader = jar.MetaDirFileHeader() 765 } else { 766 dirHeader = &zip.FileHeader{ 767 Name: cleanDir + "/", 768 } 769 dirHeader.SetMode(0700 | os.ModeDir) 770 } 771 772 dirHeader.SetModTime(z.time) 773 774 ze := make(chan *zipEntry, 1) 775 ze <- &zipEntry{ 776 fh: dirHeader, 777 } 778 close(ze) 779 z.writeOps <- ze 780 } 781 } 782 783 return nil 784 } 785 786 func (z *ZipWriter) writeSymlink(rel, file string) error { 787 fileHeader := &zip.FileHeader{ 788 Name: rel, 789 } 790 fileHeader.SetModTime(z.time) 791 fileHeader.SetMode(0700 | os.ModeSymlink) 792 793 dest, err := os.Readlink(file) 794 if err != nil { 795 return err 796 } 797 798 ze := make(chan *zipEntry, 1) 799 futureReaders := make(chan chan io.Reader, 1) 800 futureReader := make(chan io.Reader, 1) 801 futureReaders <- futureReader 802 close(futureReaders) 803 futureReader <- bytes.NewBufferString(dest) 804 close(futureReader) 805 806 ze <- &zipEntry{ 807 fh: fileHeader, 808 futureReaders: futureReaders, 809 } 810 close(ze) 811 z.writeOps <- ze 812 813 return nil 814 } 815 816 func recursiveGlobFiles(path string) []string { 817 var files []string 818 filepath.Walk(path, func(path string, info os.FileInfo, err error) error { 819 if !info.IsDir() { 820 files = append(files, path) 821 } 822 return nil 823 }) 824 825 return files 826 }