File: base.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 (280 lines) | stat: -rw-r--r-- 10,691 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// 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 "base.hpp"

#include "dbus.hpp"
#include "utils.hpp"
#include "utils/string.hpp"

#include <fmt/format.h>
#include <libdnf5/repo/repo.hpp>
#include <libdnf5/repo/repo_cache.hpp>
#include <sdbus-c++/sdbus-c++.h>
#include <unistd.h>

#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <unordered_set>

static const std::unordered_set<std::string> ALLOWED_CACHE_TYPES = {
    "all",
    "packages",
    "metadata",
    "dbcache",
    "expire-cache",
};

void Base::dbus_register() {
    auto dbus_object = session.get_dbus_object();
#ifdef SDBUS_CPP_VERSION_2
    dbus_object
        ->addVTable(
            sdbus::MethodVTableItem{
                sdbus::MethodName{"read_all_repos"},
                sdbus::Signature{""},
                {},
                sdbus::Signature{"b"},
                {"success"},
                [this](sdbus::MethodCall call) -> void {
                    session.get_threads_manager().handle_method(
                        *this, &Base::read_all_repos, call, session.session_locale);
                },
                {}},
            sdbus::MethodVTableItem{
                sdbus::MethodName{"clean_with_options"},
                sdbus::Signature{"sa{sv}"},
                {"cache_type", "options"},
                sdbus::Signature{"bs"},
                {"success", "error_msg"},
                [this](sdbus::MethodCall call) -> void {
                    session.get_threads_manager().handle_method(
                        *this, &Base::clean_with_options, call, session.session_locale);
                },
                {}},
            sdbus::MethodVTableItem{
                sdbus::MethodName{"clean"},
                sdbus::Signature{"s"},
                {"cache_type"},
                sdbus::Signature{"bs"},
                {"success", "error_msg"},
                [this](sdbus::MethodCall call) -> void {
                    session.get_threads_manager().handle_method(*this, &Base::clean, call, session.session_locale);
                },
                {}},
            sdbus::MethodVTableItem{
                sdbus::MethodName{"reset"},
                sdbus::Signature{""},
                {},
                sdbus::Signature{"bs"},
                {"success", "error_msg"},
                [this](sdbus::MethodCall call) -> void {
                    session.get_threads_manager().handle_method(*this, &Base::reset, call, session.session_locale);
                },
                {}},
            sdbus::SignalVTableItem{
                dnfdaemon::SIGNAL_DOWNLOAD_ADD_NEW,
                sdbus::Signature{"ossx"},
                {"session_object_path", "download_id", "description", "total_to_download"},
                {}},
            sdbus::SignalVTableItem{
                dnfdaemon::SIGNAL_DOWNLOAD_PROGRESS,
                sdbus::Signature{"osxx"},
                {"session_object_path", "download_id", "total_to_download", "downloaded"},
                {}},
            sdbus::SignalVTableItem{
                dnfdaemon::SIGNAL_DOWNLOAD_END,
                sdbus::Signature{"osus"},
                {"session_object_path", "download_id", "transfer_status", "message"},
                {}},
            sdbus::SignalVTableItem{
                dnfdaemon::SIGNAL_DOWNLOAD_MIRROR_FAILURE,
                sdbus::Signature{"ossss"},
                {"session_object_path", "download_id", "message", "url", "metadata"},
                {}},
            sdbus::SignalVTableItem{
                dnfdaemon::SIGNAL_REPO_KEY_IMPORT_REQUEST,
                sdbus::Signature{"osasssx"},
                {"session_object_path", "key_id", "user_ids", "key_fingerprint", "key_url", "timestamp"},
                {}})
        .forInterface(dnfdaemon::INTERFACE_BASE);
#else
    dbus_object->registerMethod(
        dnfdaemon::INTERFACE_BASE, "read_all_repos", "", {}, "b", {"success"}, [this](sdbus::MethodCall call) -> void {
            session.get_threads_manager().handle_method(*this, &Base::read_all_repos, call, session.session_locale);
        });
    dbus_object->registerMethod(
        dnfdaemon::INTERFACE_BASE,
        "clean_with_options",
        "sa{sv}",
        {"cache_type", "options"},
        "bs",
        {"success", "error_msg"},
        [this](sdbus::MethodCall call) -> void {
            session.get_threads_manager().handle_method(*this, &Base::clean_with_options, call, session.session_locale);
        });
    dbus_object->registerMethod(
        dnfdaemon::INTERFACE_BASE,
        "clean",
        "s",
        {"cache_type"},
        "bs",
        {"success", "error_msg"},
        [this](sdbus::MethodCall call) -> void {
            session.get_threads_manager().handle_method(*this, &Base::clean, call, session.session_locale);
        });
    dbus_object->registerMethod(
        dnfdaemon::INTERFACE_BASE,
        "reset",
        "",
        {},
        "bs",
        {"success", "error_msg"},
        [this](sdbus::MethodCall call) -> void {
            session.get_threads_manager().handle_method(*this, &Base::reset, call, session.session_locale);
        });

    dbus_object->registerSignal(
        dnfdaemon::INTERFACE_BASE,
        dnfdaemon::SIGNAL_DOWNLOAD_ADD_NEW,
        "ossx",
        {"session_object_path", "download_id", "description", "total_to_download"});
    dbus_object->registerSignal(
        dnfdaemon::INTERFACE_BASE,
        dnfdaemon::SIGNAL_DOWNLOAD_PROGRESS,
        "osxx",
        {"session_object_path", "download_id", "total_to_download", "downloaded"});
    dbus_object->registerSignal(
        dnfdaemon::INTERFACE_BASE,
        dnfdaemon::SIGNAL_DOWNLOAD_END,
        "osus",
        {"session_object_path", "download_id", "transfer_status", "message"});
    dbus_object->registerSignal(
        dnfdaemon::INTERFACE_BASE,
        dnfdaemon::SIGNAL_DOWNLOAD_MIRROR_FAILURE,
        "ossss",
        {"session_object_path", "download_id", "message", "url", "metadata"});
    dbus_object->registerSignal(
        dnfdaemon::INTERFACE_BASE,
        dnfdaemon::SIGNAL_REPO_KEY_IMPORT_REQUEST,
        "osasssx",
        {"session_object_path", "key_id", "user_ids", "key_fingerprint", "key_url", "timestamp"});
#endif
}

sdbus::MethodReply Base::read_all_repos(sdbus::MethodCall & call) {
    bool retval = session.read_all_repos();
    auto reply = call.createReply();
    reply << retval;
    return reply;
}

sdbus::MethodReply Base::impl_clean(
    sdbus::MethodCall & call, const std::string & cache_type, const dnfdaemon::KeyValueMap & options) {
    bool interactive = dnfdaemon::key_value_map_get<bool>(options, "interactive", true);

    // let the "expire-cache" do anyone, just as read_all_repos()
    if (cache_type != "expire-cache" &&
        !session.check_authorization(
            dnfdaemon::POLKIT_EXECUTE_RPM_TRUSTED_TRANSACTION, call.getSender(), interactive)) {
        throw std::runtime_error("Not authorized");
    }

    bool success{false};
    std::string error_msg{};

    if (ALLOWED_CACHE_TYPES.find(cache_type) == ALLOWED_CACHE_TYPES.end()) {
        error_msg = fmt::format("Unsupported cache type to clean up: \"{}\".", cache_type);
    } else {
        auto base = session.get_base();
        std::filesystem::path cachedir{base->get_config().get_cachedir_option().get_value()};
        std::error_code ec;
        std::vector<std::string> remove_errs{};
        for (const auto & dir_entry : std::filesystem::directory_iterator(cachedir, ec)) {
            if (!dir_entry.is_directory()) {
                continue;
            }
            libdnf5::repo::RepoCache cache(*base, dir_entry.path());
            try {
                if (cache_type == "all") {
                    cache.remove_all();
                } else if (cache_type == "packages") {
                    cache.remove_packages();
                } else if (cache_type == "metadata") {
                    cache.remove_metadata();
                } else if (cache_type == "dbcache") {
                    cache.remove_solv_files();
                } else if (cache_type == "expire-cache") {
                    cache.write_attribute(libdnf5::repo::RepoCache::ATTRIBUTE_EXPIRED);
                }
            } catch (const std::exception & ex) {
                remove_errs.emplace_back(fmt::format(" - \"{0}\": {1}", dir_entry.path().native(), ex.what()));
            }
        }
        if (ec) {
            error_msg = fmt::format("Cannot iterate the cache directory: \"{}\".", cachedir.string());
        } else if (!remove_errs.empty()) {
            error_msg =
                fmt::format("Failed to cleanup repository cache:\n{}", libdnf5::utils::string::join(remove_errs, "\n"));
        } else {
            success = true;
        }
    }

    auto reply = call.createReply();
    reply << success;
    reply << error_msg;
    return reply;
}

sdbus::MethodReply Base::clean_with_options(sdbus::MethodCall & call) {
    std::string cache_type{};
    dnfdaemon::KeyValueMap options;
    call >> cache_type >> options;

    return impl_clean(call, cache_type, options);
}

sdbus::MethodReply Base::clean(sdbus::MethodCall & call) {
    std::string cache_type{};
    dnfdaemon::KeyValueMap options{};
    call >> cache_type;

    return impl_clean(call, cache_type, options);
}

sdbus::MethodReply Base::reset(sdbus::MethodCall & call) {
    bool success{true};
    std::string error_msg{};

    auto & transaction_mutex = session.get_transaction_mutex();
    if (!transaction_mutex.try_lock()) {
        success = false;
        error_msg = "Cannot reset, an rpm transaction is running.";
    } else {
        std::lock_guard<std::mutex> transaction_lock(transaction_mutex, std::adopt_lock);
        session.reset_base();
    }
    auto reply = call.createReply();
    reply << success;
    reply << error_msg;
    return reply;
}