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; |
| 22 | |
| 23 | import 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 | */ |
| 31 | public 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 Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 45 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | try { |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 47 | externalFilesHelper.maybeHandleExternalFile(rootedPath, env); |
| 48 | if (env.valuesMissing()) { |
| 49 | return null; |
| 50 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 51 | return FileStateValue.create(rootedPath, tsgm); |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 52 | } catch (FileOutsidePackageRootsException e) { |
| 53 | throw new FileStateFunctionException(e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | } 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 Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 78 | |
| 79 | public FileStateFunctionException(FileOutsidePackageRootsException e) { |
| 80 | super(e, Transience.PERSISTENT); |
| 81 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | } |
| 83 | } |