1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-10-05 19:31:04 +00:00

Introduce proper error handling for file streams

This commit is contained in:
sfan5 2024-05-08 20:37:10 +02:00
parent c38e0d05bf
commit 39fd9b93c3
23 changed files with 235 additions and 149 deletions

View file

@ -278,9 +278,7 @@ void Client::scanModSubfolder(const std::string &mod_name, const std::string &mo
<< "\" as \"" << vfs_path << "\"." << std::endl;
std::string contents;
if (!fs::ReadFile(real_path, contents)) {
errorstream << "Client::scanModSubfolder(): Can't read file \""
<< real_path << "\"." << std::endl;
if (!fs::ReadFile(real_path, contents, true)) {
continue;
}
@ -1945,8 +1943,7 @@ void Client::makeScreenshot()
while (serial < SCREENSHOT_MAX_SERIAL_TRIES) {
filename = filename_base + (serial > 0 ? ("_" + itos(serial)) : "") + filename_ext;
std::ifstream tmp(filename.c_str());
if (!tmp.good())
if (!fs::PathExists(filename))
break; // File did not apparently exist, we'll go with it
serial++;
}

View file

@ -38,13 +38,9 @@ void FileCache::createDir()
bool FileCache::loadByPath(const std::string &path, std::ostream &os)
{
std::ifstream fis(path.c_str(), std::ios_base::binary);
if(!fis.good()){
verbosestream<<"FileCache: File not found in cache: "
<<path<<std::endl;
auto fis = open_ifstream(path.c_str(), true);
if (!fis.good())
return false;
}
bool bad = false;
for(;;){
@ -70,15 +66,10 @@ bool FileCache::loadByPath(const std::string &path, std::ostream &os)
bool FileCache::updateByPath(const std::string &path, std::string_view data)
{
createDir();
std::ofstream file(path.c_str(), std::ios_base::binary |
std::ios_base::trunc);
if(!file.good())
{
errorstream<<"FileCache: Can't write to file at "
<<path<<std::endl;
auto file = open_ofstream(path.c_str(), true);
if (!file.good())
return false;
}
file << data;
file.close();
@ -101,8 +92,7 @@ bool FileCache::load(const std::string &name, std::ostream &os)
bool FileCache::exists(const std::string &name)
{
std::string path = m_dir + DIR_DELIM + name;
std::ifstream fis(path.c_str(), std::ios_base::binary);
return fis.good();
return fs::PathExists(path);
}
bool FileCache::updateCopyFile(const std::string &name, const std::string &src_path)

View file

@ -160,10 +160,10 @@ public:
private:
StringMap m_programs;
std::string readFile(const std::string &path)
inline std::string readFile(const std::string &path)
{
std::string ret;
if (!fs::ReadFile(path, ret))
if (!fs::ReadFile(path, ret, true))
ret.clear();
return ret;
}

View file

@ -710,7 +710,8 @@ std::string ShadowRenderer::readShaderFile(const std::string &path)
prefix.append("#line 0\n");
std::string content;
fs::ReadFile(path, content);
if (!fs::ReadFile(path, content, true))
return "";
return prefix + content;
}