diff --git a/src/lib/object_store/File.cpp b/src/lib/object_store/File.cpp index 74f1adfb2..63d05dcae 100644 --- a/src/lib/object_store/File.cpp +++ b/src/lib/object_store/File.cpp @@ -143,6 +143,20 @@ File::~File() } } +// Check if a file exists without trying to open it +// May be used when a routine just wants to check file existence, +// and does not want to throw an error by trying to open it. +bool File::exists(const std::string& path) +{ +#ifndef _WIN32 + struct stat buf; + return stat(path.c_str(), &buf) == 0; +#else + return _access(path.c_str(), 0) == 0; +#endif +} + + // Check if the file is valid bool File::isValid() { diff --git a/src/lib/object_store/File.h b/src/lib/object_store/File.h index 433ee58cc..2c38ddc68 100644 --- a/src/lib/object_store/File.h +++ b/src/lib/object_store/File.h @@ -47,6 +47,11 @@ class File // Destructor virtual ~File(); + // Check if a file exists without trying to open it + // May be used when a routine just wants to check file existence, + // and does not want to throw an error by trying to open it. + static bool exists(const std::string& path); + // Check if the file is valid bool isValid(); diff --git a/src/lib/object_store/Generation.cpp b/src/lib/object_store/Generation.cpp index cb858796c..f79b8cb97 100644 --- a/src/lib/object_store/Generation.cpp +++ b/src/lib/object_store/Generation.cpp @@ -88,6 +88,13 @@ bool Generation::sync(File &objectFile) // Check if the target was updated bool Generation::wasUpdated() { + + // Check if path file exists before anything + if (!File::exists(path)) + { + return true; + } + if (isToken) { MutexLocker lock(genMutex);