Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dnfdaemon: implement D-Bus API for cleaning caches #1589

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions dnf5daemon-client/commands/clean/clean.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright Contributors to the libdnf project.

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 "clean.hpp"

#include "commands/shared_options.hpp"
#include "context.hpp"
#include "exception.hpp"
#include "utils/auth.hpp"

#include <dnf5daemon-server/dbus.hpp>
#include <libdnf5/conf/option_string.hpp>
#include <libdnf5/utils/bgettext/bgettext-mark-domain.h>

#include <iostream>
#include <memory>

namespace dnfdaemon::client {

using namespace libdnf5::cli;

void CleanCommand::set_parent_command() {
auto * arg_parser_parent_cmd = get_session().get_argument_parser().get_root_command();
auto * arg_parser_this_cmd = get_argument_parser_command();
arg_parser_parent_cmd->register_command(arg_parser_this_cmd);
arg_parser_parent_cmd->get_group("software_management_commands").register_argument(arg_parser_this_cmd);
}

void CleanCommand::set_argument_parser() {
auto & parser = get_context().get_argument_parser();
auto & cmd = *get_argument_parser_command();

cmd.set_description("Remove or expire cached data");

cache_types = parser.add_new_values();
auto cache_types_arg = parser.add_new_positional_arg(
"cache_types",
1,
parser.add_init_value(std::unique_ptr<libdnf5::Option>(new libdnf5::OptionString(nullptr))),
cache_types);
cache_types_arg->set_description("Cache type to clean up");
cmd.register_positional_arg(cache_types_arg);
}

void CleanCommand::run() {
auto & ctx = get_context();

if (!libdnf5::utils::am_i_root()) {
throw UnprivilegedUserError();
}

const std::string cache_type = dynamic_cast<libdnf5::OptionString *>((*cache_types)[0].get())->get_value();
bool success;
std::string error_msg;
ctx.session_proxy->callMethod("clean")
.onInterface(dnfdaemon::INTERFACE_BASE)
.withTimeout(static_cast<uint64_t>(-1))
.withArguments(cache_type)
.storeResultsTo(success, error_msg);
// make it compatible with `dnf5 clean metadata` which also cleans solv files
if (success && (cache_type == "metadata")) {
ctx.session_proxy->callMethod("clean")
.onInterface(dnfdaemon::INTERFACE_BASE)
.withTimeout(static_cast<uint64_t>(-1))
.withArguments("dbcache")
.storeResultsTo(success, error_msg);
}

if (success) {
std::cout << "Cache successfully cleaned." << std::endl;
} else {
throw libdnf5::cli::CommandExitError(1, M_("Error cleaning the cache: {}"), error_msg);
}
}

} // namespace dnfdaemon::client
42 changes: 42 additions & 0 deletions dnf5daemon-client/commands/clean/clean.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright Contributors to the libdnf project.

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/>.
*/

#ifndef DNF5DAEMON_CLIENT_COMMANDS_CLEAN_CLEAN_HPP
#define DNF5DAEMON_CLIENT_COMMANDS_CLEAN_CLEAN_HPP

#include "commands/command.hpp"

#include <libdnf5/conf/option_bool.hpp>

namespace dnfdaemon::client {

class CleanCommand : public TransactionCommand {
public:
explicit CleanCommand(Context & context) : TransactionCommand(context, "clean") {}
void set_parent_command() override;
void set_argument_parser() override;
void run() override;

private:
std::vector<std::unique_ptr<libdnf5::Option>> * cache_types{nullptr};
};

} // namespace dnfdaemon::client

#endif
4 changes: 1 addition & 3 deletions dnf5daemon-client/commands/repoquery/repoquery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,11 @@ void RepoqueryCommand::run() {
return;
}

std::string fd_id;
ctx.session_proxy->callMethod("list_fd")
.onInterface(dnfdaemon::INTERFACE_RPM)
.withTimeout(static_cast<uint64_t>(-1))
.withArguments(options)
.withArguments(sdbus::UnixFd{pipefd[1]})
.withArguments(fd_id);
.withArguments(sdbus::UnixFd{pipefd[1]});
close(pipefd[1]);

int in_fd = pipefd[0];
Expand Down
2 changes: 2 additions & 0 deletions dnf5daemon-client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
*/

#include "commands/advisory/advisory.hpp"
#include "commands/clean/clean.hpp"
#include "commands/distro-sync/distro-sync.hpp"
#include "commands/downgrade/downgrade.hpp"
#include "commands/group/group.hpp"
Expand Down Expand Up @@ -209,6 +210,7 @@ static void add_commands(Context & context) {
context.add_and_initialize_command(std::make_unique<RepolistCommand>(context, "repoinfo"));

context.add_and_initialize_command(std::make_unique<AdvisoryCommand>(context));
context.add_and_initialize_command(std::make_unique<CleanCommand>(context));
}

} // namespace dnfdaemon::client
Expand Down
15 changes: 15 additions & 0 deletions dnf5daemon-server/dbus/interfaces/org.rpm.dnf.v0.Base.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
<arg name="success" type="b" direction="out"/>
</method>

<!--
clean:
@cache_type: cache type to clean up. Supported types are "all", "packages", "metadata", "dbcache", and "expire-cache".
@success: `true` if the cache was successfully cleaned, `false` otherwise.
@error_msg: string, contains errors encountered while cleaning the cache.


Remove or expire cached data.
-->
<method name="clean">
<arg name="cache_type" type="s" direction="in"/>
<arg name="success" type="b" direction="out"/>
<arg name="error_msg" type="s" direction="out"/>
</method>

<!--
download_add_new:
@session_object_path: object path of the dnf5daemon session
Expand Down
76 changes: 76 additions & 0 deletions dnf5daemon-server/services/base/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,43 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#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 <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();
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",
"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->registerSignal(
dnfdaemon::INTERFACE_BASE,
Expand Down Expand Up @@ -71,3 +92,58 @@ sdbus::MethodReply Base::read_all_repos(sdbus::MethodCall & call) {
reply << retval;
return reply;
}

sdbus::MethodReply Base::clean(sdbus::MethodCall & call) {
if (!session.check_authorization(dnfdaemon::POLKIT_EXECUTE_RPM_TRANSACTION, call.getSender())) {
throw std::runtime_error("Not authorized");
}

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

// get the cache types and check its validity
std::string cache_type{};
call >> cache_type;
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;
}
1 change: 1 addition & 0 deletions dnf5daemon-server/services/base/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Base : public IDbusSessionService {
void dbus_deregister();

private:
sdbus::MethodReply clean(sdbus::MethodCall & call);
sdbus::MethodReply read_all_repos(sdbus::MethodCall & call);
};

Expand Down
Loading