Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 ( |
| 18 | "flag" |
| 19 | "fmt" |
Colin Cross | b1a5e9c | 2018-10-07 21:30:12 -0700 | [diff] [blame^] | 20 | "io" |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 21 | "log" |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 22 | "os" |
| 23 | "path/filepath" |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 24 | "sort" |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 25 | "strings" |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 26 | "time" |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 27 | |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 28 | "github.com/google/blueprint/pathtools" |
| 29 | |
Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 30 | "android/soong/jar" |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 31 | "android/soong/third_party/zip" |
| 32 | ) |
| 33 | |
| 34 | var ( |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 35 | input = flag.String("i", "", "zip file to read from") |
| 36 | output = flag.String("o", "", "output file") |
| 37 | sortGlobs = flag.Bool("s", false, "sort matches from each glob (defaults to the order from the input zip file)") |
Colin Cross | 8936b02 | 2017-06-23 13:00:17 -0700 | [diff] [blame] | 38 | sortJava = flag.Bool("j", false, "sort using jar ordering within each glob (META-INF/MANIFEST.MF first)") |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 39 | setTime = flag.Bool("t", false, "set timestamps to 2009-01-01 00:00:00") |
| 40 | |
| 41 | staticTime = time.Date(2009, 1, 1, 0, 0, 0, 0, time.UTC) |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 42 | |
Colin Cross | b1a5e9c | 2018-10-07 21:30:12 -0700 | [diff] [blame^] | 43 | excludes multiFlag |
| 44 | uncompress multiFlag |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 45 | ) |
| 46 | |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 47 | func init() { |
| 48 | flag.Var(&excludes, "x", "exclude a filespec from the output") |
Colin Cross | b1a5e9c | 2018-10-07 21:30:12 -0700 | [diff] [blame^] | 49 | flag.Var(&uncompress, "0", "convert a filespec to uncompressed in the output") |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 52 | func main() { |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 53 | flag.Usage = func() { |
Colin Cross | 8936b02 | 2017-06-23 13:00:17 -0700 | [diff] [blame] | 54 | fmt.Fprintln(os.Stderr, "usage: zip2zip -i zipfile -o zipfile [-s|-j] [-t] [filespec]...") |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 55 | flag.PrintDefaults() |
| 56 | fmt.Fprintln(os.Stderr, " filespec:") |
| 57 | fmt.Fprintln(os.Stderr, " <name>") |
| 58 | fmt.Fprintln(os.Stderr, " <in_name>:<out_name>") |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 59 | fmt.Fprintln(os.Stderr, " <glob>[:<out_dir>]") |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 60 | fmt.Fprintln(os.Stderr, "") |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 61 | fmt.Fprintln(os.Stderr, "<glob> uses the rules at https://godoc.org/github.com/google/blueprint/pathtools/#Match") |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 62 | fmt.Fprintln(os.Stderr, "") |
| 63 | fmt.Fprintln(os.Stderr, "Files will be copied with their existing compression from the input zipfile to") |
Colin Cross | 0638299 | 2017-06-23 14:08:42 -0700 | [diff] [blame] | 64 | fmt.Fprintln(os.Stderr, "the output zipfile, in the order of filespec arguments.") |
| 65 | fmt.Fprintln(os.Stderr, "") |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 66 | fmt.Fprintln(os.Stderr, "If no filepsec is provided all files and directories are copied.") |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 69 | flag.Parse() |
| 70 | |
Colin Cross | 0638299 | 2017-06-23 14:08:42 -0700 | [diff] [blame] | 71 | if *input == "" || *output == "" { |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 72 | flag.Usage() |
| 73 | os.Exit(1) |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 76 | log.SetFlags(log.Lshortfile) |
| 77 | |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 78 | reader, err := zip.OpenReader(*input) |
| 79 | if err != nil { |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 80 | log.Fatal(err) |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 81 | } |
| 82 | defer reader.Close() |
| 83 | |
| 84 | output, err := os.Create(*output) |
| 85 | if err != nil { |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 86 | log.Fatal(err) |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 87 | } |
| 88 | defer output.Close() |
| 89 | |
| 90 | writer := zip.NewWriter(output) |
| 91 | defer func() { |
| 92 | err := writer.Close() |
| 93 | if err != nil { |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 94 | log.Fatal(err) |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 95 | } |
| 96 | }() |
| 97 | |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 98 | if err := zip2zip(&reader.Reader, writer, *sortGlobs, *sortJava, *setTime, |
Colin Cross | b1a5e9c | 2018-10-07 21:30:12 -0700 | [diff] [blame^] | 99 | flag.Args(), excludes, uncompress); err != nil { |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 100 | |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 101 | log.Fatal(err) |
| 102 | } |
| 103 | } |
| 104 | |
Colin Cross | 8936b02 | 2017-06-23 13:00:17 -0700 | [diff] [blame] | 105 | type pair struct { |
| 106 | *zip.File |
Colin Cross | b1a5e9c | 2018-10-07 21:30:12 -0700 | [diff] [blame^] | 107 | newName string |
| 108 | uncompress bool |
Colin Cross | 8936b02 | 2017-06-23 13:00:17 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 111 | func zip2zip(reader *zip.Reader, writer *zip.Writer, sortOutput, sortJava, setTime bool, |
Colin Cross | b1a5e9c | 2018-10-07 21:30:12 -0700 | [diff] [blame^] | 112 | includes, excludes, uncompresses []string) error { |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 113 | |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 114 | matches := []pair{} |
| 115 | |
| 116 | sortMatches := func(matches []pair) { |
| 117 | if sortJava { |
| 118 | sort.SliceStable(matches, func(i, j int) bool { |
| 119 | return jar.EntryNamesLess(matches[i].newName, matches[j].newName) |
| 120 | }) |
| 121 | } else if sortOutput { |
| 122 | sort.SliceStable(matches, func(i, j int) bool { |
| 123 | return matches[i].newName < matches[j].newName |
| 124 | }) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | for _, include := range includes { |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 129 | // Reserve escaping for future implementation, so make sure no |
| 130 | // one is using \ and expecting a certain behavior. |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 131 | if strings.Contains(include, "\\") { |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 132 | return fmt.Errorf("\\ characters are not currently supported") |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 135 | input, output := includeSplit(include) |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 136 | |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 137 | var includeMatches []pair |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 138 | |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 139 | for _, file := range reader.File { |
| 140 | var newName string |
| 141 | if match, err := pathtools.Match(input, file.Name); err != nil { |
| 142 | return err |
| 143 | } else if match { |
| 144 | if output == "" { |
| 145 | newName = file.Name |
| 146 | } else { |
| 147 | if pathtools.IsGlob(input) { |
| 148 | // If the input is a glob then the output is a directory. |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 149 | _, name := filepath.Split(file.Name) |
| 150 | newName = filepath.Join(output, name) |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 151 | } else { |
| 152 | // Otherwise it is a file. |
| 153 | newName = output |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 154 | } |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 155 | } |
Colin Cross | b1a5e9c | 2018-10-07 21:30:12 -0700 | [diff] [blame^] | 156 | includeMatches = append(includeMatches, pair{file, newName, false}) |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 159 | |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 160 | sortMatches(includeMatches) |
| 161 | matches = append(matches, includeMatches...) |
| 162 | } |
| 163 | |
| 164 | if len(includes) == 0 { |
| 165 | // implicitly match everything |
| 166 | for _, file := range reader.File { |
Colin Cross | b1a5e9c | 2018-10-07 21:30:12 -0700 | [diff] [blame^] | 167 | matches = append(matches, pair{file, file.Name, false}) |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 168 | } |
| 169 | sortMatches(matches) |
| 170 | } |
| 171 | |
| 172 | var matchesAfterExcludes []pair |
| 173 | seen := make(map[string]*zip.File) |
| 174 | |
| 175 | for _, match := range matches { |
| 176 | // Filter out matches whose original file name matches an exclude filter |
| 177 | excluded := false |
| 178 | for _, exclude := range excludes { |
| 179 | if excludeMatch, err := pathtools.Match(exclude, match.File.Name); err != nil { |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 180 | return err |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 181 | } else if excludeMatch { |
| 182 | excluded = true |
| 183 | break |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 186 | |
| 187 | if excluded { |
| 188 | continue |
| 189 | } |
| 190 | |
| 191 | // Check for duplicate output names, ignoring ones that come from the same input zip entry. |
| 192 | if prev, exists := seen[match.newName]; exists { |
| 193 | if prev != match.File { |
| 194 | return fmt.Errorf("multiple entries for %q with different contents", match.newName) |
| 195 | } |
| 196 | continue |
| 197 | } |
| 198 | seen[match.newName] = match.File |
| 199 | |
Colin Cross | b1a5e9c | 2018-10-07 21:30:12 -0700 | [diff] [blame^] | 200 | for _, u := range uncompresses { |
| 201 | if uncompressMatch, err := pathtools.Match(u, match.newName); err != nil { |
| 202 | return err |
| 203 | } else if uncompressMatch { |
| 204 | match.uncompress = true |
| 205 | break |
| 206 | } |
| 207 | } |
| 208 | |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 209 | matchesAfterExcludes = append(matchesAfterExcludes, match) |
| 210 | } |
| 211 | |
| 212 | for _, match := range matchesAfterExcludes { |
| 213 | if setTime { |
| 214 | match.File.SetModTime(staticTime) |
| 215 | } |
Colin Cross | b1a5e9c | 2018-10-07 21:30:12 -0700 | [diff] [blame^] | 216 | if match.uncompress && match.File.FileHeader.Method != zip.Store { |
| 217 | fh := match.File.FileHeader |
| 218 | fh.Name = match.newName |
| 219 | fh.Method = zip.Store |
| 220 | fh.CompressedSize64 = fh.UncompressedSize64 |
| 221 | |
| 222 | zw, err := writer.CreateHeaderAndroid(&fh) |
| 223 | if err != nil { |
| 224 | return err |
| 225 | } |
| 226 | |
| 227 | zr, err := match.File.Open() |
| 228 | if err != nil { |
| 229 | return err |
| 230 | } |
| 231 | |
| 232 | _, err = io.Copy(zw, zr) |
| 233 | zr.Close() |
| 234 | if err != nil { |
| 235 | return err |
| 236 | } |
| 237 | } else { |
| 238 | err := writer.CopyFrom(match.File, match.newName) |
| 239 | if err != nil { |
| 240 | return err |
| 241 | } |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 242 | } |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 243 | } |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 244 | |
| 245 | return nil |
Dan Willemsen | 3bf1a08 | 2016-08-03 00:35:25 -0700 | [diff] [blame] | 246 | } |
Colin Cross | 8936b02 | 2017-06-23 13:00:17 -0700 | [diff] [blame] | 247 | |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 248 | func includeSplit(s string) (string, string) { |
| 249 | split := strings.SplitN(s, ":", 2) |
| 250 | if len(split) == 2 { |
| 251 | return split[0], split[1] |
| 252 | } else { |
| 253 | return split[0], "" |
| 254 | } |
| 255 | } |
| 256 | |
Colin Cross | b1a5e9c | 2018-10-07 21:30:12 -0700 | [diff] [blame^] | 257 | type multiFlag []string |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 258 | |
Colin Cross | b1a5e9c | 2018-10-07 21:30:12 -0700 | [diff] [blame^] | 259 | func (e *multiFlag) String() string { |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 260 | return strings.Join(*e, " ") |
| 261 | } |
| 262 | |
Colin Cross | b1a5e9c | 2018-10-07 21:30:12 -0700 | [diff] [blame^] | 263 | func (e *multiFlag) Set(s string) error { |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame] | 264 | *e = append(*e, s) |
| 265 | return nil |
Colin Cross | 8936b02 | 2017-06-23 13:00:17 -0700 | [diff] [blame] | 266 | } |