From 13f4cf5c9edbee82f91d80d3734e5804404df536 Mon Sep 17 00:00:00 2001 From: Nan Zhang Date: Tue, 19 Sep 2017 18:42:01 -0700 Subject: [PATCH] Add -stripDir and -zipToNotStrip option in merge_zips. In some cases, we need delete META-INF dir to void mis-using some classes files in the Java runtime. For now, when we delete META-INF, we need keep Manifest file in the jar since merge_zips doesn't have ability to add a default one. Bug: b/65455145 Test: m clean && m -j Change-Id: Iea44b1a98d348cd8df1fa7374907733b1add6935 --- cmd/merge_zips/merge_zips.go | 46 +++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/cmd/merge_zips/merge_zips.go b/cmd/merge_zips/merge_zips.go index 3c1cb9a18..9fd5ddd8e 100644 --- a/cmd/merge_zips/merge_zips.go +++ b/cmd/merge_zips/merge_zips.go @@ -19,6 +19,7 @@ import ( "fmt" "log" "os" + "path/filepath" "sort" "strings" @@ -26,26 +27,40 @@ import ( "android/soong/third_party/zip" ) -type strip struct{} +type stripDir struct{} -func (s *strip) String() string { +func (s *stripDir) String() string { return `""` } -func (s *strip) Set(path_prefix string) error { - strippings = append(strippings, path_prefix) +func (s *stripDir) Set(dir string) error { + stripDirs = append(stripDirs, filepath.Clean(dir)) + + return nil +} + +type zipToNotStrip struct{} + +func (s *zipToNotStrip) String() string { + return `""` +} + +func (s *zipToNotStrip) Set(zip_path string) error { + zipsToNotStrip[zip_path] = true return nil } var ( - sortEntries = flag.Bool("s", false, "sort entries (defaults to the order from the input zip files)") - emulateJar = flag.Bool("j", false, "sort zip entries using jar ordering (META-INF first)") - strippings []string + sortEntries = flag.Bool("s", false, "sort entries (defaults to the order from the input zip files)") + emulateJar = flag.Bool("j", false, "sort zip entries using jar ordering (META-INF first)") + stripDirs []string + zipsToNotStrip = make(map[string]bool) ) func init() { - flag.Var(&strip{}, "strip", "the prefix of file path to be excluded from the output zip") + flag.Var(&stripDir{}, "stripDir", "the prefix of file path to be excluded from the output zip") + flag.Var(&zipToNotStrip{}, "zipToNotStrip", "the input zip file which is not applicable for stripping") } func main() { @@ -132,11 +147,20 @@ func mergeZips(readers []namedZipReader, writer *zip.Writer, sortEntries bool, e orderedMappings := []fileMapping{} for _, namedReader := range readers { + _, skipStripThisZip := zipsToNotStrip[namedReader.path] FileLoop: for _, file := range namedReader.reader.File { - for _, path_prefix := range strippings { - if strings.HasPrefix(file.Name, path_prefix) { - continue FileLoop + if !skipStripThisZip { + for _, dir := range stripDirs { + if strings.HasPrefix(file.Name, dir+"/") { + if emulateJar { + if file.Name != jar.MetaDir && file.Name != jar.ManifestFile { + continue FileLoop + } + } else { + continue FileLoop + } + } } } // check for other files or directories destined for the same path