blob: 3e1033075cd2e057dd4fdc9e0820c9f84d01a6b8 [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;
22
23import java.io.IOException;
24
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
33 private final TimestampGranularityMonitor tsgm;
34 private final ExternalFilesHelper externalFilesHelper;
35
36 public FileStateFunction(TimestampGranularityMonitor tsgm,
37 ExternalFilesHelper externalFilesHelper) {
38 this.tsgm = tsgm;
39 this.externalFilesHelper = externalFilesHelper;
40 }
41
42 @Override
43 public SkyValue compute(SkyKey skyKey, Environment env) throws FileStateFunctionException {
44 RootedPath rootedPath = (RootedPath) skyKey.argument();
Michajlo Matijkiwdb110942015-03-31 23:41:02 +000045
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010046 try {
Michajlo Matijkiwdb110942015-03-31 23:41:02 +000047 externalFilesHelper.maybeHandleExternalFile(rootedPath, env);
48 if (env.valuesMissing()) {
49 return null;
50 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010051 return FileStateValue.create(rootedPath, tsgm);
Michajlo Matijkiwdb110942015-03-31 23:41:02 +000052 } catch (FileOutsidePackageRootsException e) {
53 throw new FileStateFunctionException(e);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010054 } catch (IOException e) {
55 throw new FileStateFunctionException(e);
56 } catch (InconsistentFilesystemException e) {
57 throw new FileStateFunctionException(e);
58 }
59 }
60
61 @Override
62 public String extractTag(SkyKey skyKey) {
63 return null;
64 }
65
66 /**
67 * Used to declare all the exception types that can be wrapped in the exception thrown by
68 * {@link FileStateFunction#compute}.
69 */
70 private static final class FileStateFunctionException extends SkyFunctionException {
71 public FileStateFunctionException(IOException e) {
72 super(e, Transience.TRANSIENT);
73 }
74
75 public FileStateFunctionException(InconsistentFilesystemException e) {
76 super(e, Transience.TRANSIENT);
77 }
Michajlo Matijkiwdb110942015-03-31 23:41:02 +000078
79 public FileStateFunctionException(FileOutsidePackageRootsException e) {
80 super(e, Transience.PERSISTENT);
81 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010082 }
83}