aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/ZipFilterEntryFilter.java
diff options
context:
space:
mode:
authorGravatar ajmichael <ajmichael@google.com>2017-10-31 16:05:33 -0400
committerGravatar John Cater <jcater@google.com>2017-11-01 09:58:53 -0400
commit2ad8c6978f786795b501dd4e6fa6b94cd910a485 (patch)
tree7f90fcca94a123cfd9e9bf864f00f7c56a581b02 /src/tools/android/java/com/google/devtools/build/android/ZipFilterEntryFilter.java
parentf411e2ef94e7268286a810741fff2ba324fe72c3 (diff)
Open source ZipFilterAction for use in Android testing.
https://github.com/bazelbuild/bazel/issues/903 RELNOTES: None PiperOrigin-RevId: 174079202
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/ZipFilterEntryFilter.java')
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/ZipFilterEntryFilter.java71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/ZipFilterEntryFilter.java b/src/tools/android/java/com/google/devtools/build/android/ZipFilterEntryFilter.java
new file mode 100644
index 0000000000..f6e556b626
--- /dev/null
+++ b/src/tools/android/java/com/google/devtools/build/android/ZipFilterEntryFilter.java
@@ -0,0 +1,71 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.google.devtools.build.android;
+
+import com.google.common.collect.Multimap;
+import com.google.devtools.build.singlejar.ZipEntryFilter;
+
+import java.io.IOException;
+import java.util.Map;
+
+/**
+ * A {@link ZipEntryFilter} for use with {@link ZipFilterAction}. Filters out entries that match the
+ * provided regular expression or are in the list to omit.
+ */
+class ZipFilterEntryFilter implements ZipEntryFilter {
+
+ private final String explicitFilter;
+ private final Multimap<String, Long> entriesToOmit;
+ private final Map<String, Long> inputEntries;
+ private final boolean errorOnHashMismatch;
+
+ /**
+ * Creates a new filter.
+ *
+ * @param explicitFilter a regular expression to match against entry filenames
+ * @param entriesToOmit a map of filename and CRC-32 of entries to omit
+ * @param inputEntries a map of filename and CRC-32 of entries in the input Zip file
+ * @param errorOnHashMistmatch whether to error or warn when there is a CRC-32 mismatch
+ */
+ public ZipFilterEntryFilter(String explicitFilter, Multimap<String, Long> entriesToOmit,
+ Map<String, Long> inputEntries, boolean errorOnHashMistmatch) {
+ this.explicitFilter = explicitFilter;
+ this.entriesToOmit = entriesToOmit;
+ this.inputEntries = inputEntries;
+ this.errorOnHashMismatch = errorOnHashMistmatch;
+ }
+
+ @Override
+ public void accept(String filename, StrategyCallback callback) throws IOException {
+ if (filename.matches(explicitFilter)) {
+ callback.skip();
+ } else if (entriesToOmit.containsKey(filename)) {
+ Long entryCrc = inputEntries.get(filename);
+ if (entriesToOmit.containsEntry(filename, entryCrc)) {
+ callback.skip();
+ } else {
+ if (errorOnHashMismatch) {
+ throw new IllegalStateException(String.format("Requested to filter entries of name "
+ + "'%s'; name matches but the hash does not. Aborting", filename));
+ } else {
+ System.out.printf("\u001b[35mWARNING:\u001b[0m Requested to filter entries of name "
+ + "'%s'; name matches but the hash does not. Copying anyway.\n", filename);
+ callback.copy(null);
+ }
+ }
+ } else {
+ callback.copy(null);
+ }
+ }
+}