aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/actions/cache/Metadata.java
blob: 36c52b9942c3f74a0a081e6ac2a44df81ea20040 (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
// Copyright 2014 Google Inc. 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.actions.cache;

import com.google.common.base.Preconditions;
import com.google.common.io.BaseEncoding;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;

import java.util.Arrays;
import java.util.Date;

/**
 * A class to represent file metadata.
 * ActionCacheChecker may assume that, for a given file, equal
 * metadata at different moments implies equal file-contents,
 * where metadata equality is computed using Metadata.equals().
 * <p>
 * NB! Several other parts of Blaze are relying on the fact that metadata
 * uses mtime and not ctime. If metadata is ever changed
 * to use ctime, all uses of Metadata must be carefully examined.
 */
@Immutable @ThreadSafe
public final class Metadata {
  public final long mtime;
  public final byte[] digest;

  // Convenience object for use with volatile files that we do not want checked
  // (e.g. the build-changelist.txt)
  public static final Metadata CONSTANT_METADATA = new Metadata(-1);

  public Metadata(long mtime) {
    this.mtime = mtime;
    this.digest = null;
  }

  public Metadata(byte[] digest) {
    this.mtime = 0L;
    this.digest = Preconditions.checkNotNull(digest);
  }

  @Override
  public int hashCode() {
    int hash = 0;
    if (digest != null) {
      // We are already dealing with the digest so we can just use portion of it
      // as a hash code.
      hash += digest[0] + (digest[1] << 8) + (digest[2] << 16) + (digest[3] << 24);
    } else {
      // Inlined hashCode for Long, so we don't
      // have to construct an Object, just to compute
      // a 32-bit hash out of a 64 bit value.
      hash = (int) (mtime ^ (mtime >>> 32));
    }
    return hash;
  }

  @Override
  public boolean equals(Object that) {
    if (this == that) {
      return true;
    }
    if (!(that instanceof Metadata)) {
      return false;
    }
    // Do a strict comparison - both digest and mtime should match
    return Arrays.equals(this.digest, ((Metadata) that).digest)
        && this.mtime == ((Metadata) that).mtime;
  }

  @Override
  public String toString() {
    if (digest != null) {
      return "MD5 " + BaseEncoding.base16().lowerCase().encode(digest);
    } else if (mtime > 0) {
      return "timestamp " + new Date(mtime);
    }
    return "no metadata";
  }
}