Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
| 16 | import com.google.common.annotations.VisibleForTesting; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableList; |
| 18 | import com.google.common.collect.ImmutableSet; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Michajlo Matijkiw | a09f766 | 2015-06-12 17:41:21 +0000 | [diff] [blame] | 21 | import com.google.common.io.BaseEncoding; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import com.google.devtools.build.lib.actions.ActionInput; |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.ActionInputHelper; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.Artifact; |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact; |
Shreya Bhattarai | 141b6c2 | 2016-08-22 22:00:24 +0000 | [diff] [blame^] | 26 | import com.google.devtools.build.lib.actions.cache.Md5Digest; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import com.google.devtools.build.lib.actions.cache.Metadata; |
| 28 | import com.google.devtools.build.lib.actions.cache.MetadataHandler; |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.skyframe.TreeArtifactValue.TreeArtifactException; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.util.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
| 32 | import com.google.devtools.build.lib.vfs.FileStatus; |
| 33 | import com.google.devtools.build.lib.vfs.FileStatusWithDigest; |
| 34 | import com.google.devtools.build.lib.vfs.FileStatusWithDigestAdapter; |
| 35 | import com.google.devtools.build.lib.vfs.Path; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.vfs.PathFragment; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | import com.google.devtools.build.lib.vfs.RootedPath; |
| 38 | import com.google.devtools.build.lib.vfs.Symlinks; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | import java.io.FileNotFoundException; |
| 40 | import java.io.IOException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | import java.util.Arrays; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 42 | import java.util.Collection; |
| 43 | import java.util.Collections; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | import java.util.Map; |
| 45 | import java.util.Set; |
| 46 | import java.util.concurrent.ConcurrentHashMap; |
| 47 | import java.util.concurrent.ConcurrentMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 48 | import javax.annotation.Nullable; |
| 49 | |
| 50 | /** |
| 51 | * Cache provided by an {@link ActionExecutionFunction}, allowing Blaze to obtain data from the |
| 52 | * graph and to inject data (e.g. file digests) back into the graph. |
| 53 | * |
| 54 | * <p>Data for the action's inputs is injected into this cache on construction, using the graph as |
| 55 | * the source of truth. |
| 56 | * |
| 57 | * <p>As well, this cache collects data about the action's output files, which is used in three |
| 58 | * ways. First, it is served as requested during action execution, primarily by the {@code |
| 59 | * ActionCacheChecker} when determining if the action must be rerun, and then after the action is |
Janak Ramakrishnan | ad77f97 | 2016-07-29 20:58:42 +0000 | [diff] [blame] | 60 | * run, to gather information about the outputs. Second, it is accessed by {@link ArtifactFunction}s |
| 61 | * in order to construct {@link FileArtifactValue}s, and by this class itself to generate {@link |
| 62 | * TreeArtifactValue}s. Third, the {@link FilesystemValueChecker} uses it to determine the set of |
| 63 | * output files to check for inter-build modifications. Because all these use cases are slightly |
| 64 | * different, we must occasionally store two versions of the data for a value. See {@link |
| 65 | * #getAdditionalOutputData} for elaboration on the difference between these cases, and see the |
| 66 | * javadoc for the various internal maps to see what is stored where. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 67 | */ |
| 68 | @VisibleForTesting |
Janak Ramakrishnan | a5c1f96 | 2015-04-03 23:06:31 +0000 | [diff] [blame] | 69 | public class ActionMetadataHandler implements MetadataHandler { |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Data for input artifacts. Immutable. |
| 73 | * |
| 74 | * <p>This should never be read directly. Use {@link #getInputFileArtifactValue} instead.</p> |
| 75 | */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | private final Map<Artifact, FileArtifactValue> inputArtifactData; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 77 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 78 | /** FileValues for each output Artifact. */ |
| 79 | private final ConcurrentMap<Artifact, FileValue> outputArtifactData = |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 80 | new ConcurrentHashMap<>(); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Maps output TreeArtifacts to their contents. These maps are either injected or read |
| 84 | * directly from the filesystem. |
| 85 | * If the value is null, this means nothing was injected, and the output TreeArtifact |
| 86 | * is to have its values read from disk instead. |
| 87 | */ |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 88 | private final ConcurrentMap<Artifact, Set<TreeFileArtifact>> outputDirectoryListings = |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 89 | new ConcurrentHashMap<>(); |
| 90 | |
| 91 | /** Outputs that are to be omitted. */ |
Michajlo Matijkiw | 13459b4 | 2015-03-12 19:43:20 +0000 | [diff] [blame] | 92 | private final Set<Artifact> omittedOutputs = Sets.newConcurrentHashSet(); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Contains RealArtifactValues when those values must be stored separately. |
| 96 | * See {@link #getAdditionalOutputData()} for details. |
| 97 | */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 98 | private final ConcurrentMap<Artifact, FileArtifactValue> additionalOutputData = |
| 99 | new ConcurrentHashMap<>(); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 100 | |
| 101 | /** |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 102 | * Data for TreeArtifactValues, constructed from outputArtifactData and |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 103 | * additionalOutputFileData. |
| 104 | */ |
| 105 | private final ConcurrentMap<Artifact, TreeArtifactValue> outputTreeArtifactData = |
| 106 | new ConcurrentHashMap<>(); |
| 107 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 108 | /** Tracks which Artifacts have had metadata injected. */ |
| 109 | private final Set<Artifact> injectedFiles = Sets.newConcurrentHashSet(); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 110 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 111 | private final ImmutableSet<Artifact> outputs; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 112 | private final TimestampGranularityMonitor tsgm; |
| 113 | |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 114 | @VisibleForTesting |
Janak Ramakrishnan | a5c1f96 | 2015-04-03 23:06:31 +0000 | [diff] [blame] | 115 | public ActionMetadataHandler(Map<Artifact, FileArtifactValue> inputArtifactData, |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 116 | Iterable<Artifact> outputs, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 117 | TimestampGranularityMonitor tsgm) { |
| 118 | this.inputArtifactData = Preconditions.checkNotNull(inputArtifactData); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 119 | this.outputs = ImmutableSet.copyOf(outputs); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 120 | this.tsgm = tsgm; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public Metadata getMetadataMaybe(Artifact artifact) { |
| 125 | try { |
| 126 | return getMetadata(artifact); |
| 127 | } catch (IOException e) { |
| 128 | return null; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | private static Metadata metadataFromValue(FileArtifactValue value) throws FileNotFoundException { |
Michajlo Matijkiw | 597d55e | 2015-03-25 17:17:52 +0000 | [diff] [blame] | 133 | if (value == FileArtifactValue.MISSING_FILE_MARKER |
| 134 | || value == FileArtifactValue.OMITTED_FILE_MARKER) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 135 | throw new FileNotFoundException(); |
| 136 | } |
Janak Ramakrishnan | 406c087 | 2016-07-28 21:32:01 +0000 | [diff] [blame] | 137 | // If the file is a directory, we need to return the mtime because the action cache uses mtime |
| 138 | // to determine if this artifact has changed. We want this code path to go away somehow |
| 139 | // for directories (maybe by implementing FileSet in Skyframe). |
| 140 | return value.isFile() ? new Metadata(value.getDigest()) : new Metadata(value.getModifiedTime()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public Metadata getMetadata(Artifact artifact) throws IOException { |
| 145 | Metadata metadata = getRealMetadata(artifact); |
| 146 | return artifact.isConstantMetadata() ? Metadata.CONSTANT_METADATA : metadata; |
| 147 | } |
| 148 | |
| 149 | @Nullable |
| 150 | private FileArtifactValue getInputFileArtifactValue(ActionInput input) { |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 151 | if (outputs.contains(input) || !(input instanceof Artifact)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 152 | return null; |
| 153 | } |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 154 | return Preconditions.checkNotNull(inputArtifactData.get(input), input); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 158 | * Get the real (viz. on-disk) metadata for an Artifact. |
| 159 | * A key assumption is that getRealMetadata() will be called for every Artifact in this |
| 160 | * ActionMetadataHandler, to populate additionalOutputData and outputTreeArtifactData. |
| 161 | * |
| 162 | * <p>We cache data for constant-metadata artifacts, even though it is technically unnecessary, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 163 | * because the data stored in this cache is consumed by various parts of Blaze via the {@link |
| 164 | * ActionExecutionValue} (for now, {@link FilesystemValueChecker} and {@link ArtifactFunction}). |
| 165 | * It is simpler for those parts if every output of the action is present in the cache. However, |
| 166 | * we must not return the actual metadata for a constant-metadata artifact. |
| 167 | */ |
| 168 | private Metadata getRealMetadata(Artifact artifact) throws IOException { |
| 169 | FileArtifactValue value = getInputFileArtifactValue(artifact); |
| 170 | if (value != null) { |
| 171 | return metadataFromValue(value); |
| 172 | } |
| 173 | if (artifact.isSourceArtifact()) { |
| 174 | // A discovered input we didn't have data for. |
| 175 | // TODO(bazel-team): Change this to an assertion once Skyframe has native input discovery, so |
| 176 | // all inputs will already have metadata known. |
| 177 | return null; |
| 178 | } else if (artifact.isMiddlemanArtifact()) { |
| 179 | // A middleman artifact's data was either already injected from the action cache checker using |
| 180 | // #setDigestForVirtualArtifact, or it has the default middleman value. |
| 181 | value = additionalOutputData.get(artifact); |
| 182 | if (value != null) { |
| 183 | return metadataFromValue(value); |
| 184 | } |
| 185 | value = FileArtifactValue.DEFAULT_MIDDLEMAN; |
| 186 | FileArtifactValue oldValue = additionalOutputData.putIfAbsent(artifact, value); |
| 187 | checkInconsistentData(artifact, oldValue, value); |
| 188 | return metadataFromValue(value); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 189 | } else if (artifact.isTreeArtifact()) { |
| 190 | TreeArtifactValue setValue = getTreeArtifactValue(artifact); |
| 191 | if (setValue != null && setValue != TreeArtifactValue.MISSING_TREE_ARTIFACT) { |
| 192 | return setValue.getMetadata(); |
| 193 | } |
| 194 | // We use FileNotFoundExceptions to determine if an Artifact was or wasn't found. |
| 195 | // Calling code depends on this particular exception. |
| 196 | throw new FileNotFoundException(artifact + " not found"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 197 | } |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 198 | // It's an ordinary artifact. |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 199 | FileValue fileValue = outputArtifactData.get(artifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 200 | if (fileValue != null) { |
| 201 | // Non-middleman artifacts should only have additionalOutputData if they have |
| 202 | // outputArtifactData. We don't assert this because of concurrency possibilities, but at least |
| 203 | // we don't check additionalOutputData unless we expect that we might see the artifact there. |
| 204 | value = additionalOutputData.get(artifact); |
| 205 | // If additional output data is present for this artifact, we use it in preference to the |
| 206 | // usual calculation. |
| 207 | if (value != null) { |
| 208 | return metadataFromValue(value); |
| 209 | } |
| 210 | if (!fileValue.exists()) { |
| 211 | throw new FileNotFoundException(artifact.prettyPrint() + " does not exist"); |
| 212 | } |
| 213 | return new Metadata(Preconditions.checkNotNull(fileValue.getDigest(), artifact)); |
| 214 | } |
| 215 | // We do not cache exceptions besides nonexistence here, because it is unlikely that the file |
| 216 | // will be requested from this cache too many times. |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 217 | fileValue = constructFileValue(artifact, null); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 218 | return maybeStoreAdditionalData(artifact, fileValue, null); |
| 219 | } |
| 220 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 221 | /** |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 222 | * Check that the new {@code data} we just calculated for an {@link Artifact} agrees with the |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 223 | * {@code oldData} (presumably calculated concurrently), if it was present. |
| 224 | */ |
| 225 | // Not private only because used by SkyframeActionExecutor's metadata handler. |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 226 | static void checkInconsistentData(Artifact artifact, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 227 | @Nullable Object oldData, Object data) throws IOException { |
| 228 | if (oldData != null && !oldData.equals(data)) { |
| 229 | // Another thread checked this file since we looked at the map, and got a different answer |
| 230 | // than we did. Presumably the user modified the file between reads. |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 231 | throw new IOException("Data for " + artifact.prettyPrint() + " changed to " + data |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 232 | + " after it was calculated as " + oldData); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * See {@link #getAdditionalOutputData} for why we sometimes need to store additional data, even |
| 238 | * for normal (non-middleman) artifacts. |
| 239 | */ |
| 240 | @Nullable |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 241 | private Metadata maybeStoreAdditionalData(Artifact artifact, FileValue data, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 242 | @Nullable byte[] injectedDigest) throws IOException { |
| 243 | if (!data.exists()) { |
| 244 | // Nonexistent files should only occur before executing an action. |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 245 | throw new FileNotFoundException(artifact.prettyPrint() + " does not exist"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 246 | } |
Janak Ramakrishnan | cba1645 | 2016-07-29 02:17:02 +0000 | [diff] [blame] | 247 | boolean isFile = data.isFile(); |
| 248 | if (isFile && !artifact.hasParent() && data.getDigest() != null) { |
| 249 | // We do not need to store the FileArtifactValue separately -- the digest is in the file value |
| 250 | // and that is all that is needed for this file's metadata. |
| 251 | return new Metadata(data.getDigest()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 252 | } |
Janak Ramakrishnan | cba1645 | 2016-07-29 02:17:02 +0000 | [diff] [blame] | 253 | // Unfortunately, the FileValue does not contain enough information for us to calculate the |
| 254 | // corresponding FileArtifactValue -- either the metadata must use the modified time, which we |
| 255 | // do not expose in the FileValue, or the FileValue didn't store the digest So we store the |
| 256 | // metadata separately. |
| 257 | // Use the FileValue's digest if no digest was injected, or if the file can't be digested. |
| 258 | injectedDigest = injectedDigest != null || !isFile ? injectedDigest : data.getDigest(); |
| 259 | FileArtifactValue value = |
| 260 | FileArtifactValue.create(artifact, isFile, isFile ? data.getSize() : 0, injectedDigest); |
| 261 | FileArtifactValue oldValue = additionalOutputData.putIfAbsent(artifact, value); |
| 262 | checkInconsistentData(artifact, oldValue, value); |
| 263 | return metadataFromValue(value); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
Shreya Bhattarai | 141b6c2 | 2016-08-22 22:00:24 +0000 | [diff] [blame^] | 267 | public void setDigestForVirtualArtifact(Artifact artifact, Md5Digest md5Digest) { |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 268 | Preconditions.checkArgument(artifact.isMiddlemanArtifact(), artifact); |
Shreya Bhattarai | 141b6c2 | 2016-08-22 22:00:24 +0000 | [diff] [blame^] | 269 | Preconditions.checkNotNull(md5Digest, artifact); |
| 270 | additionalOutputData.put( |
| 271 | artifact, FileArtifactValue.createProxy(md5Digest.getDigestBytesUnsafe())); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 274 | private Set<TreeFileArtifact> getTreeArtifactContents(Artifact artifact) { |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 275 | Preconditions.checkArgument(artifact.isTreeArtifact(), artifact); |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 276 | Set<TreeFileArtifact> contents = outputDirectoryListings.get(artifact); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 277 | if (contents == null) { |
| 278 | // Unfortunately, there is no such thing as a ConcurrentHashSet. |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 279 | contents = Collections.newSetFromMap(new ConcurrentHashMap<TreeFileArtifact, Boolean>()); |
| 280 | Set<TreeFileArtifact> oldContents = outputDirectoryListings.putIfAbsent(artifact, contents); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 281 | // Avoid a race condition. |
| 282 | if (oldContents != null) { |
| 283 | contents = oldContents; |
| 284 | } |
| 285 | } |
| 286 | return contents; |
| 287 | } |
| 288 | |
| 289 | private TreeArtifactValue getTreeArtifactValue(Artifact artifact) throws IOException { |
| 290 | TreeArtifactValue value = outputTreeArtifactData.get(artifact); |
| 291 | if (value != null) { |
| 292 | return value; |
| 293 | } |
| 294 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 295 | Set<TreeFileArtifact> registeredContents = outputDirectoryListings.get(artifact); |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 296 | if (registeredContents != null) { |
| 297 | // Check that our registered outputs matches on-disk outputs. Only perform this check |
| 298 | // when contents were explicitly registered. |
| 299 | // TODO(bazel-team): Provide a way for actions to register empty TreeArtifacts. |
| 300 | |
| 301 | // By the time we're constructing TreeArtifactValues, use of the metadata handler |
| 302 | // should be single threaded and there should be no race condition. |
| 303 | // The current design of ActionMetadataHandler makes this hard to enforce. |
| 304 | Set<PathFragment> paths = null; |
| 305 | try { |
| 306 | paths = TreeArtifactValue.explodeDirectory(artifact); |
| 307 | } catch (TreeArtifactException e) { |
| 308 | throw new IllegalStateException(e); |
| 309 | } |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 310 | Set<TreeFileArtifact> diskFiles = ActionInputHelper.asTreeFileArtifacts(artifact, paths); |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 311 | if (!diskFiles.equals(registeredContents)) { |
| 312 | // There might be more than one error here. We first look for missing output files. |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 313 | Set<TreeFileArtifact> missingFiles = Sets.difference(registeredContents, diskFiles); |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 314 | if (!missingFiles.isEmpty()) { |
| 315 | // Don't throw IOException--getMetadataMaybe() eats them. |
| 316 | // TODO(bazel-team): Report this error in a better way when called by checkOutputs() |
| 317 | // Currently it's hard to report this error without refactoring, since checkOutputs() |
| 318 | // likes to substitute its own error messages upon catching IOException, and falls |
| 319 | // through to unrecoverable error behavior on any other exception. |
| 320 | throw new IllegalStateException("Output file " + missingFiles.iterator().next() |
| 321 | + " was registered, but not present on disk"); |
| 322 | } |
| 323 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 324 | Set<TreeFileArtifact> extraFiles = Sets.difference(diskFiles, registeredContents); |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 325 | // extraFiles cannot be empty |
| 326 | throw new IllegalStateException( |
| 327 | "File " + extraFiles.iterator().next().getParentRelativePath() |
| 328 | + ", present in TreeArtifact " + artifact + ", was not registered"); |
| 329 | } |
| 330 | |
| 331 | value = constructTreeArtifactValue(registeredContents); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 332 | } else { |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 333 | value = constructTreeArtifactValueFromFilesystem(artifact); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | TreeArtifactValue oldValue = outputTreeArtifactData.putIfAbsent(artifact, value); |
| 337 | checkInconsistentData(artifact, oldValue, value); |
| 338 | return value; |
| 339 | } |
| 340 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 341 | private TreeArtifactValue constructTreeArtifactValue(Collection<TreeFileArtifact> contents) |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 342 | throws IOException { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 343 | Map<TreeFileArtifact, FileArtifactValue> values = |
| 344 | Maps.newHashMapWithExpectedSize(contents.size()); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 345 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 346 | for (TreeFileArtifact treeFileArtifact : contents) { |
Rumou Duan | 7387620 | 2016-06-06 18:52:08 +0000 | [diff] [blame] | 347 | FileArtifactValue cachedValue = additionalOutputData.get(treeFileArtifact); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 348 | if (cachedValue == null) { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 349 | FileValue fileValue = outputArtifactData.get(treeFileArtifact); |
| 350 | // This is similar to what's present in getRealMetadataForArtifact, except |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 351 | // we get back the FileValue, not the metadata. |
| 352 | // We do not cache exceptions besides nonexistence here, because it is unlikely that the |
| 353 | // file will be requested from this cache too many times. |
| 354 | if (fileValue == null) { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 355 | fileValue = constructFileValue(treeFileArtifact, /*statNoFollow=*/ null); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 356 | // A minor hack: maybeStoreAdditionalData will force the data to be stored |
Rumou Duan | 7387620 | 2016-06-06 18:52:08 +0000 | [diff] [blame] | 357 | // in additionalOutputData. |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 358 | maybeStoreAdditionalData(treeFileArtifact, fileValue, null); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 359 | } |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 360 | cachedValue = Preconditions.checkNotNull( |
Rumou Duan | 7387620 | 2016-06-06 18:52:08 +0000 | [diff] [blame] | 361 | additionalOutputData.get(treeFileArtifact), treeFileArtifact); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 364 | values.put(treeFileArtifact, cachedValue); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | return TreeArtifactValue.create(values); |
| 368 | } |
| 369 | |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 370 | private TreeArtifactValue constructTreeArtifactValueFromFilesystem(Artifact artifact) |
| 371 | throws IOException { |
| 372 | Preconditions.checkState(artifact.isTreeArtifact(), artifact); |
| 373 | |
| 374 | if (!artifact.getPath().isDirectory() || artifact.getPath().isSymbolicLink()) { |
| 375 | return TreeArtifactValue.MISSING_TREE_ARTIFACT; |
| 376 | } |
| 377 | |
| 378 | Set<PathFragment> paths = null; |
| 379 | try { |
| 380 | paths = TreeArtifactValue.explodeDirectory(artifact); |
| 381 | } catch (TreeArtifactException e) { |
| 382 | throw new IllegalStateException(e); |
| 383 | } |
| 384 | // If you're reading tree artifacts from disk while outputDirectoryListings are being injected, |
| 385 | // something has gone terribly wrong. |
| 386 | Object previousDirectoryListing = |
| 387 | outputDirectoryListings.put(artifact, |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 388 | Collections.newSetFromMap(new ConcurrentHashMap<TreeFileArtifact, Boolean>())); |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 389 | Preconditions.checkState(previousDirectoryListing == null, |
| 390 | "Race condition while constructing TreArtifactValue: %s, %s", |
| 391 | artifact, previousDirectoryListing); |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 392 | return constructTreeArtifactValue(ActionInputHelper.asTreeFileArtifacts(artifact, paths)); |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 395 | @Override |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 396 | public void addExpandedTreeOutput(TreeFileArtifact output) { |
| 397 | Set<TreeFileArtifact> values = getTreeArtifactContents(output.getParent()); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 398 | values.add(output); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | @Override |
| 402 | public void injectDigest(ActionInput output, FileStatus statNoFollow, byte[] digest) { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 403 | // Assumption: any non-Artifact output is 'virtual' and should be ignored here. |
| 404 | if (output instanceof Artifact) { |
| 405 | final Artifact artifact = (Artifact) output; |
| 406 | Preconditions.checkState(injectedFiles.add(artifact), artifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 407 | FileValue fileValue; |
| 408 | try { |
| 409 | // This call may do an unnecessary call to Path#getFastDigest to see if the digest is |
| 410 | // readily available. We cannot pass the digest in, though, because if it is not available |
| 411 | // from the filesystem, this FileValue will not compare equal to another one created for the |
| 412 | // same file, because the other one will be missing its digest. |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 413 | fileValue = fileValueFromArtifact(artifact, |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 414 | FileStatusWithDigestAdapter.adapt(statNoFollow), tsgm); |
Michajlo Matijkiw | a09f766 | 2015-06-12 17:41:21 +0000 | [diff] [blame] | 415 | // Ensure the digest supplied matches the actual digest if it exists. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 416 | byte[] fileDigest = fileValue.getDigest(); |
Michajlo Matijkiw | a09f766 | 2015-06-12 17:41:21 +0000 | [diff] [blame] | 417 | if (fileDigest != null && !Arrays.equals(digest, fileDigest)) { |
| 418 | BaseEncoding base16 = BaseEncoding.base16(); |
| 419 | String digestString = (digest != null) ? base16.encode(digest) : "null"; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 420 | String fileDigestString = base16.encode(fileDigest); |
Michajlo Matijkiw | a09f766 | 2015-06-12 17:41:21 +0000 | [diff] [blame] | 421 | throw new IllegalStateException("Expected digest " + digestString + " for artifact " |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 422 | + artifact + ", but got " + fileDigestString + " (" + fileValue + ")"); |
Michajlo Matijkiw | a09f766 | 2015-06-12 17:41:21 +0000 | [diff] [blame] | 423 | } |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 424 | outputArtifactData.put(artifact, fileValue); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 425 | } catch (IOException e) { |
| 426 | // Do nothing - we just failed to inject metadata. Real error handling will be done later, |
| 427 | // when somebody will try to access that file. |
| 428 | return; |
| 429 | } |
| 430 | // If needed, insert additional data. Note that this can only be true if the file is empty or |
| 431 | // the filesystem does not support fast digests. Since we usually only inject digests when |
| 432 | // running with a filesystem that supports fast digests, this is fairly unlikely. |
| 433 | try { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 434 | maybeStoreAdditionalData(artifact, fileValue, digest); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 435 | } catch (IOException e) { |
| 436 | if (fileValue.getSize() != 0) { |
| 437 | // Empty files currently have their mtimes examined, and so could throw. No other files |
| 438 | // should throw, since all filesystem access has already been done. |
| 439 | throw new IllegalStateException( |
| 440 | "Filesystem should not have been accessed while injecting data for " |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 441 | + artifact.prettyPrint(), e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 442 | } |
| 443 | // Ignore exceptions for empty files, as above. |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | @Override |
Michajlo Matijkiw | 13459b4 | 2015-03-12 19:43:20 +0000 | [diff] [blame] | 449 | public void markOmitted(ActionInput output) { |
| 450 | if (output instanceof Artifact) { |
| 451 | Artifact artifact = (Artifact) output; |
| 452 | Preconditions.checkState(omittedOutputs.add(artifact), artifact); |
| 453 | additionalOutputData.put(artifact, FileArtifactValue.OMITTED_FILE_MARKER); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | @Override |
| 458 | public boolean artifactOmitted(Artifact artifact) { |
| 459 | return omittedOutputs.contains(artifact); |
| 460 | } |
| 461 | |
| 462 | @Override |
Janak Ramakrishnan | 73055be | 2015-04-13 18:32:49 +0000 | [diff] [blame] | 463 | public void discardOutputMetadata() { |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 464 | Preconditions.checkState(injectedFiles.isEmpty(), |
| 465 | "Files cannot be injected before action execution: %s", injectedFiles); |
Janak Ramakrishnan | 73055be | 2015-04-13 18:32:49 +0000 | [diff] [blame] | 466 | Preconditions.checkState(omittedOutputs.isEmpty(), |
| 467 | "Artifacts cannot be marked omitted before action execution: %s", omittedOutputs); |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 468 | outputArtifactData.clear(); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 469 | outputDirectoryListings.clear(); |
| 470 | outputTreeArtifactData.clear(); |
Janak Ramakrishnan | 73055be | 2015-04-13 18:32:49 +0000 | [diff] [blame] | 471 | additionalOutputData.clear(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | @Override |
| 475 | public boolean artifactExists(Artifact artifact) { |
Michajlo Matijkiw | 13459b4 | 2015-03-12 19:43:20 +0000 | [diff] [blame] | 476 | Preconditions.checkState(!artifactOmitted(artifact), artifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 477 | return getMetadataMaybe(artifact) != null; |
| 478 | } |
| 479 | |
| 480 | @Override |
| 481 | public boolean isRegularFile(Artifact artifact) { |
| 482 | // Currently this method is used only for genrule input directory checks. If we need to call |
| 483 | // this on output artifacts too, this could be more efficient. |
| 484 | FileArtifactValue value = getInputFileArtifactValue(artifact); |
Michajlo Matijkiw | dee781c | 2015-05-22 23:25:34 +0000 | [diff] [blame] | 485 | if (value != null && value.isFile()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 486 | return true; |
| 487 | } |
| 488 | return artifact.getPath().isFile(); |
| 489 | } |
| 490 | |
| 491 | @Override |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 492 | public boolean isInjected(Artifact file) { |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 493 | return injectedFiles.contains(file); |
| 494 | } |
| 495 | |
| 496 | /** @return data for output files that was computed during execution. */ |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 497 | Map<Artifact, FileValue> getOutputArtifactData() { |
| 498 | return outputArtifactData; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /** |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 502 | * @return data for TreeArtifacts that was computed during execution. May contain copies of |
| 503 | * {@link TreeArtifactValue#MISSING_TREE_ARTIFACT}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 504 | */ |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 505 | Map<Artifact, TreeArtifactValue> getOutputTreeArtifactData() { |
| 506 | return outputTreeArtifactData; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Returns data for any output files whose metadata was not computable from the corresponding |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 511 | * entry in {@link #getOutputArtifactData}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 512 | * |
| 513 | * <p>There are three reasons why we might not be able to compute metadata for an artifact from |
| 514 | * the FileValue. First, middleman artifacts have no corresponding FileValues. Second, if |
| 515 | * computing a file's digest is not fast, the FileValue does not do so, so a file on a filesystem |
| 516 | * without fast digests has to have its metadata stored separately. Third, some files' metadata |
| 517 | * (directories, empty files) contain their mtimes, which the FileValue does not expose, so that |
| 518 | * has to be stored separately. |
| 519 | * |
| 520 | * <p>Note that for files that need digests, we can't easily inject the digest in the FileValue |
| 521 | * because it would complicate equality-checking on subsequent builds -- if our filesystem doesn't |
| 522 | * do fast digests, the comparison value would not have a digest. |
| 523 | */ |
| 524 | Map<Artifact, FileArtifactValue> getAdditionalOutputData() { |
| 525 | return additionalOutputData; |
| 526 | } |
| 527 | |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 528 | /** Constructs a new FileValue, saves it, and checks inconsistent data. */ |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 529 | FileValue constructFileValue(Artifact artifact, @Nullable FileStatusWithDigest statNoFollow) |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 530 | throws IOException { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 531 | FileValue value = fileValueFromArtifact(artifact, statNoFollow, tsgm); |
| 532 | FileValue oldFsValue = outputArtifactData.putIfAbsent(artifact, value); |
| 533 | checkInconsistentData(artifact, oldFsValue, null); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 534 | return value; |
| 535 | } |
| 536 | |
| 537 | @VisibleForTesting |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 538 | static FileValue fileValueFromArtifact(Artifact artifact, |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 539 | @Nullable FileStatusWithDigest statNoFollow, @Nullable TimestampGranularityMonitor tsgm) |
Janak Ramakrishnan | a5c1f96 | 2015-04-03 23:06:31 +0000 | [diff] [blame] | 540 | throws IOException { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 541 | Path path = artifact.getPath(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 542 | RootedPath rootedPath = |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 543 | RootedPath.toRootedPath(artifact.getRoot().getPath(), artifact.getRootRelativePath()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 544 | if (statNoFollow == null) { |
| 545 | statNoFollow = FileStatusWithDigestAdapter.adapt(path.statIfFound(Symlinks.NOFOLLOW)); |
| 546 | if (statNoFollow == null) { |
| 547 | return FileValue.value(rootedPath, FileStateValue.NONEXISTENT_FILE_STATE_NODE, |
| 548 | rootedPath, FileStateValue.NONEXISTENT_FILE_STATE_NODE); |
| 549 | } |
| 550 | } |
| 551 | Path realPath = path; |
| 552 | // We use FileStatus#isSymbolicLink over Path#isSymbolicLink to avoid the unnecessary stat |
| 553 | // done by the latter. |
| 554 | if (statNoFollow.isSymbolicLink()) { |
| 555 | realPath = path.resolveSymbolicLinks(); |
| 556 | // We need to protect against symlink cycles since FileValue#value assumes it's dealing with a |
| 557 | // file that's not in a symlink cycle. |
| 558 | if (realPath.equals(path)) { |
| 559 | throw new IOException("symlink cycle"); |
| 560 | } |
| 561 | } |
| 562 | RootedPath realRootedPath = RootedPath.toRootedPathMaybeUnderRoot(realPath, |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 563 | ImmutableList.of(artifact.getRoot().getPath())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 564 | FileStateValue fileStateValue; |
| 565 | FileStateValue realFileStateValue; |
| 566 | try { |
| 567 | fileStateValue = FileStateValue.createWithStatNoFollow(rootedPath, statNoFollow, tsgm); |
| 568 | // TODO(bazel-team): consider avoiding a 'stat' here when the symlink target hasn't changed |
| 569 | // and is a source file (since changes to those are checked separately). |
| 570 | realFileStateValue = realPath.equals(path) ? fileStateValue |
| 571 | : FileStateValue.create(realRootedPath, tsgm); |
| 572 | } catch (InconsistentFilesystemException e) { |
| 573 | throw new IOException(e); |
| 574 | } |
| 575 | return FileValue.value(rootedPath, fileStateValue, realRootedPath, realFileStateValue); |
| 576 | } |
| 577 | } |