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

Additional features for config-manager #1128

Merged
merged 5 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 9 additions & 8 deletions dnf5-plugins/config-manager_plugin/addrepo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ std::string get_url_part(const std::string & url, CURLUPart what_part) {


// Computes CRC32 checksum of input string.
// Slow bitwise implementation. Used to calculate the checksum of the omitted part of long URLs.
// Slow bitwise implementation. Used to calculate the hash of the omitted middle part of a long `repoid` if the `repoid`
// is generated from a URL. Not used for cryptography.
uint32_t crc32(std::string_view input) {
const uint32_t polynomial = 0x04C11DB7;
uint32_t crc = 0;
Expand Down Expand Up @@ -96,24 +97,24 @@ std::string escape(const std::string & text) {
}


// Regular expressions to sanitise filename
// Regular expressions to sanitise repository id
const std::regex RE_SCHEME{R"(^\w+:/*(\w+:|www\.)?)"};
const std::regex RE_SLASH{R"([?/:&#|~\*\[\]\(\)'\\]+)"};
const std::regex RE_BEGIN{"^[,.]*"};
const std::regex RE_FINAL{"[,.]*$"};

// Returns a filename suitable for the filesystem and for repository id.
// Generates a repository id from a URL.
// Strips dangerous and common characters, encodes some characters and limits the length.
std::string sanitize_url(const std::string & url) {
std::string generate_repoid_from_url(const std::string & url) {
std::string ret;
ret = std::regex_replace(url, RE_SCHEME, "");
ret = std::regex_replace(ret, RE_SLASH, "_");
ret = std::regex_replace(ret, RE_BEGIN, "");
ret = std::regex_replace(ret, RE_FINAL, "");
ret = escape(ret);

// Limits length of url.
// Copies the first and last 100 characters. The substring in between is replaced by a crc32 checksum.
// Limits the length of the repository id.
// Copies the first and last 100 characters. The substring in between is replaced by a crc32 hash.
if (ret.size() > 250) {
std::string_view tmp{ret};
ret = fmt::format(
Expand Down Expand Up @@ -396,7 +397,7 @@ void ConfigManagerAddRepoCommand::create_repo(
}

if (repo_id.empty()) {
repo_id = sanitize_url(url);
repo_id = generate_repoid_from_url(url);
}

if (save_filename.empty()) {
Expand All @@ -423,7 +424,7 @@ void ConfigManagerAddRepoCommand::create_repo(
parser.add_section(repo_id);

// Sets the default repository name. May be overwritten with "--set=name=<name>".
parser.set_value(repo_id, "name", "created by dnf5 config-manager");
parser.set_value(repo_id, "name", repo_id + " - Created by dnf5 config-manager");
// Enables repository by default. The repository can be disabled with "--set=enabled=0".
parser.set_value(repo_id, "enabled", "1");

Expand Down
120 changes: 85 additions & 35 deletions dnf5-plugins/config-manager_plugin/unsetopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@ using namespace libdnf5;

namespace {

bool remove_from_config(ConfigParser & parser, const std::string & section_id, const std::set<std::string> & keys) {
bool remove_from_config(
ConfigParser & parser,
const std::string & section_id,
const std::set<std::string> & keys,
std::set<std::string> & used_keys) {
bool removed = false;
for (const auto & key : keys) {
removed |= parser.remove_option(section_id, key);
if (parser.remove_option(section_id, key)) {
removed = true;
used_keys.insert(key);
}
}
return removed;
}
Expand Down Expand Up @@ -82,7 +89,7 @@ void ConfigManagerUnsetOptCommand::set_argument_parser() {
tmp_repo_conf.opt_binds().at(repo_key);
} catch (const OptionBindsOptionNotFoundError & ex) {
ctx.base.get_logger()->warning(
"config-manager: Request to remove unknown repository option from config file: {}", key);
"config-manager: Request to remove unsupported repository option: {}", key);
}

in_repos_opts_to_remove[repo_id].insert(repo_key);
Expand All @@ -92,7 +99,7 @@ void ConfigManagerUnsetOptCommand::set_argument_parser() {
tmp_config.opt_binds().at(key);
} catch (const OptionBindsOptionNotFoundError & ex) {
ctx.base.get_logger()->warning(
"config-manager: Request to remove unknown main option from config file: {}", key);
"config-manager: Request to remove unsupported main option: {}", key);
}

// Save the global option for later removing from the file.
Expand All @@ -110,48 +117,91 @@ void ConfigManagerUnsetOptCommand::configure() {
const auto & config = ctx.base.get_config();

// Remove options from main configuration file.
const auto & cfg_filepath = get_config_file_path(ctx.base.get_config());
if (!main_opts_to_remove.empty() && std::filesystem::exists(cfg_filepath)) {
ConfigParser parser;
bool changed = false;

parser.read(cfg_filepath);

changed |= remove_from_config(parser, "main", main_opts_to_remove);
if (!main_opts_to_remove.empty()) {
const auto & cfg_filepath = get_config_file_path(ctx.base.get_config());
if (std::filesystem::exists(cfg_filepath)) {
ConfigParser parser;
bool changed = false;
std::set<std::string> used_keys;

parser.read(cfg_filepath);

changed |= remove_from_config(parser, "main", main_opts_to_remove, used_keys);

// Generate warning for unused options
for (const auto & key : main_opts_to_remove) {
if (!used_keys.contains(key)) {
ctx.base.get_logger()->warning(
"config-manager: Request to remove main option but it is not present in the config file: {}",
key);
}
}

if (changed) {
parser.write(cfg_filepath, false);
if (changed) {
parser.write(cfg_filepath, false);
}
} else {
ctx.base.get_logger()->warning(
"config-manager: Request to remove main option but config file not found: {}", cfg_filepath.string());
}
}

auto repos_override_file_path = get_config_manager_repos_override_file_path(config);

// Remove options from repositories overrides configuration file, remove empty sections.
if (!in_repos_opts_to_remove.empty() && std::filesystem::exists(repos_override_file_path)) {
ConfigParser parser;
bool changed = false;
parser.read(repos_override_file_path);
if (!in_repos_opts_to_remove.empty()) {
const auto & repos_override_file_path = get_config_manager_repos_override_file_path(config);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a problem of this PR, but I encountered this:

When I run dnf5 config-manager unsetopt fedora.gpgcheck (or any other option), there is no output, but nothing gets changed. Only when I finally looked at logs, I noticed there is config-manager: Request to remove repository option but config file not found: /etc/dnf/repos.override.d/99-config_manager.repo.

Shouldn't this file get created if it doesn't exist yet? Or am I missing something?

Moreover, this makes me think even more that the warnings should get also on the stderr, because it took me some time to figure out why nothing is happening.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pkratoch

Shouldn't this file get created if it doesn't exist yet?

No. The file contains overrides of repository configuration. A non-existent file has the same meaning as an empty file - no overrides are defined.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. It doesn't make sense to create the file for unsetting the options. I was confused only because I initially expected the original repo file to be modified and seemingly nothing happened when I run the unsetopt command. But now I see it only works over the overrides and the error message makes that clear.

if (std::filesystem::exists(repos_override_file_path)) {
ConfigParser parser;
bool changed = false;
std::map<std::string, std::set<std::string>> used_repos_opts;

parser.read(repos_override_file_path);

std::vector<std::string> empty_config_sections;
for (const auto & [repo_id, setopts] : parser.get_data()) {
for (const auto & [in_repoid, keys] : in_repos_opts_to_remove) {
if (sack::match_string(repo_id, sack::QueryCmp::GLOB, in_repoid)) {
auto & used_repoid_opts = used_repos_opts[in_repoid];
changed |= remove_from_config(parser, repo_id, keys, used_repoid_opts);
}
}
if (setopts.empty()) {
empty_config_sections.emplace_back(repo_id);
}
}

std::vector<std::string> empty_config_sections;
for (const auto & [repo_id, setopts] : parser.get_data()) {
// Generate warning for unused repoids and options
for (const auto & [in_repoid, keys] : in_repos_opts_to_remove) {
if (sack::match_string(repo_id, sack::QueryCmp::GLOB, in_repoid)) {
changed |= remove_from_config(parser, repo_id, keys);
if (const auto used_repoid_opts = used_repos_opts.find(in_repoid);
used_repoid_opts == used_repos_opts.end()) {
ctx.base.get_logger()->warning(
"config-manager: Request to remove repository option but repoid is not present "
"in the overrides: {}",
in_repoid);
} else {
for (const auto & key : keys) {
if (!used_repoid_opts->second.contains(key))
ctx.base.get_logger()->warning(
"config-manager: Request to remove repository option but it is not present "
"in the overrides: {}.{}",
in_repoid,
key);
}
}
}
if (setopts.empty()) {
empty_config_sections.emplace_back(repo_id);
}
}

// Clean config - remove empty sections.
for (const auto & section : empty_config_sections) {
parser.remove_section(section);
changed = true;
}
// Clean config - remove empty sections.
for (const auto & section : empty_config_sections) {
parser.remove_section(section);
changed = true;
}

if (changed) {
parser.write(repos_override_file_path, false);
if (changed) {
parser.write(repos_override_file_path, false);
}
} else {
ctx.base.get_logger()->warning(
"config-manager: Request to remove repository option but file with overrides not found: {}",
repos_override_file_path.string());
}
}
}
Expand Down