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

@ -170,12 +170,29 @@ int fd_streambuf::underflow() {
namespace file_util {
/**
* Checks if the given file exist
*
* May also return false if the file status cannot be read
*
* Sets errno when returning false
*/
bool exists(const string& filename) {
struct stat buffer {};
return stat(filename.c_str(), &buffer) == 0;
}
/**
* Checks if the given path exists and is a file
*/
bool is_file(const string& filename) {
struct stat buffer {};
if (stat(filename.c_str(), &buffer) != 0) {
return false;
}
return S_ISREG(buffer.st_mode);
}
/**
* Picks the first existing file out of given entries
*/