diff --git a/include/libdnf5/base/transaction.hpp b/include/libdnf5/base/transaction.hpp index a2036ba8b..86a004974 100644 --- a/include/libdnf5/base/transaction.hpp +++ b/include/libdnf5/base/transaction.hpp @@ -124,6 +124,10 @@ class Transaction { /// To watch progress or trigger actions during specific transactions events, /// setup the `callbacks` object. /// + /// After a successful transaction, any temporarily downloaded packages are removed + /// if the 'keepcache' option is set to 'false' and the transaction involved an inbound action. + /// Otherwise, the packages are preserved on the disk. + /// /// @return An enum describing the result of running the transaction. TransactionRunResult run(); diff --git a/libdnf5/base/transaction.cpp b/libdnf5/base/transaction.cpp index 67285c543..5fdaeeac8 100644 --- a/libdnf5/base/transaction.cpp +++ b/libdnf5/base/transaction.cpp @@ -524,6 +524,15 @@ static void process_scriptlets_output(int fd, Logger * logger) { close(fd); } +static bool contains_any_inbound_package(std::vector & packages) { + for (const auto & package : packages) { + if (transaction_item_action_is_inbound(package.get_action())) { + return true; + } + } + return false; +} + Transaction::TransactionRunResult Transaction::Impl::test() { return this->_run(std::make_unique(), "", std::nullopt, "", true); } @@ -869,18 +878,24 @@ Transaction::TransactionRunResult Transaction::Impl::_run( if (ret == 0) { // removes any temporarily stored packages from the system - libdnf5::repo::TempFilesMemory temp_files_memory(config.get_cachedir_option().get_value()); - auto temp_files = temp_files_memory.get_files(); - for (auto & file : temp_files) { - try { - if (!std::filesystem::remove(file)) { - logger->debug("Temporary file \"{}\" doesn't exist.", file); + // only if any inbound action takes place + auto keepcache = config.get_keepcache_option().get_value(); + auto any_inbound_action_present = contains_any_inbound_package(packages); + if (!keepcache && any_inbound_action_present) { + libdnf5::repo::TempFilesMemory temp_files_memory(config.get_cachedir_option().get_value()); + auto temp_files = temp_files_memory.get_files(); + for (auto & file : temp_files) { + try { + if (!std::filesystem::remove(file)) { + logger->debug("Temporary file \"{}\" doesn't exist.", file); + } + } catch (const std::filesystem::filesystem_error & ex) { + logger->debug( + "An error occurred when trying to remove a temporary file \"{}\": {}", file, ex.what()); } - } catch (const std::filesystem::filesystem_error & ex) { - logger->debug("An error occurred when trying to remove a temporary file \"{}\": {}", file, ex.what()); } + temp_files_memory.clear(); } - temp_files_memory.clear(); return TransactionRunResult::SUCCESS; } else {