Skip to content

Commit

Permalink
vars: Ignore unreadable vars files.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
m-blaha authored and jan-kolarik committed Aug 4, 2023
1 parent 21ec9ca commit edff75e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions libdnf5/conf/vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#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"

Expand Down Expand Up @@ -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, decltype(&dir_close)> dir_guard(dir, &dir_close);
while (auto ent = readdir(dir)) {
Expand All @@ -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);
}
}
Expand Down

0 comments on commit edff75e

Please sign in to comment.