From 3bcec691f7f705a8032829f4ec7a43fa590882ae Mon Sep 17 00:00:00 2001 From: Tyler Williams Date: Tue, 25 Jul 2023 12:36:30 -0700 Subject: [PATCH] Separate new-style-hash content in DiskCache --- .../lib/remote/disk/DiskCacheClient.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheClient.java b/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheClient.java index 45965321e078e5..5c27906a447539 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheClient.java +++ b/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheClient.java @@ -15,8 +15,10 @@ import build.bazel.remote.execution.v2.ActionResult; import build.bazel.remote.execution.v2.Digest; +import build.bazel.remote.execution.v2.DigestFunction; import build.bazel.remote.execution.v2.Directory; import build.bazel.remote.execution.v2.Tree; +import com.google.common.base.Ascii; import com.google.common.collect.ImmutableSet; import com.google.common.io.ByteStreams; import com.google.common.util.concurrent.Futures; @@ -214,10 +216,25 @@ protected Path toPathNoSplit(String key) { return root.getChild(key); } + private static boolean isOldStyleDigestFunction(DigestFunction.Value digestFunction) { + // Old-style digest functions (SHA256, etc) are distinguishable by the length + // of their hash alone and do not require extra specification, but newer + // digest functions (which may have the same length hashes as the older + // functions!) must be explicitly specified in the upload resource name. + return digestFunction.getNumber() <= 7; + } + protected Path toPath(String key, boolean actionResult) { + Path rootDir = root; + if (!isOldStyleDigestFunction(digestUtil.getDigestFunction())) { + rootDir = + rootDir.getChild( + Ascii.toLowerCase(digestUtil.getDigestFunction().getValueDescriptor().getName())); + } + String cacheFolder = actionResult ? AC_DIRECTORY : CAS_DIRECTORY; // Create the file in a subfolder to bypass possible folder file count limits - return root.getChild(cacheFolder).getChild(key.substring(0, 2)).getChild(key); + return rootDir.getChild(cacheFolder).getChild(key.substring(0, 2)).getChild(key); } private void saveFile(String key, InputStream in, boolean actionResult) throws IOException {