blob: 52876f2264af4fa6b14f2555f2eef4bad761ce3d [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
16import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor;
17import com.google.devtools.build.lib.vfs.RootedPath;
18import com.google.devtools.build.skyframe.SkyFunction;
19import com.google.devtools.build.skyframe.SkyFunctionException;
20import com.google.devtools.build.skyframe.SkyKey;
21import com.google.devtools.build.skyframe.SkyValue;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010022import java.io.IOException;
Ulf Adamsc73051c62016-03-23 09:18:13 +000023import java.util.concurrent.atomic.AtomicReference;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024
25/**
26 * A {@link SkyFunction} for {@link FileStateValue}s.
27 *
28 * <p>Merely calls FileStateValue#create, but also has special handling for files outside the
29 * package roots (see {@link ExternalFilesHelper}).
30 */
31public class FileStateFunction implements SkyFunction {
32
Ulf Adamsc73051c62016-03-23 09:18:13 +000033 private final AtomicReference<TimestampGranularityMonitor> tsgm;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010034 private final ExternalFilesHelper externalFilesHelper;
35
Ulf Adamsc73051c62016-03-23 09:18:13 +000036 public FileStateFunction(AtomicReference<TimestampGranularityMonitor> tsgm,
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010037 ExternalFilesHelper externalFilesHelper) {
38 this.tsgm = tsgm;
39 this.externalFilesHelper = externalFilesHelper;
40 }
41
42 @Override
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000043 public SkyValue compute(SkyKey skyKey, Environment env)
44 throws FileStateFunctionException, InterruptedException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010045 RootedPath rootedPath = (RootedPath) skyKey.argument();
Michajlo Matijkiwdb110942015-03-31 23:41:02 +000046
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010047 try {
Michajlo Matijkiwdb110942015-03-31 23:41:02 +000048 externalFilesHelper.maybeHandleExternalFile(rootedPath, env);
49 if (env.valuesMissing()) {
50 return null;
51 }
Ulf Adamsc73051c62016-03-23 09:18:13 +000052 return FileStateValue.create(rootedPath, tsgm.get());
Nathan Harmatad4f75942016-10-18 08:55:17 +000053 } catch (ExternalFilesHelper.NonexistentImmutableExternalFileException e) {
54 return FileStateValue.NONEXISTENT_FILE_STATE_NODE;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010055 } catch (IOException e) {
56 throw new FileStateFunctionException(e);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010057 }
58 }
59
60 @Override
61 public String extractTag(SkyKey skyKey) {
62 return null;
63 }
64
65 /**
66 * Used to declare all the exception types that can be wrapped in the exception thrown by
67 * {@link FileStateFunction#compute}.
68 */
69 private static final class FileStateFunctionException extends SkyFunctionException {
70 public FileStateFunctionException(IOException e) {
71 super(e, Transience.TRANSIENT);
72 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010073 }
74}