From edff75edba723a1f3c96e183cf4cfe61b29cb59e Mon Sep 17 00:00:00 2001 From: Marek Blaha Date: Thu, 3 Aug 2023 13:17:54 +0200 Subject: [PATCH] vars: Ignore unreadable vars files. If there is an item in the vars directory that is either inaccessible (e.g. permission denied) or cannot be read from (e.g. a directory), skip this item and write a warning to the log. --- libdnf5/conf/vars.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libdnf5/conf/vars.cpp b/libdnf5/conf/vars.cpp index dcf142d34..0ab219441 100644 --- a/libdnf5/conf/vars.cpp +++ b/libdnf5/conf/vars.cpp @@ -22,6 +22,7 @@ along with libdnf. If not, see . #include "rpm/rpm_log_guard.hpp" #include "utils/fs/file.hpp" +#include "libdnf5/base/base.hpp" #include "libdnf5/common/exception.hpp" #include "libdnf5/utils/bgettext/bgettext-mark-domain.h" @@ -297,6 +298,7 @@ static void dir_close(DIR * d) { } void Vars::load_from_dir(const std::string & directory) { + auto & logger = *base->get_logger(); if (DIR * dir = opendir(directory.c_str())) { std::unique_ptr dir_guard(dir, &dir_close); while (auto ent = readdir(dir)) { @@ -310,9 +312,14 @@ void Vars::load_from_dir(const std::string & directory) { } full_path += dname; - utils::fs::File file(full_path, "r"); std::string line; - file.read_line(line); + try { + utils::fs::File file(full_path, "r"); + file.read_line(line); + } catch (const std::filesystem::filesystem_error & e) { + logger.warning("Cannot load variable from file \"{}\": {}", full_path.c_str(), e.what()); + continue; + } set(dname, line, Priority::VARSDIR); } }