Skip to content

Commit

Permalink
repo: New Repo.get_packages_download_dir() method
Browse files Browse the repository at this point in the history
Currently, the path where a package should be downloaded is computed in
several places. This patch introduces a single location for this
computation to reduce code duplication.
The patch also fixes usage of incorrect package path in
Package::get_package_path() call - the destdir config option was not
taken into account.
  • Loading branch information
m-blaha committed Aug 14, 2024
1 parent 07105d0 commit 1e83b07
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
6 changes: 6 additions & 0 deletions include/libdnf5/repo/repo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "libdnf5/repo/repo_weak.hpp"
#include "libdnf5/rpm/package.hpp"

#include <filesystem>
#include <memory>


Expand Down Expand Up @@ -372,6 +373,11 @@ class LIBDNF_API Repo {
// Add xml comps file at `path` to the repository.
LIBDNF_LOCAL void add_xml_comps(const std::string & path);

/// Gets path to the directory where repository packages are downloaded.
/// Takes destdir config option into account.
LIBDNF_LOCAL std::filesystem::path get_packages_download_dir() const;


std::unique_ptr<Impl> p_impl;
};

Expand Down
8 changes: 1 addition & 7 deletions libdnf5/repo/package_downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,7 @@ PackageDownloader::~PackageDownloader() = default;


void PackageDownloader::add(const libdnf5::rpm::Package & package, void * user_data) {
auto default_path = std::filesystem::path(package.get_repo()->get_cachedir()) / "packages";
auto & destdir_option = p_impl->base->get_config().get_destdir_option();
if (destdir_option.empty()) {
add(package, default_path, user_data);
} else {
add(package, destdir_option.get_value(), user_data);
}
add(package, package.get_repo()->get_packages_download_dir(), user_data);
}


Expand Down
8 changes: 8 additions & 0 deletions libdnf5/repo/repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,5 +626,13 @@ void Repo::mark_fresh() {
p_impl->expired = false;
}

std::filesystem::path Repo::get_packages_download_dir() const {
auto destdir_option = p_impl->base->get_config().get_destdir_option();
if (destdir_option.empty()) {
return std::filesystem::path(get_cachedir()) / "packages";
} else {
return destdir_option.get_value();
}
}

} // namespace libdnf5::repo
2 changes: 1 addition & 1 deletion libdnf5/rpm/package.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ std::string Package::get_package_path() const {
return get_location();
}
// Returns the path to the cached file.
auto dir = std::filesystem::path(repo->get_cachedir()) / "packages";
auto dir = repo->get_packages_download_dir();
return dir / std::filesystem::path(get_location()).filename();
} else {
return "";
Expand Down

0 comments on commit 1e83b07

Please sign in to comment.