Skip to content

Commit dda968a

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 <alexandre.besnard@softathome.com>
1 parent 70e0d4c commit dda968a

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/lib/object_store/File.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,28 @@ 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(std::string path)
150+
{
151+
#ifndef _WIN32
152+
struct stat buf;
153+
return stat(path.c_str(), &buf) == 0;
154+
#else
155+
int fd;
156+
// Try opening the file directly
157+
fd = _open(path.c_str(), _O_BINARY | _O_RDONLY, _S_IREAD | _S_IWRITE);
158+
if (fd != -1)
159+
{
160+
_close(fd);
161+
return true;
162+
}
163+
return false;
164+
#endif
165+
}
166+
167+
146168
// Check if the file is valid
147169
bool File::isValid()
148170
{

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(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)