Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 1 | // Copyright 2017 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 ( |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 18 | "errors" |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 19 | "flag" |
| 20 | "fmt" |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 21 | "hash/crc32" |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 22 | "log" |
| 23 | "os" |
Nan Zhang | 13f4cf5 | 2017-09-19 18:42:01 -0700 | [diff] [blame] | 24 | "path/filepath" |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 25 | "sort" |
| 26 | "strings" |
| 27 | |
| 28 | "android/soong/jar" |
| 29 | "android/soong/third_party/zip" |
| 30 | ) |
| 31 | |
Nan Zhang | 13f4cf5 | 2017-09-19 18:42:01 -0700 | [diff] [blame] | 32 | type stripDir struct{} |
Nan Zhang | d5998cc | 2017-09-13 13:17:43 -0700 | [diff] [blame] | 33 | |
Nan Zhang | 13f4cf5 | 2017-09-19 18:42:01 -0700 | [diff] [blame] | 34 | func (s *stripDir) String() string { |
Nan Zhang | d5998cc | 2017-09-13 13:17:43 -0700 | [diff] [blame] | 35 | return `""` |
| 36 | } |
| 37 | |
Nan Zhang | 13f4cf5 | 2017-09-19 18:42:01 -0700 | [diff] [blame] | 38 | func (s *stripDir) Set(dir string) error { |
| 39 | stripDirs = append(stripDirs, filepath.Clean(dir)) |
| 40 | |
| 41 | return nil |
| 42 | } |
| 43 | |
| 44 | type zipToNotStrip struct{} |
| 45 | |
| 46 | func (s *zipToNotStrip) String() string { |
| 47 | return `""` |
| 48 | } |
| 49 | |
| 50 | func (s *zipToNotStrip) Set(zip_path string) error { |
| 51 | zipsToNotStrip[zip_path] = true |
Nan Zhang | d5998cc | 2017-09-13 13:17:43 -0700 | [diff] [blame] | 52 | |
| 53 | return nil |
| 54 | } |
| 55 | |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 56 | var ( |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 57 | sortEntries = flag.Bool("s", false, "sort entries (defaults to the order from the input zip files)") |
| 58 | emulateJar = flag.Bool("j", false, "sort zip entries using jar ordering (META-INF first)") |
| 59 | stripDirs []string |
| 60 | zipsToNotStrip = make(map[string]bool) |
| 61 | stripDirEntries = flag.Bool("D", false, "strip directory entries from the output zip file") |
| 62 | manifest = flag.String("m", "", "manifest file to insert in jar") |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 63 | ) |
| 64 | |
Nan Zhang | d5998cc | 2017-09-13 13:17:43 -0700 | [diff] [blame] | 65 | func init() { |
Nan Zhang | 13f4cf5 | 2017-09-19 18:42:01 -0700 | [diff] [blame] | 66 | flag.Var(&stripDir{}, "stripDir", "the prefix of file path to be excluded from the output zip") |
| 67 | flag.Var(&zipToNotStrip{}, "zipToNotStrip", "the input zip file which is not applicable for stripping") |
Nan Zhang | d5998cc | 2017-09-13 13:17:43 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 70 | func main() { |
| 71 | flag.Usage = func() { |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 72 | fmt.Fprintln(os.Stderr, "usage: merge_zips [-jsD] [-m manifest] output [inputs...]") |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 73 | flag.PrintDefaults() |
| 74 | } |
| 75 | |
| 76 | // parse args |
| 77 | flag.Parse() |
| 78 | args := flag.Args() |
| 79 | if len(args) < 2 { |
| 80 | flag.Usage() |
| 81 | os.Exit(1) |
| 82 | } |
| 83 | outputPath := args[0] |
| 84 | inputs := args[1:] |
| 85 | |
| 86 | log.SetFlags(log.Lshortfile) |
| 87 | |
| 88 | // make writer |
| 89 | output, err := os.Create(outputPath) |
| 90 | if err != nil { |
| 91 | log.Fatal(err) |
| 92 | } |
| 93 | defer output.Close() |
| 94 | writer := zip.NewWriter(output) |
| 95 | defer func() { |
| 96 | err := writer.Close() |
| 97 | if err != nil { |
| 98 | log.Fatal(err) |
| 99 | } |
| 100 | }() |
| 101 | |
| 102 | // make readers |
| 103 | readers := []namedZipReader{} |
| 104 | for _, input := range inputs { |
| 105 | reader, err := zip.OpenReader(input) |
| 106 | if err != nil { |
| 107 | log.Fatal(err) |
| 108 | } |
| 109 | defer reader.Close() |
| 110 | namedReader := namedZipReader{path: input, reader: reader} |
| 111 | readers = append(readers, namedReader) |
| 112 | } |
| 113 | |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 114 | if *manifest != "" && !*emulateJar { |
| 115 | log.Fatal(errors.New("must specify -j when specifying a manifest via -m")) |
| 116 | } |
| 117 | |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 118 | // do merge |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 119 | err = mergeZips(readers, writer, *manifest, *sortEntries, *emulateJar, *stripDirEntries) |
| 120 | if err != nil { |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 121 | log.Fatal(err) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // a namedZipReader reads a .zip file and can say which file it's reading |
| 126 | type namedZipReader struct { |
| 127 | path string |
| 128 | reader *zip.ReadCloser |
| 129 | } |
| 130 | |
| 131 | // a zipEntryPath refers to a file contained in a zip |
| 132 | type zipEntryPath struct { |
| 133 | zipName string |
| 134 | entryName string |
| 135 | } |
| 136 | |
| 137 | func (p zipEntryPath) String() string { |
| 138 | return p.zipName + "/" + p.entryName |
| 139 | } |
| 140 | |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 141 | // a zipEntry is a zipSource that pulls its content from another zip |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 142 | type zipEntry struct { |
| 143 | path zipEntryPath |
| 144 | content *zip.File |
| 145 | } |
| 146 | |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 147 | func (ze zipEntry) String() string { |
| 148 | return ze.path.String() |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 151 | func (ze zipEntry) IsDir() bool { |
| 152 | return ze.content.FileInfo().IsDir() |
| 153 | } |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 154 | |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 155 | func (ze zipEntry) CRC32() uint32 { |
| 156 | return ze.content.FileHeader.CRC32 |
| 157 | } |
| 158 | |
| 159 | func (ze zipEntry) WriteToZip(dest string, zw *zip.Writer) error { |
| 160 | return zw.CopyFrom(ze.content, dest) |
| 161 | } |
| 162 | |
| 163 | // a bufferEntry is a zipSource that pulls its content from a []byte |
| 164 | type bufferEntry struct { |
| 165 | fh *zip.FileHeader |
| 166 | content []byte |
| 167 | } |
| 168 | |
| 169 | func (be bufferEntry) String() string { |
| 170 | return "internal buffer" |
| 171 | } |
| 172 | |
| 173 | func (be bufferEntry) IsDir() bool { |
| 174 | return be.fh.FileInfo().IsDir() |
| 175 | } |
| 176 | |
| 177 | func (be bufferEntry) CRC32() uint32 { |
| 178 | return crc32.ChecksumIEEE(be.content) |
| 179 | } |
| 180 | |
| 181 | func (be bufferEntry) WriteToZip(dest string, zw *zip.Writer) error { |
| 182 | w, err := zw.CreateHeader(be.fh) |
| 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | |
| 187 | if !be.IsDir() { |
| 188 | _, err = w.Write(be.content) |
| 189 | if err != nil { |
| 190 | return err |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return nil |
| 195 | } |
| 196 | |
| 197 | type zipSource interface { |
| 198 | String() string |
| 199 | IsDir() bool |
| 200 | CRC32() uint32 |
| 201 | WriteToZip(dest string, zw *zip.Writer) error |
| 202 | } |
| 203 | |
| 204 | // a fileMapping specifies to copy a zip entry from one place to another |
| 205 | type fileMapping struct { |
| 206 | dest string |
| 207 | source zipSource |
| 208 | } |
| 209 | |
| 210 | func mergeZips(readers []namedZipReader, writer *zip.Writer, manifest string, |
| 211 | sortEntries, emulateJar, stripDirEntries bool) error { |
| 212 | |
| 213 | sourceByDest := make(map[string]zipSource, 0) |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 214 | orderedMappings := []fileMapping{} |
| 215 | |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 216 | // if dest already exists returns a non-null zipSource for the existing source |
| 217 | addMapping := func(dest string, source zipSource) zipSource { |
| 218 | mapKey := filepath.Clean(dest) |
| 219 | if existingSource, exists := sourceByDest[mapKey]; exists { |
| 220 | return existingSource |
| 221 | } |
| 222 | |
| 223 | sourceByDest[mapKey] = source |
| 224 | orderedMappings = append(orderedMappings, fileMapping{source: source, dest: dest}) |
| 225 | return nil |
| 226 | } |
| 227 | |
| 228 | if manifest != "" { |
| 229 | if !stripDirEntries { |
| 230 | dirHeader := jar.MetaDirFileHeader() |
| 231 | dirSource := bufferEntry{dirHeader, nil} |
| 232 | addMapping(jar.MetaDir, dirSource) |
| 233 | } |
| 234 | |
| 235 | fh, buf, err := jar.ManifestFileContents(manifest) |
| 236 | if err != nil { |
| 237 | return err |
| 238 | } |
| 239 | |
| 240 | fileSource := bufferEntry{fh, buf} |
| 241 | addMapping(jar.ManifestFile, fileSource) |
| 242 | } |
| 243 | |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 244 | for _, namedReader := range readers { |
Nan Zhang | 13f4cf5 | 2017-09-19 18:42:01 -0700 | [diff] [blame] | 245 | _, skipStripThisZip := zipsToNotStrip[namedReader.path] |
Nan Zhang | d5998cc | 2017-09-13 13:17:43 -0700 | [diff] [blame] | 246 | FileLoop: |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 247 | for _, file := range namedReader.reader.File { |
Nan Zhang | 13f4cf5 | 2017-09-19 18:42:01 -0700 | [diff] [blame] | 248 | if !skipStripThisZip { |
| 249 | for _, dir := range stripDirs { |
| 250 | if strings.HasPrefix(file.Name, dir+"/") { |
| 251 | if emulateJar { |
| 252 | if file.Name != jar.MetaDir && file.Name != jar.ManifestFile { |
| 253 | continue FileLoop |
| 254 | } |
| 255 | } else { |
| 256 | continue FileLoop |
| 257 | } |
| 258 | } |
Nan Zhang | d5998cc | 2017-09-13 13:17:43 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 261 | |
| 262 | if stripDirEntries && file.FileInfo().IsDir() { |
| 263 | continue |
| 264 | } |
| 265 | |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 266 | // check for other files or directories destined for the same path |
| 267 | dest := file.Name |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 268 | |
| 269 | // make a new entry to add |
| 270 | source := zipEntry{path: zipEntryPath{zipName: namedReader.path, entryName: file.Name}, content: file} |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 271 | |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 272 | if existingSource := addMapping(dest, source); existingSource != nil { |
Colin Cross | 3454031 | 2017-09-06 12:52:37 -0700 | [diff] [blame] | 273 | // handle duplicates |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 274 | if existingSource.IsDir() != source.IsDir() { |
Colin Cross | 3454031 | 2017-09-06 12:52:37 -0700 | [diff] [blame] | 275 | return fmt.Errorf("Directory/file mismatch at %v from %v and %v\n", |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 276 | dest, existingSource, source) |
Colin Cross | 3454031 | 2017-09-06 12:52:37 -0700 | [diff] [blame] | 277 | } |
| 278 | if emulateJar && |
| 279 | file.Name == jar.ManifestFile || file.Name == jar.ModuleInfoClass { |
| 280 | // Skip manifest and module info files that are not from the first input file |
| 281 | continue |
| 282 | } |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 283 | if !source.IsDir() { |
Nan Zhang | d5998cc | 2017-09-13 13:17:43 -0700 | [diff] [blame] | 284 | if emulateJar { |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 285 | if existingSource.CRC32() != source.CRC32() { |
Nan Zhang | d5998cc | 2017-09-13 13:17:43 -0700 | [diff] [blame] | 286 | fmt.Fprintf(os.Stdout, "WARNING: Duplicate path %v found in %v and %v\n", |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 287 | dest, existingSource, source) |
Nan Zhang | d5998cc | 2017-09-13 13:17:43 -0700 | [diff] [blame] | 288 | } |
| 289 | } else { |
| 290 | return fmt.Errorf("Duplicate path %v found in %v and %v\n", |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 291 | dest, existingSource, source) |
Nan Zhang | d5998cc | 2017-09-13 13:17:43 -0700 | [diff] [blame] | 292 | } |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 295 | } |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Colin Cross | 3454031 | 2017-09-06 12:52:37 -0700 | [diff] [blame] | 298 | if emulateJar { |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 299 | jarSort(orderedMappings) |
| 300 | } else if sortEntries { |
| 301 | alphanumericSort(orderedMappings) |
| 302 | } |
| 303 | |
| 304 | for _, entry := range orderedMappings { |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 305 | if err := entry.source.WriteToZip(entry.dest, writer); err != nil { |
Jeff Gaston | 8bab5f2 | 2017-09-01 13:34:28 -0700 | [diff] [blame] | 306 | return err |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return nil |
| 311 | } |
| 312 | |
| 313 | func jarSort(files []fileMapping) { |
| 314 | sort.SliceStable(files, func(i, j int) bool { |
| 315 | return jar.EntryNamesLess(files[i].dest, files[j].dest) |
| 316 | }) |
| 317 | } |
| 318 | |
| 319 | func alphanumericSort(files []fileMapping) { |
| 320 | sort.SliceStable(files, func(i, j int) bool { |
| 321 | return files[i].dest < files[j].dest |
| 322 | }) |
| 323 | } |