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.
|
|
|
|
|
2017-10-12 21:19:14 +02:00
|
|
|
package zip
|
2015-03-31 02:20:39 +02:00
|
|
|
|
|
|
|
import (
|
2016-08-05 00:43:03 +02:00
|
|
|
"bytes"
|
|
|
|
"compress/flate"
|
2023-01-06 13:58:01 +01:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/binary"
|
2017-08-23 02:51:25 +02:00
|
|
|
"errors"
|
2015-03-31 02:20:39 +02:00
|
|
|
"fmt"
|
2023-01-06 13:58:01 +01:00
|
|
|
"hash"
|
2016-08-05 00:43:03 +02:00
|
|
|
"hash/crc32"
|
2015-03-31 02:20:39 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
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"
|
2018-09-19 02:05:15 +02:00
|
|
|
"syscall"
|
2015-03-31 02:20:39 +02:00
|
|
|
"time"
|
2021-03-23 22:16:05 +01:00
|
|
|
|
|
|
|
"android/soong/response"
|
2016-08-05 00:43:03 +02:00
|
|
|
|
2017-11-10 22:11:02 +01:00
|
|
|
"github.com/google/blueprint/pathtools"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2023-01-06 13:58:01 +01:00
|
|
|
// Sha256HeaderID is a custom Header ID for the `extra` field in
|
|
|
|
// the file header to store the SHA checksum.
|
|
|
|
const Sha256HeaderID = 0x4967
|
|
|
|
|
|
|
|
// Sha256HeaderSignature is the signature to verify that the extra
|
|
|
|
// data block is used to store the SHA checksum.
|
|
|
|
const Sha256HeaderSignature = 0x9514
|
|
|
|
|
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 {
|
2017-09-13 07:50:46 +02:00
|
|
|
*bytes.Reader
|
2017-08-24 00:41:35 +02:00
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:04:43 +01:00
|
|
|
type pathMapping struct {
|
|
|
|
dest, src string
|
2017-04-26 01:47:45 +02:00
|
|
|
zipMethod uint16
|
|
|
|
}
|
|
|
|
|
2018-09-28 00:00:07 +02:00
|
|
|
type FileArg struct {
|
|
|
|
PathPrefixInZip, SourcePrefixToStrip string
|
2023-05-09 00:05:29 +02:00
|
|
|
ExplicitPathInZip string
|
2018-09-28 00:00:07 +02:00
|
|
|
SourceFiles []string
|
|
|
|
JunkPaths bool
|
|
|
|
GlobDir string
|
|
|
|
}
|
|
|
|
|
|
|
|
type FileArgsBuilder struct {
|
|
|
|
state FileArg
|
|
|
|
err error
|
|
|
|
fs pathtools.FileSystem
|
2017-04-26 01:47:45 +02:00
|
|
|
|
2018-09-28 00:00:07 +02:00
|
|
|
fileArgs []FileArg
|
2017-04-26 01:47:45 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 00:00:07 +02:00
|
|
|
func NewFileArgsBuilder() *FileArgsBuilder {
|
|
|
|
return &FileArgsBuilder{
|
|
|
|
fs: pathtools.OsFs,
|
2017-04-26 01:47:45 +02:00
|
|
|
}
|
2018-09-28 00:00:07 +02:00
|
|
|
}
|
2017-04-26 01:47:45 +02:00
|
|
|
|
2018-09-28 00:00:07 +02:00
|
|
|
func (b *FileArgsBuilder) JunkPaths(v bool) *FileArgsBuilder {
|
|
|
|
b.state.JunkPaths = v
|
|
|
|
b.state.SourcePrefixToStrip = ""
|
|
|
|
return b
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 00:00:07 +02:00
|
|
|
func (b *FileArgsBuilder) SourcePrefixToStrip(prefixToStrip string) *FileArgsBuilder {
|
|
|
|
b.state.JunkPaths = false
|
|
|
|
b.state.SourcePrefixToStrip = prefixToStrip
|
|
|
|
return b
|
2017-10-12 21:18:19 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 00:00:07 +02:00
|
|
|
func (b *FileArgsBuilder) PathPrefixInZip(rootPrefix string) *FileArgsBuilder {
|
|
|
|
b.state.PathPrefixInZip = rootPrefix
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *FileArgsBuilder) File(name string) *FileArgsBuilder {
|
|
|
|
if b.err != nil {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
arg := b.state
|
|
|
|
arg.SourceFiles = []string{name}
|
|
|
|
b.fileArgs = append(b.fileArgs, arg)
|
2023-05-09 00:05:29 +02:00
|
|
|
|
|
|
|
if b.state.ExplicitPathInZip != "" {
|
|
|
|
b.state.ExplicitPathInZip = ""
|
|
|
|
}
|
2018-09-28 00:00:07 +02:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *FileArgsBuilder) Dir(name string) *FileArgsBuilder {
|
|
|
|
if b.err != nil {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
arg := b.state
|
|
|
|
arg.GlobDir = name
|
|
|
|
b.fileArgs = append(b.fileArgs, arg)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-11-17 04:00:27 +01:00
|
|
|
// List reads the file names from the given file and adds them to the source files list.
|
2018-09-28 00:00:07 +02:00
|
|
|
func (b *FileArgsBuilder) List(name string) *FileArgsBuilder {
|
|
|
|
if b.err != nil {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := b.fs.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
b.err = err
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
list, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
b.err = err
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
arg := b.state
|
2019-11-04 05:18:41 +01:00
|
|
|
arg.SourceFiles = strings.Fields(string(list))
|
2018-09-28 00:00:07 +02:00
|
|
|
b.fileArgs = append(b.fileArgs, arg)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-11-17 04:00:27 +01:00
|
|
|
// RspFile reads the file names from given .rsp file and adds them to the source files list.
|
2020-08-19 22:51:47 +02:00
|
|
|
func (b *FileArgsBuilder) RspFile(name string) *FileArgsBuilder {
|
|
|
|
if b.err != nil {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := b.fs.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
b.err = err
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2021-03-23 22:16:05 +01:00
|
|
|
arg := b.state
|
|
|
|
arg.SourceFiles, err = response.ReadRspFile(f)
|
2020-08-19 22:51:47 +02:00
|
|
|
if err != nil {
|
|
|
|
b.err = err
|
|
|
|
return b
|
|
|
|
}
|
2021-02-04 00:15:14 +01:00
|
|
|
for i := range arg.SourceFiles {
|
|
|
|
arg.SourceFiles[i] = pathtools.MatchEscape(arg.SourceFiles[i])
|
|
|
|
}
|
2020-08-19 22:51:47 +02:00
|
|
|
b.fileArgs = append(b.fileArgs, arg)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-05-09 00:05:29 +02:00
|
|
|
// ExplicitPathInZip sets the path in the zip file for the next File call.
|
|
|
|
func (b *FileArgsBuilder) ExplicitPathInZip(s string) *FileArgsBuilder {
|
|
|
|
b.state.ExplicitPathInZip = s
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-09-28 00:00:07 +02:00
|
|
|
func (b *FileArgsBuilder) Error() error {
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *FileArgsBuilder) FileArgs() []FileArg {
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b.fileArgs
|
|
|
|
}
|
2017-10-12 21:18:19 +02:00
|
|
|
|
2018-09-19 02:05:15 +02:00
|
|
|
type IncorrectRelativeRootError struct {
|
|
|
|
RelativeRoot string
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x IncorrectRelativeRootError) Error() string {
|
|
|
|
return fmt.Sprintf("path %q is outside relative root %q", x.Path, x.RelativeRoot)
|
|
|
|
}
|
|
|
|
|
2022-08-16 00:47:41 +02:00
|
|
|
type ConflictingFileError struct {
|
|
|
|
Dest string
|
|
|
|
Prev string
|
|
|
|
Src string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x ConflictingFileError) Error() string {
|
|
|
|
return fmt.Sprintf("destination %q has two files %q and %q", x.Dest, x.Prev, x.Src)
|
|
|
|
}
|
|
|
|
|
2017-10-12 21:18:19 +02:00
|
|
|
type ZipWriter struct {
|
2017-08-31 02:40:21 +02:00
|
|
|
time time.Time
|
|
|
|
createdFiles map[string]string
|
|
|
|
createdDirs map[string]string
|
|
|
|
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
|
2018-09-28 00:06:19 +02:00
|
|
|
|
2018-09-29 00:16:48 +02:00
|
|
|
followSymlinks pathtools.ShouldFollowSymlinks
|
|
|
|
ignoreMissingFiles bool
|
2018-09-22 00:12:39 +02:00
|
|
|
|
2018-09-29 00:16:48 +02:00
|
|
|
stderr io.Writer
|
|
|
|
fs pathtools.FileSystem
|
2023-01-06 13:58:01 +01:00
|
|
|
|
|
|
|
sha256Checksum bool
|
2016-08-05 00:43:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-12 21:18:19 +02:00
|
|
|
type ZipArgs struct {
|
2018-09-28 00:00:07 +02:00
|
|
|
FileArgs []FileArg
|
2017-10-12 21:18:19 +02:00
|
|
|
OutputFilePath string
|
|
|
|
EmulateJar bool
|
2019-06-17 23:12:41 +02:00
|
|
|
SrcJar bool
|
2017-10-12 21:18:19 +02:00
|
|
|
AddDirectoryEntriesToZip bool
|
|
|
|
CompressionLevel int
|
|
|
|
ManifestSourcePath string
|
|
|
|
NumParallelJobs int
|
|
|
|
NonDeflatedFiles map[string]bool
|
2017-11-10 22:11:02 +01:00
|
|
|
WriteIfChanged bool
|
2018-09-22 00:12:39 +02:00
|
|
|
StoreSymlinks bool
|
2018-09-29 00:16:48 +02:00
|
|
|
IgnoreMissingFiles bool
|
2023-01-06 13:58:01 +01:00
|
|
|
Sha256Checksum bool
|
2023-07-13 19:41:04 +02:00
|
|
|
DoNotWrite bool
|
2023-09-13 01:52:36 +02:00
|
|
|
Quiet bool
|
2018-09-22 00:12:39 +02:00
|
|
|
|
2018-09-29 00:16:48 +02:00
|
|
|
Stderr io.Writer
|
2018-09-22 00:12:39 +02:00
|
|
|
Filesystem pathtools.FileSystem
|
2017-10-12 21:18:19 +02:00
|
|
|
}
|
2015-03-31 02:20:39 +02:00
|
|
|
|
2020-11-17 04:00:27 +01:00
|
|
|
func zipTo(args ZipArgs, w io.Writer) error {
|
2017-10-12 21:18:19 +02:00
|
|
|
if args.EmulateJar {
|
|
|
|
args.AddDirectoryEntriesToZip = true
|
2017-08-23 05:05:28 +02:00
|
|
|
}
|
|
|
|
|
2018-09-22 00:12:39 +02:00
|
|
|
// Have Glob follow symlinks if they are not being stored as symlinks in the zip file.
|
|
|
|
followSymlinks := pathtools.ShouldFollowSymlinks(!args.StoreSymlinks)
|
|
|
|
|
2018-09-28 00:06:19 +02:00
|
|
|
z := &ZipWriter{
|
2018-09-29 00:16:48 +02:00
|
|
|
time: jar.DefaultTime,
|
|
|
|
createdDirs: make(map[string]string),
|
|
|
|
createdFiles: make(map[string]string),
|
|
|
|
directories: args.AddDirectoryEntriesToZip,
|
|
|
|
compLevel: args.CompressionLevel,
|
|
|
|
followSymlinks: followSymlinks,
|
|
|
|
ignoreMissingFiles: args.IgnoreMissingFiles,
|
|
|
|
stderr: args.Stderr,
|
|
|
|
fs: args.Filesystem,
|
2023-01-06 13:58:01 +01:00
|
|
|
sha256Checksum: args.Sha256Checksum,
|
2018-09-28 00:06:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if z.fs == nil {
|
|
|
|
z.fs = pathtools.OsFs
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
2018-09-28 00:06:19 +02:00
|
|
|
|
2018-09-29 00:16:48 +02:00
|
|
|
if z.stderr == nil {
|
|
|
|
z.stderr = os.Stderr
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:04:43 +01:00
|
|
|
pathMappings := []pathMapping{}
|
|
|
|
|
2018-09-15 00:06:31 +02:00
|
|
|
noCompression := args.CompressionLevel == 0
|
|
|
|
|
2017-10-12 21:18:19 +02:00
|
|
|
for _, fa := range args.FileArgs {
|
2018-09-19 02:05:15 +02:00
|
|
|
var srcs []string
|
|
|
|
for _, s := range fa.SourceFiles {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
if s == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-06 02:48:26 +02:00
|
|
|
result, err := z.fs.Glob(s, nil, followSymlinks)
|
2018-09-19 02:05:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-06 02:48:26 +02:00
|
|
|
if len(result.Matches) == 0 {
|
2018-09-29 00:16:48 +02:00
|
|
|
err := &os.PathError{
|
|
|
|
Op: "lstat",
|
2018-09-19 02:05:15 +02:00
|
|
|
Path: s,
|
|
|
|
Err: os.ErrNotExist,
|
|
|
|
}
|
2018-09-29 00:16:48 +02:00
|
|
|
if args.IgnoreMissingFiles {
|
2023-09-13 01:52:36 +02:00
|
|
|
if !args.Quiet {
|
|
|
|
fmt.Fprintln(z.stderr, "warning:", err)
|
|
|
|
}
|
2018-09-29 00:16:48 +02:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-19 02:05:15 +02:00
|
|
|
}
|
2021-04-06 02:48:26 +02:00
|
|
|
srcs = append(srcs, result.Matches...)
|
2018-09-19 02:05:15 +02:00
|
|
|
}
|
2017-10-12 21:18:19 +02:00
|
|
|
if fa.GlobDir != "" {
|
2018-09-19 02:05:15 +02:00
|
|
|
if exists, isDir, err := z.fs.Exists(fa.GlobDir); err != nil {
|
|
|
|
return err
|
2018-09-29 00:16:48 +02:00
|
|
|
} else if !exists && !args.IgnoreMissingFiles {
|
|
|
|
err := &os.PathError{
|
|
|
|
Op: "lstat",
|
2018-09-19 02:05:15 +02:00
|
|
|
Path: fa.GlobDir,
|
|
|
|
Err: os.ErrNotExist,
|
|
|
|
}
|
2018-09-29 00:16:48 +02:00
|
|
|
if args.IgnoreMissingFiles {
|
2023-09-13 01:52:36 +02:00
|
|
|
if !args.Quiet {
|
|
|
|
fmt.Fprintln(z.stderr, "warning:", err)
|
|
|
|
}
|
2018-09-29 00:16:48 +02:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if !isDir && !args.IgnoreMissingFiles {
|
|
|
|
err := &os.PathError{
|
|
|
|
Op: "lstat",
|
2018-09-19 02:05:15 +02:00
|
|
|
Path: fa.GlobDir,
|
|
|
|
Err: syscall.ENOTDIR,
|
|
|
|
}
|
2018-09-29 00:16:48 +02:00
|
|
|
if args.IgnoreMissingFiles {
|
2023-09-13 01:52:36 +02:00
|
|
|
if !args.Quiet {
|
|
|
|
fmt.Fprintln(z.stderr, "warning:", err)
|
|
|
|
}
|
2018-09-29 00:16:48 +02:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-19 02:05:15 +02:00
|
|
|
}
|
2021-04-06 02:48:26 +02:00
|
|
|
result, err := z.fs.Glob(filepath.Join(fa.GlobDir, "**/*"), nil, followSymlinks)
|
2018-09-19 02:05:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-06 02:48:26 +02:00
|
|
|
srcs = append(srcs, result.Matches...)
|
2017-08-30 23:12:21 +02:00
|
|
|
}
|
|
|
|
for _, src := range srcs {
|
2018-09-19 01:51:43 +02:00
|
|
|
err := fillPathPairs(fa, src, &pathMappings, args.NonDeflatedFiles, noCompression)
|
2018-09-15 00:06:31 +02:00
|
|
|
if err != nil {
|
2018-09-28 00:06:19 +02:00
|
|
|
return err
|
2017-03-17 22:04:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 23:12:41 +02:00
|
|
|
return z.write(w, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.SrcJar, args.NumParallelJobs)
|
2018-09-28 00:06:19 +02:00
|
|
|
}
|
|
|
|
|
2020-11-17 04:00:27 +01:00
|
|
|
// Zip creates an output zip archive from given sources.
|
2018-09-28 00:06:19 +02:00
|
|
|
func Zip(args ZipArgs) error {
|
|
|
|
if args.OutputFilePath == "" {
|
|
|
|
return fmt.Errorf("output file path must be nonempty")
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:11:02 +01:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
var out io.Writer = buf
|
|
|
|
|
2021-03-15 23:28:40 +01:00
|
|
|
var zipErr error
|
|
|
|
|
2023-07-13 19:41:04 +02:00
|
|
|
if args.DoNotWrite {
|
|
|
|
out = io.Discard
|
|
|
|
} else if !args.WriteIfChanged {
|
2017-11-10 22:11:02 +01:00
|
|
|
f, err := os.Create(args.OutputFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
defer func() {
|
2021-03-15 23:28:40 +01:00
|
|
|
if zipErr != nil {
|
2017-11-10 22:11:02 +01:00
|
|
|
os.Remove(args.OutputFilePath)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
out = f
|
|
|
|
}
|
|
|
|
|
2021-03-15 23:28:40 +01:00
|
|
|
zipErr = zipTo(args, out)
|
|
|
|
if zipErr != nil {
|
|
|
|
return zipErr
|
2017-11-10 22:11:02 +01:00
|
|
|
}
|
|
|
|
|
2023-07-13 19:41:04 +02:00
|
|
|
if args.WriteIfChanged && !args.DoNotWrite {
|
2017-11-10 22:11:02 +01:00
|
|
|
err := pathtools.WriteFileIfChanged(args.OutputFilePath, buf.Bytes(), 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-10-12 21:18:19 +02:00
|
|
|
|
2017-11-10 22:11:02 +01:00
|
|
|
return nil
|
2015-03-31 02:20:39 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 01:51:43 +02:00
|
|
|
func fillPathPairs(fa FileArg, src string, pathMappings *[]pathMapping,
|
2018-09-15 00:06:31 +02:00
|
|
|
nonDeflatedFiles map[string]bool, noCompression bool) error {
|
|
|
|
|
2018-09-19 01:51:43 +02:00
|
|
|
var dest string
|
|
|
|
|
2023-05-09 00:05:29 +02:00
|
|
|
if fa.ExplicitPathInZip != "" {
|
|
|
|
dest = fa.ExplicitPathInZip
|
|
|
|
} else if fa.JunkPaths {
|
2018-09-19 01:51:43 +02:00
|
|
|
dest = filepath.Base(src)
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
dest, err = filepath.Rel(fa.SourcePrefixToStrip, src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-19 02:05:15 +02:00
|
|
|
if strings.HasPrefix(dest, "../") {
|
|
|
|
return IncorrectRelativeRootError{
|
|
|
|
Path: src,
|
|
|
|
RelativeRoot: fa.SourcePrefixToStrip,
|
|
|
|
}
|
|
|
|
}
|
2017-03-17 22:04:43 +01:00
|
|
|
}
|
2018-09-19 01:51:43 +02:00
|
|
|
dest = filepath.Join(fa.PathPrefixInZip, dest)
|
2017-03-17 22:04:43 +01:00
|
|
|
|
2017-04-26 01:47:45 +02:00
|
|
|
zipMethod := zip.Deflate
|
2018-09-15 00:06:31 +02:00
|
|
|
if _, found := nonDeflatedFiles[dest]; found || noCompression {
|
2017-04-26 01:47:45 +02:00
|
|
|
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) {
|
2020-11-17 04:00:27 +01:00
|
|
|
sort.SliceStable(mappings, func(i int, j int) bool {
|
2017-08-23 02:51:25 +02:00
|
|
|
return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest)
|
2020-11-17 04:00:27 +01:00
|
|
|
})
|
2017-08-23 02:51:25 +02:00
|
|
|
}
|
|
|
|
|
2019-06-17 23:12:41 +02:00
|
|
|
func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string, emulateJar, srcJar bool,
|
|
|
|
parallelJobs int) error {
|
|
|
|
|
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-10-12 21:18:19 +02:00
|
|
|
z.cpuRateLimiter = NewCPURateLimiter(int64(parallelJobs))
|
2017-08-18 06:43:21 +02:00
|
|
|
z.memoryRateLimiter = NewMemoryRateLimiter(0)
|
|
|
|
defer func() {
|
|
|
|
z.cpuRateLimiter.Stop()
|
|
|
|
z.memoryRateLimiter.Stop()
|
|
|
|
}()
|
2017-08-23 02:51:25 +02:00
|
|
|
|
2017-10-12 21:18:19 +02:00
|
|
|
if manifest != "" && !emulateJar {
|
2017-09-13 07:50:46 +02:00
|
|
|
return errors.New("must specify --jar when specifying a manifest via -m")
|
2017-08-23 02:51:25 +02:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:18:19 +02:00
|
|
|
if emulateJar {
|
2017-09-13 07:50:46 +02:00
|
|
|
// manifest may be empty, in which case addManifest will fill in a default
|
|
|
|
pathMappings = append(pathMappings, pathMapping{jar.ManifestFile, manifest, zip.Deflate})
|
|
|
|
|
2017-08-23 02:51:25 +02:00
|
|
|
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-10-12 21:18:19 +02:00
|
|
|
if emulateJar && ele.dest == jar.ManifestFile {
|
2017-08-24 00:41:35 +02:00
|
|
|
err = z.addManifest(ele.dest, ele.src, ele.zipMethod)
|
|
|
|
} else {
|
2019-06-17 23:12:41 +02:00
|
|
|
err = z.addFile(ele.dest, ele.src, ele.zipMethod, emulateJar, srcJar)
|
2017-08-24 00:41:35 +02:00
|
|
|
}
|
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
|
|
|
|
|
2017-11-10 22:11:02 +01:00
|
|
|
var err error
|
2016-08-05 00:43:03 +02:00
|
|
|
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-11-10 22:11:02 +01:00
|
|
|
_, err := io.Copy(currentWriter, reader)
|
2016-08-05 00:43:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
currentReader = nil
|
|
|
|
|
2017-11-10 22:11:02 +01:00
|
|
|
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 {
|
2017-11-10 22:11:02 +01:00
|
|
|
case err := <-z.errors:
|
2016-08-05 00:43:03 +02:00
|
|
|
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>
|
2019-06-17 23:12:41 +02:00
|
|
|
func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar, srcJar bool) 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
|
|
|
|
2018-09-22 00:12:39 +02:00
|
|
|
var s os.FileInfo
|
|
|
|
var err error
|
|
|
|
if z.followSymlinks {
|
|
|
|
s, err = z.fs.Stat(src)
|
|
|
|
} else {
|
|
|
|
s, err = z.fs.Lstat(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2018-09-29 00:16:48 +02:00
|
|
|
if os.IsNotExist(err) && z.ignoreMissingFiles {
|
|
|
|
fmt.Fprintln(z.stderr, "warning:", err)
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-04 02:47:23 +02:00
|
|
|
return err
|
2019-06-17 23:12:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
createParentDirs := func(dest, src string) error {
|
2017-10-12 21:18:19 +02:00
|
|
|
if err := z.writeDirectory(filepath.Dir(dest), src, emulateJar); err != nil {
|
2017-08-31 02:40:21 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if prev, exists := z.createdDirs[dest]; exists {
|
|
|
|
return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
|
|
|
|
}
|
2022-08-16 00:47:41 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
checkDuplicateFiles := func(dest, src string) (bool, error) {
|
2017-08-31 02:40:21 +02:00
|
|
|
if prev, exists := z.createdFiles[dest]; exists {
|
2022-08-16 00:47:41 +02:00
|
|
|
if prev != src {
|
|
|
|
return true, ConflictingFileError{
|
|
|
|
Dest: dest,
|
|
|
|
Prev: prev,
|
|
|
|
Src: src,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
2017-08-31 02:40:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
z.createdFiles[dest] = src
|
2022-08-16 00:47:41 +02:00
|
|
|
return false, nil
|
2019-06-17 23:12:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.IsDir() {
|
|
|
|
if z.directories {
|
|
|
|
return z.writeDirectory(dest, src, emulateJar)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} else if s.Mode()&os.ModeSymlink != 0 {
|
|
|
|
err = createParentDirs(dest, src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-16 00:47:41 +02:00
|
|
|
duplicate, err := checkDuplicateFiles(dest, src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if duplicate {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-17 23:12:41 +02:00
|
|
|
return z.writeSymlink(dest, src)
|
|
|
|
} else if s.Mode().IsRegular() {
|
|
|
|
r, err := z.fs.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if srcJar && filepath.Ext(src) == ".java" {
|
|
|
|
// rewrite the destination using the package path if it can be determined
|
|
|
|
pkg, err := jar.JavaPackage(r, src)
|
|
|
|
if err != nil {
|
|
|
|
// ignore errors for now, leaving the file at in its original location in the zip
|
|
|
|
} else {
|
|
|
|
dest = filepath.Join(filepath.Join(strings.Split(pkg, ".")...), filepath.Base(src))
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = r.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-31 02:40:21 +02:00
|
|
|
}
|
|
|
|
|
2016-08-05 00:43:03 +02:00
|
|
|
fileSize = s.Size()
|
2017-03-16 03:02:51 +01:00
|
|
|
executable = s.Mode()&0100 != 0
|
2015-04-25 00:10:32 +02:00
|
|
|
|
2019-06-17 23:12:41 +02:00
|
|
|
header := &zip.FileHeader{
|
|
|
|
Name: dest,
|
|
|
|
Method: method,
|
|
|
|
UncompressedSize64: uint64(fileSize),
|
|
|
|
}
|
2017-08-24 00:12:48 +02:00
|
|
|
|
2021-06-02 21:56:08 +02:00
|
|
|
mode := os.FileMode(0644)
|
2019-06-17 23:12:41 +02:00
|
|
|
if executable {
|
2021-06-02 21:56:08 +02:00
|
|
|
mode = 0755
|
2019-06-17 23:12:41 +02:00
|
|
|
}
|
2021-05-12 01:03:57 +02:00
|
|
|
header.SetMode(mode)
|
2017-08-24 00:12:48 +02:00
|
|
|
|
2019-06-17 23:12:41 +02:00
|
|
|
err = createParentDirs(dest, src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-24 00:12:48 +02:00
|
|
|
|
2022-08-16 00:47:41 +02:00
|
|
|
duplicate, err := checkDuplicateFiles(dest, src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if duplicate {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-17 23:12:41 +02:00
|
|
|
return z.writeFileContents(header, r)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("%s is not a file, directory, or symlink", src)
|
|
|
|
}
|
2017-08-24 00:12:48 +02:00
|
|
|
}
|
|
|
|
|
2020-11-17 04:00:27 +01:00
|
|
|
func (z *ZipWriter) addManifest(dest string, src string, _ uint16) error {
|
2017-08-31 02:40:21 +02:00
|
|
|
if prev, exists := z.createdDirs[dest]; exists {
|
|
|
|
return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
|
|
|
|
}
|
|
|
|
if prev, exists := z.createdFiles[dest]; exists {
|
2022-08-16 00:47:41 +02:00
|
|
|
if prev != src {
|
|
|
|
return ConflictingFileError{
|
|
|
|
Dest: dest,
|
|
|
|
Prev: prev,
|
|
|
|
Src: src,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2017-08-31 02:40:21 +02:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:18:19 +02:00
|
|
|
if err := z.writeDirectory(filepath.Dir(dest), src, true); err != nil {
|
2017-09-13 07:50:46 +02:00
|
|
|
return err
|
2017-08-24 00:41:35 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 00:06:19 +02:00
|
|
|
var contents []byte
|
|
|
|
if src != "" {
|
|
|
|
f, err := z.fs.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
contents, err = ioutil.ReadAll(f)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fh, buf, err := jar.ManifestFileContents(contents)
|
2017-09-13 07:50:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-08-24 00:41:35 +02:00
|
|
|
}
|
|
|
|
|
2017-09-13 07:50:46 +02:00
|
|
|
reader := &byteReaderCloser{bytes.NewReader(buf), ioutil.NopCloser(nil)}
|
|
|
|
|
|
|
|
return z.writeFileContents(fh, reader)
|
2017-08-24 00:41:35 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 00:06:19 +02:00
|
|
|
func (z *ZipWriter) writeFileContents(header *zip.FileHeader, r pathtools.ReaderAtSeekerCloser) (err error) {
|
2017-08-24 00:12:48 +02:00
|
|
|
|
|
|
|
header.SetModTime(z.time)
|
|
|
|
|
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)
|
|
|
|
|
2023-01-06 13:58:01 +01:00
|
|
|
// Calculate the CRC and SHA256 in the background, since reading
|
|
|
|
// the entire file could take a while.
|
2016-08-05 00:43:03 +02:00
|
|
|
//
|
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.
|
2023-01-06 13:58:01 +01:00
|
|
|
//
|
|
|
|
// We calculate SHA256 only if `-sha256` is set.
|
2016-08-05 00:43:03 +02:00
|
|
|
wg.Add(1)
|
2023-01-06 13:58:01 +01:00
|
|
|
go z.checksumFileAsync(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
|
|
|
|
}
|
|
|
|
|
2023-01-06 13:58:01 +01:00
|
|
|
func (z *ZipWriter) checksumFileAsync(r io.ReadSeeker, 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
|
|
|
|
2023-01-06 13:58:01 +01:00
|
|
|
z.checksumFile(r, ze)
|
|
|
|
|
|
|
|
resultChan <- ze
|
|
|
|
close(resultChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ZipWriter) checksumFile(r io.ReadSeeker, ze *zipEntry) {
|
2016-08-05 00:43:03 +02:00
|
|
|
crc := crc32.NewIEEE()
|
2023-01-06 13:58:01 +01:00
|
|
|
writers := []io.Writer{crc}
|
|
|
|
|
|
|
|
var shaHasher hash.Hash
|
|
|
|
if z.sha256Checksum && !ze.fh.Mode().IsDir() {
|
|
|
|
shaHasher = sha256.New()
|
|
|
|
writers = append(writers, shaHasher)
|
|
|
|
}
|
|
|
|
|
|
|
|
w := io.MultiWriter(writers...)
|
|
|
|
|
|
|
|
_, err := io.Copy(w, r)
|
2016-08-05 00:43:03 +02:00
|
|
|
if err != nil {
|
|
|
|
z.errors <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ze.fh.CRC32 = crc.Sum32()
|
2023-01-06 13:58:01 +01:00
|
|
|
if shaHasher != nil {
|
|
|
|
z.appendSHAToExtra(ze, shaHasher.Sum(nil))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ZipWriter) appendSHAToExtra(ze *zipEntry, checksum []byte) {
|
|
|
|
// The block of SHA256 checksum consist of:
|
|
|
|
// - Header ID, equals to Sha256HeaderID (2 bytes)
|
|
|
|
// - Data size (2 bytes)
|
|
|
|
// - Data block:
|
|
|
|
// - Signature, equals to Sha256HeaderSignature (2 bytes)
|
|
|
|
// - Data, SHA checksum value
|
|
|
|
var buf []byte
|
|
|
|
buf = binary.LittleEndian.AppendUint16(buf, Sha256HeaderID)
|
|
|
|
buf = binary.LittleEndian.AppendUint16(buf, uint16(len(checksum)+2))
|
|
|
|
buf = binary.LittleEndian.AppendUint16(buf, Sha256HeaderSignature)
|
|
|
|
buf = append(buf, checksum...)
|
|
|
|
ze.fh.Extra = append(ze.fh.Extra, buf...)
|
2016-08-05 00:43:03 +02:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:18:19 +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
|
|
|
|
}
|
|
|
|
|
2017-10-12 21:18:19 +02:00
|
|
|
func (z *ZipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) {
|
2016-08-05 00:43:03 +02:00
|
|
|
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-10-12 21:18:19 +02:00
|
|
|
func (z *ZipWriter) compressWholeFile(ze *zipEntry, r io.ReadSeeker, compressChan chan *zipEntry) {
|
2023-01-06 13:58:01 +01:00
|
|
|
z.checksumFile(r, ze)
|
2016-08-05 00:43:03 +02:00
|
|
|
|
2023-01-06 13:58:01 +01: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-31 02:40:21 +02:00
|
|
|
// writeDirectory annotates that dir is a directory created for the src file or directory, and adds
|
|
|
|
// the directory entry to the zip file if directories are enabled.
|
2017-10-12 21:18:19 +02:00
|
|
|
func (z *ZipWriter) writeDirectory(dir string, src string, emulateJar bool) error {
|
2017-08-16 03:05:56 +02:00
|
|
|
// clean the input
|
2017-08-31 02:40:21 +02:00
|
|
|
dir = filepath.Clean(dir)
|
2017-08-16 03:05:56 +02:00
|
|
|
|
|
|
|
// discover any uncreated directories in the path
|
2020-11-17 04:00:27 +01:00
|
|
|
var zipDirs []string
|
2017-08-31 02:40:21 +02:00
|
|
|
for dir != "" && dir != "." {
|
|
|
|
if _, exists := z.createdDirs[dir]; exists {
|
|
|
|
break
|
|
|
|
}
|
2016-08-04 02:47:23 +02:00
|
|
|
|
2017-08-31 02:40:21 +02:00
|
|
|
if prev, exists := z.createdFiles[dir]; exists {
|
|
|
|
return fmt.Errorf("destination %q is both a directory %q and a file %q", dir, src, prev)
|
|
|
|
}
|
|
|
|
|
|
|
|
z.createdDirs[dir] = src
|
2017-08-16 03:05:56 +02:00
|
|
|
// parent directories precede their children
|
2017-08-31 02:40:21 +02:00
|
|
|
zipDirs = append([]string{dir}, zipDirs...)
|
2015-04-25 00:08:38 +02:00
|
|
|
|
2017-08-31 02:40:21 +02:00
|
|
|
dir = filepath.Dir(dir)
|
2017-08-16 03:05:56 +02:00
|
|
|
}
|
|
|
|
|
2017-08-31 02:40:21 +02:00
|
|
|
if z.directories {
|
|
|
|
// make a directory entry for each uncreated directory
|
|
|
|
for _, cleanDir := range zipDirs {
|
2017-09-13 07:50:46 +02:00
|
|
|
var dirHeader *zip.FileHeader
|
2015-04-25 00:08:38 +02:00
|
|
|
|
2017-10-12 21:18:19 +02:00
|
|
|
if emulateJar && cleanDir+"/" == jar.MetaDir {
|
2017-09-13 07:50:46 +02:00
|
|
|
dirHeader = jar.MetaDirFileHeader()
|
|
|
|
} else {
|
|
|
|
dirHeader = &zip.FileHeader{
|
|
|
|
Name: cleanDir + "/",
|
|
|
|
}
|
2021-06-02 21:56:08 +02:00
|
|
|
dirHeader.SetMode(0755 | os.ModeDir)
|
2017-08-31 02:40:21 +02:00
|
|
|
}
|
2017-08-23 05:05:28 +02:00
|
|
|
|
2017-09-13 07:50:46 +02:00
|
|
|
dirHeader.SetModTime(z.time)
|
|
|
|
|
2017-08-31 02:40:21 +02:00
|
|
|
ze := make(chan *zipEntry, 1)
|
|
|
|
ze <- &zipEntry{
|
|
|
|
fh: dirHeader,
|
|
|
|
}
|
|
|
|
close(ze)
|
|
|
|
z.writeOps <- ze
|
2015-04-25 00:08:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-04 02:47:23 +02:00
|
|
|
|
2017-10-12 21:18:19 +02:00
|
|
|
func (z *ZipWriter) writeSymlink(rel, file string) error {
|
2016-08-04 02:47:23 +02:00
|
|
|
fileHeader := &zip.FileHeader{
|
|
|
|
Name: rel,
|
|
|
|
}
|
|
|
|
fileHeader.SetModTime(z.time)
|
2018-06-23 01:37:47 +02:00
|
|
|
fileHeader.SetMode(0777 | os.ModeSymlink)
|
2016-08-04 02:47:23 +02:00
|
|
|
|
2018-09-28 00:06:19 +02:00
|
|
|
dest, err := z.fs.Readlink(file)
|
2016-08-04 02:47:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-23 01:37:47 +02:00
|
|
|
fileHeader.UncompressedSize64 = uint64(len(dest))
|
|
|
|
fileHeader.CRC32 = crc32.ChecksumIEEE([]byte(dest))
|
|
|
|
|
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
|
|
|
}
|