summaryrefslogtreecommitdiff
path: root/src/buildtool/execution_api/git/git_api.hpp
blob: e3d247431c79d1dab8ce313e8e4e47ee074f10fe (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2023 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_GIT_GIT_API_HPP
#define INCLUDED_SRC_BUILDTOOL_EXECUTION_API_GIT_GIT_API_HPP

#include <cstdio>

#include "gsl/gsl"
#include "src/buildtool/common/repository_config.hpp"
#include "src/buildtool/execution_api/bazel_msg/bazel_msg_factory.hpp"
#include "src/buildtool/execution_api/common/execution_api.hpp"
#include "src/buildtool/logging/logger.hpp"

/// \brief API for local execution.
class GitApi final : public IExecutionApi {
  public:
    auto CreateAction(
        ArtifactDigest const& /*root_digest*/,
        std::vector<std::string> const& /*command*/,
        std::vector<std::string> const& /*output_files*/,
        std::vector<std::string> const& /*output_dirs*/,
        std::map<std::string, std::string> const& /*env_vars*/,
        std::map<std::string, std::string> const& /*properties*/) noexcept
        -> IExecutionAction::Ptr final {
        // Execution not supported from git cas
        return nullptr;
    }

    // NOLINTNEXTLINE(misc-no-recursion,google-default-arguments)
    [[nodiscard]] auto RetrieveToPaths(
        std::vector<Artifact::ObjectInfo> const& artifacts_info,
        std::vector<std::filesystem::path> const& output_paths,
        IExecutionApi* /*alternative*/ = nullptr) noexcept -> bool override {
        if (artifacts_info.size() != output_paths.size()) {
            Logger::Log(LogLevel::Error,
                        "different number of digests and output paths.");
            return false;
        }
        for (std::size_t i{}; i < artifacts_info.size(); ++i) {
            auto const& info = artifacts_info[i];
            if (IsTreeObject(info.type)) {
                auto tree = RepositoryConfig::Instance().ReadTreeFromGitCAS(
                    info.digest.hash());
                if (not tree) {
                    return false;
                }
                for (auto const& [path, entry] : *tree) {
                    if (not RetrieveToPaths(
                            {Artifact::ObjectInfo{
                                .digest = ArtifactDigest{entry->Hash(),
                                                         /*size*/ 0,
                                                         entry->IsTree()},
                                .type = entry->Type(),
                                .failed = false}},
                            {output_paths[i] / path})) {
                        return false;
                    }
                }
            }
            else {
                auto blob = RepositoryConfig::Instance().ReadBlobFromGitCAS(
                    info.digest.hash());
                if (not blob) {
                    return false;
                }
                if (not FileSystemManager::CreateDirectory(
                        output_paths[i].parent_path()) or
                    not FileSystemManager::WriteFileAs</*kSetEpochTime=*/true,
                                                       /*kSetWritable=*/true>(
                        *blob, output_paths[i], info.type)) {
                    Logger::Log(LogLevel::Error,
                                "staging to output path {} failed.",
                                output_paths[i].string());
                    return false;
                }
            }
        }
        return true;
    }

    [[nodiscard]] auto RetrieveToFds(
        std::vector<Artifact::ObjectInfo> const& artifacts_info,
        std::vector<int> const& fds,
        bool raw_tree) noexcept -> bool override {
        if (artifacts_info.size() != fds.size()) {
            Logger::Log(LogLevel::Error,
                        "different number of digests and file descriptors.");
            return false;
        }
        for (std::size_t i{}; i < artifacts_info.size(); ++i) {
            auto fd = fds[i];
            auto const& info = artifacts_info[i];
            if (IsTreeObject(info.type) and not raw_tree) {
                auto tree = RepositoryConfig::Instance().ReadTreeFromGitCAS(
                    info.digest.hash());
                if (not tree) {
                    Logger::Log(LogLevel::Debug,
                                "Tree {} not known to git",
                                info.digest.hash());
                    return false;
                }
                auto json = nlohmann::json::object();
                for (auto const& [path, entry] : *tree) {
                    json[path] =
                        Artifact::ObjectInfo{
                            .digest = ArtifactDigest{entry->Hash(),
                                                     /*size*/ 0,
                                                     entry->IsTree()},
                            .type = entry->Type(),
                            .failed = false}
                            .ToString(/*size_unknown*/ true);
                }
                auto msg = json.dump(2) + "\n";
                if (gsl::owner<FILE*> out = fdopen(fd, "wb")) {  // NOLINT
                    std::fwrite(msg.data(), 1, msg.size(), out);
                    std::fclose(out);
                }
                else {
                    Logger::Log(LogLevel::Error,
                                "dumping to file descriptor {} failed.",
                                fd);
                    return false;
                }
            }
            else {
                auto blob = RepositoryConfig::Instance().ReadBlobFromGitCAS(
                    info.digest.hash());
                if (not blob) {
                    Logger::Log(LogLevel::Debug,
                                "Blob {} not known to git",
                                info.digest.hash());
                    return false;
                }
                auto msg = *blob;
                if (gsl::owner<FILE*> out = fdopen(fd, "wb")) {  // NOLINT
                    std::fwrite(msg.data(), 1, msg.size(), out);
                    std::fclose(out);
                }
                else {
                    Logger::Log(LogLevel::Error,
                                "dumping to file descriptor {} failed.",
                                fd);
                    return false;
                }
            }
        }
        return true;
    }

    [[nodiscard]] auto RetrieveToCas(
        std::vector<Artifact::ObjectInfo> const& /*artifacts_info*/,
        gsl::not_null<IExecutionApi*> const& /*api*/) noexcept
        -> bool override {
        // So far, no user of the git API ever calls RetrieveToCas.
        // Hence, we will implement it, only once the first use case arises
        // in order to avoid dead code.
        Logger::Log(LogLevel::Warning,
                    "Git API, RetrieveToCas not implemented yet.");
        return false;
    }

    [[nodiscard]] auto RetrieveToMemory(
        Artifact::ObjectInfo const& artifact_info)
        -> std::optional<std::string> override {
        return RepositoryConfig::Instance().ReadBlobFromGitCAS(
            artifact_info.digest.hash());
    }

    /// NOLINTNEXTLINE(google-default-arguments)
    [[nodiscard]] auto Upload(BlobContainer const& /*blobs*/,
                              bool /*skip_find_missing*/ = false) noexcept
        -> bool override {
        // Upload to git cas not supported
        return false;
    }

    [[nodiscard]] auto UploadTree(
        std::vector<DependencyGraph::NamedArtifactNodePtr> const&
        /*artifacts*/) noexcept -> std::optional<ArtifactDigest> override {
        // Upload to git cas not supported
        return std::nullopt;
    }

    [[nodiscard]] auto IsAvailable(ArtifactDigest const& digest) const noexcept
        -> bool override {
        return RepositoryConfig::Instance()
            .ReadBlobFromGitCAS(digest.hash())
            .has_value();
    }

    [[nodiscard]] auto IsAvailable(std::vector<ArtifactDigest> const& digests)
        const noexcept -> std::vector<ArtifactDigest> override {
        std::vector<ArtifactDigest> result;
        for (auto const& digest : digests) {
            if (not IsAvailable(digest)) {
                result.push_back(digest);
            }
        }
        return result;
    }
};

#endif