Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 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 | |
Jeff Gaston | 11b5c51 | 2017-10-12 12:19:14 -0700 | [diff] [blame] | 15 | package zip |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 18 | "bytes" |
| 19 | "compress/flate" |
Jeff Gaston | a297695 | 2017-08-22 17:51:25 -0700 | [diff] [blame] | 20 | "errors" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 21 | "fmt" |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 22 | "hash/crc32" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 23 | "io" |
| 24 | "io/ioutil" |
| 25 | "os" |
| 26 | "path/filepath" |
Jeff Gaston | a297695 | 2017-08-22 17:51:25 -0700 | [diff] [blame] | 27 | "sort" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 28 | "strings" |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 29 | "sync" |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 30 | "syscall" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 31 | "time" |
Colin Cross | fd708b5 | 2021-03-23 14:16:05 -0700 | [diff] [blame] | 32 | |
| 33 | "android/soong/response" |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 34 | |
Colin Cross | f83c150 | 2017-11-10 13:11:02 -0800 | [diff] [blame] | 35 | "github.com/google/blueprint/pathtools" |
| 36 | |
Jeff Gaston | a297695 | 2017-08-22 17:51:25 -0700 | [diff] [blame] | 37 | "android/soong/jar" |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 38 | "android/soong/third_party/zip" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 39 | ) |
| 40 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 41 | // Block size used during parallel compression of a single file. |
| 42 | const parallelBlockSize = 1 * 1024 * 1024 // 1MB |
| 43 | |
| 44 | // Minimum file size to use parallel compression. It requires more |
| 45 | // flate.Writer allocations, since we can't change the dictionary |
| 46 | // during Reset |
| 47 | const minParallelFileSize = parallelBlockSize * 6 |
| 48 | |
| 49 | // Size of the ZIP compression window (32KB) |
| 50 | const windowSize = 32 * 1024 |
| 51 | |
| 52 | type nopCloser struct { |
| 53 | io.Writer |
| 54 | } |
| 55 | |
| 56 | func (nopCloser) Close() error { |
| 57 | return nil |
| 58 | } |
| 59 | |
Jeff Gaston | cef50b9 | 2017-08-23 15:41:35 -0700 | [diff] [blame] | 60 | type byteReaderCloser struct { |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 61 | *bytes.Reader |
Jeff Gaston | cef50b9 | 2017-08-23 15:41:35 -0700 | [diff] [blame] | 62 | io.Closer |
| 63 | } |
| 64 | |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 65 | type pathMapping struct { |
| 66 | dest, src string |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 67 | zipMethod uint16 |
| 68 | } |
| 69 | |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 70 | type FileArg struct { |
| 71 | PathPrefixInZip, SourcePrefixToStrip string |
| 72 | SourceFiles []string |
Colin Cross | b7c6911 | 2018-09-18 16:51:43 -0700 | [diff] [blame] | 73 | JunkPaths bool |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 74 | GlobDir string |
| 75 | } |
| 76 | |
Colin Cross | fe945b4 | 2018-09-27 15:00:07 -0700 | [diff] [blame] | 77 | type FileArgsBuilder struct { |
| 78 | state FileArg |
| 79 | err error |
| 80 | fs pathtools.FileSystem |
| 81 | |
| 82 | fileArgs []FileArg |
| 83 | } |
| 84 | |
| 85 | func NewFileArgsBuilder() *FileArgsBuilder { |
| 86 | return &FileArgsBuilder{ |
| 87 | fs: pathtools.OsFs, |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func (b *FileArgsBuilder) JunkPaths(v bool) *FileArgsBuilder { |
| 92 | b.state.JunkPaths = v |
| 93 | b.state.SourcePrefixToStrip = "" |
| 94 | return b |
| 95 | } |
| 96 | |
| 97 | func (b *FileArgsBuilder) SourcePrefixToStrip(prefixToStrip string) *FileArgsBuilder { |
| 98 | b.state.JunkPaths = false |
| 99 | b.state.SourcePrefixToStrip = prefixToStrip |
| 100 | return b |
| 101 | } |
| 102 | |
| 103 | func (b *FileArgsBuilder) PathPrefixInZip(rootPrefix string) *FileArgsBuilder { |
| 104 | b.state.PathPrefixInZip = rootPrefix |
| 105 | return b |
| 106 | } |
| 107 | |
| 108 | func (b *FileArgsBuilder) File(name string) *FileArgsBuilder { |
| 109 | if b.err != nil { |
| 110 | return b |
| 111 | } |
| 112 | |
| 113 | arg := b.state |
| 114 | arg.SourceFiles = []string{name} |
| 115 | b.fileArgs = append(b.fileArgs, arg) |
| 116 | return b |
| 117 | } |
| 118 | |
| 119 | func (b *FileArgsBuilder) Dir(name string) *FileArgsBuilder { |
| 120 | if b.err != nil { |
| 121 | return b |
| 122 | } |
| 123 | |
| 124 | arg := b.state |
| 125 | arg.GlobDir = name |
| 126 | b.fileArgs = append(b.fileArgs, arg) |
| 127 | return b |
| 128 | } |
| 129 | |
Sasha Smundak | 8eedba6 | 2020-11-16 19:00:27 -0800 | [diff] [blame] | 130 | // List reads the file names from the given file and adds them to the source files list. |
Colin Cross | fe945b4 | 2018-09-27 15:00:07 -0700 | [diff] [blame] | 131 | func (b *FileArgsBuilder) List(name string) *FileArgsBuilder { |
| 132 | if b.err != nil { |
| 133 | return b |
| 134 | } |
| 135 | |
| 136 | f, err := b.fs.Open(name) |
| 137 | if err != nil { |
| 138 | b.err = err |
| 139 | return b |
| 140 | } |
| 141 | defer f.Close() |
| 142 | |
| 143 | list, err := ioutil.ReadAll(f) |
| 144 | if err != nil { |
| 145 | b.err = err |
| 146 | return b |
| 147 | } |
| 148 | |
| 149 | arg := b.state |
Jiyong Park | 04bbf98 | 2019-11-04 13:18:41 +0900 | [diff] [blame] | 150 | arg.SourceFiles = strings.Fields(string(list)) |
Colin Cross | fe945b4 | 2018-09-27 15:00:07 -0700 | [diff] [blame] | 151 | b.fileArgs = append(b.fileArgs, arg) |
| 152 | return b |
| 153 | } |
| 154 | |
Sasha Smundak | 8eedba6 | 2020-11-16 19:00:27 -0800 | [diff] [blame] | 155 | // RspFile reads the file names from given .rsp file and adds them to the source files list. |
Colin Cross | 053fca1 | 2020-08-19 13:51:47 -0700 | [diff] [blame] | 156 | func (b *FileArgsBuilder) RspFile(name string) *FileArgsBuilder { |
| 157 | if b.err != nil { |
| 158 | return b |
| 159 | } |
| 160 | |
| 161 | f, err := b.fs.Open(name) |
| 162 | if err != nil { |
| 163 | b.err = err |
| 164 | return b |
| 165 | } |
| 166 | defer f.Close() |
| 167 | |
Colin Cross | fd708b5 | 2021-03-23 14:16:05 -0700 | [diff] [blame] | 168 | arg := b.state |
| 169 | arg.SourceFiles, err = response.ReadRspFile(f) |
Colin Cross | 053fca1 | 2020-08-19 13:51:47 -0700 | [diff] [blame] | 170 | if err != nil { |
| 171 | b.err = err |
| 172 | return b |
| 173 | } |
Colin Cross | caf4d4c | 2021-02-03 15:15:14 -0800 | [diff] [blame] | 174 | for i := range arg.SourceFiles { |
| 175 | arg.SourceFiles[i] = pathtools.MatchEscape(arg.SourceFiles[i]) |
| 176 | } |
Colin Cross | 053fca1 | 2020-08-19 13:51:47 -0700 | [diff] [blame] | 177 | b.fileArgs = append(b.fileArgs, arg) |
| 178 | return b |
| 179 | } |
| 180 | |
Colin Cross | fe945b4 | 2018-09-27 15:00:07 -0700 | [diff] [blame] | 181 | func (b *FileArgsBuilder) Error() error { |
| 182 | if b == nil { |
| 183 | return nil |
| 184 | } |
| 185 | return b.err |
| 186 | } |
| 187 | |
| 188 | func (b *FileArgsBuilder) FileArgs() []FileArg { |
| 189 | if b == nil { |
| 190 | return nil |
| 191 | } |
| 192 | return b.fileArgs |
| 193 | } |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 194 | |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 195 | type IncorrectRelativeRootError struct { |
| 196 | RelativeRoot string |
| 197 | Path string |
| 198 | } |
| 199 | |
| 200 | func (x IncorrectRelativeRootError) Error() string { |
| 201 | return fmt.Sprintf("path %q is outside relative root %q", x.Path, x.RelativeRoot) |
| 202 | } |
| 203 | |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 204 | type ZipWriter struct { |
Colin Cross | e558097 | 2017-08-30 17:40:21 -0700 | [diff] [blame] | 205 | time time.Time |
| 206 | createdFiles map[string]string |
| 207 | createdDirs map[string]string |
| 208 | directories bool |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 209 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 210 | errors chan error |
| 211 | writeOps chan chan *zipEntry |
| 212 | |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 213 | cpuRateLimiter *CPURateLimiter |
| 214 | memoryRateLimiter *MemoryRateLimiter |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 215 | |
| 216 | compressorPool sync.Pool |
| 217 | compLevel int |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 218 | |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 219 | followSymlinks pathtools.ShouldFollowSymlinks |
| 220 | ignoreMissingFiles bool |
Colin Cross | 09f1105 | 2018-09-21 15:12:39 -0700 | [diff] [blame] | 221 | |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 222 | stderr io.Writer |
| 223 | fs pathtools.FileSystem |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | type zipEntry struct { |
| 227 | fh *zip.FileHeader |
| 228 | |
| 229 | // List of delayed io.Reader |
| 230 | futureReaders chan chan io.Reader |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 231 | |
| 232 | // Only used for passing into the MemoryRateLimiter to ensure we |
| 233 | // release as much memory as much as we request |
| 234 | allocatedSize int64 |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 237 | type ZipArgs struct { |
Colin Cross | fe945b4 | 2018-09-27 15:00:07 -0700 | [diff] [blame] | 238 | FileArgs []FileArg |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 239 | OutputFilePath string |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 240 | EmulateJar bool |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 241 | SrcJar bool |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 242 | AddDirectoryEntriesToZip bool |
| 243 | CompressionLevel int |
| 244 | ManifestSourcePath string |
| 245 | NumParallelJobs int |
| 246 | NonDeflatedFiles map[string]bool |
Colin Cross | f83c150 | 2017-11-10 13:11:02 -0800 | [diff] [blame] | 247 | WriteIfChanged bool |
Colin Cross | 09f1105 | 2018-09-21 15:12:39 -0700 | [diff] [blame] | 248 | StoreSymlinks bool |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 249 | IgnoreMissingFiles bool |
Colin Cross | 09f1105 | 2018-09-21 15:12:39 -0700 | [diff] [blame] | 250 | |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 251 | Stderr io.Writer |
Colin Cross | 09f1105 | 2018-09-21 15:12:39 -0700 | [diff] [blame] | 252 | Filesystem pathtools.FileSystem |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 253 | } |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 254 | |
Sasha Smundak | 8eedba6 | 2020-11-16 19:00:27 -0800 | [diff] [blame] | 255 | func zipTo(args ZipArgs, w io.Writer) error { |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 256 | if args.EmulateJar { |
| 257 | args.AddDirectoryEntriesToZip = true |
Jeff Gaston | 8edbb3a | 2017-08-22 20:05:28 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Colin Cross | 09f1105 | 2018-09-21 15:12:39 -0700 | [diff] [blame] | 260 | // Have Glob follow symlinks if they are not being stored as symlinks in the zip file. |
| 261 | followSymlinks := pathtools.ShouldFollowSymlinks(!args.StoreSymlinks) |
| 262 | |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 263 | z := &ZipWriter{ |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 264 | time: jar.DefaultTime, |
| 265 | createdDirs: make(map[string]string), |
| 266 | createdFiles: make(map[string]string), |
| 267 | directories: args.AddDirectoryEntriesToZip, |
| 268 | compLevel: args.CompressionLevel, |
| 269 | followSymlinks: followSymlinks, |
| 270 | ignoreMissingFiles: args.IgnoreMissingFiles, |
| 271 | stderr: args.Stderr, |
| 272 | fs: args.Filesystem, |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 273 | } |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 274 | |
| 275 | if z.fs == nil { |
| 276 | z.fs = pathtools.OsFs |
| 277 | } |
| 278 | |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 279 | if z.stderr == nil { |
| 280 | z.stderr = os.Stderr |
| 281 | } |
| 282 | |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 283 | pathMappings := []pathMapping{} |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 284 | |
Colin Cross | d321629 | 2018-09-14 15:06:31 -0700 | [diff] [blame] | 285 | noCompression := args.CompressionLevel == 0 |
| 286 | |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 287 | for _, fa := range args.FileArgs { |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 288 | var srcs []string |
| 289 | for _, s := range fa.SourceFiles { |
| 290 | s = strings.TrimSpace(s) |
| 291 | if s == "" { |
| 292 | continue |
| 293 | } |
| 294 | |
Colin Cross | 82ea3fb | 2021-04-05 17:48:26 -0700 | [diff] [blame] | 295 | result, err := z.fs.Glob(s, nil, followSymlinks) |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 296 | if err != nil { |
| 297 | return err |
| 298 | } |
Colin Cross | 82ea3fb | 2021-04-05 17:48:26 -0700 | [diff] [blame] | 299 | if len(result.Matches) == 0 { |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 300 | err := &os.PathError{ |
| 301 | Op: "lstat", |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 302 | Path: s, |
| 303 | Err: os.ErrNotExist, |
| 304 | } |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 305 | if args.IgnoreMissingFiles { |
Dan Willemsen | edc934c | 2018-12-27 12:41:25 -0800 | [diff] [blame] | 306 | fmt.Fprintln(z.stderr, "warning:", err) |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 307 | } else { |
| 308 | return err |
| 309 | } |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 310 | } |
Colin Cross | 82ea3fb | 2021-04-05 17:48:26 -0700 | [diff] [blame] | 311 | srcs = append(srcs, result.Matches...) |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 312 | } |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 313 | if fa.GlobDir != "" { |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 314 | if exists, isDir, err := z.fs.Exists(fa.GlobDir); err != nil { |
| 315 | return err |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 316 | } else if !exists && !args.IgnoreMissingFiles { |
| 317 | err := &os.PathError{ |
| 318 | Op: "lstat", |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 319 | Path: fa.GlobDir, |
| 320 | Err: os.ErrNotExist, |
| 321 | } |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 322 | if args.IgnoreMissingFiles { |
Dan Willemsen | edc934c | 2018-12-27 12:41:25 -0800 | [diff] [blame] | 323 | fmt.Fprintln(z.stderr, "warning:", err) |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 324 | } else { |
| 325 | return err |
| 326 | } |
| 327 | } else if !isDir && !args.IgnoreMissingFiles { |
| 328 | err := &os.PathError{ |
| 329 | Op: "lstat", |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 330 | Path: fa.GlobDir, |
| 331 | Err: syscall.ENOTDIR, |
| 332 | } |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 333 | if args.IgnoreMissingFiles { |
Dan Willemsen | edc934c | 2018-12-27 12:41:25 -0800 | [diff] [blame] | 334 | fmt.Fprintln(z.stderr, "warning:", err) |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 335 | } else { |
| 336 | return err |
| 337 | } |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 338 | } |
Colin Cross | 82ea3fb | 2021-04-05 17:48:26 -0700 | [diff] [blame] | 339 | result, err := z.fs.Glob(filepath.Join(fa.GlobDir, "**/*"), nil, followSymlinks) |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 340 | if err != nil { |
| 341 | return err |
| 342 | } |
Colin Cross | 82ea3fb | 2021-04-05 17:48:26 -0700 | [diff] [blame] | 343 | srcs = append(srcs, result.Matches...) |
Colin Cross | 7b10cf1 | 2017-08-30 14:12:21 -0700 | [diff] [blame] | 344 | } |
| 345 | for _, src := range srcs { |
Colin Cross | b7c6911 | 2018-09-18 16:51:43 -0700 | [diff] [blame] | 346 | err := fillPathPairs(fa, src, &pathMappings, args.NonDeflatedFiles, noCompression) |
Colin Cross | d321629 | 2018-09-14 15:06:31 -0700 | [diff] [blame] | 347 | if err != nil { |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 348 | return err |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 353 | return z.write(w, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.SrcJar, args.NumParallelJobs) |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Sasha Smundak | 8eedba6 | 2020-11-16 19:00:27 -0800 | [diff] [blame] | 356 | // Zip creates an output zip archive from given sources. |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 357 | func Zip(args ZipArgs) error { |
| 358 | if args.OutputFilePath == "" { |
| 359 | return fmt.Errorf("output file path must be nonempty") |
| 360 | } |
| 361 | |
Colin Cross | f83c150 | 2017-11-10 13:11:02 -0800 | [diff] [blame] | 362 | buf := &bytes.Buffer{} |
| 363 | var out io.Writer = buf |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 364 | |
Colin Cross | 665b678 | 2021-03-15 15:28:40 -0700 | [diff] [blame] | 365 | var zipErr error |
| 366 | |
Colin Cross | f83c150 | 2017-11-10 13:11:02 -0800 | [diff] [blame] | 367 | if !args.WriteIfChanged { |
| 368 | f, err := os.Create(args.OutputFilePath) |
| 369 | if err != nil { |
| 370 | return err |
| 371 | } |
| 372 | |
| 373 | defer f.Close() |
| 374 | defer func() { |
Colin Cross | 665b678 | 2021-03-15 15:28:40 -0700 | [diff] [blame] | 375 | if zipErr != nil { |
Colin Cross | f83c150 | 2017-11-10 13:11:02 -0800 | [diff] [blame] | 376 | os.Remove(args.OutputFilePath) |
| 377 | } |
| 378 | }() |
| 379 | |
| 380 | out = f |
| 381 | } |
| 382 | |
Colin Cross | 665b678 | 2021-03-15 15:28:40 -0700 | [diff] [blame] | 383 | zipErr = zipTo(args, out) |
| 384 | if zipErr != nil { |
| 385 | return zipErr |
Colin Cross | f83c150 | 2017-11-10 13:11:02 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | if args.WriteIfChanged { |
| 389 | err := pathtools.WriteFileIfChanged(args.OutputFilePath, buf.Bytes(), 0666) |
| 390 | if err != nil { |
| 391 | return err |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return nil |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Colin Cross | b7c6911 | 2018-09-18 16:51:43 -0700 | [diff] [blame] | 398 | func fillPathPairs(fa FileArg, src string, pathMappings *[]pathMapping, |
Colin Cross | d321629 | 2018-09-14 15:06:31 -0700 | [diff] [blame] | 399 | nonDeflatedFiles map[string]bool, noCompression bool) error { |
| 400 | |
Colin Cross | b7c6911 | 2018-09-18 16:51:43 -0700 | [diff] [blame] | 401 | var dest string |
| 402 | |
| 403 | if fa.JunkPaths { |
| 404 | dest = filepath.Base(src) |
| 405 | } else { |
| 406 | var err error |
| 407 | dest, err = filepath.Rel(fa.SourcePrefixToStrip, src) |
| 408 | if err != nil { |
| 409 | return err |
| 410 | } |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 411 | if strings.HasPrefix(dest, "../") { |
| 412 | return IncorrectRelativeRootError{ |
| 413 | Path: src, |
| 414 | RelativeRoot: fa.SourcePrefixToStrip, |
| 415 | } |
| 416 | } |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 417 | } |
Colin Cross | b7c6911 | 2018-09-18 16:51:43 -0700 | [diff] [blame] | 418 | dest = filepath.Join(fa.PathPrefixInZip, dest) |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 419 | |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 420 | zipMethod := zip.Deflate |
Colin Cross | d321629 | 2018-09-14 15:06:31 -0700 | [diff] [blame] | 421 | if _, found := nonDeflatedFiles[dest]; found || noCompression { |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 422 | zipMethod = zip.Store |
| 423 | } |
| 424 | *pathMappings = append(*pathMappings, |
| 425 | pathMapping{dest: dest, src: src, zipMethod: zipMethod}) |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 426 | |
| 427 | return nil |
| 428 | } |
| 429 | |
Jeff Gaston | a297695 | 2017-08-22 17:51:25 -0700 | [diff] [blame] | 430 | func jarSort(mappings []pathMapping) { |
Sasha Smundak | 8eedba6 | 2020-11-16 19:00:27 -0800 | [diff] [blame] | 431 | sort.SliceStable(mappings, func(i int, j int) bool { |
Jeff Gaston | a297695 | 2017-08-22 17:51:25 -0700 | [diff] [blame] | 432 | return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest) |
Sasha Smundak | 8eedba6 | 2020-11-16 19:00:27 -0800 | [diff] [blame] | 433 | }) |
Jeff Gaston | a297695 | 2017-08-22 17:51:25 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 436 | func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string, emulateJar, srcJar bool, |
| 437 | parallelJobs int) error { |
| 438 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 439 | z.errors = make(chan error) |
| 440 | defer close(z.errors) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 441 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 442 | // This channel size can be essentially unlimited -- it's used as a fifo |
| 443 | // queue decouple the CPU and IO loads. Directories don't require any |
| 444 | // compression time, but still cost some IO. Similar with small files that |
| 445 | // can be very fast to compress. Some files that are more difficult to |
| 446 | // compress won't take a corresponding longer time writing out. |
| 447 | // |
| 448 | // The optimum size here depends on your CPU and IO characteristics, and |
| 449 | // the the layout of your zip file. 1000 was chosen mostly at random as |
| 450 | // something that worked reasonably well for a test file. |
| 451 | // |
| 452 | // The RateLimit object will put the upper bounds on the number of |
| 453 | // parallel compressions and outstanding buffers. |
| 454 | z.writeOps = make(chan chan *zipEntry, 1000) |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 455 | z.cpuRateLimiter = NewCPURateLimiter(int64(parallelJobs)) |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 456 | z.memoryRateLimiter = NewMemoryRateLimiter(0) |
| 457 | defer func() { |
| 458 | z.cpuRateLimiter.Stop() |
| 459 | z.memoryRateLimiter.Stop() |
| 460 | }() |
Jeff Gaston | a297695 | 2017-08-22 17:51:25 -0700 | [diff] [blame] | 461 | |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 462 | if manifest != "" && !emulateJar { |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 463 | return errors.New("must specify --jar when specifying a manifest via -m") |
Jeff Gaston | a297695 | 2017-08-22 17:51:25 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 466 | if emulateJar { |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 467 | // manifest may be empty, in which case addManifest will fill in a default |
| 468 | pathMappings = append(pathMappings, pathMapping{jar.ManifestFile, manifest, zip.Deflate}) |
| 469 | |
Jeff Gaston | a297695 | 2017-08-22 17:51:25 -0700 | [diff] [blame] | 470 | jarSort(pathMappings) |
| 471 | } |
| 472 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 473 | go func() { |
| 474 | var err error |
| 475 | defer close(z.writeOps) |
| 476 | |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 477 | for _, ele := range pathMappings { |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 478 | if emulateJar && ele.dest == jar.ManifestFile { |
Jeff Gaston | cef50b9 | 2017-08-23 15:41:35 -0700 | [diff] [blame] | 479 | err = z.addManifest(ele.dest, ele.src, ele.zipMethod) |
| 480 | } else { |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 481 | err = z.addFile(ele.dest, ele.src, ele.zipMethod, emulateJar, srcJar) |
Jeff Gaston | cef50b9 | 2017-08-23 15:41:35 -0700 | [diff] [blame] | 482 | } |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 483 | if err != nil { |
| 484 | z.errors <- err |
| 485 | return |
| 486 | } |
| 487 | } |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 488 | }() |
| 489 | |
| 490 | zipw := zip.NewWriter(f) |
| 491 | |
| 492 | var currentWriteOpChan chan *zipEntry |
| 493 | var currentWriter io.WriteCloser |
| 494 | var currentReaders chan chan io.Reader |
| 495 | var currentReader chan io.Reader |
| 496 | var done bool |
| 497 | |
| 498 | for !done { |
| 499 | var writeOpsChan chan chan *zipEntry |
| 500 | var writeOpChan chan *zipEntry |
| 501 | var readersChan chan chan io.Reader |
| 502 | |
| 503 | if currentReader != nil { |
| 504 | // Only read and process errors |
| 505 | } else if currentReaders != nil { |
| 506 | readersChan = currentReaders |
| 507 | } else if currentWriteOpChan != nil { |
| 508 | writeOpChan = currentWriteOpChan |
| 509 | } else { |
| 510 | writeOpsChan = z.writeOps |
| 511 | } |
| 512 | |
| 513 | select { |
| 514 | case writeOp, ok := <-writeOpsChan: |
| 515 | if !ok { |
| 516 | done = true |
| 517 | } |
| 518 | |
| 519 | currentWriteOpChan = writeOp |
| 520 | |
| 521 | case op := <-writeOpChan: |
| 522 | currentWriteOpChan = nil |
| 523 | |
Colin Cross | f83c150 | 2017-11-10 13:11:02 -0800 | [diff] [blame] | 524 | var err error |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 525 | if op.fh.Method == zip.Deflate { |
| 526 | currentWriter, err = zipw.CreateCompressedHeader(op.fh) |
| 527 | } else { |
| 528 | var zw io.Writer |
Jeff Gaston | c5eb66d | 2017-08-24 14:11:27 -0700 | [diff] [blame] | 529 | |
| 530 | op.fh.CompressedSize64 = op.fh.UncompressedSize64 |
| 531 | |
| 532 | zw, err = zipw.CreateHeaderAndroid(op.fh) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 533 | currentWriter = nopCloser{zw} |
| 534 | } |
| 535 | if err != nil { |
| 536 | return err |
| 537 | } |
| 538 | |
| 539 | currentReaders = op.futureReaders |
| 540 | if op.futureReaders == nil { |
| 541 | currentWriter.Close() |
| 542 | currentWriter = nil |
| 543 | } |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 544 | z.memoryRateLimiter.Finish(op.allocatedSize) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 545 | |
| 546 | case futureReader, ok := <-readersChan: |
| 547 | if !ok { |
| 548 | // Done with reading |
| 549 | currentWriter.Close() |
| 550 | currentWriter = nil |
| 551 | currentReaders = nil |
| 552 | } |
| 553 | |
| 554 | currentReader = futureReader |
| 555 | |
| 556 | case reader := <-currentReader: |
Colin Cross | f83c150 | 2017-11-10 13:11:02 -0800 | [diff] [blame] | 557 | _, err := io.Copy(currentWriter, reader) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 558 | if err != nil { |
| 559 | return err |
| 560 | } |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 561 | |
| 562 | currentReader = nil |
| 563 | |
Colin Cross | f83c150 | 2017-11-10 13:11:02 -0800 | [diff] [blame] | 564 | case err := <-z.errors: |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 565 | return err |
| 566 | } |
| 567 | } |
| 568 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 569 | // One last chance to catch an error |
| 570 | select { |
Colin Cross | f83c150 | 2017-11-10 13:11:02 -0800 | [diff] [blame] | 571 | case err := <-z.errors: |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 572 | return err |
| 573 | default: |
| 574 | zipw.Close() |
| 575 | return nil |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 576 | } |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 579 | // imports (possibly with compression) <src> into the zip at sub-path <dest> |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 580 | func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar, srcJar bool) error { |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 581 | var fileSize int64 |
Dan Willemsen | 10462b3 | 2017-03-15 19:02:51 -0700 | [diff] [blame] | 582 | var executable bool |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 583 | |
Colin Cross | 09f1105 | 2018-09-21 15:12:39 -0700 | [diff] [blame] | 584 | var s os.FileInfo |
| 585 | var err error |
| 586 | if z.followSymlinks { |
| 587 | s, err = z.fs.Stat(src) |
| 588 | } else { |
| 589 | s, err = z.fs.Lstat(src) |
| 590 | } |
| 591 | |
| 592 | if err != nil { |
Colin Cross | 4be8f9e | 2018-09-28 15:16:48 -0700 | [diff] [blame] | 593 | if os.IsNotExist(err) && z.ignoreMissingFiles { |
| 594 | fmt.Fprintln(z.stderr, "warning:", err) |
| 595 | return nil |
| 596 | } |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 597 | return err |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | createParentDirs := func(dest, src string) error { |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 601 | if err := z.writeDirectory(filepath.Dir(dest), src, emulateJar); err != nil { |
Colin Cross | e558097 | 2017-08-30 17:40:21 -0700 | [diff] [blame] | 602 | return err |
| 603 | } |
| 604 | |
| 605 | if prev, exists := z.createdDirs[dest]; exists { |
| 606 | return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src) |
| 607 | } |
| 608 | if prev, exists := z.createdFiles[dest]; exists { |
| 609 | return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src) |
| 610 | } |
| 611 | |
| 612 | z.createdFiles[dest] = src |
| 613 | |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 614 | return nil |
| 615 | } |
| 616 | |
| 617 | if s.IsDir() { |
| 618 | if z.directories { |
| 619 | return z.writeDirectory(dest, src, emulateJar) |
| 620 | } |
| 621 | return nil |
| 622 | } else if s.Mode()&os.ModeSymlink != 0 { |
| 623 | err = createParentDirs(dest, src) |
| 624 | if err != nil { |
| 625 | return err |
| 626 | } |
| 627 | |
| 628 | return z.writeSymlink(dest, src) |
| 629 | } else if s.Mode().IsRegular() { |
| 630 | r, err := z.fs.Open(src) |
| 631 | if err != nil { |
| 632 | return err |
| 633 | } |
| 634 | |
| 635 | if srcJar && filepath.Ext(src) == ".java" { |
| 636 | // rewrite the destination using the package path if it can be determined |
| 637 | pkg, err := jar.JavaPackage(r, src) |
| 638 | if err != nil { |
| 639 | // ignore errors for now, leaving the file at in its original location in the zip |
| 640 | } else { |
| 641 | dest = filepath.Join(filepath.Join(strings.Split(pkg, ".")...), filepath.Base(src)) |
| 642 | } |
| 643 | |
| 644 | _, err = r.Seek(0, io.SeekStart) |
| 645 | if err != nil { |
| 646 | return err |
| 647 | } |
Colin Cross | e558097 | 2017-08-30 17:40:21 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 650 | fileSize = s.Size() |
Dan Willemsen | 10462b3 | 2017-03-15 19:02:51 -0700 | [diff] [blame] | 651 | executable = s.Mode()&0100 != 0 |
Colin Cross | 957cc4e | 2015-04-24 15:10:32 -0700 | [diff] [blame] | 652 | |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 653 | header := &zip.FileHeader{ |
| 654 | Name: dest, |
| 655 | Method: method, |
| 656 | UncompressedSize64: uint64(fileSize), |
| 657 | } |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 658 | |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 659 | if executable { |
| 660 | header.SetMode(0700) |
| 661 | } |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 662 | |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 663 | err = createParentDirs(dest, src) |
| 664 | if err != nil { |
| 665 | return err |
| 666 | } |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 667 | |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 668 | return z.writeFileContents(header, r) |
| 669 | } else { |
| 670 | return fmt.Errorf("%s is not a file, directory, or symlink", src) |
| 671 | } |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Sasha Smundak | 8eedba6 | 2020-11-16 19:00:27 -0800 | [diff] [blame] | 674 | func (z *ZipWriter) addManifest(dest string, src string, _ uint16) error { |
Colin Cross | e558097 | 2017-08-30 17:40:21 -0700 | [diff] [blame] | 675 | if prev, exists := z.createdDirs[dest]; exists { |
| 676 | return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src) |
| 677 | } |
| 678 | if prev, exists := z.createdFiles[dest]; exists { |
| 679 | return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src) |
| 680 | } |
| 681 | |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 682 | if err := z.writeDirectory(filepath.Dir(dest), src, true); err != nil { |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 683 | return err |
Jeff Gaston | cef50b9 | 2017-08-23 15:41:35 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 686 | var contents []byte |
| 687 | if src != "" { |
| 688 | f, err := z.fs.Open(src) |
| 689 | if err != nil { |
| 690 | return err |
| 691 | } |
| 692 | |
| 693 | contents, err = ioutil.ReadAll(f) |
| 694 | f.Close() |
| 695 | if err != nil { |
| 696 | return err |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | fh, buf, err := jar.ManifestFileContents(contents) |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 701 | if err != nil { |
| 702 | return err |
Jeff Gaston | cef50b9 | 2017-08-23 15:41:35 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 705 | reader := &byteReaderCloser{bytes.NewReader(buf), ioutil.NopCloser(nil)} |
| 706 | |
| 707 | return z.writeFileContents(fh, reader) |
Jeff Gaston | cef50b9 | 2017-08-23 15:41:35 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 710 | func (z *ZipWriter) writeFileContents(header *zip.FileHeader, r pathtools.ReaderAtSeekerCloser) (err error) { |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 711 | |
| 712 | header.SetModTime(z.time) |
| 713 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 714 | compressChan := make(chan *zipEntry, 1) |
| 715 | z.writeOps <- compressChan |
| 716 | |
| 717 | // Pre-fill a zipEntry, it will be sent in the compressChan once |
| 718 | // we're sure about the Method and CRC. |
| 719 | ze := &zipEntry{ |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 720 | fh: header, |
Dan Willemsen | 10462b3 | 2017-03-15 19:02:51 -0700 | [diff] [blame] | 721 | } |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 722 | |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 723 | ze.allocatedSize = int64(header.UncompressedSize64) |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 724 | z.cpuRateLimiter.Request() |
| 725 | z.memoryRateLimiter.Request(ze.allocatedSize) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 726 | |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 727 | fileSize := int64(header.UncompressedSize64) |
| 728 | if fileSize == 0 { |
| 729 | fileSize = int64(header.UncompressedSize) |
| 730 | } |
| 731 | |
| 732 | if header.Method == zip.Deflate && fileSize >= minParallelFileSize { |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 733 | wg := new(sync.WaitGroup) |
| 734 | |
| 735 | // Allocate enough buffer to hold all readers. We'll limit |
| 736 | // this based on actual buffer sizes in RateLimit. |
| 737 | ze.futureReaders = make(chan chan io.Reader, (fileSize/parallelBlockSize)+1) |
| 738 | |
| 739 | // Calculate the CRC in the background, since reading the entire |
| 740 | // file could take a while. |
| 741 | // |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 742 | // We could split this up into chunks as well, but it's faster |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 743 | // than the compression. Due to the Go Zip API, we also need to |
| 744 | // know the result before we can begin writing the compressed |
| 745 | // data out to the zipfile. |
| 746 | wg.Add(1) |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 747 | go z.crcFile(r, ze, compressChan, wg) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 748 | |
| 749 | for start := int64(0); start < fileSize; start += parallelBlockSize { |
| 750 | sr := io.NewSectionReader(r, start, parallelBlockSize) |
| 751 | resultChan := make(chan io.Reader, 1) |
| 752 | ze.futureReaders <- resultChan |
| 753 | |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 754 | z.cpuRateLimiter.Request() |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 755 | |
| 756 | last := !(start+parallelBlockSize < fileSize) |
| 757 | var dict []byte |
| 758 | if start >= windowSize { |
| 759 | dict, err = ioutil.ReadAll(io.NewSectionReader(r, start-windowSize, windowSize)) |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 760 | if err != nil { |
| 761 | return err |
| 762 | } |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | wg.Add(1) |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 766 | go z.compressPartialFile(sr, dict, last, resultChan, wg) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | close(ze.futureReaders) |
| 770 | |
| 771 | // Close the file handle after all readers are done |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 772 | go func(wg *sync.WaitGroup, closer io.Closer) { |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 773 | wg.Wait() |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 774 | closer.Close() |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 775 | }(wg, r) |
| 776 | } else { |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 777 | go func() { |
| 778 | z.compressWholeFile(ze, r, compressChan) |
| 779 | r.Close() |
| 780 | }() |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | return nil |
| 784 | } |
| 785 | |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 786 | func (z *ZipWriter) crcFile(r io.Reader, ze *zipEntry, resultChan chan *zipEntry, wg *sync.WaitGroup) { |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 787 | defer wg.Done() |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 788 | defer z.cpuRateLimiter.Finish() |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 789 | |
| 790 | crc := crc32.NewIEEE() |
| 791 | _, err := io.Copy(crc, r) |
| 792 | if err != nil { |
| 793 | z.errors <- err |
| 794 | return |
| 795 | } |
| 796 | |
| 797 | ze.fh.CRC32 = crc.Sum32() |
| 798 | resultChan <- ze |
| 799 | close(resultChan) |
| 800 | } |
| 801 | |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 802 | func (z *ZipWriter) compressPartialFile(r io.Reader, dict []byte, last bool, resultChan chan io.Reader, wg *sync.WaitGroup) { |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 803 | defer wg.Done() |
| 804 | |
| 805 | result, err := z.compressBlock(r, dict, last) |
| 806 | if err != nil { |
| 807 | z.errors <- err |
| 808 | return |
| 809 | } |
| 810 | |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 811 | z.cpuRateLimiter.Finish() |
| 812 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 813 | resultChan <- result |
| 814 | } |
| 815 | |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 816 | func (z *ZipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) { |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 817 | buf := new(bytes.Buffer) |
| 818 | var fw *flate.Writer |
| 819 | var err error |
| 820 | if len(dict) > 0 { |
| 821 | // There's no way to Reset a Writer with a new dictionary, so |
| 822 | // don't use the Pool |
| 823 | fw, err = flate.NewWriterDict(buf, z.compLevel, dict) |
| 824 | } else { |
| 825 | var ok bool |
| 826 | if fw, ok = z.compressorPool.Get().(*flate.Writer); ok { |
| 827 | fw.Reset(buf) |
| 828 | } else { |
| 829 | fw, err = flate.NewWriter(buf, z.compLevel) |
| 830 | } |
| 831 | defer z.compressorPool.Put(fw) |
| 832 | } |
| 833 | if err != nil { |
| 834 | return nil, err |
| 835 | } |
| 836 | |
| 837 | _, err = io.Copy(fw, r) |
| 838 | if err != nil { |
| 839 | return nil, err |
| 840 | } |
| 841 | if last { |
| 842 | fw.Close() |
| 843 | } else { |
| 844 | fw.Flush() |
| 845 | } |
| 846 | |
| 847 | return buf, nil |
| 848 | } |
| 849 | |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 850 | func (z *ZipWriter) compressWholeFile(ze *zipEntry, r io.ReadSeeker, compressChan chan *zipEntry) { |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 851 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 852 | crc := crc32.NewIEEE() |
Dan Willemsen | a8b5502 | 2017-03-15 21:49:26 -0700 | [diff] [blame] | 853 | _, err := io.Copy(crc, r) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 854 | if err != nil { |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 855 | z.errors <- err |
| 856 | return |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Dan Willemsen | a8b5502 | 2017-03-15 21:49:26 -0700 | [diff] [blame] | 859 | ze.fh.CRC32 = crc.Sum32() |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 860 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 861 | _, err = r.Seek(0, 0) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 862 | if err != nil { |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 863 | z.errors <- err |
| 864 | return |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 867 | readFile := func(reader io.ReadSeeker) ([]byte, error) { |
| 868 | _, err := reader.Seek(0, 0) |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 869 | if err != nil { |
| 870 | return nil, err |
| 871 | } |
| 872 | |
Jeff Gaston | 66dd6e5 | 2017-08-23 15:12:48 -0700 | [diff] [blame] | 873 | buf, err := ioutil.ReadAll(reader) |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 874 | if err != nil { |
| 875 | return nil, err |
| 876 | } |
| 877 | |
| 878 | return buf, nil |
| 879 | } |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 880 | |
Dan Willemsen | a8b5502 | 2017-03-15 21:49:26 -0700 | [diff] [blame] | 881 | ze.futureReaders = make(chan chan io.Reader, 1) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 882 | futureReader := make(chan io.Reader, 1) |
| 883 | ze.futureReaders <- futureReader |
| 884 | close(ze.futureReaders) |
| 885 | |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 886 | if ze.fh.Method == zip.Deflate { |
| 887 | compressed, err := z.compressBlock(r, nil, true) |
| 888 | if err != nil { |
| 889 | z.errors <- err |
| 890 | return |
| 891 | } |
| 892 | if uint64(compressed.Len()) < ze.fh.UncompressedSize64 { |
| 893 | futureReader <- compressed |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 894 | } else { |
| 895 | buf, err := readFile(r) |
| 896 | if err != nil { |
| 897 | z.errors <- err |
| 898 | return |
| 899 | } |
| 900 | ze.fh.Method = zip.Store |
| 901 | futureReader <- bytes.NewReader(buf) |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 902 | } |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 903 | } else { |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 904 | buf, err := readFile(r) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 905 | if err != nil { |
| 906 | z.errors <- err |
| 907 | return |
| 908 | } |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 909 | ze.fh.Method = zip.Store |
| 910 | futureReader <- bytes.NewReader(buf) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 911 | } |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 912 | |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 913 | z.cpuRateLimiter.Finish() |
| 914 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 915 | close(futureReader) |
| 916 | |
| 917 | compressChan <- ze |
| 918 | close(compressChan) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 919 | } |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 920 | |
Colin Cross | e558097 | 2017-08-30 17:40:21 -0700 | [diff] [blame] | 921 | // writeDirectory annotates that dir is a directory created for the src file or directory, and adds |
| 922 | // the directory entry to the zip file if directories are enabled. |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 923 | func (z *ZipWriter) writeDirectory(dir string, src string, emulateJar bool) error { |
Jeff Gaston | 2d17413 | 2017-08-15 18:05:56 -0700 | [diff] [blame] | 924 | // clean the input |
Colin Cross | e558097 | 2017-08-30 17:40:21 -0700 | [diff] [blame] | 925 | dir = filepath.Clean(dir) |
Jeff Gaston | 2d17413 | 2017-08-15 18:05:56 -0700 | [diff] [blame] | 926 | |
| 927 | // discover any uncreated directories in the path |
Sasha Smundak | 8eedba6 | 2020-11-16 19:00:27 -0800 | [diff] [blame] | 928 | var zipDirs []string |
Colin Cross | e558097 | 2017-08-30 17:40:21 -0700 | [diff] [blame] | 929 | for dir != "" && dir != "." { |
| 930 | if _, exists := z.createdDirs[dir]; exists { |
| 931 | break |
| 932 | } |
Jeff Gaston | 2d17413 | 2017-08-15 18:05:56 -0700 | [diff] [blame] | 933 | |
Colin Cross | e558097 | 2017-08-30 17:40:21 -0700 | [diff] [blame] | 934 | if prev, exists := z.createdFiles[dir]; exists { |
| 935 | return fmt.Errorf("destination %q is both a directory %q and a file %q", dir, src, prev) |
| 936 | } |
| 937 | |
| 938 | z.createdDirs[dir] = src |
Jeff Gaston | 2d17413 | 2017-08-15 18:05:56 -0700 | [diff] [blame] | 939 | // parent directories precede their children |
Colin Cross | e558097 | 2017-08-30 17:40:21 -0700 | [diff] [blame] | 940 | zipDirs = append([]string{dir}, zipDirs...) |
Jeff Gaston | 2d17413 | 2017-08-15 18:05:56 -0700 | [diff] [blame] | 941 | |
Colin Cross | e558097 | 2017-08-30 17:40:21 -0700 | [diff] [blame] | 942 | dir = filepath.Dir(dir) |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Colin Cross | e558097 | 2017-08-30 17:40:21 -0700 | [diff] [blame] | 945 | if z.directories { |
| 946 | // make a directory entry for each uncreated directory |
| 947 | for _, cleanDir := range zipDirs { |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 948 | var dirHeader *zip.FileHeader |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 949 | |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 950 | if emulateJar && cleanDir+"/" == jar.MetaDir { |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 951 | dirHeader = jar.MetaDirFileHeader() |
| 952 | } else { |
| 953 | dirHeader = &zip.FileHeader{ |
| 954 | Name: cleanDir + "/", |
| 955 | } |
| 956 | dirHeader.SetMode(0700 | os.ModeDir) |
Colin Cross | e558097 | 2017-08-30 17:40:21 -0700 | [diff] [blame] | 957 | } |
Jeff Gaston | 8edbb3a | 2017-08-22 20:05:28 -0700 | [diff] [blame] | 958 | |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 959 | dirHeader.SetModTime(z.time) |
| 960 | |
Colin Cross | e558097 | 2017-08-30 17:40:21 -0700 | [diff] [blame] | 961 | ze := make(chan *zipEntry, 1) |
| 962 | ze <- &zipEntry{ |
| 963 | fh: dirHeader, |
| 964 | } |
| 965 | close(ze) |
| 966 | z.writeOps <- ze |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 967 | } |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | return nil |
| 971 | } |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 972 | |
Jeff Gaston | c3bdc97 | 2017-10-12 12:18:19 -0700 | [diff] [blame] | 973 | func (z *ZipWriter) writeSymlink(rel, file string) error { |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 974 | fileHeader := &zip.FileHeader{ |
| 975 | Name: rel, |
| 976 | } |
| 977 | fileHeader.SetModTime(z.time) |
Colin Cross | 297d9bc | 2018-06-22 16:37:47 -0700 | [diff] [blame] | 978 | fileHeader.SetMode(0777 | os.ModeSymlink) |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 979 | |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 980 | dest, err := z.fs.Readlink(file) |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 981 | if err != nil { |
| 982 | return err |
| 983 | } |
| 984 | |
Colin Cross | 297d9bc | 2018-06-22 16:37:47 -0700 | [diff] [blame] | 985 | fileHeader.UncompressedSize64 = uint64(len(dest)) |
| 986 | fileHeader.CRC32 = crc32.ChecksumIEEE([]byte(dest)) |
| 987 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 988 | ze := make(chan *zipEntry, 1) |
| 989 | futureReaders := make(chan chan io.Reader, 1) |
| 990 | futureReader := make(chan io.Reader, 1) |
| 991 | futureReaders <- futureReader |
| 992 | close(futureReaders) |
| 993 | futureReader <- bytes.NewBufferString(dest) |
| 994 | close(futureReader) |
| 995 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 996 | ze <- &zipEntry{ |
| 997 | fh: fileHeader, |
| 998 | futureReaders: futureReaders, |
| 999 | } |
| 1000 | close(ze) |
| 1001 | z.writeOps <- ze |
| 1002 | |
| 1003 | return nil |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 1004 | } |