From e959db7cfd196fe2d8aef84c044856c3c7bddfbd Mon Sep 17 00:00:00 2001 From: Marek Blaha Date: Thu, 5 Sep 2024 10:24:41 +0200 Subject: [PATCH] dnf5: Check offline transaction state before download The transaction package download can be a lengthy and network intensive operation. Begin the download only after the user confirms that the previously scheduled offline transaction can be canceled. --- dnf5/context.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/dnf5/context.cpp b/dnf5/context.cpp index 141b8d2e4..15de0859f 100644 --- a/dnf5/context.cpp +++ b/dnf5/context.cpp @@ -345,7 +345,6 @@ void Context::Impl::store_offline(libdnf5::base::Transaction & transaction) { const auto & installroot = base.get_config().get_installroot_option().get_value(); const auto & offline_datadir = installroot / libdnf5::offline::DEFAULT_DATADIR.relative_path(); std::filesystem::create_directories(offline_datadir); - constexpr const char * packages_in_trans_dir{"./packages"}; constexpr const char * comps_in_trans_dir{"./comps"}; const auto & comps_location = offline_datadir / comps_in_trans_dir; @@ -354,15 +353,6 @@ void Context::Impl::store_offline(libdnf5::base::Transaction & transaction) { libdnf5::offline::OfflineTransactionState state{state_path}; auto & offline_data = state.get_data(); - if (offline_data.get_status() != libdnf5::offline::STATUS_DOWNLOAD_INCOMPLETE) { - print_error("There is already an offline transaction queued, initiated by the following command:"); - print_error(fmt::format("\t{}", offline_data.get_cmd_line())); - print_error("Continuing will cancel the old offline transaction and replace it with this one."); - if (!libdnf5::cli::utils::userconfirm::userconfirm(base.get_config())) { - throw libdnf5::cli::AbortedByUserError(); - } - } - offline_data.set_status(libdnf5::offline::STATUS_DOWNLOAD_INCOMPLETE); state.write(); @@ -432,6 +422,19 @@ void Context::Impl::download_and_run(libdnf5::base::Transaction & transaction) { const auto & installroot = base.get_config().get_installroot_option().get_value(); const auto & offline_datadir = installroot / libdnf5::offline::DEFAULT_DATADIR.relative_path(); std::filesystem::create_directories(offline_datadir); + const std::filesystem::path state_path{offline_datadir / libdnf5::offline::TRANSACTION_STATE_FILENAME}; + libdnf5::offline::OfflineTransactionState state{state_path}; + + // Check whether there is another pending offline transaction present + auto & offline_data = state.get_data(); + if (offline_data.get_status() != libdnf5::offline::STATUS_DOWNLOAD_INCOMPLETE) { + print_error("There is already an offline transaction queued, initiated by the following command:"); + print_error(fmt::format("\t{}", offline_data.get_cmd_line())); + print_error("Continuing will cancel the old offline transaction and replace it with this one."); + if (!libdnf5::cli::utils::userconfirm::userconfirm(base.get_config())) { + throw libdnf5::cli::AbortedByUserError(); + } + } base.get_config().get_destdir_option().set(offline_datadir / "packages"); transaction.set_download_local_pkgs(true);