aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/remote/ContentDigests.java
blob: d192598bd2b6658b01b3ad2578a2c4c088db1eb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2016 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.lib.remote;

import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import com.google.devtools.build.lib.actions.ActionInput;
import com.google.devtools.build.lib.actions.ActionInputFileCache;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
import com.google.devtools.build.lib.remote.RemoteProtocol.Action;
import com.google.devtools.build.lib.remote.RemoteProtocol.ContentDigest;
import com.google.devtools.build.lib.vfs.Path;
import com.google.protobuf.ByteString;
import com.google.protobuf.Message;
import java.io.IOException;

/** Helper methods relating to computing ContentDigest messages for remote execution. */
@ThreadSafe
public final class ContentDigests {
  private ContentDigests() {}

  public static ContentDigest computeDigest(byte[] blob) {
    return buildDigest(Hashing.sha1().hashBytes(blob).asBytes(), blob.length);
  }

  // TODO(olaola): cache these in ActionInputFileCache!
  public static ContentDigest computeDigest(Path file) throws IOException {
    return buildDigest(file.getSHA1Digest(), file.getFileSize());
  }

  /**
   * Computes a digest of the given proto message. Currently, we simply rely on message output as
   * bytes, but this implementation relies on the stability of the proto encoding, in particular
   * between different platforms and languages. TODO(olaola): upgrade to a better implementation!
   */
  public static ContentDigest computeDigest(Message message) {
    return computeDigest(message.toByteArray());
  }

  /**
   * A special type of ContentDigest that is used only as a remote action cache key. This is a
   * separate type in order to prevent accidentally using other ContentDigests as action keys.
   */
  public static final class ActionKey {
    private final ContentDigest digest;

    public ContentDigest getDigest() {
      return digest;
    }

    private ActionKey(ContentDigest digest) {
      this.digest = digest;
    }
  }

  public static ActionKey computeActionKey(Action action) {
    return new ActionKey(computeDigest(action));
  }

  /**
   * Assumes that the given ContentDigest is a valid digest of an Action, and creates an ActionKey
   * wrapper. This should not be called on the client side!
   */
  public static ActionKey unsafeActionKeyFromDigest(ContentDigest digest) {
    return new ActionKey(digest);
  }

  public static ContentDigest buildDigest(byte[] digest, long size) {
    ContentDigest.Builder b = ContentDigest.newBuilder();
    b.setDigest(ByteString.copyFrom(digest)).setSizeBytes(size);
    return b.build();
  }

  public static ContentDigest getDigestFromInputCache(ActionInput input, ActionInputFileCache cache)
      throws IOException {
    return buildDigest(cache.getDigest(input), cache.getSizeInBytes(input));
  }

  public static String toHexString(ContentDigest digest) {
    return HashCode.fromBytes(digest.getDigest().toByteArray()).toString();
  }

  public static String toString(ContentDigest digest) {
    return "<digest: " + toHexString(digest) + ", size: " + digest.getSizeBytes() + " bytes>";
  }
}