Skip to content

Commit

Permalink
Implement command provides
Browse files Browse the repository at this point in the history
This introduces error codes for provides and slightly different error
messages.

Examples.

- All packages match:

dnf provides dnf rpm libdnf -> exit code 0
<provides output>

- One package queried and it doesn't match:

dnf provides nonexistent_pkg -> exit code 1
No matches found. If searching for a file, try specifying the full path or using a wildcard prefix ("*/") at the beginning.

- Some packages match, some don't match:

dnf provides dnf nonexistent_pkg -> exit code 1
<provides output>
No matches found for nonexistent_pkg.
If searching for a file, try specifying the full path or using a wildcard prefix ("*/") at the beginning.

- All queried packages don't match:

dnf provides nonexistent_pkg_1 nonexistent_pkg_2 -> exit code 1
<provides output>
No matches found. If searching for a file, try specifying the full path or using a wildcard prefix ("*/") at the beginning.
  • Loading branch information
inknos committed Nov 23, 2023
1 parent ca3c428 commit 57ab3ca
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 0 deletions.
127 changes: 127 additions & 0 deletions dnf5/commands/provides/provides.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
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 "libdnf5-cli/output/provides.hpp"

#include "provides.hpp"

#include "libdnf5/common/sack/query_cmp.hpp"
#include "libdnf5/conf/option_string.hpp"

#include <libdnf5/conf/const.hpp>
#include <libdnf5/rpm/package_query.hpp>

#include <iostream>

namespace dnf5 {

using namespace libdnf5::cli;

void ProvidesCommand::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 ProvidesCommand::set_argument_parser() {
auto & ctx = get_context();
auto & parser = ctx.get_argument_parser();

auto & cmd = *get_argument_parser_command();
cmd.set_description("Find what package provides the given value");

auto * keys = parser.add_new_positional_arg("specs", ArgumentParser::PositionalArg::AT_LEAST_ONE, nullptr, nullptr);
keys->set_description("List of package specs to query");
keys->set_parse_hook_func(
[this]([[maybe_unused]] ArgumentParser::PositionalArg * arg, int argc, const char * const argv[]) {
for (int i = 0; i < argc; ++i) {
pkg_specs.emplace_back(argv[i]);
}
return true;
});
keys->set_complete_hook_func([&ctx](const char * arg) { return match_specs(ctx, arg, false, true, true, false); });
cmd.register_positional_arg(keys);
}

void ProvidesCommand::configure() {
auto & context = get_context();
context.set_load_system_repo(true);
context.base.get_config().get_optional_metadata_types_option().add_item(
libdnf5::Option::Priority::RUNTIME, libdnf5::METADATA_TYPE_FILELISTS);
context.set_load_available_repos(Context::LoadAvailableRepos::ENABLED);
}

void ProvidesCommand::run() {
auto & ctx = get_context();
bool any_match = false;
std::set<std::string> unmatched_specs;

for (auto & spec : pkg_specs) {
libdnf5::rpm::PackageSet result_pset(ctx.base);
libdnf5::rpm::PackageQuery full_package_query(ctx.base);
auto provides_query = full_package_query;
auto filename_query = full_package_query;
int match = 0;
// if the spec starts with "/" assume it's a provide by filename and skip this query
if (!(spec.rfind("/", 0) == 0)) {
provides_query.filter_provides(std::vector<std::string>({spec}), libdnf5::sack::QueryCmp::GLOB);
if (!provides_query.empty()) {
full_package_query = provides_query;
match = libdnf5::cli::output::ProvidesMatchedBy::PROVIDES;
}
}
// compatibility for packages that didn't do UsrMove
if ((spec.rfind("/bin/", 0) == 0) || (spec.rfind("/sbin/", 0) == 0)) {
spec.insert(0, "/usr");
}
filename_query.filter_file(std::vector<std::string>({spec}), libdnf5::sack::QueryCmp::GLOB);
if (!filename_query.empty()) {
full_package_query = filename_query;
match = libdnf5::cli::output::ProvidesMatchedBy::FILENAME;
}

result_pset |= full_package_query;

for (auto package : result_pset) {
if (match != 0) {
libdnf5::cli::output::print_provides_table(package, spec.c_str(), match);
any_match = true;
} else {
unmatched_specs.insert(spec);
}
}
if (!unmatched_specs.empty() && any_match) {
for (auto const & spec : unmatched_specs) {
std::cerr << "No matches found for " << spec << "." << std::endl;
}
std::cerr << "If searching for a file, try specifying the full "
"path or using a wildcard prefix (\"*/\") at the beginning."
<< std::endl;
throw libdnf5::cli::SilentCommandExitError(1);
}
}
if (!any_match) {
std::cerr << "No matches found. If searching for a file, try specifying the full "
"path or using a wildcard prefix (\"*/\") at the beginning."
<< std::endl;
throw libdnf5::cli::SilentCommandExitError(1);
}
}

} // namespace dnf5
43 changes: 43 additions & 0 deletions dnf5/commands/provides/provides.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
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 DNF5_COMMANDS_PROVIDES_PROVIDES_HPP
#define DNF5_COMMANDS_PROVIDES_PROVIDES_HPP

#include <dnf5/context.hpp>

#include <string>
#include <vector>

namespace dnf5 {

class ProvidesCommand : public Command {
public:
explicit ProvidesCommand(Context & context) : Command(context, "provides") {}
void set_parent_command() override;
void set_argument_parser() override;
void configure() override;
void run() override;

private:
std::vector<std::string> pkg_specs;
};
} // namespace dnf5

#endif // DNF5_COMMANDS_PROVIDES_PROVIDES_HPP
2 changes: 2 additions & 0 deletions dnf5/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "commands/makecache/makecache.hpp"
#include "commands/mark/mark.hpp"
#include "commands/module/module.hpp"
#include "commands/provides/provides.hpp"
#include "commands/reinstall/reinstall.hpp"
#include "commands/remove/remove.hpp"
#include "commands/repo/repo.hpp"
Expand Down Expand Up @@ -667,6 +668,7 @@ static void add_commands(Context & context) {
context.add_and_initialize_command(std::make_unique<SwapCommand>(context));
context.add_and_initialize_command(std::make_unique<MarkCommand>(context));
context.add_and_initialize_command(std::make_unique<AutoremoveCommand>(context));
context.add_and_initialize_command(std::make_unique<ProvidesCommand>(context));

context.add_and_initialize_command(std::make_unique<LeavesCommand>(context));
context.add_and_initialize_command(std::make_unique<RepoqueryCommand>(context));
Expand Down
103 changes: 103 additions & 0 deletions include/libdnf5-cli/output/provides.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
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 Lesser General Public License as published by
the Free Software Foundation, either version 2.1 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with libdnf. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef LIBDNF5_CLI_OUTPUT_PROVIDES_HPP
#define LIBDNF5_CLI_OUTPUT_PROVIDES_HPP

#include "libdnf5-cli/output/key_value_table.hpp"

#include <libdnf5/common/sack/match_string.hpp>
#include <libsmartcols/libsmartcols.h>

#include <iostream>

namespace libdnf5::cli::output {

enum ProvidesMatchedBy : int { PROVIDES = 1, FILENAME = 2 };

static void add_line_into_provides_table(struct libscols_table * table, const char * key, const char * value) {
struct libscols_line * ln = scols_table_new_line(table, nullptr);
scols_line_set_data(ln, 0, key);
scols_line_set_data(ln, 1, value);
}

template <class Package>
static struct libscols_table * create_provides_heading_table(Package & package) {
struct libscols_table * table = scols_new_table();
scols_table_enable_noheadings(table, 1);
scols_table_set_column_separator(table, " : ");
scols_table_new_column(table, "key", 5, 0);
struct libscols_column * cl = scols_table_new_column(table, "value", 10, SCOLS_FL_WRAP);
scols_column_set_safechars(cl, "\n");
scols_column_set_wrapfunc(cl, scols_wrapnl_chunksize, scols_wrapnl_nextchunk, nullptr);

add_line_into_provides_table(table, package.get_nevra().c_str(), package.get_summary().c_str());
return table;
}

template <class Package>
static struct libscols_table * create_provides_table(Package & package, const char * spec, int match) {
struct libscols_table * table = scols_new_table();
// don't print provides if don't match for at least one
if (!match) {
return table;
}
scols_table_enable_noheadings(table, 1);
scols_table_set_column_separator(table, " : ");
scols_table_new_column(table, "key", 5, 0);
struct libscols_column * cl = scols_table_new_column(table, "value", 10, SCOLS_FL_WRAP);
scols_column_set_safechars(cl, "\n");
scols_column_set_wrapfunc(cl, scols_wrapnl_chunksize, scols_wrapnl_nextchunk, nullptr);

add_line_into_provides_table(table, "Repo", package.get_repo_id().c_str());
add_line_into_provides_table(table, "Matched From", "");
switch (match) {
case ProvidesMatchedBy::PROVIDES: {
std::string spec_and_version =
package.get_name() + " = " + package.get_version() + "-" + package.get_release();
add_line_into_provides_table(table, "Provide", spec_and_version.c_str());
break;
}
case ProvidesMatchedBy::FILENAME: {
std::string pattern(spec);
for (const auto & file : package.get_files()) {
if (sack::match_string(file, sack::QueryCmp::GLOB, pattern)) {
add_line_into_provides_table(table, "Filename", file.c_str());
}
}
break;
}
}

return table;
}

template <class Package>
static void print_provides_table(Package & package, const char * spec, int match) {
auto table_head = create_provides_heading_table(package);
auto table = create_provides_table(package, spec, match);
scols_print_table(table_head);
scols_print_table(table);
scols_unref_table(table_head);
scols_unref_table(table);
std::cout << std::endl;
}
} // namespace libdnf5::cli::output

#endif // LIBDNF5_CLI_OUTPUT_PROVIDES_HPP

0 comments on commit 57ab3ca

Please sign in to comment.