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