blob: 699753c56680b6c96dafc8d01c47584bc34810a3 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.
14package com.google.devtools.build.lib.skyframe;
15
shahan602cc852018-06-06 20:09:57 -070016import com.google.devtools.build.lib.actions.FileStateValue;
janakrb8bc2842021-12-08 12:48:02 -080017import com.google.devtools.build.lib.io.InconsistentFilesystemException;
Googlerba4862d2019-05-03 06:13:23 -070018import com.google.devtools.build.lib.skyframe.ExternalFilesHelper.FileType;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor;
20import com.google.devtools.build.lib.vfs.RootedPath;
janakrfc1d79c2022-01-27 13:02:07 -080021import com.google.devtools.build.lib.vfs.SyscallCache;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010022import com.google.devtools.build.skyframe.SkyFunction;
23import com.google.devtools.build.skyframe.SkyFunctionException;
24import com.google.devtools.build.skyframe.SkyKey;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010025import java.io.IOException;
janakrfc1d79c2022-01-27 13:02:07 -080026import java.util.function.Supplier;
Googler5cc598c2022-07-06 03:29:45 -070027import javax.annotation.Nullable;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028
29/**
30 * A {@link SkyFunction} for {@link FileStateValue}s.
31 *
32 * <p>Merely calls FileStateValue#create, but also has special handling for files outside the
33 * package roots (see {@link ExternalFilesHelper}).
34 */
35public class FileStateFunction implements SkyFunction {
36
janakrfc1d79c2022-01-27 13:02:07 -080037 private final Supplier<TimestampGranularityMonitor> tsgm;
janakrb2a94342022-02-05 22:02:14 -080038 private final SyscallCache syscallCache;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010039 private final ExternalFilesHelper externalFilesHelper;
40
djasper7faa0ef2019-03-28 10:00:00 -070041 public FileStateFunction(
janakrfc1d79c2022-01-27 13:02:07 -080042 Supplier<TimestampGranularityMonitor> tsgm,
janakrb2a94342022-02-05 22:02:14 -080043 SyscallCache syscallCache,
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010044 ExternalFilesHelper externalFilesHelper) {
45 this.tsgm = tsgm;
djasper7faa0ef2019-03-28 10:00:00 -070046 this.syscallCache = syscallCache;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010047 this.externalFilesHelper = externalFilesHelper;
48 }
49
janakrb8bc2842021-12-08 12:48:02 -080050 // InconsistentFilesystemException catch block needs to be separate from IOException catch block
51 // below because Java does "single dispatch": the runtime type of e is all that is considered when
52 // deciding which overload of FileStateFunctionException() to call.
53 @SuppressWarnings("UseMultiCatch")
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010054 @Override
Googler5cc598c2022-07-06 03:29:45 -070055 @Nullable
Googlerda3c0092018-10-11 14:48:17 -070056 public FileStateValue compute(SkyKey skyKey, Environment env)
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000057 throws FileStateFunctionException, InterruptedException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010058 RootedPath rootedPath = (RootedPath) skyKey.argument();
Michajlo Matijkiwdb110942015-03-31 23:41:02 +000059
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010060 try {
wyv9732d232021-05-20 02:57:24 -070061 FileType fileType = externalFilesHelper.maybeHandleExternalFile(rootedPath, env);
Michajlo Matijkiwdb110942015-03-31 23:41:02 +000062 if (env.valuesMissing()) {
63 return null;
64 }
Googlercbf81592022-06-21 05:00:30 -070065 if (fileType == FileType.EXTERNAL_REPO) {
Googlerba4862d2019-05-03 06:13:23 -070066 // do not use syscallCache as files under repositories get generated during the build
janakrd1b2f962022-03-04 13:33:44 -080067 return FileStateValue.create(rootedPath, SyscallCache.NO_CACHE, tsgm.get());
Googlerba4862d2019-05-03 06:13:23 -070068 }
janakrb2a94342022-02-05 22:02:14 -080069 return FileStateValue.create(rootedPath, syscallCache, tsgm.get());
Nathan Harmatad4f75942016-10-18 08:55:17 +000070 } catch (ExternalFilesHelper.NonexistentImmutableExternalFileException e) {
71 return FileStateValue.NONEXISTENT_FILE_STATE_NODE;
janakrb8bc2842021-12-08 12:48:02 -080072 } catch (InconsistentFilesystemException e) {
73 throw new FileStateFunctionException(e);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010074 } catch (IOException e) {
75 throw new FileStateFunctionException(e);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010076 }
77 }
78
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010079 /**
janakrc64db332021-11-04 09:15:18 -070080 * Used to declare all the exception types that can be wrapped in the exception thrown by {@link
81 * FileStateFunction#compute}.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010082 */
janakrc64db332021-11-04 09:15:18 -070083 public static final class FileStateFunctionException extends SkyFunctionException {
janakrb8bc2842021-12-08 12:48:02 -080084 private final boolean isCatastrophic;
85
86 private FileStateFunctionException(InconsistentFilesystemException e) {
87 super(e, Transience.TRANSIENT);
88 this.isCatastrophic = true;
89 }
90
janakrc64db332021-11-04 09:15:18 -070091 private FileStateFunctionException(IOException e) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010092 super(e, Transience.TRANSIENT);
janakrb8bc2842021-12-08 12:48:02 -080093 this.isCatastrophic = false;
94 }
95
96 @Override
97 public boolean isCatastrophic() {
98 return isCatastrophic;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010099 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100100 }
101}