Skip to content

Commit

Permalink
libdnf5: Add a plugin to download and install repo's Appstream data
Browse files Browse the repository at this point in the history
Repositories can provide Appstream data for the packages they contain.
This Appstream data is consumed by applications like the GNOME Software
or the KDE Discover, thus the users can see the packages (apps) in them.

This is to be in pair with PackageKit, which does download and install
the repo's Appstream data.

The plugin is built by default, but the Appstream data is not downloaded
unless "optional_metadata_types" config option contains "appstream".

The plugin adds a dependency on the `appstream` library. It can be disabled
with a WITH_PLUGIN_APPSTREAM CMake option set to OFF.

Closes #1564
  • Loading branch information
mcrha committed Jan 7, 2025
1 parent 1363f3e commit 68ed27b
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 11 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ option(WITH_LIBDNF5_CLI "Build library for working with a terminal in a command-
option(WITH_DNF5 "Build dnf5 command-line package manager" ON)
option(WITH_DNF5_PLUGINS "Build plugins for dnf5 command-line package manager" ON)
option(WITH_PLUGIN_ACTIONS "Build a dnf5 actions plugin" ON)
option(WITH_PLUGIN_APPSTREAM "Build with plugin to install repo's Appstream metadata" ON)
option(WITH_PLUGIN_RHSM "Build a libdnf5 rhsm (Red Hat Subscription Manager) plugin" OFF)
option(WITH_PYTHON_PLUGINS_LOADER "Build a special dnf5 plugin that loads Python plugins. Requires WITH_PYTHON3=ON." ON)

Expand Down
20 changes: 20 additions & 0 deletions dnf5.spec
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Provides: dnf5-command(versionlock)
%bcond_without dnf5
%bcond_without dnf5_plugins
%bcond_without plugin_actions
%bcond_without plugin_appstream
%bcond_without plugin_rhsm
%bcond_without python_plugins_loader

Expand Down Expand Up @@ -604,6 +605,24 @@ Libdnf5 plugin that allows to run actions (external executables) on hooks.
%{_mandir}/man8/libdnf5-actions.8.*
%endif

# ========== libdnf5-plugin-appstream ==========

%if %{with plugin_appstream}

%package -n libdnf5-plugin-appstream
Summary: Libdnf5 plugin to install repo Appstream data
License: LGPL-2.1-or-later
Requires: libdnf5%{?_isa} = %{version}-%{release}
BuildRequires: pkgconfig(appstream) >= 0.16

%description -n libdnf5-plugin-appstream
Libdnf5 plugin that installs repository's Appstream data, for repositories which provide them.

%files -n libdnf5-plugin-appstream
%{_libdir}/libdnf5/plugins/appstream.so
%config %{_sysconfdir}/dnf/libdnf5-plugins/appstream.conf

%endif

# ========== libdnf5-plugin-plugin_rhsm ==========

Expand Down Expand Up @@ -805,6 +824,7 @@ automatically and regularly from systemd timers, cron jobs or similar.
-DWITH_LIBDNF5_CLI=%{?with_libdnf_cli:ON}%{!?with_libdnf_cli:OFF} \
-DWITH_DNF5=%{?with_dnf5:ON}%{!?with_dnf5:OFF} \
-DWITH_PLUGIN_ACTIONS=%{?with_plugin_actions:ON}%{!?with_plugin_actions:OFF} \
-DWITH_PLUGIN_APPSTREAM=%{?with_plugin_appstream:ON}%{!?with_plugin_appstream:OFF} \
-DWITH_PLUGIN_RHSM=%{?with_plugin_rhsm:ON}%{!?with_plugin_rhsm:OFF} \
-DWITH_PYTHON_PLUGINS_LOADER=%{?with_python_plugins_loader:ON}%{!?with_python_plugins_loader:OFF} \
\
Expand Down
8 changes: 7 additions & 1 deletion include/libdnf5/conf/const.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ constexpr const char * METADATA_TYPE_OTHER = "other";
constexpr const char * METADATA_TYPE_PRESTO = "presto";
constexpr const char * METADATA_TYPE_UPDATEINFO = "updateinfo";
constexpr const char * METADATA_TYPE_ALL = "all";
constexpr const char * METADATA_TYPE_APPSTREAM = "appstream";

const std::set<std::string> OPTIONAL_METADATA_TYPES{
METADATA_TYPE_COMPS, METADATA_TYPE_FILELISTS, METADATA_TYPE_OTHER, METADATA_TYPE_PRESTO, METADATA_TYPE_UPDATEINFO};
METADATA_TYPE_COMPS,
METADATA_TYPE_FILELISTS,
METADATA_TYPE_OTHER,
METADATA_TYPE_PRESTO,
METADATA_TYPE_UPDATEINFO,
METADATA_TYPE_APPSTREAM};

} // namespace libdnf5

Expand Down
3 changes: 3 additions & 0 deletions include/libdnf5/repo/repo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ class LIBDNF_API Repo {
// @replaces libdnf:repo/Repo.hpp:method:Repo.downloadMetadata(const std::string & destdir)
void download_metadata(const std::string & destdir);

/// Returns a list of pairs of the rpmmd type and filename of the Appstream data of the repo
std::vector<std::pair<std::string, std::string>> get_appstream_metadata() const;

private:
class LIBDNF_LOCAL Impl;
friend class RepoSack;
Expand Down
1 change: 1 addition & 0 deletions libdnf5-plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_C_VISIBILITY_PRESET hidden)

add_subdirectory("actions")
add_subdirectory("appstream")
add_subdirectory("python_plugins_loader")
add_subdirectory("rhsm")
17 changes: 17 additions & 0 deletions libdnf5-plugins/appstream/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
if(NOT WITH_PLUGIN_APPSTREAM)
return()
endif()

add_library(appstream_plugin MODULE appstream.cpp)

# disable the 'lib' prefix in order to create appstream.so
set_target_properties(appstream_plugin PROPERTIES PREFIX "")
set_target_properties(appstream_plugin PROPERTIES OUTPUT_NAME "appstream")

pkg_check_modules(APPSTREAM REQUIRED appstream>=0.16)
include_directories(${APPSTREAM_INCLUDE_DIRS})
target_link_libraries(appstream_plugin PRIVATE ${APPSTREAM_LIBRARIES})
target_link_libraries(appstream_plugin PRIVATE libdnf5)

install(TARGETS appstream_plugin LIBRARY DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/libdnf5/plugins/")
install(FILES "appstream.conf" DESTINATION "${CMAKE_INSTALL_FULL_SYSCONFDIR}/dnf/libdnf5-plugins")
3 changes: 3 additions & 0 deletions libdnf5-plugins/appstream/appstream.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[main]
name = appstream
enabled = 1
128 changes: 128 additions & 0 deletions libdnf5-plugins/appstream/appstream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
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 <appstream.h>
#include <libdnf5/base/base.hpp>
#include <libdnf5/conf/const.hpp>
#include <libdnf5/plugin/iplugin.hpp>
#include <libdnf5/repo/repo.hpp>
#include <libdnf5/repo/repo_query.hpp>

#include <iostream>

using namespace libdnf5;

namespace {

constexpr const char * PLUGIN_NAME{"appstream"};
constexpr libdnf5::plugin::Version PLUGIN_VERSION{.major = 1, .minor = 0, .micro = 0};

constexpr const char * attrs[]{"author.name", "author.email", "description", nullptr};
constexpr const char * attrs_value[]{"Milan Crha", "[email protected]", "install repo Appstream data."};

class AppstreamPlugin : public plugin::IPlugin {
public:
AppstreamPlugin(libdnf5::plugin::IPluginData & data, libdnf5::ConfigParser &) : IPlugin(data) {}
virtual ~AppstreamPlugin() = default;

PluginAPIVersion get_api_version() const noexcept override { return PLUGIN_API_VERSION; }

const char * get_name() const noexcept override { return PLUGIN_NAME; }

plugin::Version get_version() const noexcept override { return PLUGIN_VERSION; }

const char * const * get_attributes() const noexcept override { return attrs; }

const char * get_attribute(const char * attribute) const noexcept override {
for (size_t i = 0; attrs[i]; ++i) {
if (std::strcmp(attribute, attrs[i]) == 0) {
return attrs_value[i];
}
}
return nullptr;
}

void repos_loaded() override {
Base & base = get_base();
repo::RepoQuery repos(base);
repos.filter_enabled(true);
for (const auto & repo : repos) {
auto type = repo->get_type();
if (type == repo::Repo::Type::AVAILABLE || type == repo::Repo::Type::SYSTEM) {
install_appstream(repo.get());
}
}
}

private:
void install_appstream(libdnf5::repo::Repo * repo);
};

void AppstreamPlugin::install_appstream(libdnf5::repo::Repo * repo) {
libdnf5::Base & base = get_base();
if (!repo->get_config().get_main_config().get_optional_metadata_types_option().get_value().contains(
libdnf5::METADATA_TYPE_APPSTREAM))
return;

std::string repo_id = repo->get_config().get_id();
auto appstream_metadata = repo->get_appstream_metadata();
for (auto & item : appstream_metadata) {
const std::string path = item.second;
GError * local_error = NULL;

if (!as_utils_install_metadata_file(
AS_METADATA_LOCATION_CACHE, path.c_str(), repo_id.c_str(), NULL, &local_error)) {
base.get_logger()->debug(
"Failed to install Appstream metadata file '{}' for repo '{}': {}",
path,
repo_id,
local_error ? local_error->message : "Unknown error");
}

g_clear_error(&local_error);
}
}

} // namespace


PluginAPIVersion libdnf_plugin_get_api_version(void) {
return PLUGIN_API_VERSION;
}

const char * libdnf_plugin_get_name(void) {
return PLUGIN_NAME;
}

plugin::Version libdnf_plugin_get_version(void) {
return PLUGIN_VERSION;
}

plugin::IPlugin * libdnf_plugin_new_instance(
[[maybe_unused]] LibraryVersion library_version,
libdnf5::plugin::IPluginData & data,
libdnf5::ConfigParser & parser) try {
return new AppstreamPlugin(data, parser);
} catch (...) {
return nullptr;
}

void libdnf_plugin_delete_instance(plugin::IPlugin * plugin_object) {
delete plugin_object;
}
11 changes: 10 additions & 1 deletion libdnf5/repo/repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ extern "C" {
#include <filesystem>
#include <set>


namespace libdnf5::repo {

static void is_readable_rpm(const std::string & fn) {
Expand Down Expand Up @@ -203,6 +202,9 @@ void Repo::read_metadata_cache() {
p_impl->downloader->load_local();
}

std::vector<std::pair<std::string, std::string>> Repo::get_appstream_metadata() const {
return get_downloader().get_appstream_metadata();
}

bool Repo::is_in_sync() {
if (!p_impl->config.get_metalink_option().empty() && !p_impl->config.get_metalink_option().get_value().empty()) {
Expand Down Expand Up @@ -413,6 +415,13 @@ void Repo::load_available_repo() {
auto optional_metadata = p_impl->config.get_main_config().get_optional_metadata_types_option().get_value();
const bool all_metadata = optional_metadata.contains(libdnf5::METADATA_TYPE_ALL);

if (all_metadata || optional_metadata.contains(libdnf5::METADATA_TYPE_APPSTREAM)) {
auto appstream_metadata = p_impl->downloader->get_appstream_metadata();
for (auto & item : appstream_metadata) {
p_impl->solv_repo->load_repo_ext(RepodataType::APPSTREAM, item.first, *p_impl->downloader.get());
}
}

if (all_metadata || optional_metadata.contains(libdnf5::METADATA_TYPE_FILELISTS)) {
p_impl->solv_repo->load_repo_ext(RepodataType::FILELISTS, *p_impl->downloader.get());
}
Expand Down
47 changes: 46 additions & 1 deletion libdnf5/repo/repo_downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,25 @@ const std::string & RepoDownloader::get_metadata_path(const std::string & metada
return it != metadata_paths.end() ? it->second : empty;
}

bool RepoDownloader::is_appstream_metadata_type(const std::string & type) const {
/* TODO: make the list configurable with this default */
return utils::string::starts_with(type, "appstream") || utils::string::starts_with(type, "appdata");
}

std::vector<std::pair<std::string, std::string>> RepoDownloader::get_appstream_metadata() const {
std::vector<std::pair<std::string, std::string>> appstream_metadata;
/* The RepoDownloader::common_handle_setup() sets the expected names,
check for the starts_with() only here, to avoid copying the list here. */

for (auto it = metadata_paths.begin(); it != metadata_paths.end(); it++) {
const std::string type = it->first;
const std::string path = it->second;

if (is_appstream_metadata_type(type))
appstream_metadata.push_back(std::pair<std::string, std::string>(type, path));
}
return appstream_metadata;
}

LibrepoHandle RepoDownloader::init_local_handle() {
LibrepoHandle h;
Expand Down Expand Up @@ -497,8 +516,34 @@ void RepoDownloader::common_handle_setup(LibrepoHandle & h) {

// download the rest metadata added by 3rd parties
for (auto & item : optional_metadata) {
dlist.push_back(item.c_str());
// the appstream metadata is a "virtual" type, the list
// of the types is read from the repomd file
if (item.compare(libdnf5::METADATA_TYPE_APPSTREAM) != 0)
dlist.push_back(item.c_str());
}
if (optional_metadata.extract(libdnf5::METADATA_TYPE_APPSTREAM)) {
// ideally, the repomd.xml file should be read and every type matching is_appstream_metadata_type()
// would be added from it, but the content is not known at this point and when it is known, then
// it's too late, thus declare some "expected" types to be downloaded here
dlist.push_back("appstream");
dlist.push_back("appstream-icons");
dlist.push_back("appstream-icons-48x48");
dlist.push_back("appstream-icons-48x48@2");
dlist.push_back("appstream-icons-64x64");
dlist.push_back("appstream-icons-64x64@2");
dlist.push_back("appstream-icons-128x128");
dlist.push_back("appstream-icons-128x128@2");
// consult the prefixes with the is_appstream_metadata_type()
dlist.push_back("appdata");
dlist.push_back("appdata-icons");
dlist.push_back("appdata-icons-48x48");
dlist.push_back("appdata-icons-48x48@2");
dlist.push_back("appdata-icons-64x64");
dlist.push_back("appdata-icons-64x64@2");
dlist.push_back("appdata-icons-128x128");
dlist.push_back("appdata-icons-128x128@2");
}

dlist.push_back(nullptr);
h.set_opt(LRO_YUMDLIST, dlist.data());
}
Expand Down
3 changes: 3 additions & 0 deletions libdnf5/repo/repo_downloader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class RepoDownloader {
static constexpr const char * MD_FILENAME_GROUP_GZ = "group_gz";
static constexpr const char * MD_FILENAME_GROUP = "group";
static constexpr const char * MD_FILENAME_MODULES = "modules";
static constexpr const char * MD_FILENAME_APPSTREAM = "appstream";

RepoDownloader(const libdnf5::BaseWeakPtr & base, const ConfigRepo & config, Repo::Type repo_type);

Expand All @@ -74,6 +75,7 @@ class RepoDownloader {
void * get_user_data() const noexcept;

const std::string & get_metadata_path(const std::string & metadata_type) const;
std::vector<std::pair<std::string, std::string>> get_appstream_metadata() const;


private:
Expand All @@ -99,6 +101,7 @@ class RepoDownloader {
time_t get_system_epoch() const;

std::set<std::string> get_optional_metadata() const;
bool is_appstream_metadata_type(const std::string & type) const;

libdnf5::BaseWeakPtr base;
const ConfigRepo & config;
Expand Down
Loading

0 comments on commit 68ed27b

Please sign in to comment.