File: advisory.cpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (184 lines) | stat: -rw-r--r-- 6,188 bytes parent folder | download
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
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Libdnf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libdnf.  If not, see <https://www.gnu.org/licenses/>.

#include "libdnf5/advisory/advisory.hpp"

#include "solv/pool.hpp"
#include "utils/string.hpp"

#include "libdnf5/advisory/advisory_collection.hpp"
#include "libdnf5/advisory/advisory_reference.hpp"
#include "libdnf5/common/exception.hpp"
#include "libdnf5/utils/bgettext/bgettext-mark-domain.h"

#include <fmt/format.h>

namespace libdnf5::advisory {

class Advisory::Impl {
public:
    Impl(const libdnf5::BaseWeakPtr & base, AdvisoryId id) : base(base), id(id) {}

private:
    friend Advisory;

    BaseWeakPtr base;

    AdvisoryId id;
};

Advisory::Advisory(const libdnf5::BaseWeakPtr & base, AdvisoryId id) : p_impl(std::make_unique<Impl>(base, id)) {}

Advisory::Advisory(const Advisory & src) : p_impl(new Impl(*src.p_impl)) {}
Advisory & Advisory::operator=(const Advisory & src) {
    if (this != &src) {
        if (p_impl) {
            *p_impl = *src.p_impl;
        } else {
            p_impl = std::make_unique<Impl>(*src.p_impl);
        }
    }

    return *this;
}

Advisory::Advisory(Advisory && src) noexcept = default;
Advisory & Advisory::operator=(Advisory && src) noexcept = default;

bool Advisory::operator==(const Advisory & other) const noexcept {
    return p_impl->id == other.p_impl->id && p_impl->base == other.p_impl->base;
}

bool Advisory::operator!=(const Advisory & other) const noexcept {
    return !(*this == other);
}


std::string Advisory::get_name() const {
    const char * name;
    name = get_rpm_pool(p_impl->base).lookup_str(p_impl->id.id, SOLVABLE_NAME);

    if (strncmp(
            libdnf5::solv::SOLVABLE_NAME_ADVISORY_PREFIX, name, libdnf5::solv::SOLVABLE_NAME_ADVISORY_PREFIX_LENGTH) !=
        0) {
        throw RuntimeError(
            M_("Bad libsolv id for advisory \"{}\", solvable name \"{}\" doesn't have advisory prefix \"{}\""),
            p_impl->id.id,
            std::string(name),
            std::string(libdnf5::solv::SOLVABLE_NAME_ADVISORY_PREFIX));
    }

    return std::string(name + libdnf5::solv::SOLVABLE_NAME_ADVISORY_PREFIX_LENGTH);
}

std::string Advisory::get_type() const {
    return std::string(get_rpm_pool(p_impl->base).lookup_str(p_impl->id.id, SOLVABLE_PATCHCATEGORY));
}

std::string Advisory::get_severity() const {
    //TODO(amatej): should we call SolvPrivate::internalize_libsolv_repo(solvable->repo);
    //              before pool.lookup_str?
    //              If so do this just once in solv::advisroy_private
    return libdnf5::utils::string::c_to_str(get_rpm_pool(p_impl->base).lookup_str(p_impl->id.id, UPDATE_SEVERITY));
}

unsigned long long Advisory::get_buildtime() const {
    return get_rpm_pool(p_impl->base).lookup_num(p_impl->id.id, SOLVABLE_BUILDTIME);
}

std::string Advisory::get_title() const {
    // SOLVABLE_SUMMARY is misnamed, it actually stores the title
    return libdnf5::utils::string::c_to_str(get_rpm_pool(p_impl->base).lookup_str(p_impl->id.id, SOLVABLE_SUMMARY));
}

std::string Advisory::get_vendor() const {
    return libdnf5::utils::string::c_to_str(get_rpm_pool(p_impl->base).lookup_str(p_impl->id.id, SOLVABLE_VENDOR));
}

std::string Advisory::get_rights() const {
    return libdnf5::utils::string::c_to_str(get_rpm_pool(p_impl->base).lookup_str(p_impl->id.id, UPDATE_RIGHTS));
}

std::string Advisory::get_status() const {
    return libdnf5::utils::string::c_to_str(get_rpm_pool(p_impl->base).lookup_str(p_impl->id.id, UPDATE_STATUS));
}

std::string Advisory::get_message() const {
    return libdnf5::utils::string::c_to_str(get_rpm_pool(p_impl->base).lookup_str(p_impl->id.id, UPDATE_MESSAGE));
}

std::string Advisory::get_description() const {
    return libdnf5::utils::string::c_to_str(get_rpm_pool(p_impl->base).lookup_str(p_impl->id.id, SOLVABLE_DESCRIPTION));
}

AdvisoryId Advisory::get_id() const {
    return p_impl->id;
}

std::vector<AdvisoryReference> Advisory::get_references(std::vector<std::string> types) const {
    auto & pool = get_rpm_pool(p_impl->base);

    std::vector<AdvisoryReference> output;

    Dataiterator di;
    dataiterator_init(&di, *pool, 0, p_impl->id.id, UPDATE_REFERENCE, 0, 0);

    for (int index = 0; dataiterator_step(&di); index++) {
        dataiterator_setpos(&di);
        std::string current_type = std::string(pool.lookup_str(SOLVID_POS, UPDATE_REFERENCE_TYPE));

        if (types.empty() || std::find(types.begin(), types.end(), current_type) != types.end()) {
            output.emplace_back(AdvisoryReference(p_impl->base, p_impl->id, index));
        }
    }

    dataiterator_free(&di);
    return output;
}

std::vector<AdvisoryCollection> Advisory::get_collections() const {
    std::vector<AdvisoryCollection> output;

    Dataiterator di;
    dataiterator_init(&di, *get_rpm_pool(p_impl->base), 0, p_impl->id.id, UPDATE_COLLECTIONLIST, 0, 0);

    for (int index = 0; dataiterator_step(&di); index++) {
        dataiterator_setpos(&di);
        output.emplace_back(AdvisoryCollection(p_impl->base, p_impl->id, index));
    }

    dataiterator_free(&di);

    return output;
}

//TODO(amatej): this could be possibly removed?
bool Advisory::is_applicable() const {
    for (const auto & collection : get_collections()) {
        if (collection.is_applicable()) {
            return true;
        }
    }

    return false;
}

Advisory::~Advisory() = default;

}  // namespace libdnf5::advisory