blob: 95ff70b06287115ada750bb597f5ad50150ef20e [file] [log] [blame]
Jeff Gaston8bab5f22017-09-01 13:34:28 -07001// 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
15package main
16
17import (
Colin Cross635acc92017-09-12 22:50:46 -070018 "errors"
Jeff Gaston8bab5f22017-09-01 13:34:28 -070019 "flag"
20 "fmt"
Colin Cross635acc92017-09-12 22:50:46 -070021 "hash/crc32"
Nan Zhang5925b0f2017-12-19 15:13:40 -080022 "io/ioutil"
Jeff Gaston8bab5f22017-09-01 13:34:28 -070023 "log"
24 "os"
Nan Zhang13f4cf52017-09-19 18:42:01 -070025 "path/filepath"
Jeff Gaston8bab5f22017-09-01 13:34:28 -070026 "sort"
Colin Cross4c03f682018-07-15 08:16:31 -070027
28 "github.com/google/blueprint/pathtools"
Jeff Gaston8bab5f22017-09-01 13:34:28 -070029
30 "android/soong/jar"
31 "android/soong/third_party/zip"
32)
33
Colin Cross0cf45cd2017-10-04 17:04:16 -070034type fileList []string
Nan Zhangd5998cc2017-09-13 13:17:43 -070035
Colin Cross0cf45cd2017-10-04 17:04:16 -070036func (f *fileList) String() string {
Nan Zhangd5998cc2017-09-13 13:17:43 -070037 return `""`
38}
39
Colin Cross0cf45cd2017-10-04 17:04:16 -070040func (f *fileList) Set(name string) error {
41 *f = append(*f, filepath.Clean(name))
Nan Zhang13f4cf52017-09-19 18:42:01 -070042
43 return nil
44}
45
Colin Cross0cf45cd2017-10-04 17:04:16 -070046type zipsToNotStripSet map[string]bool
Nan Zhang13f4cf52017-09-19 18:42:01 -070047
Colin Cross0cf45cd2017-10-04 17:04:16 -070048func (s zipsToNotStripSet) String() string {
Nan Zhang13f4cf52017-09-19 18:42:01 -070049 return `""`
50}
51
Colin Cross0cf45cd2017-10-04 17:04:16 -070052func (s zipsToNotStripSet) Set(zip_path string) error {
53 s[zip_path] = true
Nan Zhangd5998cc2017-09-13 13:17:43 -070054
55 return nil
56}
57
Jeff Gaston8bab5f22017-09-01 13:34:28 -070058var (
Colin Crosse909e1e2017-11-22 14:09:40 -080059 sortEntries = flag.Bool("s", false, "sort entries (defaults to the order from the input zip files)")
60 emulateJar = flag.Bool("j", false, "sort zip entries using jar ordering (META-INF first)")
Nan Zhang5925b0f2017-12-19 15:13:40 -080061 emulatePar = flag.Bool("p", false, "merge zip entries based on par format")
Colin Crosse909e1e2017-11-22 14:09:40 -080062 stripDirs fileList
63 stripFiles fileList
64 zipsToNotStrip = make(zipsToNotStripSet)
65 stripDirEntries = flag.Bool("D", false, "strip directory entries from the output zip file")
66 manifest = flag.String("m", "", "manifest file to insert in jar")
Nan Zhang1db85402017-12-18 13:20:23 -080067 pyMain = flag.String("pm", "", "__main__.py file to insert in par")
Nan Zhang5925b0f2017-12-19 15:13:40 -080068 entrypoint = flag.String("e", "", "par entrypoint file to insert in par")
Colin Crosse909e1e2017-11-22 14:09:40 -080069 ignoreDuplicates = flag.Bool("ignore-duplicates", false, "take each entry from the first zip it exists in and don't warn")
Jeff Gaston8bab5f22017-09-01 13:34:28 -070070)
71
Nan Zhangd5998cc2017-09-13 13:17:43 -070072func init() {
Colin Cross4c03f682018-07-15 08:16:31 -070073 flag.Var(&stripDirs, "stripDir", "directories to be excluded from the output zip, accepts wildcards")
74 flag.Var(&stripFiles, "stripFile", "files to be excluded from the output zip, accepts wildcards")
Colin Cross0cf45cd2017-10-04 17:04:16 -070075 flag.Var(&zipsToNotStrip, "zipToNotStrip", "the input zip file which is not applicable for stripping")
Nan Zhangd5998cc2017-09-13 13:17:43 -070076}
77
Jeff Gaston8bab5f22017-09-01 13:34:28 -070078func main() {
79 flag.Usage = func() {
Nan Zhang1db85402017-12-18 13:20:23 -080080 fmt.Fprintln(os.Stderr, "usage: merge_zips [-jpsD] [-m manifest] [-e entrypoint] [-pm __main__.py] output [inputs...]")
Jeff Gaston8bab5f22017-09-01 13:34:28 -070081 flag.PrintDefaults()
82 }
83
84 // parse args
85 flag.Parse()
86 args := flag.Args()
Colin Cross5c6ecc12017-10-23 18:12:27 -070087 if len(args) < 1 {
Jeff Gaston8bab5f22017-09-01 13:34:28 -070088 flag.Usage()
89 os.Exit(1)
90 }
91 outputPath := args[0]
92 inputs := args[1:]
93
94 log.SetFlags(log.Lshortfile)
95
96 // make writer
97 output, err := os.Create(outputPath)
98 if err != nil {
99 log.Fatal(err)
100 }
101 defer output.Close()
102 writer := zip.NewWriter(output)
103 defer func() {
104 err := writer.Close()
105 if err != nil {
106 log.Fatal(err)
107 }
108 }()
109
110 // make readers
111 readers := []namedZipReader{}
112 for _, input := range inputs {
113 reader, err := zip.OpenReader(input)
114 if err != nil {
115 log.Fatal(err)
116 }
117 defer reader.Close()
Colin Cross24860652018-07-14 22:19:14 -0700118 namedReader := namedZipReader{path: input, reader: &reader.Reader}
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700119 readers = append(readers, namedReader)
120 }
121
Colin Cross635acc92017-09-12 22:50:46 -0700122 if *manifest != "" && !*emulateJar {
123 log.Fatal(errors.New("must specify -j when specifying a manifest via -m"))
124 }
125
Nan Zhang5925b0f2017-12-19 15:13:40 -0800126 if *entrypoint != "" && !*emulatePar {
127 log.Fatal(errors.New("must specify -p when specifying a entrypoint via -e"))
128 }
129
Nan Zhang1db85402017-12-18 13:20:23 -0800130 if *pyMain != "" && !*emulatePar {
131 log.Fatal(errors.New("must specify -p when specifying a Python __main__.py via -pm"))
132 }
133
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700134 // do merge
Nan Zhang1db85402017-12-18 13:20:23 -0800135 err = mergeZips(readers, writer, *manifest, *entrypoint, *pyMain, *sortEntries, *emulateJar, *emulatePar,
Colin Cross24860652018-07-14 22:19:14 -0700136 *stripDirEntries, *ignoreDuplicates, []string(stripFiles), []string(stripDirs), map[string]bool(zipsToNotStrip))
Colin Cross635acc92017-09-12 22:50:46 -0700137 if err != nil {
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700138 log.Fatal(err)
139 }
140}
141
142// a namedZipReader reads a .zip file and can say which file it's reading
143type namedZipReader struct {
144 path string
Colin Cross24860652018-07-14 22:19:14 -0700145 reader *zip.Reader
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700146}
147
148// a zipEntryPath refers to a file contained in a zip
149type zipEntryPath struct {
150 zipName string
151 entryName string
152}
153
154func (p zipEntryPath) String() string {
155 return p.zipName + "/" + p.entryName
156}
157
Colin Cross635acc92017-09-12 22:50:46 -0700158// a zipEntry is a zipSource that pulls its content from another zip
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700159type zipEntry struct {
160 path zipEntryPath
161 content *zip.File
162}
163
Colin Cross635acc92017-09-12 22:50:46 -0700164func (ze zipEntry) String() string {
165 return ze.path.String()
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700166}
167
Colin Cross635acc92017-09-12 22:50:46 -0700168func (ze zipEntry) IsDir() bool {
169 return ze.content.FileInfo().IsDir()
170}
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700171
Colin Cross635acc92017-09-12 22:50:46 -0700172func (ze zipEntry) CRC32() uint32 {
173 return ze.content.FileHeader.CRC32
174}
175
176func (ze zipEntry) WriteToZip(dest string, zw *zip.Writer) error {
177 return zw.CopyFrom(ze.content, dest)
178}
179
180// a bufferEntry is a zipSource that pulls its content from a []byte
181type bufferEntry struct {
182 fh *zip.FileHeader
183 content []byte
184}
185
186func (be bufferEntry) String() string {
187 return "internal buffer"
188}
189
190func (be bufferEntry) IsDir() bool {
191 return be.fh.FileInfo().IsDir()
192}
193
194func (be bufferEntry) CRC32() uint32 {
195 return crc32.ChecksumIEEE(be.content)
196}
197
198func (be bufferEntry) WriteToZip(dest string, zw *zip.Writer) error {
199 w, err := zw.CreateHeader(be.fh)
200 if err != nil {
201 return err
202 }
203
204 if !be.IsDir() {
205 _, err = w.Write(be.content)
206 if err != nil {
207 return err
208 }
209 }
210
211 return nil
212}
213
214type zipSource interface {
215 String() string
216 IsDir() bool
217 CRC32() uint32
218 WriteToZip(dest string, zw *zip.Writer) error
219}
220
221// a fileMapping specifies to copy a zip entry from one place to another
222type fileMapping struct {
223 dest string
224 source zipSource
225}
226
Nan Zhang1db85402017-12-18 13:20:23 -0800227func mergeZips(readers []namedZipReader, writer *zip.Writer, manifest, entrypoint, pyMain string,
Colin Cross24860652018-07-14 22:19:14 -0700228 sortEntries, emulateJar, emulatePar, stripDirEntries, ignoreDuplicates bool,
229 stripFiles, stripDirs []string, zipsToNotStrip map[string]bool) error {
Colin Cross635acc92017-09-12 22:50:46 -0700230
231 sourceByDest := make(map[string]zipSource, 0)
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700232 orderedMappings := []fileMapping{}
233
Colin Cross635acc92017-09-12 22:50:46 -0700234 // if dest already exists returns a non-null zipSource for the existing source
235 addMapping := func(dest string, source zipSource) zipSource {
236 mapKey := filepath.Clean(dest)
237 if existingSource, exists := sourceByDest[mapKey]; exists {
238 return existingSource
239 }
240
241 sourceByDest[mapKey] = source
242 orderedMappings = append(orderedMappings, fileMapping{source: source, dest: dest})
243 return nil
244 }
245
246 if manifest != "" {
247 if !stripDirEntries {
248 dirHeader := jar.MetaDirFileHeader()
249 dirSource := bufferEntry{dirHeader, nil}
250 addMapping(jar.MetaDir, dirSource)
251 }
252
253 fh, buf, err := jar.ManifestFileContents(manifest)
254 if err != nil {
255 return err
256 }
257
258 fileSource := bufferEntry{fh, buf}
259 addMapping(jar.ManifestFile, fileSource)
260 }
261
Nan Zhang5925b0f2017-12-19 15:13:40 -0800262 if entrypoint != "" {
263 buf, err := ioutil.ReadFile(entrypoint)
264 if err != nil {
265 return err
266 }
267 fh := &zip.FileHeader{
268 Name: "entry_point.txt",
269 Method: zip.Store,
270 UncompressedSize64: uint64(len(buf)),
271 }
272 fh.SetMode(0700)
273 fh.SetModTime(jar.DefaultTime)
274 fileSource := bufferEntry{fh, buf}
275 addMapping("entry_point.txt", fileSource)
276 }
277
Nan Zhang1db85402017-12-18 13:20:23 -0800278 if pyMain != "" {
279 buf, err := ioutil.ReadFile(pyMain)
280 if err != nil {
281 return err
282 }
283 fh := &zip.FileHeader{
284 Name: "__main__.py",
285 Method: zip.Store,
286 UncompressedSize64: uint64(len(buf)),
287 }
288 fh.SetMode(0700)
289 fh.SetModTime(jar.DefaultTime)
290 fileSource := bufferEntry{fh, buf}
291 addMapping("__main__.py", fileSource)
292 }
293
Nan Zhang5925b0f2017-12-19 15:13:40 -0800294 if emulatePar {
295 // the runfiles packages needs to be populated with "__init__.py".
296 newPyPkgs := []string{}
297 // the runfiles dirs have been treated as packages.
298 existingPyPkgSet := make(map[string]bool)
299 // put existing __init__.py files to a set first. This set is used for preventing
300 // generated __init__.py files from overwriting existing ones.
301 for _, namedReader := range readers {
302 for _, file := range namedReader.reader.File {
303 if filepath.Base(file.Name) != "__init__.py" {
304 continue
305 }
306 pyPkg := pathBeforeLastSlash(file.Name)
307 if _, found := existingPyPkgSet[pyPkg]; found {
308 panic(fmt.Errorf("found __init__.py path duplicates during pars merging: %q.", file.Name))
309 } else {
310 existingPyPkgSet[pyPkg] = true
311 }
312 }
313 }
314 for _, namedReader := range readers {
315 for _, file := range namedReader.reader.File {
316 var parentPath string /* the path after trimming last "/" */
317 if filepath.Base(file.Name) == "__init__.py" {
318 // for existing __init__.py files, we should trim last "/" for twice.
319 // eg. a/b/c/__init__.py ---> a/b
320 parentPath = pathBeforeLastSlash(pathBeforeLastSlash(file.Name))
321 } else {
322 parentPath = pathBeforeLastSlash(file.Name)
323 }
324 populateNewPyPkgs(parentPath, existingPyPkgSet, &newPyPkgs)
325 }
326 }
327 for _, pkg := range newPyPkgs {
328 var emptyBuf []byte
329 fh := &zip.FileHeader{
330 Name: filepath.Join(pkg, "__init__.py"),
331 Method: zip.Store,
332 UncompressedSize64: uint64(len(emptyBuf)),
333 }
334 fh.SetMode(0700)
335 fh.SetModTime(jar.DefaultTime)
336 fileSource := bufferEntry{fh, emptyBuf}
337 addMapping(filepath.Join(pkg, "__init__.py"), fileSource)
338 }
339 }
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700340 for _, namedReader := range readers {
Nan Zhang13f4cf52017-09-19 18:42:01 -0700341 _, skipStripThisZip := zipsToNotStrip[namedReader.path]
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700342 for _, file := range namedReader.reader.File {
Colin Cross4c03f682018-07-15 08:16:31 -0700343 if !skipStripThisZip {
344 if skip, err := shouldStripEntry(emulateJar, stripFiles, stripDirs, file.Name); err != nil {
345 return err
346 } else if skip {
347 continue
348 }
Nan Zhangd5998cc2017-09-13 13:17:43 -0700349 }
Colin Cross635acc92017-09-12 22:50:46 -0700350
351 if stripDirEntries && file.FileInfo().IsDir() {
352 continue
353 }
354
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700355 // check for other files or directories destined for the same path
356 dest := file.Name
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700357
358 // make a new entry to add
359 source := zipEntry{path: zipEntryPath{zipName: namedReader.path, entryName: file.Name}, content: file}
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700360
Colin Cross635acc92017-09-12 22:50:46 -0700361 if existingSource := addMapping(dest, source); existingSource != nil {
Colin Cross34540312017-09-06 12:52:37 -0700362 // handle duplicates
Colin Cross635acc92017-09-12 22:50:46 -0700363 if existingSource.IsDir() != source.IsDir() {
Colin Cross34540312017-09-06 12:52:37 -0700364 return fmt.Errorf("Directory/file mismatch at %v from %v and %v\n",
Colin Cross635acc92017-09-12 22:50:46 -0700365 dest, existingSource, source)
Colin Cross34540312017-09-06 12:52:37 -0700366 }
Colin Crosse909e1e2017-11-22 14:09:40 -0800367 if ignoreDuplicates {
368 continue
369 }
Colin Cross34540312017-09-06 12:52:37 -0700370 if emulateJar &&
371 file.Name == jar.ManifestFile || file.Name == jar.ModuleInfoClass {
372 // Skip manifest and module info files that are not from the first input file
373 continue
374 }
Colin Cross635acc92017-09-12 22:50:46 -0700375 if !source.IsDir() {
Nan Zhangd5998cc2017-09-13 13:17:43 -0700376 if emulateJar {
Colin Cross635acc92017-09-12 22:50:46 -0700377 if existingSource.CRC32() != source.CRC32() {
Nan Zhangd5998cc2017-09-13 13:17:43 -0700378 fmt.Fprintf(os.Stdout, "WARNING: Duplicate path %v found in %v and %v\n",
Colin Cross635acc92017-09-12 22:50:46 -0700379 dest, existingSource, source)
Nan Zhangd5998cc2017-09-13 13:17:43 -0700380 }
381 } else {
382 return fmt.Errorf("Duplicate path %v found in %v and %v\n",
Colin Cross635acc92017-09-12 22:50:46 -0700383 dest, existingSource, source)
Nan Zhangd5998cc2017-09-13 13:17:43 -0700384 }
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700385 }
386 }
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700387 }
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700388 }
389
Colin Cross34540312017-09-06 12:52:37 -0700390 if emulateJar {
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700391 jarSort(orderedMappings)
392 } else if sortEntries {
393 alphanumericSort(orderedMappings)
394 }
395
396 for _, entry := range orderedMappings {
Colin Cross635acc92017-09-12 22:50:46 -0700397 if err := entry.source.WriteToZip(entry.dest, writer); err != nil {
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700398 return err
399 }
400 }
401
402 return nil
403}
404
Nan Zhang5925b0f2017-12-19 15:13:40 -0800405// Sets the given directory and all its ancestor directories as Python packages.
406func populateNewPyPkgs(pkgPath string, existingPyPkgSet map[string]bool, newPyPkgs *[]string) {
407 for pkgPath != "" {
408 if _, found := existingPyPkgSet[pkgPath]; !found {
409 existingPyPkgSet[pkgPath] = true
410 *newPyPkgs = append(*newPyPkgs, pkgPath)
411 // Gets its ancestor directory by trimming last slash.
412 pkgPath = pathBeforeLastSlash(pkgPath)
413 } else {
414 break
415 }
416 }
417}
418
419func pathBeforeLastSlash(path string) string {
420 ret := filepath.Dir(path)
421 // filepath.Dir("abc") -> "." and filepath.Dir("/abc") -> "/".
422 if ret == "." || ret == "/" {
423 return ""
424 }
425 return ret
426}
427
Colin Cross4c03f682018-07-15 08:16:31 -0700428func shouldStripEntry(emulateJar bool, stripFiles, stripDirs []string, name string) (bool, error) {
Colin Cross0cf45cd2017-10-04 17:04:16 -0700429 for _, dir := range stripDirs {
Colin Cross4c03f682018-07-15 08:16:31 -0700430 dir = filepath.Clean(dir)
431 patterns := []string{
432 dir + "/", // the directory itself
433 dir + "/**/*", // files recursively in the directory
434 dir + "/**/*/", // directories recursively in the directory
435 }
436
437 for _, pattern := range patterns {
438 match, err := pathtools.Match(pattern, name)
439 if err != nil {
440 return false, fmt.Errorf("%s: %s", err.Error(), pattern)
441 } else if match {
442 if emulateJar {
443 // When merging jar files, don't strip META-INF/MANIFEST.MF even if stripping META-INF is
444 // requested.
445 // TODO(ccross): which files does this affect?
446 if name != jar.MetaDir && name != jar.ManifestFile {
447 return true, nil
448 }
Colin Cross0cf45cd2017-10-04 17:04:16 -0700449 }
Colin Cross4c03f682018-07-15 08:16:31 -0700450 return true, nil
Colin Cross0cf45cd2017-10-04 17:04:16 -0700451 }
452 }
453 }
Colin Cross4c03f682018-07-15 08:16:31 -0700454
Colin Cross0cf45cd2017-10-04 17:04:16 -0700455 for _, pattern := range stripFiles {
Colin Cross4c03f682018-07-15 08:16:31 -0700456 if match, err := pathtools.Match(pattern, name); err != nil {
457 return false, fmt.Errorf("%s: %s", err.Error(), pattern)
Colin Cross0cf45cd2017-10-04 17:04:16 -0700458 } else if match {
Colin Cross4c03f682018-07-15 08:16:31 -0700459 return true, nil
Colin Cross0cf45cd2017-10-04 17:04:16 -0700460 }
461 }
Colin Cross4c03f682018-07-15 08:16:31 -0700462 return false, nil
Colin Cross0cf45cd2017-10-04 17:04:16 -0700463}
464
Jeff Gaston8bab5f22017-09-01 13:34:28 -0700465func jarSort(files []fileMapping) {
466 sort.SliceStable(files, func(i, j int) bool {
467 return jar.EntryNamesLess(files[i].dest, files[j].dest)
468 })
469}
470
471func alphanumericSort(files []fileMapping) {
472 sort.SliceStable(files, func(i, j int) bool {
473 return files[i].dest < files[j].dest
474 })
475}