Make merge_zips -stripFile use blueprint style globs
merge_zips -stripFile was only considering the name of the file and
ignoring the path. Make it more useful by supporting blueprint style
globs. The previous behavior can be recreated by prefixing with **/.
Bug: 111389216
Test: m checkbuild
Change-Id: I25760fe3f1f77704dd9da9d107d9a38a415d681f
diff --git a/cmd/merge_zips/merge_zips.go b/cmd/merge_zips/merge_zips.go
index 84eace6..95ff70b 100644
--- a/cmd/merge_zips/merge_zips.go
+++ b/cmd/merge_zips/merge_zips.go
@@ -24,7 +24,8 @@
"os"
"path/filepath"
"sort"
- "strings"
+
+ "github.com/google/blueprint/pathtools"
"android/soong/jar"
"android/soong/third_party/zip"
@@ -69,8 +70,8 @@
)
func init() {
- flag.Var(&stripDirs, "stripDir", "the prefix of file path to be excluded from the output zip")
- flag.Var(&stripFiles, "stripFile", "filenames to be excluded from the output zip, accepts wildcards")
+ flag.Var(&stripDirs, "stripDir", "directories to be excluded from the output zip, accepts wildcards")
+ flag.Var(&stripFiles, "stripFile", "files to be excluded from the output zip, accepts wildcards")
flag.Var(&zipsToNotStrip, "zipToNotStrip", "the input zip file which is not applicable for stripping")
}
@@ -339,8 +340,12 @@
for _, namedReader := range readers {
_, skipStripThisZip := zipsToNotStrip[namedReader.path]
for _, file := range namedReader.reader.File {
- if !skipStripThisZip && shouldStripFile(emulateJar, stripFiles, stripDirs, file.Name) {
- continue
+ if !skipStripThisZip {
+ if skip, err := shouldStripEntry(emulateJar, stripFiles, stripDirs, file.Name); err != nil {
+ return err
+ } else if skip {
+ continue
+ }
}
if stripDirEntries && file.FileInfo().IsDir() {
@@ -420,26 +425,41 @@
return ret
}
-func shouldStripFile(emulateJar bool, stripFiles, stripDirs []string, name string) bool {
+func shouldStripEntry(emulateJar bool, stripFiles, stripDirs []string, name string) (bool, error) {
for _, dir := range stripDirs {
- if strings.HasPrefix(name, dir+"/") {
- if emulateJar {
- if name != jar.MetaDir && name != jar.ManifestFile {
- return true
+ dir = filepath.Clean(dir)
+ patterns := []string{
+ dir + "/", // the directory itself
+ dir + "/**/*", // files recursively in the directory
+ dir + "/**/*/", // directories recursively in the directory
+ }
+
+ for _, pattern := range patterns {
+ match, err := pathtools.Match(pattern, name)
+ if err != nil {
+ return false, fmt.Errorf("%s: %s", err.Error(), pattern)
+ } else if match {
+ if emulateJar {
+ // When merging jar files, don't strip META-INF/MANIFEST.MF even if stripping META-INF is
+ // requested.
+ // TODO(ccross): which files does this affect?
+ if name != jar.MetaDir && name != jar.ManifestFile {
+ return true, nil
+ }
}
- } else {
- return true
+ return true, nil
}
}
}
+
for _, pattern := range stripFiles {
- if match, err := filepath.Match(pattern, filepath.Base(name)); err != nil {
- panic(fmt.Errorf("%s: %s", err.Error(), pattern))
+ if match, err := pathtools.Match(pattern, name); err != nil {
+ return false, fmt.Errorf("%s: %s", err.Error(), pattern)
} else if match {
- return true
+ return true, nil
}
}
- return false
+ return false, nil
}
func jarSort(files []fileMapping) {