|
13 | 13 | // limitations under the License. |
14 | 14 | package com.google.devtools.build.android; |
15 | 15 |
|
| 16 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 17 | + |
16 | 18 | import com.beust.jcommander.IStringConverter; |
17 | 19 | import com.beust.jcommander.IValueValidator; |
18 | 20 | import com.beust.jcommander.JCommander; |
|
23 | 25 | import com.google.common.base.Joiner; |
24 | 26 | import com.google.common.base.Stopwatch; |
25 | 27 | import com.google.common.collect.ImmutableList; |
26 | | -import com.google.common.collect.ImmutableMap; |
27 | 28 | import com.google.common.collect.ImmutableSetMultimap; |
28 | 29 | import com.google.common.collect.Multimap; |
29 | | -import com.google.devtools.build.singlejar.ZipCombiner; |
30 | | -import com.google.devtools.build.singlejar.ZipCombiner.OutputMode; |
| 30 | +import java.io.BufferedReader; |
| 31 | +import java.io.BufferedWriter; |
| 32 | +import java.io.File; |
| 33 | +import java.io.FileOutputStream; |
31 | 34 | import java.io.IOException; |
32 | | -import java.io.OutputStream; |
| 35 | +import java.io.InputStreamReader; |
33 | 36 | import java.nio.file.FileSystems; |
34 | 37 | import java.nio.file.Files; |
35 | 38 | import java.nio.file.Path; |
| 39 | +import java.util.ArrayList; |
36 | 40 | import java.util.Collection; |
37 | 41 | import java.util.Enumeration; |
38 | 42 | import java.util.LinkedHashSet; |
|
43 | 47 | import java.util.regex.Pattern; |
44 | 48 | import java.util.zip.ZipEntry; |
45 | 49 | import java.util.zip.ZipFile; |
| 50 | +import java.util.zip.ZipOutputStream; |
46 | 51 |
|
47 | 52 | /** |
48 | 53 | * Action to filter entries out of a Zip file. |
|
71 | 76 | * </pre> |
72 | 77 | */ |
73 | 78 | public class ZipFilterAction { |
| 79 | + /** A copy of Bazel's singlejar.ZipCombiner#OutputMode enum. */ |
| 80 | + enum OutputMode { |
| 81 | + |
| 82 | + /** Output entries using any method. */ |
| 83 | + DONT_CARE, |
| 84 | + |
| 85 | + /** |
| 86 | + * Output all entries using DEFLATE method, except directory entries. It is always more |
| 87 | + * efficient to store directory entries uncompressed. |
| 88 | + */ |
| 89 | + FORCE_DEFLATE, |
| 90 | + |
| 91 | + /** Output all entries using STORED method. */ |
| 92 | + FORCE_STORED, |
| 93 | + } |
| 94 | + |
| 95 | + record GenerateExcludeListResult(int sawErrors, ArrayList<String> excludeList) {} |
74 | 96 |
|
75 | 97 | private static final Logger logger = Logger.getLogger(ZipFilterAction.class.getName()); |
76 | 98 |
|
@@ -200,45 +222,133 @@ public static void main(String[] args) throws IOException { |
200 | 222 | System.exit(run(args)); |
201 | 223 | } |
202 | 224 |
|
203 | | - static int run(String[] args) throws IOException { |
204 | | - Options options = new Options(); |
205 | | - new JCommander(options).parse(args); |
206 | | - logger.fine( |
207 | | - String.format( |
208 | | - "Creating filter from entries of type %s, in zip files %s.", |
209 | | - options.filterTypes, options.filterZips)); |
210 | | - |
211 | | - final Stopwatch timer = Stopwatch.createStarted(); |
| 225 | + static GenerateExcludeListResult generateExcludeList(Options options, Stopwatch timer) |
| 226 | + throws IOException { |
212 | 227 | Multimap<String, Long> entriesToOmit = |
213 | 228 | getEntriesToOmit(options.filterZips, options.filterTypes); |
214 | 229 | final String explicitFilter = |
215 | 230 | options.explicitFilters.isEmpty() |
216 | 231 | ? "" |
217 | 232 | : String.format(".*(%s).*", Joiner.on("|").join(options.explicitFilters)); |
218 | 233 | logger.fine(String.format("Filter created in %dms", timer.elapsed(TimeUnit.MILLISECONDS))); |
| 234 | + ArrayList<String> excludeList = new ArrayList<>(); |
219 | 235 |
|
220 | | - ImmutableMap.Builder<String, Long> inputEntries = ImmutableMap.builder(); |
221 | | - try (ZipFile zf = new ZipFile(options.inputZip.toFile())) { |
| 236 | + int sawErrors = 0; |
| 237 | + try (ZipFile zf = new ZipFile(options.inputZip.toFile()); |
| 238 | + ZipOutputStream zos = |
| 239 | + new ZipOutputStream(new FileOutputStream(options.outputZip.toString()))) { |
222 | 240 | Enumeration<? extends ZipEntry> entries = zf.entries(); |
223 | 241 |
|
224 | 242 | while (entries.hasMoreElements()) { |
225 | 243 | ZipEntry entry = entries.nextElement(); |
226 | | - inputEntries.put(entry.getName(), entry.getCrc()); |
| 244 | + if (entry.getName().matches(explicitFilter)) { |
| 245 | + excludeList.add(entry.getName()); |
| 246 | + } else if (entriesToOmit.containsEntry(entry.getName(), entry.getCrc())) { |
| 247 | + excludeList.add(entry.getName()); |
| 248 | + } else if (entriesToOmit.containsKey(entry.getName())) { |
| 249 | + // entriesToOmit contains the filename, but a different CRC was observed. |
| 250 | + if (options.hashMismatchCheckMode == HashMismatchCheckMode.IGNORE) { |
| 251 | + // Just add it to the excluded entry list. |
| 252 | + excludeList.add(entry.getName()); |
| 253 | + } else { |
| 254 | + if (options.hashMismatchCheckMode == HashMismatchCheckMode.ERROR) { |
| 255 | + logger.severe( |
| 256 | + String.format( |
| 257 | + "ERROR: Requested to filter entries of name " |
| 258 | + + "'%s'; name matches but the hash does not.\n", |
| 259 | + entry.getName())); |
| 260 | + sawErrors = 1; |
| 261 | + excludeList.add(entry.getName()); |
| 262 | + } else { |
| 263 | + logger.severe( |
| 264 | + String.format( |
| 265 | + "WARNING: Requested to filter entries of name " |
| 266 | + + "'%s'; name matches but the hash does not. Copying anyway.\n", |
| 267 | + entry.getName())); |
| 268 | + } |
| 269 | + } |
| 270 | + } |
227 | 271 | } |
228 | 272 | } |
| 273 | + return new GenerateExcludeListResult(sawErrors, excludeList); |
| 274 | + } |
229 | 275 |
|
230 | | - ZipFilterEntryFilter entryFilter = |
231 | | - new ZipFilterEntryFilter( |
232 | | - explicitFilter, |
233 | | - entriesToOmit, |
234 | | - inputEntries.buildOrThrow(), |
235 | | - options.hashMismatchCheckMode); |
| 276 | + @SuppressWarnings("RuntimeExec") |
| 277 | + static int run(String[] args) throws IOException { |
| 278 | + Options options = new Options(); |
| 279 | + new JCommander(options).parse(args); |
| 280 | + logger.fine( |
| 281 | + String.format( |
| 282 | + "Creating filter from entries of type %s, in zip files %s.", |
| 283 | + options.filterTypes, options.filterZips)); |
| 284 | + |
| 285 | + String compressionStrategy = "--dont_change_compression"; |
| 286 | + if (options.outputMode == OutputMode.FORCE_STORED) { |
| 287 | + compressionStrategy = "--compression"; |
| 288 | + } else if (options.outputMode == OutputMode.FORCE_DEFLATE) { |
| 289 | + throw new IllegalArgumentException("FORCE_DEFLATE is not supported."); |
| 290 | + } |
| 291 | + |
| 292 | + String singleJarPath = |
| 293 | + Path.of(System.getProperty("runfiles.path"), System.getProperty("singlejar.path")) |
| 294 | + .toString(); |
| 295 | + ImmutableList.Builder<String> singleJarArgsBuilder = ImmutableList.builder(); |
| 296 | + singleJarArgsBuilder |
| 297 | + .add("--sources") |
| 298 | + .add(options.inputZip.toString()) |
| 299 | + .add("--output") |
| 300 | + .add(options.outputZip.toString()) |
| 301 | + .add(compressionStrategy) |
| 302 | + .add("--exclude_build_data") |
| 303 | + .add("--normalize"); |
| 304 | + |
| 305 | + final Stopwatch timer = Stopwatch.createStarted(); |
| 306 | + GenerateExcludeListResult excludeListResult = generateExcludeList(options, timer); |
| 307 | + int sawErrors = excludeListResult.sawErrors(); |
| 308 | + ArrayList<String> excludeList = excludeListResult.excludeList(); |
| 309 | + |
| 310 | + if (!excludeList.isEmpty()) { |
| 311 | + singleJarArgsBuilder.add("--exclude_zip_entries").addAll(excludeList); |
| 312 | + } |
| 313 | + |
| 314 | + ImmutableList<String> singleJarArgs = singleJarArgsBuilder.build(); |
| 315 | + // Dump the singlejar args into a params file |
| 316 | + File paramsFile = File.createTempFile("singlejar_params", ".txt"); |
| 317 | + try (BufferedWriter writer = Files.newBufferedWriter(paramsFile.toPath(), UTF_8)) { |
| 318 | + for (String arg : singleJarArgs) { |
| 319 | + writer.write(arg + "\n"); |
| 320 | + } |
| 321 | + } |
236 | 322 |
|
237 | | - try (OutputStream out = Files.newOutputStream(options.outputZip); |
238 | | - ZipCombiner combiner = new ZipCombiner(options.outputMode, entryFilter, out)) { |
239 | | - combiner.addZip(options.inputZip.toFile()); |
| 323 | + boolean singleJarError = false; |
| 324 | + Process singleJarProcess = |
| 325 | + Runtime.getRuntime().exec(new String[] {singleJarPath, "@" + paramsFile.getAbsolutePath()}); |
| 326 | + try { |
| 327 | + int singlejarExitCode = singleJarProcess.waitFor(); |
| 328 | + if (singlejarExitCode != 0) { |
| 329 | + sawErrors = 1; |
| 330 | + singleJarError = true; |
| 331 | + logger.severe( |
| 332 | + String.format( |
| 333 | + "ERROR: singlejar failed with exit code %d. See logs for details.", |
| 334 | + singlejarExitCode)); |
| 335 | + } |
| 336 | + } catch (InterruptedException e) { |
| 337 | + singleJarError = true; |
| 338 | + logger.severe(String.format("ERROR: singlejar was interrupted: %s", e.getMessage())); |
| 339 | + sawErrors = 1; |
| 340 | + } |
| 341 | + // Dump out the singlejar stderr if there was an issue. |
| 342 | + if (singleJarError) { |
| 343 | + InputStreamReader isr = new InputStreamReader(singleJarProcess.getErrorStream(), UTF_8); |
| 344 | + BufferedReader br = new BufferedReader(isr); |
| 345 | + String line; |
| 346 | + while ((line = br.readLine()) != null) { |
| 347 | + System.out.println(" [singlejar stderr] " + line); |
| 348 | + } |
240 | 349 | } |
241 | 350 | logger.fine(String.format("Filtering completed in %dms", timer.elapsed(TimeUnit.MILLISECONDS))); |
242 | | - return entryFilter.sawErrors() ? 1 : 0; |
| 351 | + |
| 352 | + return sawErrors; |
243 | 353 | } |
244 | 354 | } |
0 commit comments