buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 1 | // Copyright 2017 The Bazel Authors. All rights reserved. |
| 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.runtime; |
| 15 | |
ccalvarin | 78142a6 | 2018-08-01 19:25:22 -0700 | [diff] [blame] | 16 | import com.google.common.base.Preconditions; |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 17 | import com.google.devtools.build.lib.unix.UnixFileSystem; |
| 18 | import com.google.devtools.build.lib.util.AbruptExitException; |
| 19 | import com.google.devtools.build.lib.util.ExitCode; |
| 20 | import com.google.devtools.build.lib.util.OS; |
ccalvarin | bda12a1 | 2018-06-21 18:57:26 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
ccalvarin | 78142a6 | 2018-08-01 19:25:22 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.vfs.DigestHashFunction.DefaultAlreadySetException; |
ccalvarin | bda12a1 | 2018-06-21 18:57:26 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.vfs.DigestHashFunction.DigestFunctionConverter; |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.vfs.FileSystem; |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 25 | import com.google.devtools.build.lib.vfs.JavaIoFileSystem; |
| 26 | import com.google.devtools.build.lib.windows.WindowsFileSystem; |
| 27 | import com.google.devtools.common.options.OptionsParsingException; |
juliexxia | e91a450 | 2018-08-15 14:42:29 -0700 | [diff] [blame] | 28 | import com.google.devtools.common.options.OptionsParsingResult; |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Module to provide a {@link FileSystem} instance that uses {@code SHA256} as the default hash |
| 32 | * function, or else what's specified by {@code -Dbazel.DigestFunction}. |
| 33 | * |
| 34 | * <p>For legacy reasons we can't make the {@link FileSystem} class use {@code SHA256} by default. |
| 35 | */ |
| 36 | public class BazelFileSystemModule extends BlazeModule { |
| 37 | |
| 38 | @Override |
juliexxia | e91a450 | 2018-08-15 14:42:29 -0700 | [diff] [blame] | 39 | public void globalInit(OptionsParsingResult startupOptionsProvider) throws AbruptExitException { |
ccalvarin | 78142a6 | 2018-08-01 19:25:22 -0700 | [diff] [blame] | 40 | BlazeServerStartupOptions startupOptions = |
| 41 | Preconditions.checkNotNull( |
| 42 | startupOptionsProvider.getOptions(BlazeServerStartupOptions.class)); |
| 43 | DigestHashFunction commandLineHashFunction = startupOptions.digestHashFunction; |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 44 | try { |
ccalvarin | 78142a6 | 2018-08-01 19:25:22 -0700 | [diff] [blame] | 45 | if (commandLineHashFunction != null) { |
| 46 | DigestHashFunction.setDefault(commandLineHashFunction); |
| 47 | } else { |
| 48 | String value = System.getProperty("bazel.DigestFunction", "SHA256"); |
| 49 | DigestHashFunction jvmPropertyHash; |
| 50 | try { |
| 51 | jvmPropertyHash = new DigestFunctionConverter().convert(value); |
| 52 | } catch (OptionsParsingException e) { |
| 53 | throw new AbruptExitException(ExitCode.COMMAND_LINE_ERROR, e); |
| 54 | } |
| 55 | DigestHashFunction.setDefault(jvmPropertyHash); |
| 56 | } |
| 57 | } catch (DefaultAlreadySetException e) { |
| 58 | throw new AbruptExitException(ExitCode.BLAZE_INTERNAL_ERROR, e); |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 59 | } |
ccalvarin | 78142a6 | 2018-08-01 19:25:22 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
juliexxia | e91a450 | 2018-08-15 14:42:29 -0700 | [diff] [blame] | 63 | public FileSystem getFileSystem(OptionsParsingResult startupOptions) { |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 64 | if ("0".equals(System.getProperty("io.bazel.EnableJni"))) { |
| 65 | // Ignore UnixFileSystem, to be used for bootstrapping. |
ccalvarin | 78142a6 | 2018-08-01 19:25:22 -0700 | [diff] [blame] | 66 | return OS.getCurrent() == OS.WINDOWS ? new WindowsFileSystem() : new JavaIoFileSystem(); |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 67 | } |
| 68 | // The JNI-based UnixFileSystem is faster, but on Windows it is not available. |
ccalvarin | 78142a6 | 2018-08-01 19:25:22 -0700 | [diff] [blame] | 69 | return OS.getCurrent() == OS.WINDOWS ? new WindowsFileSystem() : new UnixFileSystem(); |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 70 | } |
| 71 | } |