1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef INCLUDED_SRC_BUILDTOOL_EXECUTION_API_LOCAL_LOCAL_RESPONSE_HPP
#define INCLUDED_SRC_BUILDTOOL_EXECUTION_API_LOCAL_LOCAL_RESPONSE_HPP
#include <cstddef>
#include <string>
#include <utility>
#include "gsl/gsl"
#include "src/buildtool/crypto/hash_function.hpp"
#include "src/buildtool/execution_api/common/execution_response.hpp"
#include "src/buildtool/execution_api/local/local_action.hpp"
#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/buildtool/storage/storage.hpp"
/// \brief Response of a LocalAction.
class LocalResponse final : public IExecutionResponse {
friend class LocalAction;
public:
auto Status() const noexcept -> StatusCode final {
return StatusCode::Success; // unused
}
auto HasStdErr() const noexcept -> bool final {
return (output_.action.stderr_digest().size_bytes() != 0);
}
auto HasStdOut() const noexcept -> bool final {
return (output_.action.stdout_digest().size_bytes() != 0);
}
auto StdErr() noexcept -> std::string final {
if (auto path = storage_.CAS().BlobPath(
static_cast<ArtifactDigest>(output_.action.stderr_digest()),
/*is_executable=*/false)) {
if (auto content = FileSystemManager::ReadFile(*path)) {
return std::move(*content);
}
}
Logger::Log(LogLevel::Debug, "reading stderr failed");
return {};
}
auto StdOut() noexcept -> std::string final {
if (auto path = storage_.CAS().BlobPath(
static_cast<ArtifactDigest>(output_.action.stdout_digest()),
/*is_executable=*/false)) {
if (auto content = FileSystemManager::ReadFile(*path)) {
return std::move(*content);
}
}
Logger::Log(LogLevel::Debug, "reading stdout failed");
return {};
}
auto ExitCode() const noexcept -> int final {
return output_.action.exit_code();
}
auto IsCached() const noexcept -> bool final { return output_.is_cached; };
auto ActionDigest() const noexcept -> std::string const& final {
return action_id_;
}
auto Artifacts() noexcept -> ArtifactInfos const& final {
Populate();
return artifacts_;
}
auto DirectorySymlinks() noexcept -> DirSymlinks const& final {
Populate();
return dir_symlinks_;
}
private:
std::string action_id_{};
LocalAction::Output output_{};
Storage const& storage_;
ArtifactInfos artifacts_;
DirSymlinks dir_symlinks_;
bool populated_ = false;
explicit LocalResponse(
std::string action_id,
LocalAction::Output output,
gsl::not_null<Storage const*> const& storage) noexcept
: action_id_{std::move(action_id)},
output_{std::move(output)},
storage_{*storage} {}
void Populate() noexcept {
// Initialized only once lazily
if (populated_) {
return;
}
populated_ = true;
ArtifactInfos artifacts{};
auto const& action_result = output_.action;
artifacts.reserve(
static_cast<std::size_t>(action_result.output_files_size()) +
static_cast<std::size_t>(
action_result.output_file_symlinks_size()) +
static_cast<std::size_t>(
action_result.output_directory_symlinks_size()) +
static_cast<std::size_t>(action_result.output_directories_size()));
DirSymlinks dir_symlinks{};
dir_symlinks.reserve(static_cast<std::size_t>(
action_result.output_directory_symlinks_size()));
// collect files and store them
for (auto const& file : action_result.output_files()) {
try {
artifacts.emplace(
file.path(),
Artifact::ObjectInfo{
.digest = ArtifactDigest{file.digest()},
.type = file.is_executable() ? ObjectType::Executable
: ObjectType::File});
} catch (...) {
return;
}
}
// collect all symlinks and store them
for (auto const& link : action_result.output_file_symlinks()) {
try {
artifacts.emplace(
link.path(),
Artifact::ObjectInfo{
.digest = ArtifactDigest::Create<ObjectType::File>(
storage_.GetHashFunction(), link.target()),
.type = ObjectType::Symlink});
} catch (...) {
return;
}
}
for (auto const& link : action_result.output_directory_symlinks()) {
try {
artifacts.emplace(
link.path(),
Artifact::ObjectInfo{
.digest = ArtifactDigest::Create<ObjectType::File>(
storage_.GetHashFunction(), link.target()),
.type = ObjectType::Symlink});
dir_symlinks.emplace(link.path()); // add it to set
} catch (...) {
return;
}
}
// collect directories and store them
for (auto const& dir : action_result.output_directories()) {
try {
artifacts.emplace(
dir.path(),
Artifact::ObjectInfo{
.digest = ArtifactDigest{dir.tree_digest()},
.type = ObjectType::Tree});
} catch (...) {
return;
}
}
artifacts_ = std::move(artifacts);
dir_symlinks_ = std::move(dir_symlinks);
}
};
#endif // INCLUDED_SRC_BUILDTOOL_EXECUTION_API_LOCAL_LOCAL_RESPONSE_HPP
|