Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.1.0] DigestUtils: avoid throwing on invalid digest function name #20650

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
// limitations under the License.
package com.google.devtools.build.lib.remote.util;

import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.nio.charset.StandardCharsets.UTF_8;

import build.bazel.remote.execution.v2.Action;
import build.bazel.remote.execution.v2.Digest;
import build.bazel.remote.execution.v2.DigestFunction;
import com.google.common.collect.ImmutableSet;
import com.google.common.hash.HashCode;
import com.google.common.io.BaseEncoding;
import com.google.devtools.build.lib.actions.cache.VirtualActionInput;
Expand All @@ -30,6 +32,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;

/** Utility methods to work with {@link Digest}. */
public class DigestUtil {
Expand All @@ -43,12 +46,13 @@ public DigestUtil(XattrProvider xattrProvider, DigestHashFunction hashFn) {
this.digestFunction = getDigestFunctionFromHashFunction(hashFn);
}

private static final ImmutableSet<String> DIGEST_FUNCTION_NAMES =
Arrays.stream(DigestFunction.Value.values()).map(Enum::name).collect(toImmutableSet());

private static DigestFunction.Value getDigestFunctionFromHashFunction(DigestHashFunction hashFn) {
for (String name : hashFn.getNames()) {
try {
if (DIGEST_FUNCTION_NAMES.contains(name)) {
return DigestFunction.Value.valueOf(name);
} catch (IllegalArgumentException e) {
// continue.
}
}
return DigestFunction.Value.UNKNOWN;
Expand Down
Loading