summaryrefslogtreecommitdiff
path: root/src/buildtool/execution_api/common/stream_dumper.hpp
blob: 31f9b292faeb1fa4783aa76ceccdcc06af221927 (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
// Copyright 2024 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_COMMON_STREAM_DUMPER_HPP
#define INCLUDED_SRC_BUILDTOOL_EXECUTION_API_COMMON_STREAM_DUMPER_HPP

#include <cstdio>
#include <optional>
#include <string>
#include <utility>

#include "gsl/gsl"
#include "src/buildtool/common/artifact.hpp"
#include "src/buildtool/compatibility/compatibility.hpp"
#include "src/buildtool/execution_api/bazel_msg/bazel_msg_factory.hpp"
#include "src/buildtool/file_system/object_type.hpp"

template <typename TImpl>
class StreamDumper final {
  public:
    template <typename... Args>
    explicit StreamDumper(Args&&... args) noexcept
        : impl_(std::forward<Args>(args)...) {}

    /// \brief Dump artifact to file stream.
    /// Tree artifacts are pretty-printed (i.e., contents are listed) unless
    /// raw_tree is set, then the raw tree will be written to the file stream.
    /// \param info         The object info of the artifact to dump.
    /// \param stream       The file stream to dump to.
    /// \param raw_tree     Dump tree as raw blob.
    /// \returns true on success.
    [[nodiscard]] auto DumpToStream(Artifact::ObjectInfo const& info,
                                    gsl::not_null<FILE*> const& stream,
                                    bool raw_tree) const noexcept -> bool {
        const bool is_tree = IsTreeObject(info.type);
        if (is_tree and raw_tree) {
            return DumpRawTree(info, stream);
        }
        return is_tree ? DumpTree(info, stream) : DumpBlob(info, stream);
    }

  private:
    TImpl impl_;

    [[nodiscard]] auto DumpRawTree(
        Artifact::ObjectInfo const& info,
        gsl::not_null<FILE*> const& stream) const noexcept -> bool {
        auto writer = [this, &stream](std::string const& data) -> bool {
            return DumpString(data, stream);
        };
        return impl_.DumpRawTree(info, writer);
    }

    [[nodiscard]] auto DumpTree(
        Artifact::ObjectInfo const& info,
        gsl::not_null<FILE*> const& stream) const noexcept -> bool {
        if (Compatibility::IsCompatible()) {
            auto directory = impl_.ReadDirectory(info.digest);
            auto data = directory
                            ? BazelMsgFactory::DirectoryToString(*directory)
                            : std::nullopt;
            if (data) {
                return DumpString(*data, stream);
            }
        }
        else {
            auto entries = impl_.ReadGitTree(info.digest);
            auto data = entries ? BazelMsgFactory::GitTreeToString(*entries)
                                : std::nullopt;
            if (data) {
                return DumpString(*data, stream);
            }
        }
        return false;
    }

    [[nodiscard]] auto DumpBlob(
        Artifact::ObjectInfo const& info,
        gsl::not_null<FILE*> const& stream) const noexcept -> bool {
        auto writer = [this, &stream](std::string const& data) -> bool {
            return DumpString(data, stream);
        };
        return impl_.DumpBlob(info, writer);
    }

    [[nodiscard]] auto DumpString(
        std::string const& data,
        gsl::not_null<FILE*> const& stream) const noexcept -> bool {
        return std::fwrite(data.data(), 1, data.size(), stream) == data.size();
    }
};

#endif  // INCLUDED_SRC_BUILDTOOL_EXECUTION_API_COMMON_STREAM_DUMPER_HPP