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 | |
| 16 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
| 17 | import com.google.devtools.build.lib.vfs.RootedPath; |
| 18 | import com.google.devtools.build.skyframe.SkyFunction; |
| 19 | import com.google.devtools.build.skyframe.SkyFunctionException; |
| 20 | import com.google.devtools.build.skyframe.SkyKey; |
| 21 | import com.google.devtools.build.skyframe.SkyValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import java.io.IOException; |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 23 | import java.util.concurrent.atomic.AtomicReference; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 | */ |
| 31 | public class FileStateFunction implements SkyFunction { |
| 32 | |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 33 | private final AtomicReference<TimestampGranularityMonitor> tsgm; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 34 | private final ExternalFilesHelper externalFilesHelper; |
| 35 | |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 36 | public FileStateFunction(AtomicReference<TimestampGranularityMonitor> tsgm, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | ExternalFilesHelper externalFilesHelper) { |
| 38 | this.tsgm = tsgm; |
| 39 | this.externalFilesHelper = externalFilesHelper; |
| 40 | } |
| 41 | |
| 42 | @Override |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 43 | public SkyValue compute(SkyKey skyKey, Environment env) |
| 44 | throws FileStateFunctionException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | RootedPath rootedPath = (RootedPath) skyKey.argument(); |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 46 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | try { |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 48 | externalFilesHelper.maybeHandleExternalFile(rootedPath, env); |
| 49 | if (env.valuesMissing()) { |
| 50 | return null; |
| 51 | } |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 52 | return FileStateValue.create(rootedPath, tsgm.get()); |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 53 | } catch (ExternalFilesHelper.NonexistentImmutableExternalFileException e) { |
| 54 | return FileStateValue.NONEXISTENT_FILE_STATE_NODE; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | } catch (IOException e) { |
| 56 | throw new FileStateFunctionException(e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 57 | } |
| 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | } |
| 74 | } |