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 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 18 | "bytes" |
| 19 | "compress/flate" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 20 | "flag" |
| 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" |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 25 | "log" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 26 | "os" |
| 27 | "path/filepath" |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 28 | "runtime" |
| 29 | "runtime/pprof" |
| 30 | "runtime/trace" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 31 | "strings" |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 32 | "sync" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 33 | "time" |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 34 | |
| 35 | "android/soong/third_party/zip" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 36 | ) |
| 37 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 38 | // Block size used during parallel compression of a single file. |
| 39 | const parallelBlockSize = 1 * 1024 * 1024 // 1MB |
| 40 | |
| 41 | // Minimum file size to use parallel compression. It requires more |
| 42 | // flate.Writer allocations, since we can't change the dictionary |
| 43 | // during Reset |
| 44 | const minParallelFileSize = parallelBlockSize * 6 |
| 45 | |
| 46 | // Size of the ZIP compression window (32KB) |
| 47 | const windowSize = 32 * 1024 |
| 48 | |
| 49 | type nopCloser struct { |
| 50 | io.Writer |
| 51 | } |
| 52 | |
| 53 | func (nopCloser) Close() error { |
| 54 | return nil |
| 55 | } |
| 56 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 57 | type fileArg struct { |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 58 | pathPrefixInZip, sourcePrefixToStrip string |
| 59 | sourceFiles []string |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | type pathMapping struct { |
| 63 | dest, src string |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 64 | zipMethod uint16 |
| 65 | } |
| 66 | |
| 67 | type uniqueSet map[string]bool |
| 68 | |
| 69 | func (u *uniqueSet) String() string { |
| 70 | return `""` |
| 71 | } |
| 72 | |
| 73 | func (u *uniqueSet) Set(s string) error { |
| 74 | if _, found := (*u)[s]; found { |
| 75 | return fmt.Errorf("File %q was specified twice as a file to not deflate", s) |
| 76 | } else { |
| 77 | (*u)[s] = true |
| 78 | } |
| 79 | |
| 80 | return nil |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | type fileArgs []fileArg |
| 84 | |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 85 | type file struct{} |
| 86 | |
| 87 | type listFiles struct{} |
| 88 | |
| 89 | func (f *file) String() string { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 90 | return `""` |
| 91 | } |
| 92 | |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 93 | func (f *file) Set(s string) error { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 94 | if *relativeRoot == "" { |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 95 | return fmt.Errorf("must pass -C before -f or -l") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 98 | fArgs = append(fArgs, fileArg{ |
| 99 | pathPrefixInZip: filepath.Clean(*rootPrefix), |
| 100 | sourcePrefixToStrip: filepath.Clean(*relativeRoot), |
| 101 | sourceFiles: []string{s}, |
| 102 | }) |
| 103 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 104 | return nil |
| 105 | } |
| 106 | |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 107 | func (l *listFiles) String() string { |
| 108 | return `""` |
| 109 | } |
| 110 | |
| 111 | func (l *listFiles) Set(s string) error { |
| 112 | if *relativeRoot == "" { |
| 113 | return fmt.Errorf("must pass -C before -f or -l") |
| 114 | } |
| 115 | |
| 116 | list, err := ioutil.ReadFile(s) |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | |
| 121 | fArgs = append(fArgs, fileArg{ |
| 122 | pathPrefixInZip: filepath.Clean(*rootPrefix), |
| 123 | sourcePrefixToStrip: filepath.Clean(*relativeRoot), |
| 124 | sourceFiles: strings.Split(string(list), "\n"), |
| 125 | }) |
| 126 | |
| 127 | return nil |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | var ( |
Dan Willemsen | 47ec28f | 2016-08-10 16:12:30 -0700 | [diff] [blame] | 131 | out = flag.String("o", "", "file to write zip file to") |
| 132 | manifest = flag.String("m", "", "input jar manifest file name") |
| 133 | directories = flag.Bool("d", false, "include directories in zip") |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 134 | rootPrefix = flag.String("P", "", "path prefix within the zip at which to place files") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 135 | relativeRoot = flag.String("C", "", "path to use as relative root of files in next -f or -l argument") |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 136 | parallelJobs = flag.Int("j", runtime.NumCPU(), "number of parallel threads to use") |
| 137 | compLevel = flag.Int("L", 5, "deflate compression level (0-9)") |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 138 | |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 139 | fArgs fileArgs |
| 140 | nonDeflatedFiles = make(uniqueSet) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 141 | |
| 142 | cpuProfile = flag.String("cpuprofile", "", "write cpu profile to file") |
| 143 | traceFile = flag.String("trace", "", "write trace to file") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 144 | ) |
| 145 | |
| 146 | func init() { |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 147 | flag.Var(&listFiles{}, "l", "file containing list of .class files") |
| 148 | flag.Var(&file{}, "f", "file to include in zip") |
| 149 | flag.Var(&nonDeflatedFiles, "s", "file path to be stored within the zip without compression") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | func usage() { |
Dan Willemsen | 47ec28f | 2016-08-10 16:12:30 -0700 | [diff] [blame] | 153 | fmt.Fprintf(os.Stderr, "usage: soong_zip -o zipfile [-m manifest] -C dir [-f|-l file]...\n") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 154 | flag.PrintDefaults() |
| 155 | os.Exit(2) |
| 156 | } |
| 157 | |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 158 | type zipWriter struct { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 159 | time time.Time |
| 160 | createdDirs map[string]bool |
| 161 | directories bool |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 162 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 163 | errors chan error |
| 164 | writeOps chan chan *zipEntry |
| 165 | |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 166 | cpuRateLimiter *CPURateLimiter |
| 167 | memoryRateLimiter *MemoryRateLimiter |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 168 | |
| 169 | compressorPool sync.Pool |
| 170 | compLevel int |
| 171 | } |
| 172 | |
| 173 | type zipEntry struct { |
| 174 | fh *zip.FileHeader |
| 175 | |
| 176 | // List of delayed io.Reader |
| 177 | futureReaders chan chan io.Reader |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 178 | |
| 179 | // Only used for passing into the MemoryRateLimiter to ensure we |
| 180 | // release as much memory as much as we request |
| 181 | allocatedSize int64 |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | func main() { |
| 185 | flag.Parse() |
| 186 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 187 | if *cpuProfile != "" { |
| 188 | f, err := os.Create(*cpuProfile) |
| 189 | if err != nil { |
| 190 | fmt.Fprintln(os.Stderr, err.Error()) |
| 191 | os.Exit(1) |
| 192 | } |
| 193 | defer f.Close() |
| 194 | pprof.StartCPUProfile(f) |
| 195 | defer pprof.StopCPUProfile() |
| 196 | } |
| 197 | |
| 198 | if *traceFile != "" { |
| 199 | f, err := os.Create(*traceFile) |
| 200 | if err != nil { |
| 201 | fmt.Fprintln(os.Stderr, err.Error()) |
| 202 | os.Exit(1) |
| 203 | } |
| 204 | defer f.Close() |
| 205 | err = trace.Start(f) |
| 206 | if err != nil { |
| 207 | fmt.Fprintln(os.Stderr, err.Error()) |
| 208 | os.Exit(1) |
| 209 | } |
| 210 | defer trace.Stop() |
| 211 | } |
| 212 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 213 | if *out == "" { |
| 214 | fmt.Fprintf(os.Stderr, "error: -o is required\n") |
| 215 | usage() |
| 216 | } |
| 217 | |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 218 | w := &zipWriter{ |
Dan Willemsen | 77a6b86 | 2016-08-04 20:38:47 -0700 | [diff] [blame] | 219 | time: time.Date(2009, 1, 1, 0, 0, 0, 0, time.UTC), |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 220 | createdDirs: make(map[string]bool), |
| 221 | directories: *directories, |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 222 | compLevel: *compLevel, |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 225 | pathMappings := []pathMapping{} |
| 226 | set := make(map[string]string) |
| 227 | |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 228 | for _, fa := range fArgs { |
| 229 | for _, src := range fa.sourceFiles { |
| 230 | if err := fillPathPairs(fa.pathPrefixInZip, |
| 231 | fa.sourcePrefixToStrip, src, set, &pathMappings); err != nil { |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 232 | log.Fatal(err) |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 237 | err := w.write(*out, pathMappings, *manifest) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 238 | if err != nil { |
| 239 | fmt.Fprintln(os.Stderr, err.Error()) |
| 240 | os.Exit(1) |
| 241 | } |
| 242 | } |
| 243 | |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 244 | func fillPathPairs(prefix, rel, src string, set map[string]string, pathMappings *[]pathMapping) error { |
| 245 | src = strings.TrimSpace(src) |
| 246 | if src == "" { |
| 247 | return nil |
| 248 | } |
| 249 | src = filepath.Clean(src) |
| 250 | dest, err := filepath.Rel(rel, src) |
| 251 | if err != nil { |
| 252 | return err |
| 253 | } |
| 254 | dest = filepath.Join(prefix, dest) |
| 255 | |
| 256 | if _, found := set[dest]; found { |
| 257 | return fmt.Errorf("found two file paths to be copied into dest path: %q,"+ |
| 258 | " both [%q]%q and [%q]%q!", |
| 259 | dest, dest, src, dest, set[dest]) |
| 260 | } else { |
| 261 | set[dest] = src |
| 262 | } |
| 263 | |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 264 | zipMethod := zip.Deflate |
| 265 | if _, found := nonDeflatedFiles[dest]; found { |
| 266 | zipMethod = zip.Store |
| 267 | } |
| 268 | *pathMappings = append(*pathMappings, |
| 269 | pathMapping{dest: dest, src: src, zipMethod: zipMethod}) |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 270 | |
| 271 | return nil |
| 272 | } |
| 273 | |
| 274 | func (z *zipWriter) write(out string, pathMappings []pathMapping, manifest string) error { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 275 | f, err := os.Create(out) |
| 276 | if err != nil { |
| 277 | return err |
| 278 | } |
| 279 | |
| 280 | defer f.Close() |
| 281 | defer func() { |
| 282 | if err != nil { |
| 283 | os.Remove(out) |
| 284 | } |
| 285 | }() |
| 286 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 287 | z.errors = make(chan error) |
| 288 | defer close(z.errors) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 289 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 290 | // This channel size can be essentially unlimited -- it's used as a fifo |
| 291 | // queue decouple the CPU and IO loads. Directories don't require any |
| 292 | // compression time, but still cost some IO. Similar with small files that |
| 293 | // can be very fast to compress. Some files that are more difficult to |
| 294 | // compress won't take a corresponding longer time writing out. |
| 295 | // |
| 296 | // The optimum size here depends on your CPU and IO characteristics, and |
| 297 | // the the layout of your zip file. 1000 was chosen mostly at random as |
| 298 | // something that worked reasonably well for a test file. |
| 299 | // |
| 300 | // The RateLimit object will put the upper bounds on the number of |
| 301 | // parallel compressions and outstanding buffers. |
| 302 | z.writeOps = make(chan chan *zipEntry, 1000) |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 303 | z.cpuRateLimiter = NewCPURateLimiter(int64(*parallelJobs)) |
| 304 | z.memoryRateLimiter = NewMemoryRateLimiter(0) |
| 305 | defer func() { |
| 306 | z.cpuRateLimiter.Stop() |
| 307 | z.memoryRateLimiter.Stop() |
| 308 | }() |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 309 | go func() { |
| 310 | var err error |
| 311 | defer close(z.writeOps) |
| 312 | |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 313 | for _, ele := range pathMappings { |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 314 | err = z.writeFile(ele.dest, ele.src, ele.zipMethod) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 315 | if err != nil { |
| 316 | z.errors <- err |
| 317 | return |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | if manifest != "" { |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 322 | err = z.writeFile("META-INF/MANIFEST.MF", manifest, zip.Deflate) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 323 | if err != nil { |
| 324 | z.errors <- err |
| 325 | return |
| 326 | } |
| 327 | } |
| 328 | }() |
| 329 | |
| 330 | zipw := zip.NewWriter(f) |
| 331 | |
| 332 | var currentWriteOpChan chan *zipEntry |
| 333 | var currentWriter io.WriteCloser |
| 334 | var currentReaders chan chan io.Reader |
| 335 | var currentReader chan io.Reader |
| 336 | var done bool |
| 337 | |
| 338 | for !done { |
| 339 | var writeOpsChan chan chan *zipEntry |
| 340 | var writeOpChan chan *zipEntry |
| 341 | var readersChan chan chan io.Reader |
| 342 | |
| 343 | if currentReader != nil { |
| 344 | // Only read and process errors |
| 345 | } else if currentReaders != nil { |
| 346 | readersChan = currentReaders |
| 347 | } else if currentWriteOpChan != nil { |
| 348 | writeOpChan = currentWriteOpChan |
| 349 | } else { |
| 350 | writeOpsChan = z.writeOps |
| 351 | } |
| 352 | |
| 353 | select { |
| 354 | case writeOp, ok := <-writeOpsChan: |
| 355 | if !ok { |
| 356 | done = true |
| 357 | } |
| 358 | |
| 359 | currentWriteOpChan = writeOp |
| 360 | |
| 361 | case op := <-writeOpChan: |
| 362 | currentWriteOpChan = nil |
| 363 | |
| 364 | if op.fh.Method == zip.Deflate { |
| 365 | currentWriter, err = zipw.CreateCompressedHeader(op.fh) |
| 366 | } else { |
| 367 | var zw io.Writer |
| 368 | zw, err = zipw.CreateHeader(op.fh) |
| 369 | currentWriter = nopCloser{zw} |
| 370 | } |
| 371 | if err != nil { |
| 372 | return err |
| 373 | } |
| 374 | |
| 375 | currentReaders = op.futureReaders |
| 376 | if op.futureReaders == nil { |
| 377 | currentWriter.Close() |
| 378 | currentWriter = nil |
| 379 | } |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 380 | z.memoryRateLimiter.Finish(op.allocatedSize) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 381 | |
| 382 | case futureReader, ok := <-readersChan: |
| 383 | if !ok { |
| 384 | // Done with reading |
| 385 | currentWriter.Close() |
| 386 | currentWriter = nil |
| 387 | currentReaders = nil |
| 388 | } |
| 389 | |
| 390 | currentReader = futureReader |
| 391 | |
| 392 | case reader := <-currentReader: |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 393 | _, err = io.Copy(currentWriter, reader) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 394 | if err != nil { |
| 395 | return err |
| 396 | } |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 397 | |
| 398 | currentReader = nil |
| 399 | |
| 400 | case err = <-z.errors: |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 401 | return err |
| 402 | } |
| 403 | } |
| 404 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 405 | // One last chance to catch an error |
| 406 | select { |
| 407 | case err = <-z.errors: |
| 408 | return err |
| 409 | default: |
| 410 | zipw.Close() |
| 411 | return nil |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 412 | } |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 415 | func (z *zipWriter) writeFile(dest, src string, method uint16) error { |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 416 | var fileSize int64 |
Dan Willemsen | 10462b3 | 2017-03-15 19:02:51 -0700 | [diff] [blame] | 417 | var executable bool |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 418 | |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 419 | if s, err := os.Lstat(src); err != nil { |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 420 | return err |
| 421 | } else if s.IsDir() { |
Colin Cross | 957cc4e | 2015-04-24 15:10:32 -0700 | [diff] [blame] | 422 | if z.directories { |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 423 | return z.writeDirectory(dest) |
Colin Cross | 957cc4e | 2015-04-24 15:10:32 -0700 | [diff] [blame] | 424 | } |
| 425 | return nil |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 426 | } else if s.Mode()&os.ModeSymlink != 0 { |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 427 | return z.writeSymlink(dest, src) |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 428 | } else if !s.Mode().IsRegular() { |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 429 | return fmt.Errorf("%s is not a file, directory, or symlink", src) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 430 | } else { |
| 431 | fileSize = s.Size() |
Dan Willemsen | 10462b3 | 2017-03-15 19:02:51 -0700 | [diff] [blame] | 432 | executable = s.Mode()&0100 != 0 |
Colin Cross | 957cc4e | 2015-04-24 15:10:32 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 435 | if z.directories { |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 436 | dir, _ := filepath.Split(dest) |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 437 | err := z.writeDirectory(dir) |
| 438 | if err != nil { |
| 439 | return err |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 443 | compressChan := make(chan *zipEntry, 1) |
| 444 | z.writeOps <- compressChan |
| 445 | |
| 446 | // Pre-fill a zipEntry, it will be sent in the compressChan once |
| 447 | // we're sure about the Method and CRC. |
| 448 | ze := &zipEntry{ |
| 449 | fh: &zip.FileHeader{ |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 450 | Name: dest, |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 451 | Method: method, |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 452 | |
| 453 | UncompressedSize64: uint64(fileSize), |
| 454 | }, |
| 455 | } |
| 456 | ze.fh.SetModTime(z.time) |
Dan Willemsen | 10462b3 | 2017-03-15 19:02:51 -0700 | [diff] [blame] | 457 | if executable { |
| 458 | ze.fh.SetMode(0700) |
| 459 | } |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 460 | |
Nan Zhang | 9067b04 | 2017-03-17 14:04:43 -0700 | [diff] [blame] | 461 | r, err := os.Open(src) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 462 | if err != nil { |
| 463 | return err |
| 464 | } |
| 465 | |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 466 | ze.allocatedSize = fileSize |
| 467 | z.cpuRateLimiter.Request() |
| 468 | z.memoryRateLimiter.Request(ze.allocatedSize) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 469 | |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 470 | if method == zip.Deflate && fileSize >= minParallelFileSize { |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 471 | wg := new(sync.WaitGroup) |
| 472 | |
| 473 | // Allocate enough buffer to hold all readers. We'll limit |
| 474 | // this based on actual buffer sizes in RateLimit. |
| 475 | ze.futureReaders = make(chan chan io.Reader, (fileSize/parallelBlockSize)+1) |
| 476 | |
| 477 | // Calculate the CRC in the background, since reading the entire |
| 478 | // file could take a while. |
| 479 | // |
| 480 | // We could split this up into chuncks as well, but it's faster |
| 481 | // than the compression. Due to the Go Zip API, we also need to |
| 482 | // know the result before we can begin writing the compressed |
| 483 | // data out to the zipfile. |
| 484 | wg.Add(1) |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 485 | go z.crcFile(r, ze, compressChan, wg) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 486 | |
| 487 | for start := int64(0); start < fileSize; start += parallelBlockSize { |
| 488 | sr := io.NewSectionReader(r, start, parallelBlockSize) |
| 489 | resultChan := make(chan io.Reader, 1) |
| 490 | ze.futureReaders <- resultChan |
| 491 | |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 492 | z.cpuRateLimiter.Request() |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 493 | |
| 494 | last := !(start+parallelBlockSize < fileSize) |
| 495 | var dict []byte |
| 496 | if start >= windowSize { |
| 497 | dict, err = ioutil.ReadAll(io.NewSectionReader(r, start-windowSize, windowSize)) |
| 498 | } |
| 499 | |
| 500 | wg.Add(1) |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 501 | go z.compressPartialFile(sr, dict, last, resultChan, wg) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | close(ze.futureReaders) |
| 505 | |
| 506 | // Close the file handle after all readers are done |
| 507 | go func(wg *sync.WaitGroup, f *os.File) { |
| 508 | wg.Wait() |
| 509 | f.Close() |
| 510 | }(wg, r) |
| 511 | } else { |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 512 | go z.compressWholeFile(ze, r, compressChan) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | return nil |
| 516 | } |
| 517 | |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 518 | 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] | 519 | defer wg.Done() |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 520 | defer z.cpuRateLimiter.Finish() |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 521 | |
| 522 | crc := crc32.NewIEEE() |
| 523 | _, err := io.Copy(crc, r) |
| 524 | if err != nil { |
| 525 | z.errors <- err |
| 526 | return |
| 527 | } |
| 528 | |
| 529 | ze.fh.CRC32 = crc.Sum32() |
| 530 | resultChan <- ze |
| 531 | close(resultChan) |
| 532 | } |
| 533 | |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 534 | 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] | 535 | defer wg.Done() |
| 536 | |
| 537 | result, err := z.compressBlock(r, dict, last) |
| 538 | if err != nil { |
| 539 | z.errors <- err |
| 540 | return |
| 541 | } |
| 542 | |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 543 | z.cpuRateLimiter.Finish() |
| 544 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 545 | resultChan <- result |
| 546 | } |
| 547 | |
| 548 | func (z *zipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) { |
| 549 | buf := new(bytes.Buffer) |
| 550 | var fw *flate.Writer |
| 551 | var err error |
| 552 | if len(dict) > 0 { |
| 553 | // There's no way to Reset a Writer with a new dictionary, so |
| 554 | // don't use the Pool |
| 555 | fw, err = flate.NewWriterDict(buf, z.compLevel, dict) |
| 556 | } else { |
| 557 | var ok bool |
| 558 | if fw, ok = z.compressorPool.Get().(*flate.Writer); ok { |
| 559 | fw.Reset(buf) |
| 560 | } else { |
| 561 | fw, err = flate.NewWriter(buf, z.compLevel) |
| 562 | } |
| 563 | defer z.compressorPool.Put(fw) |
| 564 | } |
| 565 | if err != nil { |
| 566 | return nil, err |
| 567 | } |
| 568 | |
| 569 | _, err = io.Copy(fw, r) |
| 570 | if err != nil { |
| 571 | return nil, err |
| 572 | } |
| 573 | if last { |
| 574 | fw.Close() |
| 575 | } else { |
| 576 | fw.Flush() |
| 577 | } |
| 578 | |
| 579 | return buf, nil |
| 580 | } |
| 581 | |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 582 | func (z *zipWriter) compressWholeFile(ze *zipEntry, r *os.File, compressChan chan *zipEntry) { |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 583 | defer r.Close() |
| 584 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 585 | crc := crc32.NewIEEE() |
Dan Willemsen | a8b5502 | 2017-03-15 21:49:26 -0700 | [diff] [blame] | 586 | _, err := io.Copy(crc, r) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 587 | if err != nil { |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 588 | z.errors <- err |
| 589 | return |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Dan Willemsen | a8b5502 | 2017-03-15 21:49:26 -0700 | [diff] [blame] | 592 | ze.fh.CRC32 = crc.Sum32() |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 593 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 594 | _, err = r.Seek(0, 0) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 595 | if err != nil { |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 596 | z.errors <- err |
| 597 | return |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 600 | readFile := func(r *os.File) ([]byte, error) { |
| 601 | _, err = r.Seek(0, 0) |
| 602 | if err != nil { |
| 603 | return nil, err |
| 604 | } |
| 605 | |
| 606 | buf, err := ioutil.ReadAll(r) |
| 607 | if err != nil { |
| 608 | return nil, err |
| 609 | } |
| 610 | |
| 611 | return buf, nil |
| 612 | } |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 613 | |
Dan Willemsen | a8b5502 | 2017-03-15 21:49:26 -0700 | [diff] [blame] | 614 | ze.futureReaders = make(chan chan io.Reader, 1) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 615 | futureReader := make(chan io.Reader, 1) |
| 616 | ze.futureReaders <- futureReader |
| 617 | close(ze.futureReaders) |
| 618 | |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 619 | if ze.fh.Method == zip.Deflate { |
| 620 | compressed, err := z.compressBlock(r, nil, true) |
| 621 | if err != nil { |
| 622 | z.errors <- err |
| 623 | return |
| 624 | } |
| 625 | if uint64(compressed.Len()) < ze.fh.UncompressedSize64 { |
| 626 | futureReader <- compressed |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 627 | } else { |
| 628 | buf, err := readFile(r) |
| 629 | if err != nil { |
| 630 | z.errors <- err |
| 631 | return |
| 632 | } |
| 633 | ze.fh.Method = zip.Store |
| 634 | futureReader <- bytes.NewReader(buf) |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 635 | } |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 636 | } else { |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 637 | buf, err := readFile(r) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 638 | if err != nil { |
| 639 | z.errors <- err |
| 640 | return |
| 641 | } |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 642 | ze.fh.Method = zip.Store |
| 643 | futureReader <- bytes.NewReader(buf) |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 644 | } |
Nan Zhang | f281bd8 | 2017-04-25 16:47:45 -0700 | [diff] [blame] | 645 | |
Jeff Gaston | 175f34c | 2017-08-17 21:43:21 -0700 | [diff] [blame] | 646 | z.cpuRateLimiter.Finish() |
| 647 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 648 | close(futureReader) |
| 649 | |
| 650 | compressChan <- ze |
| 651 | close(compressChan) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 652 | } |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 653 | |
| 654 | func (z *zipWriter) writeDirectory(dir string) error { |
Jeff Gaston | 2d17413 | 2017-08-15 18:05:56 -0700 | [diff] [blame] | 655 | // clean the input |
| 656 | cleanDir := filepath.Clean(dir) |
| 657 | |
| 658 | // discover any uncreated directories in the path |
| 659 | zipDirs := []string{} |
| 660 | for cleanDir != "" && cleanDir != "." && !z.createdDirs[cleanDir] { |
| 661 | |
| 662 | z.createdDirs[cleanDir] = true |
| 663 | // parent directories precede their children |
| 664 | zipDirs = append([]string{cleanDir}, zipDirs...) |
| 665 | |
| 666 | cleanDir = filepath.Dir(cleanDir) |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Jeff Gaston | 2d17413 | 2017-08-15 18:05:56 -0700 | [diff] [blame] | 669 | // make a directory entry for each uncreated directory |
| 670 | for _, cleanDir := range zipDirs { |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 671 | dirHeader := &zip.FileHeader{ |
Jeff Gaston | 2d17413 | 2017-08-15 18:05:56 -0700 | [diff] [blame] | 672 | Name: cleanDir + "/", |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 673 | } |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 674 | dirHeader.SetMode(0700 | os.ModeDir) |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 675 | dirHeader.SetModTime(z.time) |
| 676 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 677 | ze := make(chan *zipEntry, 1) |
| 678 | ze <- &zipEntry{ |
| 679 | fh: dirHeader, |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 680 | } |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 681 | close(ze) |
| 682 | z.writeOps <- ze |
Colin Cross | e19c793 | 2015-04-24 15:08:38 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | return nil |
| 686 | } |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 687 | |
| 688 | func (z *zipWriter) writeSymlink(rel, file string) error { |
| 689 | if z.directories { |
| 690 | dir, _ := filepath.Split(rel) |
| 691 | if err := z.writeDirectory(dir); err != nil { |
| 692 | return err |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | fileHeader := &zip.FileHeader{ |
| 697 | Name: rel, |
| 698 | } |
| 699 | fileHeader.SetModTime(z.time) |
| 700 | fileHeader.SetMode(0700 | os.ModeSymlink) |
| 701 | |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 702 | dest, err := os.Readlink(file) |
| 703 | if err != nil { |
| 704 | return err |
| 705 | } |
| 706 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 707 | ze := make(chan *zipEntry, 1) |
| 708 | futureReaders := make(chan chan io.Reader, 1) |
| 709 | futureReader := make(chan io.Reader, 1) |
| 710 | futureReaders <- futureReader |
| 711 | close(futureReaders) |
| 712 | futureReader <- bytes.NewBufferString(dest) |
| 713 | close(futureReader) |
| 714 | |
Dan Willemsen | 017d893 | 2016-08-04 15:43:03 -0700 | [diff] [blame] | 715 | ze <- &zipEntry{ |
| 716 | fh: fileHeader, |
| 717 | futureReaders: futureReaders, |
| 718 | } |
| 719 | close(ze) |
| 720 | z.writeOps <- ze |
| 721 | |
| 722 | return nil |
Dan Willemsen | a59a3bc | 2016-08-03 17:47:23 -0700 | [diff] [blame] | 723 | } |