2015-03-31 02:20:39 +02:00
|
|
|
// Copyright 2015 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-08-05 00:43:03 +02:00
|
|
|
"bytes"
|
|
|
|
"compress/flate"
|
2017-08-23 02:51:25 +02:00
|
|
|
"errors"
|
2015-03-31 02:20:39 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2016-08-05 00:43:03 +02:00
|
|
|
"hash/crc32"
|
2015-03-31 02:20:39 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2017-03-17 22:04:43 +01:00
|
|
|
"log"
|
2015-03-31 02:20:39 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-08-05 00:43:03 +02:00
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
|
|
|
"runtime/trace"
|
2017-08-23 02:51:25 +02:00
|
|
|
"sort"
|
2015-03-31 02:20:39 +02:00
|
|
|
"strings"
|
2016-08-05 00:43:03 +02:00
|
|
|
"sync"
|
2015-03-31 02:20:39 +02:00
|
|
|
"time"
|
2016-08-05 00:43:03 +02:00
|
|
|
|
2017-08-23 02:51:25 +02:00
|
|
|
"android/soong/jar"
|
2016-08-05 00:43:03 +02:00
|
|
|
"android/soong/third_party/zip"
|
2015-03-31 02:20:39 +02:00
|
|
|
)
|
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
// Block size used during parallel compression of a single file.
|
|
|
|
const parallelBlockSize = 1 * 1024 * 1024 // 1MB
|
|
|
|
|
|
|
|
// Minimum file size to use parallel compression. It requires more
|
|
|
|
// flate.Writer allocations, since we can't change the dictionary
|
|
|
|
// during Reset
|
|
|
|
const minParallelFileSize = parallelBlockSize * 6
|
|
|
|
|
|
|
|
// Size of the ZIP compression window (32KB)
|
|
|
|
const windowSize = 32 * 1024
|
|
|
|
|
|
|
|
type nopCloser struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nopCloser) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-24 00:41:35 +02:00
|
|
|
type byteReaderCloser struct {
|
|
|
|
bytes.Reader
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
// the file path in the zip at which a Java manifest file gets written
|
|
|
|
const manifestDest = "META-INF/MANIFEST.MF"
|
|
|
|
|
2015-03-31 02:20:39 +02:00
|
|
|
type fileArg struct {
|
2017-04-26 01:47:45 +02:00
|
|
|
pathPrefixInZip, sourcePrefixToStrip string
|
|
|
|
sourceFiles []string
|
2017-08-30 23:12:21 +02:00
|
|
|
globDir string
|
2017-03-17 22:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type pathMapping struct {
|
|
|
|
dest, src string
|
2017-04-26 01:47:45 +02:00
|
|
|
zipMethod uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
type uniqueSet map[string]bool
|
|
|
|
|
|
|
|
func (u *uniqueSet) String() string {
|
|
|
|
return `""`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *uniqueSet) Set(s string) error {
|
|
|
|
if _, found := (*u)[s]; found {
|
|
|
|
return fmt.Errorf("File %q was specified twice as a file to not deflate", s)
|
|
|
|
} else {
|
|
|
|
(*u)[s] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type fileArgs []fileArg
|
|
|
|
|
2017-04-26 01:47:45 +02:00
|
|
|
type file struct{}
|
|
|
|
|
|
|
|
type listFiles struct{}
|
|
|
|
|
2017-08-30 23:12:21 +02:00
|
|
|
type dir struct{}
|
|
|
|
|
2017-04-26 01:47:45 +02:00
|
|
|
func (f *file) String() string {
|
2015-03-31 02:20:39 +02:00
|
|
|
return `""`
|
|
|
|
}
|
|
|
|
|
2017-04-26 01:47:45 +02:00
|
|
|
func (f *file) Set(s string) error {
|
2015-03-31 02:20:39 +02:00
|
|
|
if *relativeRoot == "" {
|
2017-08-30 23:12:21 +02:00
|
|
|
return fmt.Errorf("must pass -C before -f")
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
|
|
|
|
2017-04-26 01:47:45 +02:00
|
|
|
fArgs = append(fArgs, fileArg{
|
|
|
|
pathPrefixInZip: filepath.Clean(*rootPrefix),
|
|
|
|
sourcePrefixToStrip: filepath.Clean(*relativeRoot),
|
|
|
|
sourceFiles: []string{s},
|
|
|
|
})
|
|
|
|
|
2015-03-31 02:20:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-26 01:47:45 +02:00
|
|
|
func (l *listFiles) String() string {
|
|
|
|
return `""`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *listFiles) Set(s string) error {
|
|
|
|
if *relativeRoot == "" {
|
2017-08-30 23:12:21 +02:00
|
|
|
return fmt.Errorf("must pass -C before -l")
|
2017-04-26 01:47:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
list, err := ioutil.ReadFile(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fArgs = append(fArgs, fileArg{
|
|
|
|
pathPrefixInZip: filepath.Clean(*rootPrefix),
|
|
|
|
sourcePrefixToStrip: filepath.Clean(*relativeRoot),
|
|
|
|
sourceFiles: strings.Split(string(list), "\n"),
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 23:12:21 +02:00
|
|
|
func (d *dir) String() string {
|
|
|
|
return `""`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dir) Set(s string) error {
|
|
|
|
if *relativeRoot == "" {
|
|
|
|
return fmt.Errorf("must pass -C before -D")
|
|
|
|
}
|
|
|
|
|
|
|
|
fArgs = append(fArgs, fileArg{
|
|
|
|
pathPrefixInZip: filepath.Clean(*rootPrefix),
|
|
|
|
sourcePrefixToStrip: filepath.Clean(*relativeRoot),
|
|
|
|
globDir: filepath.Clean(s),
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-31 02:20:39 +02:00
|
|
|
var (
|
2016-08-11 01:12:30 +02:00
|
|
|
out = flag.String("o", "", "file to write zip file to")
|
|
|
|
manifest = flag.String("m", "", "input jar manifest file name")
|
|
|
|
directories = flag.Bool("d", false, "include directories in zip")
|
2017-03-17 22:04:43 +01:00
|
|
|
rootPrefix = flag.String("P", "", "path prefix within the zip at which to place files")
|
2017-08-30 23:12:21 +02:00
|
|
|
relativeRoot = flag.String("C", "", "path to use as relative root of files in following -f, -l, or -D arguments")
|
2016-08-05 00:43:03 +02:00
|
|
|
parallelJobs = flag.Int("j", runtime.NumCPU(), "number of parallel threads to use")
|
|
|
|
compLevel = flag.Int("L", 5, "deflate compression level (0-9)")
|
2017-08-23 02:51:25 +02:00
|
|
|
emulateJar = flag.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'")
|
2017-03-17 22:04:43 +01:00
|
|
|
|
2017-04-26 01:47:45 +02:00
|
|
|
fArgs fileArgs
|
|
|
|
nonDeflatedFiles = make(uniqueSet)
|
2016-08-05 00:43:03 +02:00
|
|
|
|
|
|
|
cpuProfile = flag.String("cpuprofile", "", "write cpu profile to file")
|
|
|
|
traceFile = flag.String("trace", "", "write trace to file")
|
2015-03-31 02:20:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-04-26 01:47:45 +02:00
|
|
|
flag.Var(&listFiles{}, "l", "file containing list of .class files")
|
2017-08-30 23:12:21 +02:00
|
|
|
flag.Var(&dir{}, "D", "directory to include in zip")
|
2017-04-26 01:47:45 +02:00
|
|
|
flag.Var(&file{}, "f", "file to include in zip")
|
|
|
|
flag.Var(&nonDeflatedFiles, "s", "file path to be stored within the zip without compression")
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func usage() {
|
2016-08-11 01:12:30 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "usage: soong_zip -o zipfile [-m manifest] -C dir [-f|-l file]...\n")
|
2015-03-31 02:20:39 +02:00
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
2015-04-25 00:08:38 +02:00
|
|
|
type zipWriter struct {
|
2015-03-31 02:20:39 +02:00
|
|
|
time time.Time
|
|
|
|
createdDirs map[string]bool
|
|
|
|
directories bool
|
2015-04-25 00:08:38 +02:00
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
errors chan error
|
|
|
|
writeOps chan chan *zipEntry
|
|
|
|
|
2017-08-18 06:43:21 +02:00
|
|
|
cpuRateLimiter *CPURateLimiter
|
|
|
|
memoryRateLimiter *MemoryRateLimiter
|
2016-08-05 00:43:03 +02:00
|
|
|
|
|
|
|
compressorPool sync.Pool
|
|
|
|
compLevel int
|
|
|
|
}
|
|
|
|
|
|
|
|
type zipEntry struct {
|
|
|
|
fh *zip.FileHeader
|
|
|
|
|
|
|
|
// List of delayed io.Reader
|
|
|
|
futureReaders chan chan io.Reader
|
2017-08-18 06:43:21 +02:00
|
|
|
|
|
|
|
// Only used for passing into the MemoryRateLimiter to ensure we
|
|
|
|
// release as much memory as much as we request
|
|
|
|
allocatedSize int64
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
if *cpuProfile != "" {
|
|
|
|
f, err := os.Create(*cpuProfile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
|
|
|
if *traceFile != "" {
|
|
|
|
f, err := os.Create(*traceFile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
err = trace.Start(f)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer trace.Stop()
|
|
|
|
}
|
|
|
|
|
2015-03-31 02:20:39 +02:00
|
|
|
if *out == "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: -o is required\n")
|
|
|
|
usage()
|
|
|
|
}
|
|
|
|
|
2017-08-23 05:05:28 +02:00
|
|
|
if *emulateJar {
|
|
|
|
*directories = true
|
|
|
|
}
|
|
|
|
|
2015-04-25 00:08:38 +02:00
|
|
|
w := &zipWriter{
|
2016-08-05 05:38:47 +02:00
|
|
|
time: time.Date(2009, 1, 1, 0, 0, 0, 0, time.UTC),
|
2015-03-31 02:20:39 +02:00
|
|
|
createdDirs: make(map[string]bool),
|
|
|
|
directories: *directories,
|
2016-08-05 00:43:03 +02:00
|
|
|
compLevel: *compLevel,
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-17 22:04:43 +01:00
|
|
|
pathMappings := []pathMapping{}
|
|
|
|
set := make(map[string]string)
|
|
|
|
|
2017-04-26 01:47:45 +02:00
|
|
|
for _, fa := range fArgs {
|
2017-08-30 23:12:21 +02:00
|
|
|
srcs := fa.sourceFiles
|
|
|
|
if fa.globDir != "" {
|
|
|
|
srcs = append(srcs, recursiveGlobFiles(fa.globDir)...)
|
|
|
|
}
|
|
|
|
for _, src := range srcs {
|
2017-04-26 01:47:45 +02:00
|
|
|
if err := fillPathPairs(fa.pathPrefixInZip,
|
|
|
|
fa.sourcePrefixToStrip, src, set, &pathMappings); err != nil {
|
2017-03-17 22:04:43 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := w.write(*out, pathMappings, *manifest)
|
2015-03-31 02:20:39 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:04:43 +01:00
|
|
|
func fillPathPairs(prefix, rel, src string, set map[string]string, pathMappings *[]pathMapping) error {
|
|
|
|
src = strings.TrimSpace(src)
|
|
|
|
if src == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
src = filepath.Clean(src)
|
|
|
|
dest, err := filepath.Rel(rel, src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dest = filepath.Join(prefix, dest)
|
|
|
|
|
|
|
|
if _, found := set[dest]; found {
|
|
|
|
return fmt.Errorf("found two file paths to be copied into dest path: %q,"+
|
|
|
|
" both [%q]%q and [%q]%q!",
|
|
|
|
dest, dest, src, dest, set[dest])
|
|
|
|
} else {
|
|
|
|
set[dest] = src
|
|
|
|
}
|
|
|
|
|
2017-04-26 01:47:45 +02:00
|
|
|
zipMethod := zip.Deflate
|
|
|
|
if _, found := nonDeflatedFiles[dest]; found {
|
|
|
|
zipMethod = zip.Store
|
|
|
|
}
|
|
|
|
*pathMappings = append(*pathMappings,
|
|
|
|
pathMapping{dest: dest, src: src, zipMethod: zipMethod})
|
2017-03-17 22:04:43 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-23 02:51:25 +02:00
|
|
|
func jarSort(mappings []pathMapping) {
|
|
|
|
less := func(i int, j int) (smaller bool) {
|
|
|
|
return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest)
|
|
|
|
}
|
|
|
|
sort.SliceStable(mappings, less)
|
|
|
|
}
|
|
|
|
|
2017-08-24 00:12:48 +02:00
|
|
|
type readerSeekerCloser interface {
|
|
|
|
io.Reader
|
|
|
|
io.ReaderAt
|
|
|
|
io.Closer
|
|
|
|
io.Seeker
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:04:43 +01:00
|
|
|
func (z *zipWriter) write(out string, pathMappings []pathMapping, manifest string) error {
|
2015-03-31 02:20:39 +02:00
|
|
|
f, err := os.Create(out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(out)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
z.errors = make(chan error)
|
|
|
|
defer close(z.errors)
|
|
|
|
|
|
|
|
// This channel size can be essentially unlimited -- it's used as a fifo
|
|
|
|
// queue decouple the CPU and IO loads. Directories don't require any
|
|
|
|
// compression time, but still cost some IO. Similar with small files that
|
|
|
|
// can be very fast to compress. Some files that are more difficult to
|
|
|
|
// compress won't take a corresponding longer time writing out.
|
|
|
|
//
|
|
|
|
// The optimum size here depends on your CPU and IO characteristics, and
|
|
|
|
// the the layout of your zip file. 1000 was chosen mostly at random as
|
|
|
|
// something that worked reasonably well for a test file.
|
|
|
|
//
|
|
|
|
// The RateLimit object will put the upper bounds on the number of
|
|
|
|
// parallel compressions and outstanding buffers.
|
|
|
|
z.writeOps = make(chan chan *zipEntry, 1000)
|
2017-08-18 06:43:21 +02:00
|
|
|
z.cpuRateLimiter = NewCPURateLimiter(int64(*parallelJobs))
|
|
|
|
z.memoryRateLimiter = NewMemoryRateLimiter(0)
|
|
|
|
defer func() {
|
|
|
|
z.cpuRateLimiter.Stop()
|
|
|
|
z.memoryRateLimiter.Stop()
|
|
|
|
}()
|
2017-08-23 02:51:25 +02:00
|
|
|
|
|
|
|
if manifest != "" {
|
|
|
|
if !*emulateJar {
|
|
|
|
return errors.New("must specify --jar when specifying a manifest via -m")
|
|
|
|
}
|
2017-08-24 00:41:35 +02:00
|
|
|
pathMappings = append(pathMappings, pathMapping{manifestDest, manifest, zip.Deflate})
|
2017-08-23 02:51:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if *emulateJar {
|
|
|
|
jarSort(pathMappings)
|
|
|
|
}
|
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
go func() {
|
|
|
|
var err error
|
|
|
|
defer close(z.writeOps)
|
|
|
|
|
2017-03-17 22:04:43 +01:00
|
|
|
for _, ele := range pathMappings {
|
2017-08-24 00:41:35 +02:00
|
|
|
if *emulateJar && ele.dest == manifestDest {
|
|
|
|
err = z.addManifest(ele.dest, ele.src, ele.zipMethod)
|
|
|
|
} else {
|
|
|
|
err = z.addFile(ele.dest, ele.src, ele.zipMethod)
|
|
|
|
}
|
2016-08-05 00:43:03 +02:00
|
|
|
if err != nil {
|
|
|
|
z.errors <- err
|
|
|
|
return
|
|
|
|
}
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
2016-08-05 00:43:03 +02:00
|
|
|
}()
|
2015-03-31 02:20:39 +02:00
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
zipw := zip.NewWriter(f)
|
|
|
|
|
|
|
|
var currentWriteOpChan chan *zipEntry
|
|
|
|
var currentWriter io.WriteCloser
|
|
|
|
var currentReaders chan chan io.Reader
|
|
|
|
var currentReader chan io.Reader
|
|
|
|
var done bool
|
|
|
|
|
|
|
|
for !done {
|
|
|
|
var writeOpsChan chan chan *zipEntry
|
|
|
|
var writeOpChan chan *zipEntry
|
|
|
|
var readersChan chan chan io.Reader
|
|
|
|
|
|
|
|
if currentReader != nil {
|
|
|
|
// Only read and process errors
|
|
|
|
} else if currentReaders != nil {
|
|
|
|
readersChan = currentReaders
|
|
|
|
} else if currentWriteOpChan != nil {
|
|
|
|
writeOpChan = currentWriteOpChan
|
|
|
|
} else {
|
|
|
|
writeOpsChan = z.writeOps
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case writeOp, ok := <-writeOpsChan:
|
|
|
|
if !ok {
|
|
|
|
done = true
|
|
|
|
}
|
|
|
|
|
|
|
|
currentWriteOpChan = writeOp
|
|
|
|
|
|
|
|
case op := <-writeOpChan:
|
|
|
|
currentWriteOpChan = nil
|
|
|
|
|
|
|
|
if op.fh.Method == zip.Deflate {
|
|
|
|
currentWriter, err = zipw.CreateCompressedHeader(op.fh)
|
|
|
|
} else {
|
|
|
|
var zw io.Writer
|
2017-08-24 23:11:27 +02:00
|
|
|
|
|
|
|
op.fh.CompressedSize64 = op.fh.UncompressedSize64
|
|
|
|
|
|
|
|
zw, err = zipw.CreateHeaderAndroid(op.fh)
|
2016-08-05 00:43:03 +02:00
|
|
|
currentWriter = nopCloser{zw}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
currentReaders = op.futureReaders
|
|
|
|
if op.futureReaders == nil {
|
|
|
|
currentWriter.Close()
|
|
|
|
currentWriter = nil
|
|
|
|
}
|
2017-08-18 06:43:21 +02:00
|
|
|
z.memoryRateLimiter.Finish(op.allocatedSize)
|
2016-08-05 00:43:03 +02:00
|
|
|
|
|
|
|
case futureReader, ok := <-readersChan:
|
|
|
|
if !ok {
|
|
|
|
// Done with reading
|
|
|
|
currentWriter.Close()
|
|
|
|
currentWriter = nil
|
|
|
|
currentReaders = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
currentReader = futureReader
|
|
|
|
|
|
|
|
case reader := <-currentReader:
|
2017-08-18 06:43:21 +02:00
|
|
|
_, err = io.Copy(currentWriter, reader)
|
2016-08-05 00:43:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
currentReader = nil
|
|
|
|
|
|
|
|
case err = <-z.errors:
|
2015-03-31 02:20:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
// One last chance to catch an error
|
|
|
|
select {
|
|
|
|
case err = <-z.errors:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
zipw.Close()
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 00:12:48 +02:00
|
|
|
// imports (possibly with compression) <src> into the zip at sub-path <dest>
|
2017-08-24 00:41:35 +02:00
|
|
|
func (z *zipWriter) addFile(dest, src string, method uint16) error {
|
2016-08-05 00:43:03 +02:00
|
|
|
var fileSize int64
|
2017-03-16 03:02:51 +01:00
|
|
|
var executable bool
|
2016-08-05 00:43:03 +02:00
|
|
|
|
2017-03-17 22:04:43 +01:00
|
|
|
if s, err := os.Lstat(src); err != nil {
|
2016-08-04 02:47:23 +02:00
|
|
|
return err
|
|
|
|
} else if s.IsDir() {
|
2015-04-25 00:10:32 +02:00
|
|
|
if z.directories {
|
2017-03-17 22:04:43 +01:00
|
|
|
return z.writeDirectory(dest)
|
2015-04-25 00:10:32 +02:00
|
|
|
}
|
|
|
|
return nil
|
2016-08-04 02:47:23 +02:00
|
|
|
} else if s.Mode()&os.ModeSymlink != 0 {
|
2017-03-17 22:04:43 +01:00
|
|
|
return z.writeSymlink(dest, src)
|
2016-08-04 02:47:23 +02:00
|
|
|
} else if !s.Mode().IsRegular() {
|
2017-03-17 22:04:43 +01:00
|
|
|
return fmt.Errorf("%s is not a file, directory, or symlink", src)
|
2016-08-05 00:43:03 +02:00
|
|
|
} else {
|
|
|
|
fileSize = s.Size()
|
2017-03-16 03:02:51 +01:00
|
|
|
executable = s.Mode()&0100 != 0
|
2015-04-25 00:10:32 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 00:12:48 +02:00
|
|
|
r, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
header := &zip.FileHeader{
|
|
|
|
Name: dest,
|
|
|
|
Method: method,
|
|
|
|
UncompressedSize64: uint64(fileSize),
|
|
|
|
}
|
|
|
|
|
|
|
|
if executable {
|
|
|
|
header.SetMode(0700)
|
|
|
|
}
|
|
|
|
|
|
|
|
return z.writeFileContents(header, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// writes the contents of r according to the specifications in header
|
2017-08-24 00:41:35 +02:00
|
|
|
func (z *zipWriter) addManifest(dest string, src string, method uint16) error {
|
|
|
|
givenBytes, err := ioutil.ReadFile(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestMarker := []byte("Manifest-Version:")
|
|
|
|
header := append(manifestMarker, []byte(" 1.0\nCreated-By: soong_zip\n")...)
|
|
|
|
|
|
|
|
var finalBytes []byte
|
|
|
|
if !bytes.Contains(givenBytes, manifestMarker) {
|
|
|
|
finalBytes = append(append(header, givenBytes...), byte('\n'))
|
|
|
|
} else {
|
|
|
|
finalBytes = givenBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
byteReader := bytes.NewReader(finalBytes)
|
|
|
|
|
|
|
|
reader := &byteReaderCloser{*byteReader, ioutil.NopCloser(nil)}
|
|
|
|
|
|
|
|
fileHeader := &zip.FileHeader{
|
|
|
|
Name: dest,
|
|
|
|
Method: zip.Store,
|
|
|
|
UncompressedSize64: uint64(byteReader.Len()),
|
|
|
|
}
|
|
|
|
|
|
|
|
return z.writeFileContents(fileHeader, reader)
|
|
|
|
}
|
|
|
|
|
2017-08-24 00:12:48 +02:00
|
|
|
func (z *zipWriter) writeFileContents(header *zip.FileHeader, r readerSeekerCloser) (err error) {
|
|
|
|
|
|
|
|
header.SetModTime(z.time)
|
|
|
|
|
2015-04-25 00:08:38 +02:00
|
|
|
if z.directories {
|
2017-08-24 00:12:48 +02:00
|
|
|
dest := header.Name
|
2017-03-17 22:04:43 +01:00
|
|
|
dir, _ := filepath.Split(dest)
|
2015-04-25 00:08:38 +02:00
|
|
|
err := z.writeDirectory(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
compressChan := make(chan *zipEntry, 1)
|
|
|
|
z.writeOps <- compressChan
|
|
|
|
|
|
|
|
// Pre-fill a zipEntry, it will be sent in the compressChan once
|
|
|
|
// we're sure about the Method and CRC.
|
|
|
|
ze := &zipEntry{
|
2017-08-24 00:12:48 +02:00
|
|
|
fh: header,
|
2017-03-16 03:02:51 +01:00
|
|
|
}
|
2016-08-05 00:43:03 +02:00
|
|
|
|
2017-08-24 00:12:48 +02:00
|
|
|
ze.allocatedSize = int64(header.UncompressedSize64)
|
2017-08-18 06:43:21 +02:00
|
|
|
z.cpuRateLimiter.Request()
|
|
|
|
z.memoryRateLimiter.Request(ze.allocatedSize)
|
2016-08-05 00:43:03 +02:00
|
|
|
|
2017-08-24 00:12:48 +02:00
|
|
|
fileSize := int64(header.UncompressedSize64)
|
|
|
|
if fileSize == 0 {
|
|
|
|
fileSize = int64(header.UncompressedSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
if header.Method == zip.Deflate && fileSize >= minParallelFileSize {
|
2016-08-05 00:43:03 +02:00
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
|
|
|
|
// Allocate enough buffer to hold all readers. We'll limit
|
|
|
|
// this based on actual buffer sizes in RateLimit.
|
|
|
|
ze.futureReaders = make(chan chan io.Reader, (fileSize/parallelBlockSize)+1)
|
|
|
|
|
|
|
|
// Calculate the CRC in the background, since reading the entire
|
|
|
|
// file could take a while.
|
|
|
|
//
|
2017-08-24 00:12:48 +02:00
|
|
|
// We could split this up into chunks as well, but it's faster
|
2016-08-05 00:43:03 +02:00
|
|
|
// than the compression. Due to the Go Zip API, we also need to
|
|
|
|
// know the result before we can begin writing the compressed
|
|
|
|
// data out to the zipfile.
|
|
|
|
wg.Add(1)
|
2017-08-18 06:43:21 +02:00
|
|
|
go z.crcFile(r, ze, compressChan, wg)
|
2016-08-05 00:43:03 +02:00
|
|
|
|
|
|
|
for start := int64(0); start < fileSize; start += parallelBlockSize {
|
|
|
|
sr := io.NewSectionReader(r, start, parallelBlockSize)
|
|
|
|
resultChan := make(chan io.Reader, 1)
|
|
|
|
ze.futureReaders <- resultChan
|
|
|
|
|
2017-08-18 06:43:21 +02:00
|
|
|
z.cpuRateLimiter.Request()
|
2016-08-05 00:43:03 +02:00
|
|
|
|
|
|
|
last := !(start+parallelBlockSize < fileSize)
|
|
|
|
var dict []byte
|
|
|
|
if start >= windowSize {
|
|
|
|
dict, err = ioutil.ReadAll(io.NewSectionReader(r, start-windowSize, windowSize))
|
2017-08-24 00:12:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-05 00:43:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
2017-08-18 06:43:21 +02:00
|
|
|
go z.compressPartialFile(sr, dict, last, resultChan, wg)
|
2016-08-05 00:43:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(ze.futureReaders)
|
|
|
|
|
|
|
|
// Close the file handle after all readers are done
|
2017-08-24 00:12:48 +02:00
|
|
|
go func(wg *sync.WaitGroup, closer io.Closer) {
|
2016-08-05 00:43:03 +02:00
|
|
|
wg.Wait()
|
2017-08-24 00:12:48 +02:00
|
|
|
closer.Close()
|
2016-08-05 00:43:03 +02:00
|
|
|
}(wg, r)
|
|
|
|
} else {
|
2017-08-24 00:12:48 +02:00
|
|
|
go func() {
|
|
|
|
z.compressWholeFile(ze, r, compressChan)
|
|
|
|
r.Close()
|
|
|
|
}()
|
2016-08-05 00:43:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-18 06:43:21 +02:00
|
|
|
func (z *zipWriter) crcFile(r io.Reader, ze *zipEntry, resultChan chan *zipEntry, wg *sync.WaitGroup) {
|
2016-08-05 00:43:03 +02:00
|
|
|
defer wg.Done()
|
2017-08-18 06:43:21 +02:00
|
|
|
defer z.cpuRateLimiter.Finish()
|
2016-08-05 00:43:03 +02:00
|
|
|
|
|
|
|
crc := crc32.NewIEEE()
|
|
|
|
_, err := io.Copy(crc, r)
|
|
|
|
if err != nil {
|
|
|
|
z.errors <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ze.fh.CRC32 = crc.Sum32()
|
|
|
|
resultChan <- ze
|
|
|
|
close(resultChan)
|
|
|
|
}
|
|
|
|
|
2017-08-18 06:43:21 +02:00
|
|
|
func (z *zipWriter) compressPartialFile(r io.Reader, dict []byte, last bool, resultChan chan io.Reader, wg *sync.WaitGroup) {
|
2016-08-05 00:43:03 +02:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
result, err := z.compressBlock(r, dict, last)
|
|
|
|
if err != nil {
|
|
|
|
z.errors <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-18 06:43:21 +02:00
|
|
|
z.cpuRateLimiter.Finish()
|
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
resultChan <- result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *zipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
var fw *flate.Writer
|
|
|
|
var err error
|
|
|
|
if len(dict) > 0 {
|
|
|
|
// There's no way to Reset a Writer with a new dictionary, so
|
|
|
|
// don't use the Pool
|
|
|
|
fw, err = flate.NewWriterDict(buf, z.compLevel, dict)
|
|
|
|
} else {
|
|
|
|
var ok bool
|
|
|
|
if fw, ok = z.compressorPool.Get().(*flate.Writer); ok {
|
|
|
|
fw.Reset(buf)
|
|
|
|
} else {
|
|
|
|
fw, err = flate.NewWriter(buf, z.compLevel)
|
|
|
|
}
|
|
|
|
defer z.compressorPool.Put(fw)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(fw, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if last {
|
|
|
|
fw.Close()
|
|
|
|
} else {
|
|
|
|
fw.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2017-08-24 00:12:48 +02:00
|
|
|
func (z *zipWriter) compressWholeFile(ze *zipEntry, r io.ReadSeeker, compressChan chan *zipEntry) {
|
2016-08-05 00:43:03 +02:00
|
|
|
|
|
|
|
crc := crc32.NewIEEE()
|
2017-03-16 05:49:26 +01:00
|
|
|
_, err := io.Copy(crc, r)
|
2015-03-31 02:20:39 +02:00
|
|
|
if err != nil {
|
2016-08-05 00:43:03 +02:00
|
|
|
z.errors <- err
|
|
|
|
return
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-16 05:49:26 +01:00
|
|
|
ze.fh.CRC32 = crc.Sum32()
|
2016-08-05 00:43:03 +02:00
|
|
|
|
|
|
|
_, err = r.Seek(0, 0)
|
2015-03-31 02:20:39 +02:00
|
|
|
if err != nil {
|
2016-08-05 00:43:03 +02:00
|
|
|
z.errors <- err
|
|
|
|
return
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 00:12:48 +02:00
|
|
|
readFile := func(reader io.ReadSeeker) ([]byte, error) {
|
|
|
|
_, err := reader.Seek(0, 0)
|
2017-04-26 01:47:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-24 00:12:48 +02:00
|
|
|
buf, err := ioutil.ReadAll(reader)
|
2017-04-26 01:47:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
2016-08-05 00:43:03 +02:00
|
|
|
|
2017-03-16 05:49:26 +01:00
|
|
|
ze.futureReaders = make(chan chan io.Reader, 1)
|
2016-08-05 00:43:03 +02:00
|
|
|
futureReader := make(chan io.Reader, 1)
|
|
|
|
ze.futureReaders <- futureReader
|
|
|
|
close(ze.futureReaders)
|
|
|
|
|
2017-04-26 01:47:45 +02:00
|
|
|
if ze.fh.Method == zip.Deflate {
|
|
|
|
compressed, err := z.compressBlock(r, nil, true)
|
2016-08-05 00:43:03 +02:00
|
|
|
if err != nil {
|
|
|
|
z.errors <- err
|
|
|
|
return
|
|
|
|
}
|
2017-04-26 01:47:45 +02:00
|
|
|
if uint64(compressed.Len()) < ze.fh.UncompressedSize64 {
|
|
|
|
futureReader <- compressed
|
|
|
|
} else {
|
|
|
|
buf, err := readFile(r)
|
|
|
|
if err != nil {
|
|
|
|
z.errors <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ze.fh.Method = zip.Store
|
|
|
|
futureReader <- bytes.NewReader(buf)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf, err := readFile(r)
|
2016-08-05 00:43:03 +02:00
|
|
|
if err != nil {
|
|
|
|
z.errors <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ze.fh.Method = zip.Store
|
|
|
|
futureReader <- bytes.NewReader(buf)
|
|
|
|
}
|
2017-04-26 01:47:45 +02:00
|
|
|
|
2017-08-18 06:43:21 +02:00
|
|
|
z.cpuRateLimiter.Finish()
|
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
close(futureReader)
|
|
|
|
|
|
|
|
compressChan <- ze
|
|
|
|
close(compressChan)
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
2015-04-25 00:08:38 +02:00
|
|
|
|
2017-08-23 05:05:28 +02:00
|
|
|
func (z *zipWriter) addExtraField(zipHeader *zip.FileHeader, fieldHeader [2]byte, data []byte) {
|
|
|
|
// add the field header in little-endian order
|
|
|
|
zipHeader.Extra = append(zipHeader.Extra, fieldHeader[1], fieldHeader[0])
|
|
|
|
|
|
|
|
// specify the length of the data (in little-endian order)
|
|
|
|
dataLength := len(data)
|
|
|
|
lengthBytes := []byte{byte(dataLength % 256), byte(dataLength / 256)}
|
|
|
|
zipHeader.Extra = append(zipHeader.Extra, lengthBytes...)
|
|
|
|
|
|
|
|
// add the contents of the extra field
|
|
|
|
zipHeader.Extra = append(zipHeader.Extra, data...)
|
|
|
|
}
|
|
|
|
|
2015-04-25 00:08:38 +02:00
|
|
|
func (z *zipWriter) writeDirectory(dir string) error {
|
2017-08-16 03:05:56 +02:00
|
|
|
// clean the input
|
|
|
|
cleanDir := filepath.Clean(dir)
|
|
|
|
|
|
|
|
// discover any uncreated directories in the path
|
|
|
|
zipDirs := []string{}
|
|
|
|
for cleanDir != "" && cleanDir != "." && !z.createdDirs[cleanDir] {
|
2016-08-04 02:47:23 +02:00
|
|
|
|
2017-08-16 03:05:56 +02:00
|
|
|
z.createdDirs[cleanDir] = true
|
|
|
|
// parent directories precede their children
|
|
|
|
zipDirs = append([]string{cleanDir}, zipDirs...)
|
2015-04-25 00:08:38 +02:00
|
|
|
|
2017-08-16 03:05:56 +02:00
|
|
|
cleanDir = filepath.Dir(cleanDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a directory entry for each uncreated directory
|
|
|
|
for _, cleanDir := range zipDirs {
|
2015-04-25 00:08:38 +02:00
|
|
|
dirHeader := &zip.FileHeader{
|
2017-08-16 03:05:56 +02:00
|
|
|
Name: cleanDir + "/",
|
2015-04-25 00:08:38 +02:00
|
|
|
}
|
2016-08-04 02:47:23 +02:00
|
|
|
dirHeader.SetMode(0700 | os.ModeDir)
|
2015-04-25 00:08:38 +02:00
|
|
|
dirHeader.SetModTime(z.time)
|
|
|
|
|
2017-08-23 05:05:28 +02:00
|
|
|
if *emulateJar && dir == "META-INF/" {
|
|
|
|
// Jar files have a 0-length extra field with header "CAFE"
|
|
|
|
z.addExtraField(dirHeader, [2]byte{0xca, 0xfe}, []byte{})
|
|
|
|
}
|
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
ze := make(chan *zipEntry, 1)
|
|
|
|
ze <- &zipEntry{
|
|
|
|
fh: dirHeader,
|
2015-04-25 00:08:38 +02:00
|
|
|
}
|
2016-08-05 00:43:03 +02:00
|
|
|
close(ze)
|
|
|
|
z.writeOps <- ze
|
2015-04-25 00:08:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-04 02:47:23 +02:00
|
|
|
|
|
|
|
func (z *zipWriter) writeSymlink(rel, file string) error {
|
|
|
|
if z.directories {
|
|
|
|
dir, _ := filepath.Split(rel)
|
|
|
|
if err := z.writeDirectory(dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileHeader := &zip.FileHeader{
|
|
|
|
Name: rel,
|
|
|
|
}
|
|
|
|
fileHeader.SetModTime(z.time)
|
|
|
|
fileHeader.SetMode(0700 | os.ModeSymlink)
|
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
dest, err := os.Readlink(file)
|
2016-08-04 02:47:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
ze := make(chan *zipEntry, 1)
|
|
|
|
futureReaders := make(chan chan io.Reader, 1)
|
|
|
|
futureReader := make(chan io.Reader, 1)
|
|
|
|
futureReaders <- futureReader
|
|
|
|
close(futureReaders)
|
|
|
|
futureReader <- bytes.NewBufferString(dest)
|
|
|
|
close(futureReader)
|
|
|
|
|
|
|
|
ze <- &zipEntry{
|
|
|
|
fh: fileHeader,
|
|
|
|
futureReaders: futureReaders,
|
2016-08-04 02:47:23 +02:00
|
|
|
}
|
2016-08-05 00:43:03 +02:00
|
|
|
close(ze)
|
|
|
|
z.writeOps <- ze
|
2016-08-04 02:47:23 +02:00
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
return nil
|
2016-08-04 02:47:23 +02:00
|
|
|
}
|
2017-08-30 23:12:21 +02:00
|
|
|
|
|
|
|
func recursiveGlobFiles(path string) []string {
|
|
|
|
var files []string
|
|
|
|
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if !info.IsDir() {
|
|
|
|
files = append(files, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return files
|
|
|
|
}
|