From 8f49d405b7843cbb5ea9542bfc13ff61febf8965 Mon Sep 17 00:00:00 2001 From: Alexandre Besnard Date: Tue, 16 Dec 2025 10:49:30 +0100 Subject: [PATCH] fix: object_store: stop throwing errors when checking generation file A new routine is added to File class, to check whether a file exists or not. It will allow file existence check, without trying to open the file and thus throw an error when the use case is valid. This new routine is added to Generation wasUpdated routine: if the generation file does not exist, the routine will exit without error, instead of trying to open the file and report an error. Fixes #753 Signed-off-by: Alexandre Besnard --- src/lib/object_store/File.cpp | 14 ++++++++++++++ src/lib/object_store/File.h | 5 +++++ src/lib/object_store/Generation.cpp | 7 +++++++ 3 files changed, 26 insertions(+) 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);