Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 1 | // Copyright 2016 The Bazel Authors. All rights reserved. |
| 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 | |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 16 | import static com.google.common.base.Preconditions.checkArgument; |
| 17 | import static com.google.common.base.Preconditions.checkNotNull; |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkState; |
Googler | 9bd8833 | 2019-03-19 18:09:44 -0700 | [diff] [blame] | 19 | import static com.google.common.collect.ImmutableSet.toImmutableSet; |
| 20 | |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 21 | import com.google.auto.value.AutoValue; |
| 22 | import com.google.common.annotations.VisibleForTesting; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 23 | import com.google.common.base.MoreObjects; |
| 24 | import com.google.common.collect.ImmutableMap; |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableSet; |
Googler | 9bd8833 | 2019-03-19 18:09:44 -0700 | [diff] [blame] | 26 | import com.google.common.collect.ImmutableSortedMap; |
ajurkowski | aad74a3 | 2021-07-12 12:56:47 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.actions.ActionInput; |
| 28 | import com.google.devtools.build.lib.actions.ActionInputHelper; |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.actions.Artifact.ArchivedTreeArtifact; |
Googler | dce0800 | 2020-07-07 11:49:31 -0700 | [diff] [blame] | 30 | import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact; |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact; |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.actions.FileArtifactValue; |
janakr | dd8afa0 | 2021-01-29 14:35:33 -0800 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.FileStateType; |
felly | f1e30f3 | 2019-07-09 12:26:10 -0700 | [diff] [blame] | 34 | import com.google.devtools.build.lib.actions.HasDigest; |
janakr | 95704d1 | 2020-09-11 07:11:28 -0700 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.cache.MetadataDigestUtils; |
janakr | b4e8cd7 | 2018-02-27 15:39:08 -0800 | [diff] [blame] | 36 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 37 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant; |
jhorvitz | 1397e1e | 2020-07-21 10:02:17 -0700 | [diff] [blame] | 38 | import com.google.devtools.build.lib.util.Fingerprint; |
Googler | feb2670 | 2018-11-06 10:23:45 -0800 | [diff] [blame] | 39 | import com.google.devtools.build.lib.vfs.Dirent; |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.vfs.Path; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.vfs.PathFragment; |
Googler | feb2670 | 2018-11-06 10:23:45 -0800 | [diff] [blame] | 42 | import com.google.devtools.build.lib.vfs.Symlinks; |
Janak Ramakrishnan | ad77f97 | 2016-07-29 20:58:42 +0000 | [diff] [blame] | 43 | import com.google.devtools.build.skyframe.SkyValue; |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 44 | import java.io.IOException; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 45 | import java.util.Arrays; |
ajurkowski | aad74a3 | 2021-07-12 12:56:47 -0700 | [diff] [blame] | 46 | import java.util.Comparator; |
Googler | dce0800 | 2020-07-07 11:49:31 -0700 | [diff] [blame] | 47 | import java.util.HashMap; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 48 | import java.util.Map; |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 49 | import java.util.Optional; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 50 | import javax.annotation.Nullable; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 51 | |
| 52 | /** |
Janak Ramakrishnan | ad77f97 | 2016-07-29 20:58:42 +0000 | [diff] [blame] | 53 | * Value for TreeArtifacts, which contains a digest and the {@link FileArtifactValue}s of its child |
| 54 | * {@link TreeFileArtifact}s. |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 55 | */ |
felly | f1e30f3 | 2019-07-09 12:26:10 -0700 | [diff] [blame] | 56 | public class TreeArtifactValue implements HasDigest, SkyValue { |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 57 | |
ajurkowski | aad74a3 | 2021-07-12 12:56:47 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Comparator based on exec path which works on {@link ActionInput} as opposed to {@link |
| 60 | * com.google.devtools.build.lib.actions.Artifact}. This way, we can use an {@link ActionInput} to |
| 61 | * search {@link #childData}. |
| 62 | */ |
| 63 | @SerializationConstant @AutoCodec.VisibleForSerialization |
| 64 | static final Comparator<ActionInput> EXEC_PATH_COMPARATOR = |
| 65 | (input1, input2) -> input1.getExecPath().compareTo(input2.getExecPath()); |
| 66 | |
| 67 | private static final ImmutableSortedMap<TreeFileArtifact, FileArtifactValue> EMPTY_MAP = |
Googler | f53ab60 | 2022-01-11 10:07:28 -0800 | [diff] [blame^] | 68 | childDataBuilder().buildOrThrow(); |
ajurkowski | aad74a3 | 2021-07-12 12:56:47 -0700 | [diff] [blame] | 69 | |
| 70 | @SuppressWarnings("unchecked") |
| 71 | private static ImmutableSortedMap.Builder<TreeFileArtifact, FileArtifactValue> |
| 72 | childDataBuilder() { |
| 73 | return new ImmutableSortedMap.Builder<>(EXEC_PATH_COMPARATOR); |
| 74 | } |
| 75 | |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 76 | /** Returns an empty {@link TreeArtifactValue}. */ |
| 77 | public static TreeArtifactValue empty() { |
| 78 | return EMPTY; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns a new {@link Builder} for the given parent tree artifact. |
| 83 | * |
| 84 | * <p>The returned builder only supports adding children under this parent. To build multiple tree |
| 85 | * artifacts at once, use {@link MultiBuilder}. |
| 86 | */ |
| 87 | public static Builder newBuilder(SpecialArtifact parent) { |
| 88 | return new Builder(parent); |
| 89 | } |
| 90 | |
Googler | dce0800 | 2020-07-07 11:49:31 -0700 | [diff] [blame] | 91 | /** Builder for constructing multiple instances of {@link TreeArtifactValue} at once. */ |
ajurkowski | 49c9475a | 2020-09-17 16:43:29 -0700 | [diff] [blame] | 92 | public static final class MultiBuilder { |
| 93 | |
| 94 | private final Map<SpecialArtifact, Builder> map = new HashMap<>(); |
| 95 | |
| 96 | private MultiBuilder() {} |
| 97 | |
Googler | dce0800 | 2020-07-07 11:49:31 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Puts a child tree file into this builder under its {@linkplain TreeFileArtifact#getParent |
| 100 | * parent}. |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 101 | * |
| 102 | * @return {@code this} for convenience |
Googler | dce0800 | 2020-07-07 11:49:31 -0700 | [diff] [blame] | 103 | */ |
ajurkowski | 49c9475a | 2020-09-17 16:43:29 -0700 | [diff] [blame] | 104 | public MultiBuilder putChild(TreeFileArtifact child, FileArtifactValue metadata) { |
| 105 | map.computeIfAbsent(child.getParent(), Builder::new).putChild(child, metadata); |
| 106 | return this; |
| 107 | } |
Googler | dce0800 | 2020-07-07 11:49:31 -0700 | [diff] [blame] | 108 | |
| 109 | /** |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 110 | * Sets the archived representation and its metadata for the {@linkplain |
| 111 | * ArchivedTreeArtifact#getParent parent} of the provided tree artifact. |
| 112 | * |
| 113 | * <p>Setting an archived representation is only allowed once per {@linkplain SpecialArtifact |
| 114 | * tree artifact}. |
| 115 | */ |
ajurkowski | 49c9475a | 2020-09-17 16:43:29 -0700 | [diff] [blame] | 116 | public MultiBuilder setArchivedRepresentation( |
| 117 | ArchivedTreeArtifact archivedArtifact, FileArtifactValue metadata) { |
| 118 | map.computeIfAbsent(archivedArtifact.getParent(), Builder::new) |
| 119 | .setArchivedRepresentation(ArchivedRepresentation.create(archivedArtifact, metadata)); |
| 120 | return this; |
| 121 | } |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 122 | |
| 123 | /** |
ajurkowski | dd299d1 | 2020-09-22 19:22:20 -0700 | [diff] [blame] | 124 | * Removes all of collected data for a given tree artifact. |
| 125 | * |
| 126 | * <p>No-op if there is no data for a given tree artifact. |
| 127 | */ |
| 128 | public MultiBuilder remove(SpecialArtifact treeArtifact) { |
| 129 | checkArgument(treeArtifact.isTreeArtifact(), "Not a tree artifact: %s", treeArtifact); |
| 130 | map.remove(treeArtifact); |
| 131 | return this; |
| 132 | } |
| 133 | |
| 134 | /** |
Googler | dce0800 | 2020-07-07 11:49:31 -0700 | [diff] [blame] | 135 | * For each unique parent seen by this builder, passes the aggregated metadata to {@link |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 136 | * TreeArtifactInjector#injectTree}. |
Googler | dce0800 | 2020-07-07 11:49:31 -0700 | [diff] [blame] | 137 | */ |
ajurkowski | 49c9475a | 2020-09-17 16:43:29 -0700 | [diff] [blame] | 138 | public void injectTo(TreeArtifactInjector treeInjector) { |
| 139 | map.forEach((parent, builder) -> treeInjector.injectTree(parent, builder.build())); |
| 140 | } |
Googler | dce0800 | 2020-07-07 11:49:31 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** Returns a new {@link MultiBuilder}. */ |
| 144 | public static MultiBuilder newMultiBuilder() { |
ajurkowski | 49c9475a | 2020-09-17 16:43:29 -0700 | [diff] [blame] | 145 | return new MultiBuilder(); |
Googler | dce0800 | 2020-07-07 11:49:31 -0700 | [diff] [blame] | 146 | } |
| 147 | |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Archived representation of a tree artifact which contains a representation of the filesystem |
| 150 | * tree starting with the tree artifact directory. |
| 151 | * |
| 152 | * <p>Contains both the {@linkplain ArchivedTreeArtifact artifact} for the archived file and the |
| 153 | * metadata for it. |
| 154 | */ |
| 155 | @AutoValue |
Chi Wang | 4e29042 | 2021-08-03 17:56:19 -0700 | [diff] [blame] | 156 | public abstract static class ArchivedRepresentation { |
| 157 | public abstract ArchivedTreeArtifact archivedTreeFileArtifact(); |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 158 | |
Chi Wang | 4e29042 | 2021-08-03 17:56:19 -0700 | [diff] [blame] | 159 | public abstract FileArtifactValue archivedFileValue(); |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 160 | |
Chi Wang | 4e29042 | 2021-08-03 17:56:19 -0700 | [diff] [blame] | 161 | public static ArchivedRepresentation create( |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 162 | ArchivedTreeArtifact archivedTreeFileArtifact, FileArtifactValue fileArtifactValue) { |
| 163 | return new AutoValue_TreeArtifactValue_ArchivedRepresentation( |
| 164 | archivedTreeFileArtifact, fileArtifactValue); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | @SuppressWarnings("WeakerAccess") // Serialization constant. |
| 169 | @SerializationConstant |
| 170 | @AutoCodec.VisibleForSerialization |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 171 | static final TreeArtifactValue EMPTY = |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 172 | new TreeArtifactValue( |
janakr | 95704d1 | 2020-09-11 07:11:28 -0700 | [diff] [blame] | 173 | MetadataDigestUtils.fromMetadata(ImmutableMap.of()), |
ajurkowski | aad74a3 | 2021-07-12 12:56:47 -0700 | [diff] [blame] | 174 | EMPTY_MAP, |
janakr | dd8afa0 | 2021-01-29 14:35:33 -0800 | [diff] [blame] | 175 | 0L, |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 176 | /*archivedRepresentation=*/ null, |
Googler | a4fff87 | 2020-06-17 14:26:35 -0700 | [diff] [blame] | 177 | /*entirelyRemote=*/ false); |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 178 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 179 | private final byte[] digest; |
Googler | 9bd8833 | 2019-03-19 18:09:44 -0700 | [diff] [blame] | 180 | private final ImmutableSortedMap<TreeFileArtifact, FileArtifactValue> childData; |
janakr | dd8afa0 | 2021-01-29 14:35:33 -0800 | [diff] [blame] | 181 | private final long totalChildSize; |
ajurkowski | 49c9475a | 2020-09-17 16:43:29 -0700 | [diff] [blame] | 182 | |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Optional archived representation of the entire tree artifact which can be sent instead of all |
| 185 | * the items in the directory. |
| 186 | */ |
| 187 | @Nullable private final ArchivedRepresentation archivedRepresentation; |
| 188 | |
Googler | a4fff87 | 2020-06-17 14:26:35 -0700 | [diff] [blame] | 189 | private final boolean entirelyRemote; |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 190 | |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 191 | private TreeArtifactValue( |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 192 | byte[] digest, |
| 193 | ImmutableSortedMap<TreeFileArtifact, FileArtifactValue> childData, |
janakr | dd8afa0 | 2021-01-29 14:35:33 -0800 | [diff] [blame] | 194 | long totalChildSize, |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 195 | @Nullable ArchivedRepresentation archivedRepresentation, |
Googler | a4fff87 | 2020-06-17 14:26:35 -0700 | [diff] [blame] | 196 | boolean entirelyRemote) { |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 197 | this.digest = digest; |
Googler | 9bd8833 | 2019-03-19 18:09:44 -0700 | [diff] [blame] | 198 | this.childData = childData; |
janakr | dd8afa0 | 2021-01-29 14:35:33 -0800 | [diff] [blame] | 199 | this.totalChildSize = totalChildSize; |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 200 | this.archivedRepresentation = archivedRepresentation; |
Googler | a4fff87 | 2020-06-17 14:26:35 -0700 | [diff] [blame] | 201 | this.entirelyRemote = entirelyRemote; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 202 | } |
| 203 | |
ajurkowski | aad74a3 | 2021-07-12 12:56:47 -0700 | [diff] [blame] | 204 | public FileArtifactValue getMetadata() { |
Googler | 206d6e4 | 2020-06-18 12:39:17 -0700 | [diff] [blame] | 205 | return FileArtifactValue.createProxy(digest); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Googler | 9bd8833 | 2019-03-19 18:09:44 -0700 | [diff] [blame] | 208 | ImmutableSet<PathFragment> getChildPaths() { |
| 209 | return childData.keySet().stream() |
| 210 | .map(TreeFileArtifact::getParentRelativePath) |
| 211 | .collect(toImmutableSet()); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 212 | } |
| 213 | |
felly | f1e30f3 | 2019-07-09 12:26:10 -0700 | [diff] [blame] | 214 | @Override |
| 215 | public byte[] getDigest() { |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 216 | return digest.clone(); |
| 217 | } |
| 218 | |
Googler | 04c546f | 2020-05-12 18:22:18 -0700 | [diff] [blame] | 219 | public ImmutableSet<TreeFileArtifact> getChildren() { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 220 | return childData.keySet(); |
| 221 | } |
| 222 | |
janakr | dd8afa0 | 2021-01-29 14:35:33 -0800 | [diff] [blame] | 223 | public long getTotalChildBytes() { |
| 224 | return totalChildSize; |
| 225 | } |
| 226 | |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 227 | /** Return archived representation of the tree artifact (if present). */ |
Chi Wang | 4e29042 | 2021-08-03 17:56:19 -0700 | [diff] [blame] | 228 | public Optional<ArchivedRepresentation> getArchivedRepresentation() { |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 229 | return Optional.ofNullable(archivedRepresentation); |
| 230 | } |
| 231 | |
| 232 | @VisibleForTesting |
ajurkowski | dd299d1 | 2020-09-22 19:22:20 -0700 | [diff] [blame] | 233 | @Nullable |
| 234 | public ArchivedTreeArtifact getArchivedArtifactForTesting() { |
| 235 | return archivedRepresentation != null |
| 236 | ? archivedRepresentation.archivedTreeFileArtifact() |
| 237 | : null; |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 238 | } |
| 239 | |
ajurkowski | aad74a3 | 2021-07-12 12:56:47 -0700 | [diff] [blame] | 240 | public ImmutableSortedMap<TreeFileArtifact, FileArtifactValue> getChildValues() { |
Rumou Duan | baeed33 | 2016-10-18 15:30:25 +0000 | [diff] [blame] | 241 | return childData; |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 242 | } |
| 243 | |
ajurkowski | aad74a3 | 2021-07-12 12:56:47 -0700 | [diff] [blame] | 244 | /** Returns an entry for child with given exec path or null if no such child is present. */ |
| 245 | @SuppressWarnings("unchecked") |
| 246 | @Nullable |
| 247 | public Map.Entry<TreeFileArtifact, FileArtifactValue> findChildEntryByExecPath( |
| 248 | PathFragment execPath) { |
| 249 | ActionInput searchToken = ActionInputHelper.fromPath(execPath); |
| 250 | // Not really a copy -- original map is already an ImmutableSortedMap using the same comparator. |
| 251 | ImmutableSortedMap<ActionInput, FileArtifactValue> casted = |
| 252 | ImmutableSortedMap.copyOf(childData, EXEC_PATH_COMPARATOR); |
| 253 | checkState(casted == (Object) childData, "Casting children resulted with a copy"); |
| 254 | Map.Entry<? extends ActionInput, FileArtifactValue> entry = casted.floorEntry(searchToken); |
| 255 | return entry != null && entry.getKey().getExecPath().equals(execPath) |
| 256 | ? (Map.Entry<TreeFileArtifact, FileArtifactValue>) entry |
| 257 | : null; |
| 258 | } |
| 259 | |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 260 | /** Returns true if the {@link TreeFileArtifact}s are only stored remotely. */ |
Googler | a4fff87 | 2020-06-17 14:26:35 -0700 | [diff] [blame] | 261 | public boolean isEntirelyRemote() { |
| 262 | return entirelyRemote; |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 265 | @Override |
| 266 | public int hashCode() { |
| 267 | return Arrays.hashCode(digest); |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public boolean equals(Object other) { |
| 272 | if (this == other) { |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | if (!(other instanceof TreeArtifactValue)) { |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | TreeArtifactValue that = (TreeArtifactValue) other; |
Shreya Bhattarai | 3b09680 | 2016-08-17 23:14:52 +0000 | [diff] [blame] | 281 | if (!Arrays.equals(digest, that.digest)) { |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 282 | return false; |
| 283 | } |
| 284 | |
| 285 | return childData.equals(that.childData); |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public String toString() { |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 290 | return MoreObjects.toStringHelper(this) |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 291 | .add("digest", digest) |
| 292 | .add("childData", childData) |
| 293 | .toString(); |
| 294 | } |
| 295 | |
| 296 | /** |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 297 | * Represents a tree artifact that was intentionally omitted, similar to {@link |
| 298 | * FileArtifactValue#OMITTED_FILE_MARKER}. |
| 299 | */ |
| 300 | @SerializationConstant |
| 301 | public static final TreeArtifactValue OMITTED_TREE_MARKER = createMarker("OMITTED_TREE_MARKER"); |
| 302 | |
| 303 | /** |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 304 | * A TreeArtifactValue that represents a missing TreeArtifact. This is occasionally useful because |
| 305 | * Java's concurrent collections disallow null members. |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 306 | */ |
Chi Wang | 4e29042 | 2021-08-03 17:56:19 -0700 | [diff] [blame] | 307 | public static final TreeArtifactValue MISSING_TREE_ARTIFACT = |
| 308 | createMarker("MISSING_TREE_ARTIFACT"); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 309 | |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 310 | private static TreeArtifactValue createMarker(String toStringRepresentation) { |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 311 | return new TreeArtifactValue( |
ajurkowski | aad74a3 | 2021-07-12 12:56:47 -0700 | [diff] [blame] | 312 | null, EMPTY_MAP, 0L, /*archivedRepresentation=*/ null, /*entirelyRemote=*/ false) { |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 313 | @Override |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 314 | public ImmutableSet<TreeFileArtifact> getChildren() { |
| 315 | throw new UnsupportedOperationException(toString()); |
| 316 | } |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 317 | |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 318 | @Override |
ajurkowski | aad74a3 | 2021-07-12 12:56:47 -0700 | [diff] [blame] | 319 | public ImmutableSortedMap<TreeFileArtifact, FileArtifactValue> getChildValues() { |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 320 | throw new UnsupportedOperationException(toString()); |
| 321 | } |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 322 | |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 323 | @Override |
ajurkowski | aad74a3 | 2021-07-12 12:56:47 -0700 | [diff] [blame] | 324 | public FileArtifactValue getMetadata() { |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 325 | throw new UnsupportedOperationException(toString()); |
| 326 | } |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 327 | |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 328 | @Override |
| 329 | ImmutableSet<PathFragment> getChildPaths() { |
| 330 | throw new UnsupportedOperationException(toString()); |
| 331 | } |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 332 | |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 333 | @Nullable |
| 334 | @Override |
| 335 | public byte[] getDigest() { |
| 336 | throw new UnsupportedOperationException(toString()); |
| 337 | } |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 338 | |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 339 | @Override |
| 340 | public int hashCode() { |
| 341 | return System.identityHashCode(this); |
| 342 | } |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 343 | |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 344 | @Override |
| 345 | public boolean equals(Object other) { |
| 346 | return this == other; |
| 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public String toString() { |
| 351 | return toStringRepresentation; |
| 352 | } |
| 353 | }; |
| 354 | } |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 355 | |
jhorvitz | 0d0758a | 2020-07-14 12:50:24 -0700 | [diff] [blame] | 356 | /** Visitor for use in {@link #visitTree}. */ |
| 357 | @FunctionalInterface |
| 358 | public interface TreeArtifactVisitor { |
| 359 | /** |
| 360 | * Called for every directory entry encountered during tree traversal. |
| 361 | * |
| 362 | * <p>Symlinks are not followed during traversal and are simply reported as {@link |
| 363 | * Dirent.Type#SYMLINK} regardless of whether they point to a file, directory, or are dangling. |
| 364 | * |
| 365 | * <p>{@code type} is guaranteed never to be {@link Dirent.Type#UNKNOWN}, since if this type is |
| 366 | * encountered, {@link IOException} is immediately thrown without invoking the visitor. |
| 367 | * |
| 368 | * <p>If the implementation throws {@link IOException}, traversal is immediately halted and the |
| 369 | * exception is propagated. |
| 370 | */ |
| 371 | void visit(PathFragment parentRelativePath, Dirent.Type type) throws IOException; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Recursively visits all descendants under a directory. |
| 376 | * |
| 377 | * <p>{@link TreeArtifactVisitor#visit} is invoked on {@code visitor} for each directory, file, |
| 378 | * and symlink under the given {@code parentDir}. |
| 379 | * |
| 380 | * <p>This method is intended to provide uniform semantics for constructing a tree artifact, |
| 381 | * including special logic that validates directory entries. Invalid directory entries include a |
| 382 | * symlink that traverses outside of the tree artifact and any entry of {@link |
| 383 | * Dirent.Type#UNKNOWN}, such as a named pipe. |
| 384 | * |
| 385 | * @throws IOException if there is any problem reading or validating outputs under the given tree |
| 386 | * artifact directory, or if {@link TreeArtifactVisitor#visit} throws {@link IOException} |
| 387 | */ |
| 388 | public static void visitTree(Path parentDir, TreeArtifactVisitor visitor) throws IOException { |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 389 | visitTree(parentDir, PathFragment.EMPTY_FRAGMENT, checkNotNull(visitor)); |
jhorvitz | 0d0758a | 2020-07-14 12:50:24 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | private static void visitTree(Path parentDir, PathFragment subdir, TreeArtifactVisitor visitor) |
Rumou Duan | 9ad28cd | 2016-10-19 19:28:06 +0000 | [diff] [blame] | 393 | throws IOException { |
jhorvitz | 0d0758a | 2020-07-14 12:50:24 -0700 | [diff] [blame] | 394 | for (Dirent dirent : parentDir.getRelative(subdir).readdir(Symlinks.NOFOLLOW)) { |
| 395 | PathFragment parentRelativePath = subdir.getChild(dirent.getName()); |
| 396 | Dirent.Type type = dirent.getType(); |
| 397 | |
| 398 | if (type == Dirent.Type.UNKNOWN) { |
| 399 | throw new IOException( |
| 400 | "Could not determine type of file for " + parentRelativePath + " under " + parentDir); |
| 401 | } |
| 402 | |
| 403 | if (type == Dirent.Type.SYMLINK) { |
| 404 | checkSymlink(subdir, parentDir.getRelative(parentRelativePath)); |
| 405 | } |
| 406 | |
| 407 | visitor.visit(parentRelativePath, type); |
| 408 | |
| 409 | if (type == Dirent.Type.DIRECTORY) { |
| 410 | visitTree(parentDir, parentRelativePath, visitor); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
jhorvitz | 0d0758a | 2020-07-14 12:50:24 -0700 | [diff] [blame] | 415 | private static void checkSymlink(PathFragment subDir, Path path) throws IOException { |
| 416 | PathFragment linkTarget = path.readSymbolicLinkUnchecked(); |
| 417 | if (linkTarget.isAbsolute()) { |
| 418 | // We tolerate absolute symlinks here. They will probably be dangling if any downstream |
| 419 | // consumer tries to read them, but let that be downstream's problem. |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | // Visit each path segment of the link target to catch any path traversal outside of the |
| 424 | // TreeArtifact root directory. For example, for TreeArtifact a/b/c, it is possible to have a |
| 425 | // symlink, a/b/c/sym_link that points to ../outside_dir/../c/link_target. Although this symlink |
| 426 | // points to a file under the TreeArtifact, the link target traverses outside of the |
| 427 | // TreeArtifact into a/b/outside_dir. |
| 428 | PathFragment intermediatePath = subDir; |
jhorvitz | 6034795 | 2021-02-18 10:05:05 -0800 | [diff] [blame] | 429 | for (String pathSegment : linkTarget.segments()) { |
jhorvitz | 0d0758a | 2020-07-14 12:50:24 -0700 | [diff] [blame] | 430 | intermediatePath = intermediatePath.getRelative(pathSegment); |
| 431 | if (intermediatePath.containsUplevelReferences()) { |
| 432 | String errorMessage = |
| 433 | String.format( |
| 434 | "A TreeArtifact may not contain relative symlinks whose target paths traverse " |
| 435 | + "outside of the TreeArtifact, found %s pointing to %s.", |
| 436 | path, linkTarget); |
| 437 | throw new IOException(errorMessage); |
| 438 | } |
| 439 | } |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 440 | } |
Googler | dce0800 | 2020-07-07 11:49:31 -0700 | [diff] [blame] | 441 | |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 442 | /** Builder for a {@link TreeArtifactValue}. */ |
| 443 | public static final class Builder { |
| 444 | private final ImmutableSortedMap.Builder<TreeFileArtifact, FileArtifactValue> childData = |
ajurkowski | aad74a3 | 2021-07-12 12:56:47 -0700 | [diff] [blame] | 445 | childDataBuilder(); |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 446 | private ArchivedRepresentation archivedRepresentation; |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 447 | private final SpecialArtifact parent; |
| 448 | |
| 449 | Builder(SpecialArtifact parent) { |
| 450 | checkArgument(parent.isTreeArtifact(), "%s is not a tree artifact", parent); |
| 451 | this.parent = parent; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Adds a child to this builder. |
| 456 | * |
| 457 | * <p>The child's {@linkplain TreeFileArtifact#getParent parent} <em>must</em> match the parent |
| 458 | * with which this builder was initialized. |
| 459 | * |
| 460 | * <p>Children may be added in any order. The children are sorted prior to constructing the |
| 461 | * final {@link TreeArtifactValue}. |
| 462 | * |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 463 | * <p>It is illegal to call this method with {@link FileArtifactValue#OMITTED_FILE_MARKER}. When |
| 464 | * |
| 465 | * <p>It is illegal to call this method with {@link FileArtifactValue#OMITTED_FILE_MARKER}. When |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 466 | * children are omitted, use {@link TreeArtifactValue#OMITTED_TREE_MARKER}. |
| 467 | * |
| 468 | * @return {@code this} for convenience |
| 469 | */ |
| 470 | public Builder putChild(TreeFileArtifact child, FileArtifactValue metadata) { |
| 471 | checkArgument( |
| 472 | child.getParent().equals(parent), |
| 473 | "While building TreeArtifactValue for %s, got %s with parent %s", |
| 474 | parent, |
| 475 | child, |
| 476 | child.getParent()); |
| 477 | checkArgument( |
| 478 | !FileArtifactValue.OMITTED_FILE_MARKER.equals(metadata), |
| 479 | "Cannot construct TreeArtifactValue for %s because child %s was omitted", |
| 480 | parent, |
| 481 | child); |
| 482 | childData.put(child, metadata); |
| 483 | return this; |
| 484 | } |
| 485 | |
ajurkowski | d83b124 | 2020-08-31 10:02:05 -0700 | [diff] [blame] | 486 | public Builder setArchivedRepresentation( |
| 487 | ArchivedTreeArtifact archivedTreeArtifact, FileArtifactValue metadata) { |
| 488 | return setArchivedRepresentation( |
| 489 | ArchivedRepresentation.create(archivedTreeArtifact, metadata)); |
| 490 | } |
| 491 | |
Chi Wang | 4e29042 | 2021-08-03 17:56:19 -0700 | [diff] [blame] | 492 | public Builder setArchivedRepresentation(ArchivedRepresentation archivedRepresentation) { |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 493 | checkState( |
| 494 | this.archivedRepresentation == null, |
| 495 | "Tried to add 2 archived representations for: %s", |
ajurkowski | d83b124 | 2020-08-31 10:02:05 -0700 | [diff] [blame] | 496 | parent); |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 497 | checkArgument( |
ajurkowski | d83b124 | 2020-08-31 10:02:05 -0700 | [diff] [blame] | 498 | parent.equals(archivedRepresentation.archivedTreeFileArtifact().getParent()), |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 499 | "Cannot add archived representation: %s for a mismatching tree artifact: %s", |
| 500 | archivedRepresentation, |
| 501 | parent); |
ajurkowski | d83b124 | 2020-08-31 10:02:05 -0700 | [diff] [blame] | 502 | checkArgument( |
| 503 | !archivedRepresentation.archivedFileValue().equals(FileArtifactValue.OMITTED_FILE_MARKER), |
| 504 | "Cannot add archived representation: %s to %s because it has omitted metadata.", |
| 505 | archivedRepresentation, |
| 506 | parent); |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 507 | this.archivedRepresentation = archivedRepresentation; |
| 508 | return this; |
| 509 | } |
| 510 | |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 511 | /** Builds the final {@link TreeArtifactValue}. */ |
| 512 | public TreeArtifactValue build() { |
Googler | f53ab60 | 2022-01-11 10:07:28 -0800 | [diff] [blame^] | 513 | ImmutableSortedMap<TreeFileArtifact, FileArtifactValue> finalChildData = |
| 514 | childData.buildOrThrow(); |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 515 | if (finalChildData.isEmpty() && archivedRepresentation == null) { |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 516 | return EMPTY; |
| 517 | } |
| 518 | |
| 519 | Fingerprint fingerprint = new Fingerprint(); |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 520 | boolean entirelyRemote = |
| 521 | archivedRepresentation == null || archivedRepresentation.archivedFileValue().isRemote(); |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 522 | |
janakr | dd8afa0 | 2021-01-29 14:35:33 -0800 | [diff] [blame] | 523 | long totalChildSize = 0; |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 524 | for (Map.Entry<TreeFileArtifact, FileArtifactValue> childData : finalChildData.entrySet()) { |
| 525 | // Digest will be deterministic because children are sorted. |
| 526 | fingerprint.addPath(childData.getKey().getParentRelativePath()); |
janakr | dd8afa0 | 2021-01-29 14:35:33 -0800 | [diff] [blame] | 527 | FileArtifactValue metadata = childData.getValue(); |
| 528 | metadata.addTo(fingerprint); |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 529 | |
jhorvitz | 7749741 | 2020-08-07 12:38:22 -0700 | [diff] [blame] | 530 | // Tolerate a mix of local and remote children (b/152496153#comment80). |
janakr | dd8afa0 | 2021-01-29 14:35:33 -0800 | [diff] [blame] | 531 | entirelyRemote &= metadata.isRemote(); |
| 532 | |
| 533 | if (metadata.getType() == FileStateType.REGULAR_FILE) { |
| 534 | totalChildSize += metadata.getSize(); |
| 535 | } |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 536 | } |
| 537 | |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 538 | if (archivedRepresentation != null) { |
| 539 | archivedRepresentation.archivedFileValue().addTo(fingerprint); |
| 540 | } |
| 541 | |
| 542 | return new TreeArtifactValue( |
janakr | dd8afa0 | 2021-01-29 14:35:33 -0800 | [diff] [blame] | 543 | fingerprint.digestAndReset(), |
| 544 | finalChildData, |
| 545 | totalChildSize, |
| 546 | archivedRepresentation, |
| 547 | entirelyRemote); |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 548 | } |
| 549 | } |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 550 | } |