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

Make offline transactions work with local rpm files #1586

Merged
merged 4 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions dnf5/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ void Context::Impl::download_and_run(libdnf5::base::Transaction & transaction) {
std::filesystem::create_directories(offline_datadir);

base.get_config().get_destdir_option().set(offline_datadir / "packages");
transaction.set_download_local_pkgs(true);
}

transaction.download();
Expand Down
6 changes: 6 additions & 0 deletions include/libdnf5/base/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ class LIBDNF_API Transaction {
/// specified path.
void store_comps(const std::filesystem::path & comps_path) const;

/// Set whether local packages should be copied to the destination directory during the download().
///
/// Default: false
void set_download_local_pkgs(bool value);
bool get_download_local_pkgs() const noexcept;

private:
friend class TransactionEnvironment;
friend class TransactionGroup;
Expand Down
11 changes: 10 additions & 1 deletion libdnf5/base/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ void Transaction::download() {
libdnf5::repo::PackageDownloader downloader(p_impl->base);
for (auto & tspkg : this->get_transaction_packages()) {
if (transaction_item_action_is_inbound(tspkg.get_action()) &&
tspkg.get_package().get_repo()->get_type() != libdnf5::repo::Repo::Type::COMMANDLINE) {
(get_download_local_pkgs() ||
tspkg.get_package().get_repo()->get_type() != libdnf5::repo::Repo::Type::COMMANDLINE)) {
downloader.add(tspkg.get_package());
}
}
Expand Down Expand Up @@ -1506,4 +1507,12 @@ void Transaction::store_comps(const std::filesystem::path & comps_path) const {
}
}

bool Transaction::get_download_local_pkgs() const noexcept {
return p_impl->download_local_pkgs;
}

void Transaction::set_download_local_pkgs(bool value) {
p_impl->download_local_pkgs = value;
}

} // namespace libdnf5::base
3 changes: 3 additions & 0 deletions libdnf5/base/transaction_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class Transaction::Impl {
// history db transaction id
int64_t history_db_id = 0;

// whether also the command line repo packages should be downloaded to the destination
bool download_local_pkgs{false};

TransactionRunResult _run(
std::unique_ptr<libdnf5::rpm::TransactionCallbacks> && callbacks,
const std::string & description,
Expand Down
25 changes: 24 additions & 1 deletion libdnf5/repo/package_downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "libdnf5/repo/repo_errors.hpp"
#include "libdnf5/utils/bgettext/bgettext-mark-domain.h"

#include <fmt/format.h>
#include <librepo/librepo.h>

#include <algorithm>
Expand Down Expand Up @@ -136,7 +137,6 @@ void PackageDownloader::download() try {

auto & config = p_impl->base->get_config();
auto use_cache_only = config.get_cacheonly_option().get_value() == "all";

GError * err{nullptr};

std::vector<std::unique_ptr<LrPackageTarget>> lr_targets;
Expand All @@ -157,6 +157,29 @@ void PackageDownloader::download() try {
static_cast<double>(pkg_target.package.get_download_size()));
}

if (pkg_target.package.is_available_locally()) {
// Copy local packages to their destination directories
std::filesystem::path source = pkg_target.package.get_package_path();
std::filesystem::path destination = pkg_target.destination / source.filename();
std::error_code ec;
if (!std::filesystem::equivalent(source, destination, ec)) {
std::filesystem::copy(source, destination, std::filesystem::copy_options::overwrite_existing, ec);
if (auto * download_callbacks = pkg_target.package.get_base()->get_download_callbacks()) {
std::string msg;
DownloadCallbacks::TransferStatus status;
if (ec) {
status = DownloadCallbacks::TransferStatus::ERROR;
msg = ec.message();
} else {
status = DownloadCallbacks::TransferStatus::ALREADYEXISTS;
msg = fmt::format("Copied from {}", source.string());
}
download_callbacks->end(pkg_target.user_cb_data, status, msg.c_str());
}
}
continue;
}

auto * lr_target = lr_packagetarget_new_v3(
pkg_target.package.get_repo()->get_downloader().get_cached_handle().get(),
pkg_target.package.get_location().c_str(),
Expand Down
Loading