config: Better error messages when opening files

If a config file is a directory, ifstream would just read it as an empty
file without any errors.

Failing early here is a good idea.
This commit is contained in:
patrick96
2020-11-25 01:35:38 +01:00
committed by Patrick Ziegler
parent 9d31b51a63
commit 75eb41f5ad
4 changed files with 31 additions and 2 deletions

View File

@ -1,8 +1,13 @@
#include "components/config_parser.hpp"
#include <algorithm>
#include <cerrno>
#include <cstring>
#include <fstream>
#include "utils/file.hpp"
#include "utils/string.hpp"
POLYBAR_NS
config_parser::config_parser(const logger& logger, string&& file, string&& bar)
@ -89,6 +94,14 @@ void config_parser::parse_file(const string& file, file_list path) {
throw application_error("include-file: Dependency cycle detected:\n" + path_str);
}
if (!file_util::exists(file)) {
throw application_error("Failed to open config file " + file + ": " + strerror(errno));
}
if (!file_util::is_file(file)) {
throw application_error("Config file " + file + " is not a file");
}
m_log.trace("config_parser: Parsing %s", file);
int file_index;