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 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 16 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.common.base.Verify; |
| 18 | import com.google.common.collect.Collections2; |
Googler | c8acdbd | 2018-11-08 10:47:15 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 20 | import com.google.devtools.build.lib.actions.Artifact; |
Googler | c8acdbd | 2018-11-08 10:47:15 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact; |
shahan | e35e8cf | 2018-06-18 08:14:01 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.actions.ArtifactSkyKey; |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.FileArtifactValue; |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.FileValue; |
Laszlo Csomor | 207140f | 2015-12-07 15:07:33 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
| 27 | import com.google.devtools.build.lib.events.Event; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import com.google.devtools.build.lib.skyframe.RecursiveFilesystemTraversalValue.ResolvedFile; |
Laszlo Csomor | b4d482b | 2015-12-04 13:23:54 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.skyframe.RecursiveFilesystemTraversalValue.ResolvedFileFactory; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | import com.google.devtools.build.lib.skyframe.RecursiveFilesystemTraversalValue.TraversalRequest; |
| 31 | import com.google.devtools.build.lib.vfs.Dirent; |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 32 | import com.google.devtools.build.lib.vfs.FileStatus; |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 33 | import com.google.devtools.build.lib.vfs.Path; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 34 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 35 | import com.google.devtools.build.lib.vfs.Root; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | import com.google.devtools.build.lib.vfs.RootedPath; |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 37 | import com.google.devtools.build.lib.vfs.Symlinks; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | import com.google.devtools.build.skyframe.SkyFunction; |
| 39 | import com.google.devtools.build.skyframe.SkyFunctionException; |
| 40 | import com.google.devtools.build.skyframe.SkyKey; |
| 41 | import com.google.devtools.build.skyframe.SkyValue; |
kush | 95bf7c8 | 2017-08-30 00:27:35 +0200 | [diff] [blame] | 42 | import java.io.IOException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | import java.util.ArrayList; |
| 44 | import java.util.Collection; |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 45 | import java.util.Collections; |
| 46 | import java.util.HashMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | import java.util.List; |
| 48 | import java.util.Map; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | import javax.annotation.Nullable; |
| 50 | |
| 51 | /** A {@link SkyFunction} to build {@link RecursiveFilesystemTraversalValue}s. */ |
| 52 | public final class RecursiveFilesystemTraversalFunction implements SkyFunction { |
| 53 | |
| 54 | private static final class MissingDepException extends Exception {} |
| 55 | |
| 56 | /** Base class for exceptions that {@link RecursiveFilesystemTraversalFunctionException} wraps. */ |
| 57 | public abstract static class RecursiveFilesystemTraversalException extends Exception { |
| 58 | protected RecursiveFilesystemTraversalException(String message) { |
| 59 | super(message); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** Thrown when a generated directory's root-relative path conflicts with a package's path. */ |
| 64 | public static final class GeneratedPathConflictException extends |
| 65 | RecursiveFilesystemTraversalException { |
| 66 | GeneratedPathConflictException(TraversalRequest traversal) { |
tomlu | 8cc5dcf | 2018-01-19 09:28:06 -0800 | [diff] [blame] | 67 | super( |
| 68 | String.format( |
| 69 | "Generated directory %s conflicts with package under the same path. " |
| 70 | + "Additional info: %s", |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 71 | traversal.root.asRootedPath().getRootRelativePath().getPathString(), |
tomlu | 8cc5dcf | 2018-01-19 09:28:06 -0800 | [diff] [blame] | 72 | traversal.errorInfo != null ? traversal.errorInfo : traversal.toString())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Thrown when the traversal encounters a subdirectory with a BUILD file but is not allowed to |
Laszlo Csomor | f04efcc | 2015-02-12 17:08:06 +0000 | [diff] [blame] | 78 | * recurse into it. See {@code PackageBoundaryMode#REPORT_ERROR}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 79 | */ |
| 80 | public static final class CannotCrossPackageBoundaryException extends |
| 81 | RecursiveFilesystemTraversalException { |
| 82 | CannotCrossPackageBoundaryException(String message) { |
| 83 | super(message); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Thrown when a dangling symlink is attempted to be dereferenced. |
| 89 | * |
| 90 | * <p>Note: this class is not identical to the one in com.google.devtools.build.lib.view.fileset |
| 91 | * and it's not easy to merge the two because of the dependency structure. The other one will |
| 92 | * probably be removed along with the rest of the legacy Fileset code. |
| 93 | */ |
| 94 | public static final class DanglingSymlinkException extends RecursiveFilesystemTraversalException { |
| 95 | public final String path; |
| 96 | public final String unresolvedLink; |
| 97 | |
| 98 | public DanglingSymlinkException(String path, String unresolvedLink) { |
Laszlo Csomor | 0ad729f | 2015-12-02 15:20:35 +0000 | [diff] [blame] | 99 | super( |
| 100 | String.format( |
| 101 | "Found dangling symlink: %s, unresolved path: \"%s\"", path, unresolvedLink)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 102 | Preconditions.checkArgument(path != null && !path.isEmpty()); |
| 103 | Preconditions.checkArgument(unresolvedLink != null && !unresolvedLink.isEmpty()); |
| 104 | this.path = path; |
| 105 | this.unresolvedLink = unresolvedLink; |
| 106 | } |
| 107 | |
| 108 | public String getPath() { |
| 109 | return path; |
| 110 | } |
| 111 | } |
| 112 | |
kush | 95bf7c8 | 2017-08-30 00:27:35 +0200 | [diff] [blame] | 113 | /** Thrown when we encounter errors from underlying File operations */ |
| 114 | public static final class FileOperationException extends RecursiveFilesystemTraversalException { |
| 115 | public FileOperationException(String message) { |
| 116 | super(message); |
| 117 | } |
| 118 | } |
| 119 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 120 | /** Exception type thrown by {@link RecursiveFilesystemTraversalFunction#compute}. */ |
| 121 | private static final class RecursiveFilesystemTraversalFunctionException extends |
| 122 | SkyFunctionException { |
| 123 | RecursiveFilesystemTraversalFunctionException(RecursiveFilesystemTraversalException e) { |
| 124 | super(e, Transience.PERSISTENT); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public SkyValue compute(SkyKey skyKey, Environment env) |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 130 | throws RecursiveFilesystemTraversalFunctionException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 131 | TraversalRequest traversal = (TraversalRequest) skyKey.argument(); |
| 132 | try { |
| 133 | // Stat the traversal root. |
| 134 | FileInfo rootInfo = lookUpFileInfo(env, traversal); |
| 135 | |
| 136 | if (!rootInfo.type.exists()) { |
| 137 | // May be a dangling symlink or a non-existent file. Handle gracefully. |
| 138 | if (rootInfo.type.isSymlink()) { |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 139 | return resultForDanglingSymlink(traversal.root.asRootedPath(), rootInfo); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 140 | } else { |
| 141 | return RecursiveFilesystemTraversalValue.EMPTY; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (rootInfo.type.isFile()) { |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 146 | return resultForFileRoot(traversal.root.asRootedPath(), rootInfo); |
Googler | c8acdbd | 2018-11-08 10:47:15 -0800 | [diff] [blame] | 147 | } else if (rootInfo.type.isDirectory() && rootInfo.metadata instanceof TreeArtifactValue) { |
| 148 | final TreeArtifactValue value = (TreeArtifactValue) rootInfo.metadata; |
| 149 | ImmutableList.Builder<RecursiveFilesystemTraversalValue> list = ImmutableList.builder(); |
| 150 | for (Map.Entry<TreeFileArtifact, FileArtifactValue> entry |
| 151 | : value.getChildValues().entrySet()) { |
| 152 | RootedPath path = |
| 153 | RootedPath.toRootedPath( |
| 154 | traversal.root.asRootedPath().getRoot(), entry.getKey().getPath()); |
| 155 | list.add(resultForFileRoot( |
| 156 | path, |
| 157 | // TreeArtifact can't have symbolic inside. So the assumption for FileType.FILE |
| 158 | // is always true. |
| 159 | new FileInfo(FileType.FILE, entry.getValue(), path, null))); |
| 160 | } |
| 161 | return resultForDirectory(traversal, rootInfo, list.build()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Otherwise the root is a directory or a symlink to one. |
| 165 | PkgLookupResult pkgLookupResult = checkIfPackage(env, traversal, rootInfo); |
| 166 | traversal = pkgLookupResult.traversal; |
| 167 | |
| 168 | if (pkgLookupResult.isConflicting()) { |
| 169 | // The traversal was requested for an output directory whose root-relative path conflicts |
| 170 | // with a source package. We can't handle that, bail out. |
| 171 | throw new RecursiveFilesystemTraversalFunctionException( |
| 172 | new GeneratedPathConflictException(traversal)); |
| 173 | } else if (pkgLookupResult.isPackage() && !traversal.skipTestingForSubpackage) { |
| 174 | // The traversal was requested for a directory that defines a package. |
tomlu | 8cc5dcf | 2018-01-19 09:28:06 -0800 | [diff] [blame] | 175 | String msg = |
| 176 | traversal.errorInfo |
| 177 | + " crosses package boundary into package rooted at " |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 178 | + traversal.root.asRootedPath().getRootRelativePath().getPathString(); |
Laszlo Csomor | f04efcc | 2015-02-12 17:08:06 +0000 | [diff] [blame] | 179 | switch (traversal.crossPkgBoundaries) { |
| 180 | case CROSS: |
| 181 | // We are free to traverse the subpackage but we need to display a warning. |
Ulf Adams | 760e709 | 2016-04-21 08:09:51 +0000 | [diff] [blame] | 182 | env.getListener().handle(Event.warn(null, msg)); |
Laszlo Csomor | f04efcc | 2015-02-12 17:08:06 +0000 | [diff] [blame] | 183 | break; |
| 184 | case DONT_CROSS: |
| 185 | // We cannot traverse the subpackage and should skip it silently. Return empty results. |
| 186 | return RecursiveFilesystemTraversalValue.EMPTY; |
| 187 | case REPORT_ERROR: |
| 188 | // We cannot traverse the subpackage and should complain loudly (display an error). |
| 189 | throw new RecursiveFilesystemTraversalFunctionException( |
| 190 | new CannotCrossPackageBoundaryException(msg)); |
| 191 | default: |
| 192 | throw new IllegalStateException(traversal.toString()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | // We are free to traverse this directory. |
| 197 | Collection<SkyKey> dependentKeys = createRecursiveTraversalKeys(env, traversal); |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 198 | return resultForDirectory( |
| 199 | traversal, |
| 200 | rootInfo, |
| 201 | traverseChildren(env, dependentKeys, /*inline=*/ traversal.isRootGenerated)); |
ulfjack | e479453 | 2018-01-12 02:11:17 -0800 | [diff] [blame] | 202 | } catch (IOException e) { |
kush | 95bf7c8 | 2017-08-30 00:27:35 +0200 | [diff] [blame] | 203 | throw new RecursiveFilesystemTraversalFunctionException( |
| 204 | new FileOperationException("Error while traversing fileset: " + e.getMessage())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 205 | } catch (MissingDepException e) { |
| 206 | return null; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public String extractTag(SkyKey skyKey) { |
| 212 | return null; |
| 213 | } |
| 214 | |
| 215 | private static final class FileInfo { |
| 216 | final FileType type; |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 217 | final Object metadata; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 218 | @Nullable final RootedPath realPath; |
| 219 | @Nullable final PathFragment unresolvedSymlinkTarget; |
| 220 | |
kush | 4b120e7 | 2018-07-11 16:21:27 -0700 | [diff] [blame] | 221 | FileInfo( |
| 222 | FileType type, |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 223 | Object metadata, |
kush | 4b120e7 | 2018-07-11 16:21:27 -0700 | [diff] [blame] | 224 | @Nullable RootedPath realPath, |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 225 | @Nullable PathFragment unresolvedSymlinkTarget) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 226 | this.type = Preconditions.checkNotNull(type); |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 227 | this.metadata = metadata; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 228 | this.realPath = realPath; |
| 229 | this.unresolvedSymlinkTarget = unresolvedSymlinkTarget; |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public String toString() { |
| 234 | if (type.isSymlink()) { |
| 235 | return String.format("(%s: link_value=%s, real_path=%s)", type, |
| 236 | unresolvedSymlinkTarget.getPathString(), realPath); |
| 237 | } else { |
| 238 | return String.format("(%s: real_path=%s)", type, realPath); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 243 | private static final FileInfo NON_EXISTENT_FILE_INFO = |
| 244 | new FileInfo(FileType.NONEXISTENT, new Integer(0), null, null); |
| 245 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 246 | private static FileInfo lookUpFileInfo(Environment env, TraversalRequest traversal) |
ulfjack | e479453 | 2018-01-12 02:11:17 -0800 | [diff] [blame] | 247 | throws MissingDepException, IOException, InterruptedException { |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 248 | if (traversal.isRootGenerated) { |
Googler | c8acdbd | 2018-11-08 10:47:15 -0800 | [diff] [blame] | 249 | SkyValue fsVal = null; |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 250 | if (traversal.root.getOutputArtifact() != null) { |
| 251 | Artifact artifact = traversal.root.getOutputArtifact(); |
| 252 | SkyKey artifactKey = ArtifactSkyKey.key(artifact, true); |
| 253 | SkyValue value = env.getValue(artifactKey); |
| 254 | if (env.valuesMissing()) { |
| 255 | throw new MissingDepException(); |
| 256 | } |
kush | 95bf7c8 | 2017-08-30 00:27:35 +0200 | [diff] [blame] | 257 | |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 258 | if (value instanceof FileArtifactValue) { |
Googler | c8acdbd | 2018-11-08 10:47:15 -0800 | [diff] [blame] | 259 | fsVal = value; |
| 260 | } else if (value instanceof TreeArtifactValue) { |
| 261 | fsVal = value; |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 262 | } else { |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 263 | return NON_EXISTENT_FILE_INFO; |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 264 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 265 | } |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 266 | RootedPath realPath = traversal.root.asRootedPath(); |
felly | 6c399d6 | 2018-07-18 15:55:59 -0700 | [diff] [blame] | 267 | if (traversal.strictOutputFiles) { |
| 268 | Preconditions.checkNotNull(fsVal, "Strict Fileset output tree has null FileArtifactValue"); |
Googler | c8acdbd | 2018-11-08 10:47:15 -0800 | [diff] [blame] | 269 | return new FileInfo( |
| 270 | (fsVal instanceof TreeArtifactValue ? FileType.DIRECTORY : FileType.FILE), |
| 271 | fsVal, realPath, null); |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 272 | } else { |
felly | 6c399d6 | 2018-07-18 15:55:59 -0700 | [diff] [blame] | 273 | // FileArtifactValue does not currently track symlinks. If it did, we could potentially |
| 274 | // remove some of the filesystem operations we're doing here. |
| 275 | Path path = traversal.root.asRootedPath().asPath(); |
| 276 | FileStatus noFollowStat = path.stat(Symlinks.NOFOLLOW); |
| 277 | FileStatus followStat = path.statIfFound(Symlinks.FOLLOW); |
| 278 | FileType type; |
| 279 | PathFragment unresolvedLinkTarget = null; |
| 280 | if (followStat == null) { |
| 281 | type = FileType.DANGLING_SYMLINK; |
| 282 | if (!noFollowStat.isSymbolicLink()) { |
| 283 | throw new IOException("Expected symlink for " + path + ", but got: " + noFollowStat); |
| 284 | } |
| 285 | unresolvedLinkTarget = path.readSymbolicLink(); |
| 286 | } else if (noFollowStat.isFile()) { |
| 287 | type = FileType.FILE; |
| 288 | } else if (noFollowStat.isDirectory()) { |
| 289 | type = FileType.DIRECTORY; |
| 290 | } else { |
| 291 | unresolvedLinkTarget = path.readSymbolicLink(); |
| 292 | realPath = |
| 293 | RootedPath.toRootedPath( |
| 294 | Root.absoluteRoot(path.getFileSystem()), path.resolveSymbolicLinks()); |
| 295 | type = followStat.isFile() ? FileType.SYMLINK_TO_FILE : FileType.SYMLINK_TO_DIRECTORY; |
| 296 | } |
| 297 | return new FileInfo( |
| 298 | type, fsVal != null ? fsVal : noFollowStat.hashCode(), realPath, unresolvedLinkTarget); |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 299 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 300 | } else { |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 301 | // Stat the file. |
| 302 | FileValue fileValue = |
| 303 | (FileValue) env.getValueOrThrow( |
| 304 | FileValue.key(traversal.root.asRootedPath()), IOException.class); |
| 305 | |
| 306 | if (env.valuesMissing()) { |
| 307 | throw new MissingDepException(); |
| 308 | } |
| 309 | if (fileValue.exists()) { |
| 310 | // If it exists, it may either be a symlink or a file/directory. |
| 311 | PathFragment unresolvedLinkTarget = null; |
| 312 | FileType type; |
| 313 | if (fileValue.isSymlink()) { |
| 314 | unresolvedLinkTarget = fileValue.getUnresolvedLinkTarget(); |
| 315 | type = fileValue.isDirectory() ? FileType.SYMLINK_TO_DIRECTORY : FileType.SYMLINK_TO_FILE; |
| 316 | } else { |
| 317 | type = fileValue.isDirectory() ? FileType.DIRECTORY : FileType.FILE; |
| 318 | } |
kush | 4b120e7 | 2018-07-11 16:21:27 -0700 | [diff] [blame] | 319 | return new FileInfo( |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 320 | type, fileValue.realFileStateValue(), fileValue.realRootedPath(), unresolvedLinkTarget); |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 321 | } else { |
| 322 | // If it doesn't exist, or it's a dangling symlink, we still want to handle that gracefully. |
| 323 | return new FileInfo( |
| 324 | fileValue.isSymlink() ? FileType.DANGLING_SYMLINK : FileType.NONEXISTENT, |
kush | 4b120e7 | 2018-07-11 16:21:27 -0700 | [diff] [blame] | 325 | fileValue.realFileStateValue(), |
| 326 | null, |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 327 | fileValue.isSymlink() ? fileValue.getUnresolvedLinkTarget() : null); |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 328 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | private static final class PkgLookupResult { |
| 333 | private enum Type { |
| 334 | CONFLICT, DIRECTORY, PKG |
| 335 | } |
| 336 | |
| 337 | private final Type type; |
| 338 | final TraversalRequest traversal; |
| 339 | final FileInfo rootInfo; |
| 340 | |
| 341 | /** Result for a generated directory that conflicts with a source package. */ |
| 342 | static PkgLookupResult conflict(TraversalRequest traversal, FileInfo rootInfo) { |
| 343 | return new PkgLookupResult(Type.CONFLICT, traversal, rootInfo); |
| 344 | } |
| 345 | |
| 346 | /** Result for a source or generated directory (not a package). */ |
| 347 | static PkgLookupResult directory(TraversalRequest traversal, FileInfo rootInfo) { |
| 348 | return new PkgLookupResult(Type.DIRECTORY, traversal, rootInfo); |
| 349 | } |
| 350 | |
| 351 | /** Result for a package, i.e. a directory with a BUILD file. */ |
| 352 | static PkgLookupResult pkg(TraversalRequest traversal, FileInfo rootInfo) { |
| 353 | return new PkgLookupResult(Type.PKG, traversal, rootInfo); |
| 354 | } |
| 355 | |
| 356 | private PkgLookupResult(Type type, TraversalRequest traversal, FileInfo rootInfo) { |
| 357 | this.type = Preconditions.checkNotNull(type); |
| 358 | this.traversal = Preconditions.checkNotNull(traversal); |
| 359 | this.rootInfo = Preconditions.checkNotNull(rootInfo); |
| 360 | } |
| 361 | |
| 362 | boolean isPackage() { |
| 363 | return type == Type.PKG; |
| 364 | } |
| 365 | |
| 366 | boolean isConflicting() { |
| 367 | return type == Type.CONFLICT; |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public String toString() { |
| 372 | return String.format("(%s: info=%s, traversal=%s)", type, rootInfo, traversal); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Checks whether the {@code traversal}'s path refers to a package directory. |
| 378 | * |
| 379 | * @return the result of the lookup; it contains potentially new {@link TraversalRequest} and |
| 380 | * {@link FileInfo} so the caller should use these instead of the old ones (this happens when |
| 381 | * a package is found, but under a different root than expected) |
| 382 | */ |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 383 | private static PkgLookupResult checkIfPackage( |
| 384 | Environment env, TraversalRequest traversal, FileInfo rootInfo) |
ulfjack | e479453 | 2018-01-12 02:11:17 -0800 | [diff] [blame] | 385 | throws MissingDepException, IOException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 386 | Preconditions.checkArgument(rootInfo.type.exists() && !rootInfo.type.isFile(), |
| 387 | "{%s} {%s}", traversal, rootInfo); |
tomlu | 8cc5dcf | 2018-01-19 09:28:06 -0800 | [diff] [blame] | 388 | PackageLookupValue pkgLookup = |
| 389 | (PackageLookupValue) |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 390 | getDependentSkyValue(env, |
| 391 | PackageLookupValue.key(traversal.root.asRootedPath().getRootRelativePath())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 392 | |
| 393 | if (pkgLookup.packageExists()) { |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 394 | if (traversal.isRootGenerated) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 395 | // The traversal's root was a generated directory, but its root-relative path conflicts with |
| 396 | // an existing package. |
| 397 | return PkgLookupResult.conflict(traversal, rootInfo); |
| 398 | } else { |
| 399 | // The traversal's root was a source directory and it defines a package. |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 400 | Root pkgRoot = pkgLookup.getRoot(); |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 401 | if (!pkgRoot.equals(traversal.root.asRootedPath().getRoot())) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 402 | // However the root of this package is different from what we expected. stat() the real |
| 403 | // BUILD file of that package. |
| 404 | traversal = traversal.forChangedRootPath(pkgRoot); |
| 405 | rootInfo = lookUpFileInfo(env, traversal); |
| 406 | Verify.verify(rootInfo.type.exists(), "{%s} {%s}", traversal, rootInfo); |
| 407 | } |
| 408 | return PkgLookupResult.pkg(traversal, rootInfo); |
| 409 | } |
| 410 | } else { |
| 411 | // The traversal's root was a directory (source or generated one), no package exists under the |
| 412 | // same root-relative path. |
| 413 | return PkgLookupResult.directory(traversal, rootInfo); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * List the directory and create {@code SkyKey}s to request contents of its children recursively. |
| 419 | * |
| 420 | * <p>The returned keys are of type {@link SkyFunctions#RECURSIVE_FILESYSTEM_TRAVERSAL}. |
| 421 | */ |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 422 | private static Collection<SkyKey> createRecursiveTraversalKeys( |
| 423 | Environment env, TraversalRequest traversal) |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 424 | throws MissingDepException, InterruptedException, IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 425 | // Use the traversal's path, even if it's a symlink. The contents of the directory, as listed |
| 426 | // in the result, must be relative to it. |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 427 | Iterable<Dirent> dirents; |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 428 | if (traversal.isRootGenerated) { |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 429 | // If we're dealing with an output file, read the directory directly instead of creating |
| 430 | // filesystem nodes under the output tree. |
| 431 | List<Dirent> direntsCollection = |
| 432 | new ArrayList<>( |
| 433 | traversal.root.asRootedPath().asPath().readdir(Symlinks.FOLLOW)); |
| 434 | Collections.sort(direntsCollection); |
| 435 | dirents = direntsCollection; |
| 436 | } else { |
| 437 | dirents = ((DirectoryListingValue) getDependentSkyValue(env, |
| 438 | DirectoryListingValue.key(traversal.root.asRootedPath()))).getDirents(); |
| 439 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 440 | |
| 441 | List<SkyKey> result = new ArrayList<>(); |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 442 | for (Dirent dirent : dirents) { |
tomlu | 8cc5dcf | 2018-01-19 09:28:06 -0800 | [diff] [blame] | 443 | RootedPath childPath = |
| 444 | RootedPath.toRootedPath( |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 445 | traversal.root.asRootedPath().getRoot(), |
| 446 | traversal.root.asRootedPath().getRootRelativePath().getRelative(dirent.getName())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 447 | TraversalRequest childTraversal = traversal.forChildEntry(childPath); |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 448 | result.add(childTraversal); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 449 | } |
| 450 | return result; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Creates result for a dangling symlink. |
| 455 | * |
| 456 | * @param linkName path to the symbolic link |
| 457 | * @param info the {@link FileInfo} associated with the link file |
| 458 | */ |
| 459 | private static RecursiveFilesystemTraversalValue resultForDanglingSymlink(RootedPath linkName, |
| 460 | FileInfo info) { |
| 461 | Preconditions.checkState(info.type.isSymlink() && !info.type.exists(), "{%s} {%s}", linkName, |
| 462 | info.type); |
| 463 | return RecursiveFilesystemTraversalValue.of( |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 464 | ResolvedFileFactory.danglingSymlink(linkName, info.unresolvedSymlinkTarget, info.metadata)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Creates results for a file or for a symlink that points to one. |
| 469 | * |
| 470 | * <p>A symlink may be direct (points to a file) or transitive (points at a direct or transitive |
| 471 | * symlink). |
| 472 | */ |
| 473 | private static RecursiveFilesystemTraversalValue resultForFileRoot(RootedPath path, |
| 474 | FileInfo info) { |
| 475 | Preconditions.checkState(info.type.isFile() && info.type.exists(), "{%s} {%s}", path, |
| 476 | info.type); |
| 477 | if (info.type.isSymlink()) { |
Laszlo Csomor | b4d482b | 2015-12-04 13:23:54 +0000 | [diff] [blame] | 478 | return RecursiveFilesystemTraversalValue.of( |
| 479 | ResolvedFileFactory.symlinkToFile( |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 480 | info.realPath, path, info.unresolvedSymlinkTarget, info.metadata)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 481 | } else { |
Laszlo Csomor | b4d482b | 2015-12-04 13:23:54 +0000 | [diff] [blame] | 482 | return RecursiveFilesystemTraversalValue.of( |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 483 | ResolvedFileFactory.regularFile(path, info.metadata)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
| 487 | private static RecursiveFilesystemTraversalValue resultForDirectory(TraversalRequest traversal, |
| 488 | FileInfo rootInfo, Collection<RecursiveFilesystemTraversalValue> subdirTraversals) { |
| 489 | // Collect transitive closure of files in subdirectories. |
| 490 | NestedSetBuilder<ResolvedFile> paths = NestedSetBuilder.stableOrder(); |
| 491 | for (RecursiveFilesystemTraversalValue child : subdirTraversals) { |
| 492 | paths.addTransitive(child.getTransitiveFiles()); |
| 493 | } |
| 494 | ResolvedFile root; |
| 495 | if (rootInfo.type.isSymlink()) { |
Laszlo Csomor | 207140f | 2015-12-07 15:07:33 +0000 | [diff] [blame] | 496 | NestedSet<ResolvedFile> children = paths.build(); |
Laszlo Csomor | b4d482b | 2015-12-04 13:23:54 +0000 | [diff] [blame] | 497 | root = |
| 498 | ResolvedFileFactory.symlinkToDirectory( |
| 499 | rootInfo.realPath, |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 500 | traversal.root.asRootedPath(), |
Laszlo Csomor | b4d482b | 2015-12-04 13:23:54 +0000 | [diff] [blame] | 501 | rootInfo.unresolvedSymlinkTarget, |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 502 | hashDirectorySymlink(children, rootInfo.metadata)); |
Laszlo Csomor | 207140f | 2015-12-07 15:07:33 +0000 | [diff] [blame] | 503 | paths = NestedSetBuilder.<ResolvedFile>stableOrder().addTransitive(children).add(root); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 504 | } else { |
Laszlo Csomor | b4d482b | 2015-12-04 13:23:54 +0000 | [diff] [blame] | 505 | root = ResolvedFileFactory.directory(rootInfo.realPath); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 506 | } |
| 507 | return RecursiveFilesystemTraversalValue.of(root, paths.build()); |
| 508 | } |
| 509 | |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 510 | private static int hashDirectorySymlink(Iterable<ResolvedFile> children, Object metadata) { |
Laszlo Csomor | 207140f | 2015-12-07 15:07:33 +0000 | [diff] [blame] | 511 | // If the root is a directory symlink, the associated FileStateValue does not change when the |
| 512 | // linked directory's contents change, so we can't use the FileStateValue as metadata like we |
| 513 | // do with other ResolvedFile kinds. Instead we compute a metadata hash from the child |
| 514 | // elements and return that as the ResolvedFile's metadata hash. |
| 515 | |
| 516 | // Compute the hash using the method described in Effective Java, 2nd ed., Item 9. |
| 517 | int result = 0; |
| 518 | for (ResolvedFile c : children) { |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 519 | result = 31 * result + c.getMetadata().hashCode(); |
Laszlo Csomor | 207140f | 2015-12-07 15:07:33 +0000 | [diff] [blame] | 520 | } |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 521 | return 31 * result + metadata.hashCode(); |
Laszlo Csomor | 207140f | 2015-12-07 15:07:33 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 524 | private static SkyValue getDependentSkyValue(Environment env, SkyKey key) |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 525 | throws MissingDepException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 526 | SkyValue value = env.getValue(key); |
| 527 | if (env.valuesMissing()) { |
| 528 | throw new MissingDepException(); |
| 529 | } |
| 530 | return value; |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Requests Skyframe to compute the dependent values and returns them. |
| 535 | * |
| 536 | * <p>The keys must all be {@link SkyFunctions#RECURSIVE_FILESYSTEM_TRAVERSAL} keys. |
| 537 | */ |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 538 | private Collection<RecursiveFilesystemTraversalValue> traverseChildren( |
| 539 | Environment env, Iterable<SkyKey> keys, boolean inline) |
| 540 | throws MissingDepException, InterruptedException, |
| 541 | RecursiveFilesystemTraversalFunctionException { |
| 542 | Map<SkyKey, SkyValue> values; |
| 543 | if (inline) { |
| 544 | // Don't create Skyframe nodes for a recursive traversal over the output tree. |
| 545 | // Instead, inline the recursion in the top-level request. |
| 546 | values = new HashMap<>(); |
| 547 | for (SkyKey depKey : keys) { |
| 548 | values.put(depKey, compute(depKey, env)); |
| 549 | } |
| 550 | } else { |
| 551 | values = env.getValues(keys); |
| 552 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 553 | if (env.valuesMissing()) { |
| 554 | throw new MissingDepException(); |
| 555 | } |
felly | 5be4dd6 | 2018-02-05 11:11:53 -0800 | [diff] [blame] | 556 | |
kush | b39c693 | 2018-07-12 21:25:23 -0700 | [diff] [blame] | 557 | return Collections2.transform(values.values(), RecursiveFilesystemTraversalValue.class::cast); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /** Type information about the filesystem entry residing at a path. */ |
| 561 | enum FileType { |
| 562 | /** A regular file. */ |
| 563 | FILE { |
| 564 | @Override boolean isFile() { return true; } |
| 565 | @Override boolean exists() { return true; } |
| 566 | @Override public String toString() { return "<f>"; } |
| 567 | }, |
| 568 | /** |
| 569 | * A symlink to a regular file. |
| 570 | * |
| 571 | * <p>The symlink may be direct (points to a non-symlink (here a file)) or it may be transitive |
| 572 | * (points to a direct or transitive symlink). |
| 573 | */ |
| 574 | SYMLINK_TO_FILE { |
| 575 | @Override boolean isFile() { return true; } |
| 576 | @Override boolean isSymlink() { return true; } |
| 577 | @Override boolean exists() { return true; } |
| 578 | @Override public String toString() { return "<lf>"; } |
| 579 | }, |
| 580 | /** A directory. */ |
| 581 | DIRECTORY { |
| 582 | @Override boolean isDirectory() { return true; } |
| 583 | @Override boolean exists() { return true; } |
| 584 | @Override public String toString() { return "<d>"; } |
| 585 | }, |
| 586 | /** |
| 587 | * A symlink to a directory. |
| 588 | * |
| 589 | * <p>The symlink may be direct (points to a non-symlink (here a directory)) or it may be |
| 590 | * transitive (points to a direct or transitive symlink). |
| 591 | */ |
| 592 | SYMLINK_TO_DIRECTORY { |
| 593 | @Override boolean isDirectory() { return true; } |
| 594 | @Override boolean isSymlink() { return true; } |
| 595 | @Override boolean exists() { return true; } |
| 596 | @Override public String toString() { return "<ld>"; } |
| 597 | }, |
| 598 | /** A dangling symlink, i.e. one whose target is known not to exist. */ |
| 599 | DANGLING_SYMLINK { |
| 600 | @Override boolean isFile() { throw new UnsupportedOperationException(); } |
| 601 | @Override boolean isDirectory() { throw new UnsupportedOperationException(); } |
| 602 | @Override boolean isSymlink() { return true; } |
| 603 | @Override public String toString() { return "<l?>"; } |
| 604 | }, |
| 605 | /** A path that does not exist or should be ignored. */ |
| 606 | NONEXISTENT { |
| 607 | @Override public String toString() { return "<?>"; } |
| 608 | }; |
| 609 | |
| 610 | boolean isFile() { return false; } |
| 611 | boolean isDirectory() { return false; } |
| 612 | boolean isSymlink() { return false; } |
| 613 | boolean exists() { return false; } |
| 614 | @Override public abstract String toString(); |
| 615 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 616 | } |