blob: 990da32134da97cee8531dfbc062b2b418d0ae3f [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.
14
15package com.google.devtools.build.lib.exec;
16
17import com.google.common.collect.Iterables;
kush8ae7a3b2018-06-29 15:43:11 -070018import com.google.devtools.build.lib.actions.AbstractAction;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019import com.google.devtools.build.lib.actions.ActionExecutionContext;
20import com.google.devtools.build.lib.actions.EnvironmentalExecException;
Benjamin Peterson757e4812019-03-21 03:16:51 -070021import com.google.devtools.build.lib.actions.RunningActionEvent;
ulfjacka0da4342019-04-05 04:26:31 -070022import com.google.devtools.build.lib.actions.SpawnContinuation;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023import com.google.devtools.build.lib.analysis.actions.AbstractFileWriteAction;
jcater3751ba92020-04-06 07:04:30 -070024import com.google.devtools.build.lib.analysis.actions.DeterministicWriter;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010025import com.google.devtools.build.lib.analysis.actions.FileWriteActionContext;
Nathan Harmata43e22a82015-09-08 21:21:04 +000026import com.google.devtools.build.lib.profiler.AutoProfiler;
janakr30e99152020-04-03 08:01:25 -070027import com.google.devtools.build.lib.profiler.GoogleAutoProfilerUtils;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028import com.google.devtools.build.lib.vfs.Path;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029import java.io.BufferedOutputStream;
30import java.io.IOException;
31import java.io.OutputStream;
janakr30e99152020-04-03 08:01:25 -070032import java.time.Duration;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010033
34/**
35 * A strategy for executing an {@link AbstractFileWriteAction}.
36 */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010037public final class FileWriteStrategy implements FileWriteActionContext {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010038 public static final Class<FileWriteStrategy> TYPE = FileWriteStrategy.class;
janakr30e99152020-04-03 08:01:25 -070039 private static final Duration MIN_LOGGING = Duration.ofMillis(100);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041 @Override
ulfjacka0da4342019-04-05 04:26:31 -070042 public SpawnContinuation beginWriteOutputToFile(
kush8ae7a3b2018-06-29 15:43:11 -070043 AbstractAction action,
44 ActionExecutionContext actionExecutionContext,
45 DeterministicWriter deterministicWriter,
ulfjacka0da4342019-04-05 04:26:31 -070046 boolean makeExecutable,
ulfjack415165a2019-09-12 07:56:40 -070047 boolean isRemotable) {
jmmv716e19c2019-04-01 07:59:54 -070048 actionExecutionContext.getEventHandler().post(new RunningActionEvent(action, "local"));
Ulf Adamscddaaa62017-03-02 17:32:28 +000049 // TODO(ulfjack): Consider acquiring local resources here before trying to write the file.
Janak Ramakrishnand41017b2016-01-30 01:21:57 +000050 try (AutoProfiler p =
janakr30e99152020-04-03 08:01:25 -070051 GoogleAutoProfilerUtils.logged(
52 "running write for action " + action.prettyPrint(), MIN_LOGGING)) {
Benjamin Peterson757e4812019-03-21 03:16:51 -070053 Path outputPath =
54 actionExecutionContext.getInputPath(Iterables.getOnlyElement(action.getOutputs()));
Nathan Harmata43e22a82015-09-08 21:21:04 +000055 try {
Nathan Harmata43e22a82015-09-08 21:21:04 +000056 try (OutputStream out = new BufferedOutputStream(outputPath.getOutputStream())) {
kush8ae7a3b2018-06-29 15:43:11 -070057 deterministicWriter.writeOutputFile(out);
Nathan Harmata43e22a82015-09-08 21:21:04 +000058 }
kush8ae7a3b2018-06-29 15:43:11 -070059 if (makeExecutable) {
Nathan Harmata43e22a82015-09-08 21:21:04 +000060 outputPath.setExecutable(true);
61 }
62 } catch (IOException e) {
ulfjack415165a2019-09-12 07:56:40 -070063 return SpawnContinuation.failedWithExecException(new EnvironmentalExecException(e));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010064 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010065 }
ulfjacka0da4342019-04-05 04:26:31 -070066 return SpawnContinuation.immediate();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010067 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010068}