From 4ba7211c9454d9ee40982b25185e4530771c01f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= Date: Wed, 18 Sep 2024 13:09:29 +0200 Subject: [PATCH] dnf clean: Do not report an error on a nonexistent cache directory If /var/cache/libdnf5 directory did not exist, "dnf clean all" reported and error: Cannot iterate the cache directory: "/var/cache/libdnf5" This patch optimizes this case to no operation. If there is no cache, there is nothing to remove. If the iterator fails for a different reason, the message is ammended with an operating system error message, explaining the cause. Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2313032 --- dnf5/commands/clean/clean.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/dnf5/commands/clean/clean.cpp b/dnf5/commands/clean/clean.cpp index deaa76206..2c38f01d6 100644 --- a/dnf5/commands/clean/clean.cpp +++ b/dnf5/commands/clean/clean.cpp @@ -22,10 +22,12 @@ along with libdnf. If not, see . #include "dnf5/shared_options.hpp" +#include #include #include #include #include +#include #include #include @@ -168,7 +170,14 @@ void CleanCommand::run() { } if (ec) { - throw std::runtime_error(fmt::format("Cannot iterate the cache directory: \"{}\"", cachedir.string())); + if (ec.value() == ENOENT) { + std::cout << libdnf5::utils::sformat( + _("Cache directory \"{}\" does not exist. Nothing to clean."), cachedir.string()) + << std::endl; + return; + } + throw std::runtime_error(libdnf5::utils::sformat( + _("Cannot iterate the cache directory: \"{}\": {}"), cachedir.string(), ec.message())); } std::cout << fmt::format(