diff options
author | Colin Cross <ccross@android.com> | 2018-10-09 10:19:54 -0700 |
---|---|---|
committer | Colin Cross <ccross@android.com> | 2018-10-09 17:48:16 +0000 |
commit | 3824771b2d3c4695476d4721b1e00c5da69e8b2c (patch) | |
tree | 58eca9ef5af201266ea3f710a23acbea78c02f6c /cmd | |
parent | b1a5e9cadfdd0765f763883fd7410add24486ef6 (diff) | |
download | build_soong-3824771b2d3c4695476d4721b1e00c5da69e8b2c.tar.gz build_soong-3824771b2d3c4695476d4721b1e00c5da69e8b2c.tar.bz2 build_soong-3824771b2d3c4695476d4721b1e00c5da69e8b2c.zip |
zip2zip: support included an excluded file
Make needs to exclude all files that match a filespec except for
those specified in a list. Add a -X argument that overrides the
-x argument to allow files be included that were previously
excluded.
Bug: 69500920
Test: zip2zip_test.go
Change-Id: Icc8aebc788b53bcdde73a39c9c7cf3107a049251
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/zip2zip/zip2zip.go | 55 | ||||
-rw-r--r-- | cmd/zip2zip/zip2zip_test.go | 30 |
2 files changed, 62 insertions, 23 deletions
diff --git a/cmd/zip2zip/zip2zip.go b/cmd/zip2zip/zip2zip.go index d3b349c5..c4fb3d6c 100644 --- a/cmd/zip2zip/zip2zip.go +++ b/cmd/zip2zip/zip2zip.go @@ -41,11 +41,13 @@ var ( staticTime = time.Date(2009, 1, 1, 0, 0, 0, 0, time.UTC) excludes multiFlag + includes multiFlag uncompress multiFlag ) func init() { flag.Var(&excludes, "x", "exclude a filespec from the output") + flag.Var(&includes, "X", "include a filespec in the output that was previously excluded") flag.Var(&uncompress, "0", "convert a filespec to uncompressed in the output") } @@ -96,7 +98,7 @@ func main() { }() if err := zip2zip(&reader.Reader, writer, *sortGlobs, *sortJava, *setTime, - flag.Args(), excludes, uncompress); err != nil { + flag.Args(), excludes, includes, uncompress); err != nil { log.Fatal(err) } @@ -109,7 +111,7 @@ type pair struct { } func zip2zip(reader *zip.Reader, writer *zip.Writer, sortOutput, sortJava, setTime bool, - includes, excludes, uncompresses []string) error { + args []string, excludes, includes multiFlag, uncompresses []string) error { matches := []pair{} @@ -125,14 +127,14 @@ func zip2zip(reader *zip.Reader, writer *zip.Writer, sortOutput, sortJava, setTi } } - for _, include := range includes { + for _, arg := range args { // Reserve escaping for future implementation, so make sure no // one is using \ and expecting a certain behavior. - if strings.Contains(include, "\\") { + if strings.Contains(arg, "\\") { return fmt.Errorf("\\ characters are not currently supported") } - input, output := includeSplit(include) + input, output := includeSplit(arg) var includeMatches []pair @@ -161,7 +163,7 @@ func zip2zip(reader *zip.Reader, writer *zip.Writer, sortOutput, sortJava, setTi matches = append(matches, includeMatches...) } - if len(includes) == 0 { + if len(args) == 0 { // implicitly match everything for _, file := range reader.File { matches = append(matches, pair{file, file.Name, false}) @@ -173,21 +175,18 @@ func zip2zip(reader *zip.Reader, writer *zip.Writer, sortOutput, sortJava, setTi seen := make(map[string]*zip.File) for _, match := range matches { - // Filter out matches whose original file name matches an exclude filter - excluded := false - for _, exclude := range excludes { - if excludeMatch, err := pathtools.Match(exclude, match.File.Name); err != nil { + // Filter out matches whose original file name matches an exclude filter, unless it also matches an + // include filter + if exclude, err := excludes.Match(match.File.Name); err != nil { + return err + } else if exclude { + if include, err := includes.Match(match.File.Name); err != nil { return err - } else if excludeMatch { - excluded = true - break + } else if !include { + continue } } - if excluded { - continue - } - // Check for duplicate output names, ignoring ones that come from the same input zip entry. if prev, exists := seen[match.newName]; exists { if prev != match.File { @@ -256,11 +255,25 @@ func includeSplit(s string) (string, string) { type multiFlag []string -func (e *multiFlag) String() string { - return strings.Join(*e, " ") +func (m *multiFlag) String() string { + return strings.Join(*m, " ") } -func (e *multiFlag) Set(s string) error { - *e = append(*e, s) +func (m *multiFlag) Set(s string) error { + *m = append(*m, s) return nil } + +func (m *multiFlag) Match(s string) (bool, error) { + if m == nil { + return false, nil + } + for _, f := range *m { + if match, err := pathtools.Match(f, s); err != nil { + return false, err + } else if match { + return true, nil + } + } + return false, nil +} diff --git a/cmd/zip2zip/zip2zip_test.go b/cmd/zip2zip/zip2zip_test.go index e032fe69..ae164944 100644 --- a/cmd/zip2zip/zip2zip_test.go +++ b/cmd/zip2zip/zip2zip_test.go @@ -31,6 +31,7 @@ var testCases = []struct { sortJava bool args []string excludes []string + includes []string uncompresses []string outputFiles []string @@ -228,7 +229,7 @@ var testCases = []struct { }, }, { - name: "excludes with include", + name: "excludes with args", inputFiles: []string{ "a/a", @@ -242,6 +243,31 @@ var testCases = []struct { }, }, { + name: "excludes over args", + + inputFiles: []string{ + "a/a", + "a/b", + }, + args: []string{"a/a"}, + excludes: []string{"a/*"}, + + outputFiles: nil, + }, + { + name: "excludes with includes", + + inputFiles: []string{ + "a/a", + "a/b", + }, + args: nil, + excludes: []string{"a/*"}, + includes: []string{"a/b"}, + + outputFiles: []string{"a/b"}, + }, + { name: "excludes with glob", inputFiles: []string{ @@ -358,7 +384,7 @@ func TestZip2Zip(t *testing.T) { outputWriter := zip.NewWriter(outputBuf) err = zip2zip(inputReader, outputWriter, testCase.sortGlobs, testCase.sortJava, false, - testCase.args, testCase.excludes, testCase.uncompresses) + testCase.args, testCase.excludes, testCase.includes, testCase.uncompresses) if errorString(testCase.err) != errorString(err) { t.Fatalf("Unexpected error:\n got: %q\nwant: %q", errorString(err), errorString(testCase.err)) } |