blob: db26f144d58b262586448eb71e6357f33aaa41c4 [file] [log] [blame]
buchgr559a07d2017-11-30 11:09:35 -08001// 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.
14package com.google.devtools.build.lib.runtime;
15
ccalvarin78142a62018-08-01 19:25:22 -070016import com.google.common.base.Preconditions;
buchgr559a07d2017-11-30 11:09:35 -080017import com.google.devtools.build.lib.unix.UnixFileSystem;
18import com.google.devtools.build.lib.util.AbruptExitException;
19import com.google.devtools.build.lib.util.ExitCode;
20import com.google.devtools.build.lib.util.OS;
ccalvarinbda12a12018-06-21 18:57:26 -070021import com.google.devtools.build.lib.vfs.DigestHashFunction;
ccalvarin78142a62018-08-01 19:25:22 -070022import com.google.devtools.build.lib.vfs.DigestHashFunction.DefaultAlreadySetException;
ccalvarinbda12a12018-06-21 18:57:26 -070023import com.google.devtools.build.lib.vfs.DigestHashFunction.DigestFunctionConverter;
buchgr559a07d2017-11-30 11:09:35 -080024import com.google.devtools.build.lib.vfs.FileSystem;
buchgr559a07d2017-11-30 11:09:35 -080025import com.google.devtools.build.lib.vfs.JavaIoFileSystem;
26import com.google.devtools.build.lib.windows.WindowsFileSystem;
27import com.google.devtools.common.options.OptionsParsingException;
juliexxiae91a4502018-08-15 14:42:29 -070028import com.google.devtools.common.options.OptionsParsingResult;
buchgr559a07d2017-11-30 11:09:35 -080029
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 */
36public class BazelFileSystemModule extends BlazeModule {
37
38 @Override
juliexxiae91a4502018-08-15 14:42:29 -070039 public void globalInit(OptionsParsingResult startupOptionsProvider) throws AbruptExitException {
ccalvarin78142a62018-08-01 19:25:22 -070040 BlazeServerStartupOptions startupOptions =
41 Preconditions.checkNotNull(
42 startupOptionsProvider.getOptions(BlazeServerStartupOptions.class));
43 DigestHashFunction commandLineHashFunction = startupOptions.digestHashFunction;
buchgr559a07d2017-11-30 11:09:35 -080044 try {
ccalvarin78142a62018-08-01 19:25:22 -070045 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);
buchgr559a07d2017-11-30 11:09:35 -080059 }
ccalvarin78142a62018-08-01 19:25:22 -070060 }
61
62 @Override
juliexxiae91a4502018-08-15 14:42:29 -070063 public FileSystem getFileSystem(OptionsParsingResult startupOptions) {
buchgr559a07d2017-11-30 11:09:35 -080064 if ("0".equals(System.getProperty("io.bazel.EnableJni"))) {
65 // Ignore UnixFileSystem, to be used for bootstrapping.
ccalvarin78142a62018-08-01 19:25:22 -070066 return OS.getCurrent() == OS.WINDOWS ? new WindowsFileSystem() : new JavaIoFileSystem();
buchgr559a07d2017-11-30 11:09:35 -080067 }
68 // The JNI-based UnixFileSystem is faster, but on Windows it is not available.
ccalvarin78142a62018-08-01 19:25:22 -070069 return OS.getCurrent() == OS.WINDOWS ? new WindowsFileSystem() : new UnixFileSystem();
buchgr559a07d2017-11-30 11:09:35 -080070 }
71}