Skip to content

module: Warn if module config file is inaccessible #1695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libdnf/conf/ConfigParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ void ConfigParser::read(const std::string & filePath)
try {
IniParser parser(filePath);
::libdnf::read(*this, parser);
} catch (const IniParser::FileDoesNotExist & e) {
throw FileDoesNotExist(e.what());
} catch (const IniParser::CantOpenFile & e) {
throw CantOpenFile(e.what());
} catch (const IniParser::Exception & e) {
Expand Down
3 changes: 3 additions & 0 deletions libdnf/conf/ConfigParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ struct ConfigParser {
struct CantOpenFile : public Exception {
CantOpenFile(const std::string & what) : Exception(what) {}
};
struct FileDoesNotExist : public CantOpenFile {
FileDoesNotExist(const std::string & what) : CantOpenFile(what) {}
};
struct ParsingError : public Exception {
ParsingError(const std::string & what) : Exception(what) {}
};
Expand Down
11 changes: 8 additions & 3 deletions libdnf/module/ModulePackageContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1370,10 +1370,10 @@ static inline void
parseConfig(ConfigParser &parser, const std::string &name, const char *path)
{
auto logger(Log::getLogger());
const auto fname = name + ".module";
g_autofree gchar * cfn = g_build_filename(path, fname.c_str(), NULL);

try {
const auto fname = name + ".module";
g_autofree gchar * cfn = g_build_filename(path, fname.c_str(), NULL);
parser.read(cfn);

/* FIXME: init empty config or throw error? */
Expand All @@ -1393,10 +1393,15 @@ parseConfig(ConfigParser &parser, const std::string &name, const char *path)
parser.setValue(name, "state", parser.getValue(name, "enabled"));
parser.removeOption(name, "enabled");
}
} catch (const ConfigParser::CantOpenFile &) {
} catch (const ConfigParser::FileDoesNotExist &) {
/* No module config file present. Fill values in */
initConfig(parser, name);
return;
} catch (const ConfigParser::CantOpenFile &) {
/* File exists but is not readable. */
logger->warning(tfm::format("Cannot read \"%s\". Modular filtering may be affected.", cfn));
initConfig(parser, name);
return;
}
}

Expand Down
15 changes: 14 additions & 1 deletion libdnf/utils/iniparser/iniparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@

#include "iniparser.hpp"

#include <errno.h>
#include <sys/stat.h>

constexpr char DELIMITER = '\n';

const char * IniParser::CantOpenFile::what() const noexcept
{
return "IniParser: Can't open file";
}

const char * IniParser::FileDoesNotExist::what() const noexcept
{
return "IniParser: File does not exist";
}

const char * IniParser::MissingSectionHeader::what() const noexcept
{
return "IniParser: Missing section header";
Expand Down Expand Up @@ -65,8 +73,13 @@ const char * IniParser::MissingEqual::what() const noexcept
IniParser::IniParser(const std::string & filePath)
: is(new std::ifstream(filePath))
{
if (!(*is))
if (!(*is)) {
struct stat buffer;
if (stat(filePath.c_str(), &buffer) != 0 && errno == ENOENT) {
throw FileDoesNotExist();
}
throw CantOpenFile();
}
is->exceptions(std::ifstream::badbit);
lineNumber = 0;
lineReady = false;
Expand Down
4 changes: 4 additions & 0 deletions libdnf/utils/iniparser/iniparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class IniParser {
CantOpenFile() {}
const char * what() const noexcept override;
};
struct FileDoesNotExist : public CantOpenFile {
FileDoesNotExist() {}
const char * what() const noexcept override;
};
struct MissingSectionHeader : public Exception {
MissingSectionHeader(int lineNumber) : Exception(lineNumber) {}
const char * what() const noexcept override;
Expand Down
Loading