Skip to content

Commit 8f49d40

Browse files
committed
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 <[email protected]>
1 parent 70e0d4c commit 8f49d40

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/lib/object_store/File.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@ File::~File()
143143
}
144144
}
145145

146+
// Check if a file exists without trying to open it
147+
// May be used when a routine just wants to check file existence,
148+
// and does not want to throw an error by trying to open it.
149+
bool File::exists(const std::string& path)
150+
{
151+
#ifndef _WIN32
152+
struct stat buf;
153+
return stat(path.c_str(), &buf) == 0;
154+
#else
155+
return _access(path.c_str(), 0) == 0;
156+
#endif
157+
}
158+
159+
146160
// Check if the file is valid
147161
bool File::isValid()
148162
{

src/lib/object_store/File.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class File
4747
// Destructor
4848
virtual ~File();
4949

50+
// Check if a file exists without trying to open it
51+
// May be used when a routine just wants to check file existence,
52+
// and does not want to throw an error by trying to open it.
53+
static bool exists(const std::string& path);
54+
5055
// Check if the file is valid
5156
bool isValid();
5257

src/lib/object_store/Generation.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ bool Generation::sync(File &objectFile)
8888
// Check if the target was updated
8989
bool Generation::wasUpdated()
9090
{
91+
92+
// Check if path file exists before anything
93+
if (!File::exists(path))
94+
{
95+
return true;
96+
}
97+
9198
if (isToken)
9299
{
93100
MutexLocker lock(genMutex);

0 commit comments

Comments
 (0)