Skip to content

Commit

Permalink
dnf clean: Do not report an error on a nonexistent cache directory
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ppisar committed Sep 18, 2024
1 parent 76fef26 commit 1e8e10e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion dnf5/commands/clean/clean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "dnf5/shared_options.hpp"

#include <errno.h>
#include <libdnf5-cli/argument_parser.hpp>
#include <libdnf5/repo/repo_cache.hpp>
#include <libdnf5/utils/bgettext/bgettext-lib.h>
#include <libdnf5/utils/bgettext/bgettext-mark-domain.h>
#include <libdnf5/utils/format.hpp>

#include <filesystem>
#include <iostream>
Expand Down Expand Up @@ -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 cache directory \"{}\": {}"), cachedir.string(), ec.message()));
}

std::cout << fmt::format(
Expand Down

0 comments on commit 1e8e10e

Please sign in to comment.