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 (177 lines) | stat: -rw-r--r-- 7,446 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
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: GPL-2.0-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 General Public License as published by
// the Free Software Foundation, either version 2 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libdnf.  If not, see <https://www.gnu.org/licenses/>.

#include "advisory.hpp"

#include <fmt/format.h>
#include <libdnf5/advisory/advisory_collection.hpp>
#include <libdnf5/advisory/advisory_reference.hpp>
#include <libdnf5/rpm/nevra.hpp>

#include <map>

namespace dnfdaemon {

// map string advisory attribute name to actual attribute
const std::map<std::string, AdvisoryAttribute> advisory_attributes{
    {"advisoryid", AdvisoryAttribute::advisoryid},
    {"name", AdvisoryAttribute::name},
    {"severity", AdvisoryAttribute::severity},
    {"type", AdvisoryAttribute::type},
    {"buildtime", AdvisoryAttribute::buildtime},
    {"vendor", AdvisoryAttribute::vendor},
    {"description", AdvisoryAttribute::description},
    {"title", AdvisoryAttribute::title},
    {"status", AdvisoryAttribute::status},
    {"rights", AdvisoryAttribute::rights},
    {"message", AdvisoryAttribute::message},
    {"references", AdvisoryAttribute::references},
    {"collections", AdvisoryAttribute::collections},
};

std::vector<AdvisoryReference> references_to_list(const libdnf5::advisory::Advisory & libdnf_advisory) {
    std::vector<AdvisoryReference> references;

    for (const auto & ref : libdnf_advisory.get_references()) {
        references.emplace_back(ref.get_id(), ref.get_type(), ref.get_title(), ref.get_url());
    }

    return references;
}

KeyValueMapList collections_to_list(
    const libdnf5::advisory::Advisory & libdnf_advisory,
    const std::unordered_map<std::string, libdnf5::rpm::Package> & installed_versions) {
    KeyValueMapList collections;
    for (auto & col : libdnf_advisory.get_collections()) {
        KeyValueMapList packages;
        auto libdnf_packages = col.get_packages();
        std::sort(
            libdnf_packages.begin(),
            libdnf_packages.end(),
            libdnf5::rpm::cmp_nevra<libdnf5::advisory::AdvisoryPackage>);
        for (const auto & pkg : libdnf_packages) {
            KeyValueMap package;
            auto name = pkg.get_name();
            auto arch = pkg.get_arch();

            package["n"] = sdbus::Variant(name);
            package["e"] = sdbus::Variant(pkg.get_epoch());
            package["v"] = sdbus::Variant(pkg.get_version());
            package["r"] = sdbus::Variant(pkg.get_release());
            package["a"] = sdbus::Variant(arch);
            package["nevra"] = sdbus::Variant(pkg.get_nevra());

            std::string na{std::move(name)};
            na.append(".");
            na.append(arch);
            auto it = installed_versions.find(na);
            if (it == installed_versions.end()) {
                // advisory package is not installed => not related to system
                package["applicability"] = sdbus::Variant("unrelated");
            } else if (libdnf5::rpm::evrcmp(it->second, pkg) < 0) {
                // installed version is lower than one in advisory
                package["applicability"] = sdbus::Variant("available");
            } else {
                package["applicability"] = sdbus::Variant("installed");
            }

            packages.emplace_back(std::move(package));
        }

        KeyValueMapList modules;
        auto libdnf_modules = col.get_modules();
        for (const auto & mdl : libdnf_modules) {
            KeyValueMap col_module;
            col_module["n"] = sdbus::Variant(mdl.get_name());
            col_module["s"] = sdbus::Variant(mdl.get_stream());
            col_module["v"] = sdbus::Variant(mdl.get_version());
            col_module["c"] = sdbus::Variant(mdl.get_context());
            col_module["a"] = sdbus::Variant(mdl.get_arch());
            col_module["nsvca"] = sdbus::Variant(mdl.get_nsvca());
            modules.emplace_back(std::move(col_module));
        }

        KeyValueMap collection;
        collection["packages"] = sdbus::Variant(std::move(packages));
        collection["modules"] = sdbus::Variant(std::move(modules));
        collections.emplace_back(std::move(collection));
    }
    return collections;
}

KeyValueMap advisory_to_map(
    const libdnf5::advisory::Advisory & libdnf_advisory,
    const std::vector<std::string> & attributes,
    const std::unordered_map<std::string, libdnf5::rpm::Package> & installed_versions) {
    KeyValueMap dbus_advisory;
    // add advisory id by default
    dbus_advisory.emplace("advisoryid", libdnf_advisory.get_id().id);
    // attributes required by client
    for (auto & attr : attributes) {
        auto it = advisory_attributes.find(attr);
        if (it == advisory_attributes.end()) {
            throw std::runtime_error(fmt::format("Advisory attribute '{}' not supported", attr));
        }
        switch (it->second) {
            case AdvisoryAttribute::advisoryid:
                // already added by default
                break;
            case AdvisoryAttribute::name:
                dbus_advisory.emplace(attr, libdnf_advisory.get_name());
                break;
            case AdvisoryAttribute::severity:
                dbus_advisory.emplace(attr, libdnf_advisory.get_severity());
                break;
            case AdvisoryAttribute::type:
                dbus_advisory.emplace(attr, libdnf_advisory.get_type());
                break;
            case AdvisoryAttribute::buildtime:
                dbus_advisory.emplace(attr, static_cast<uint64_t>(libdnf_advisory.get_buildtime()));
                break;
            case AdvisoryAttribute::vendor:
                dbus_advisory.emplace(attr, libdnf_advisory.get_vendor());
                break;
            case AdvisoryAttribute::description:
                dbus_advisory.emplace(attr, libdnf_advisory.get_description());
                break;
            case AdvisoryAttribute::title:
                dbus_advisory.emplace(attr, libdnf_advisory.get_title());
                break;
            case AdvisoryAttribute::status:
                dbus_advisory.emplace(attr, libdnf_advisory.get_status());
                break;
            case AdvisoryAttribute::rights:
                dbus_advisory.emplace(attr, libdnf_advisory.get_rights());
                break;
            case AdvisoryAttribute::message:
                dbus_advisory.emplace(attr, libdnf_advisory.get_message());
                break;
            case AdvisoryAttribute::collections:
                dbus_advisory.emplace(attr, collections_to_list(libdnf_advisory, installed_versions));
                break;
            case AdvisoryAttribute::references:
                dbus_advisory.emplace(attr, references_to_list(libdnf_advisory));
                break;
        }
    }
    return dbus_advisory;
}

}  // namespace dnfdaemon