Skip to content

Commit

Permalink
dnf5daemon: Introduce enabling repositories in Repo interface
Browse files Browse the repository at this point in the history
Move the existing functionality of enabling and disabling of repositories from the RepoConf interface.

Drop the RepoConf interface as the remaining list functionality
that is required by clients is already provided by the Repo::list.

Change the logic of commands to be strict against invalid arguments.
  • Loading branch information
jan-kolarik committed Feb 21, 2024
1 parent e3e8175 commit cf0f5c8
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 203 deletions.
6 changes: 3 additions & 3 deletions dnf5daemon-server/dbus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ const char * const DBUS_OBJECT_PATH = "/org/rpm/dnf/v0";
// interfaces
const char * const INTERFACE_BASE = "org.rpm.dnf.v0.Base";
const char * const INTERFACE_REPO = "org.rpm.dnf.v0.rpm.Repo";
const char * const INTERFACE_REPOCONF = "org.rpm.dnf.v0.rpm.RepoConf";
const char * const INTERFACE_RPM = "org.rpm.dnf.v0.rpm.Rpm";
const char * const INTERFACE_GOAL = "org.rpm.dnf.v0.Goal";
const char * const INTERFACE_GROUP = "org.rpm.dnf.v0.comps.Group";
Expand Down Expand Up @@ -87,13 +86,14 @@ const char * const SIGNAL_TRANSACTION_ELEM_PROGRESS = "transaction_elem_progress
const char * const SIGNAL_TRANSACTION_FINISHED = "transaction_finished";

// polkit actions
const char * const POLKIT_REPOCONF_WRITE = "org.rpm.dnf.v0.rpm.RepoConf.write";
const char * const POLKIT_EXECUTE_RPM_TRANSACTION = "org.rpm.dnf.v0.rpm.execute_transaction";
const char * const POLKIT_CONFIRM_KEY_IMPORT = "org.rpm.dnf.v0.rpm.Repo.confirm_key";
const char * const POLKIT_REPOCONF_WRITE = "org.rpm.dnf.v0.rpm.Repo.conf_write";

// errors
const char * const ERROR = "org.rpm.dnf.v0.Error";
const char * const ERROR_REPOCONF = "org.rpm.dnf.v0.rpm.RepoConf.Error";
const char * const ERROR_REPOCONF = "org.rpm.dnf.v0.rpm.Repo.ConfError";
const char * const ERROR_REPO_ID_UNKNOWN = "org.rpm.dnf.v0.rpm.Repo.NoMatchingIdError";
const char * const ERROR_RESOLVE = "org.rpm.dnf.v0.rpm.Rpm.ResolveError";
const char * const ERROR_TRANSACTION = "org.rpm.dnf.v0.rpm.Rpm.TransactionError";

Expand Down
20 changes: 20 additions & 0 deletions dnf5daemon-server/dbus/interfaces/org.rpm.dnf.v0.rpm.Repo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
<arg name="confirmed" type="b" direction="in"/>
</method>

<!--
enable:
@ids: array of strings containing all repo ids to be enabled
Enable repositories based on the list of given ids.
-->
<method name="enable">
<arg name="ids" type="as" direction="in"/>
</method>

<!--
disable:
@ids: array of strings containing all repo ids to be disabled
Disable repositories based on the list of given ids.
-->
<method name="disable">
<arg name="ids" type="as" direction="in"/>
</method>

</interface>

</node>
2 changes: 1 addition & 1 deletion dnf5daemon-server/polkit/org.rpm.dnf.v0.policy
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<vendor>Dnf</vendor>
<vendor_url>https://github.com/rpm-software-management/libdnf/</vendor_url>

<action id="org.rpm.dnf.v0.rpm.RepoConf.write">
<action id="org.rpm.dnf.v0.rpm.Repo.conf_write">
<description>Write repository configuration file</description>
<message>Writing changes to repository configuration file requires authentication.</message>
<defaults>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
*/


#ifndef DNF5DAEMON_SERVER_SERVICES_REPOCONF_CONFIGURATION_HPP
#define DNF5DAEMON_SERVER_SERVICES_REPOCONF_CONFIGURATION_HPP
#ifndef DNF5DAEMON_SERVER_SERVICES_REPO_CONFIGURATION_HPP
#define DNF5DAEMON_SERVER_SERVICES_REPO_CONFIGURATION_HPP

#include "session.hpp"

Expand Down
59 changes: 59 additions & 0 deletions dnf5daemon-server/services/repo/repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "repo.hpp"

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

#include <fmt/format.h>
#include <libdnf5/repo/repo.hpp>
#include <libdnf5/rpm/package_query.hpp>
#include <libdnf5/rpm/package_set.hpp>
#include <sdbus-c++/sdbus-c++.h>

#include <algorithm>
#include <chrono>
#include <iostream>
#include <ranges>
#include <string>

namespace {
Expand Down Expand Up @@ -264,6 +268,12 @@ void Repo::dbus_register() {
dnfdaemon::INTERFACE_REPO, "confirm_key", "sb", "", [this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(*this, &Repo::confirm_key, call);
});
dbus_object->registerMethod(dnfdaemon::INTERFACE_REPO, "enable", "as", "", [this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(*this, &Repo::enable, call, session.session_locale);
});
dbus_object->registerMethod(dnfdaemon::INTERFACE_REPO, "disable", "as", "", [this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(*this, &Repo::disable, call, session.session_locale);
});
}

sdbus::MethodReply Repo::confirm_key(sdbus::MethodCall & call) {
Expand Down Expand Up @@ -340,3 +350,52 @@ sdbus::MethodReply Repo::list(sdbus::MethodCall & call) {
reply << out_repositories;
return reply;
}

void Repo::enable_disable_repos(const std::vector<std::string> & ids, const bool enable) {
Configuration cfg(session);
cfg.read_configuration();

auto missing_ids = ids | std::views::filter([&](auto & id) { return !cfg.find_repo(id); });
if (!std::ranges::empty(missing_ids)) {
std::vector<std::string> missing_ids_vector = {std::begin(missing_ids), std::end(missing_ids)};
throw sdbus::Error(
dnfdaemon::ERROR_REPO_ID_UNKNOWN,
std::string("No matching repositories found for following ids: ") +
libdnf5::utils::string::join(missing_ids_vector, ","));
}

std::vector<std::string> changed_config_files;
for (auto & repoid : ids) {
auto repoinfo = cfg.find_repo(repoid);
if (repoinfo->repoconfig->get_enabled_option().get_value() != enable) {
auto parser = cfg.find_parser(repoinfo->file_path);
if (parser) {
parser->set_value(repoid, "enabled", enable ? "1" : "0");
changed_config_files.push_back(repoinfo->file_path);
}
}
}
for (auto & config_file : changed_config_files) {
try {
cfg.find_parser(config_file)->write(config_file, false);
} catch (std::exception & e) {
throw sdbus::Error(
dnfdaemon::ERROR_REPOCONF, std::string("Unable to write configuration file: ") + e.what());
}
}
}

sdbus::MethodReply Repo::enable_disable(sdbus::MethodCall && call, const bool & enable) {
auto sender = call.getSender();
std::vector<std::string> ids;
call >> ids;
auto is_authorized = session.check_authorization(dnfdaemon::POLKIT_REPOCONF_WRITE, sender);
if (!is_authorized) {
throw sdbus::Error(dnfdaemon::ERROR_REPOCONF, "Not authorized.");
}

enable_disable_repos(ids, enable);

auto reply = call.createReply();
return reply;
}
5 changes: 5 additions & 0 deletions dnf5daemon-server/services/repo/repo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class Repo : public IDbusSessionService {
private:
sdbus::MethodReply list(sdbus::MethodCall & call);
sdbus::MethodReply confirm_key(sdbus::MethodCall & call);
sdbus::MethodReply enable_disable(sdbus::MethodCall && call, const bool & enable);
sdbus::MethodReply enable(sdbus::MethodCall & call) { return enable_disable(std::move(call), true); };
sdbus::MethodReply disable(sdbus::MethodCall & call) { return enable_disable(std::move(call), false); };

void enable_disable_repos(const std::vector<std::string> & ids, const bool enable);
};

#endif
146 changes: 0 additions & 146 deletions dnf5daemon-server/services/repoconf/repo_conf.cpp

This file was deleted.

49 changes: 0 additions & 49 deletions dnf5daemon-server/services/repoconf/repo_conf.hpp

This file was deleted.

2 changes: 0 additions & 2 deletions dnf5daemon-server/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "services/comps/group.hpp"
#include "services/goal/goal.hpp"
#include "services/repo/repo.hpp"
#include "services/repoconf/repo_conf.hpp"
#include "services/rpm/rpm.hpp"
#include "utils.hpp"

Expand Down Expand Up @@ -119,7 +118,6 @@ Session::Session(

// instantiate all services provided by the daemon
services.emplace_back(std::make_unique<Base>(*this));
services.emplace_back(std::make_unique<RepoConf>(*this));
services.emplace_back(std::make_unique<Repo>(*this));
services.emplace_back(std::make_unique<Rpm>(*this));
services.emplace_back(std::make_unique<Goal>(*this));
Expand Down

0 comments on commit cf0f5c8

Please sign in to comment.