Skip to content

Commit 1bb911f

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 1bb911f

4 files changed

Lines changed: 32 additions & 2 deletions

File tree

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

src/lib/object_store/test/FileTests.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,18 @@ void FileTests::testExistNotExist()
7474
// Test pre-condition
7575
CPPUNIT_ASSERT(!exists("nonExistentFile"));
7676

77-
// Attempt to open a file known not to exist
77+
// Check the 'exists' static routine, then attempt to open a file known not to exist
7878
#ifndef _WIN32
79+
CPPUNIT_ASSERT(!File::exists("testdir/nonExistentFile"));
7980
File doesntExist("testdir/nonExistentFile", DEFAULT_UMASK);
8081
#else
82+
CPPUNIT_ASSERT(!File::exists("testdir\\nonExistentFile"));
8183
File doesntExist("testdir\\nonExistentFile", DEFAULT_UMASK);
8284
#endif
8385

8486
CPPUNIT_ASSERT(!doesntExist.isValid());
8587

86-
// Attempt to open a file known to exist
88+
// Check the 'exists' static routine, then attempt to open a file known to exist
8789
#ifndef _WIN32
8890
CPPUNIT_ASSERT(!system("echo someStuff > testdir/existingFile"));
8991
#else
@@ -92,8 +94,10 @@ void FileTests::testExistNotExist()
9294
CPPUNIT_ASSERT(exists("existingFile"));
9395

9496
#ifndef _WIN32
97+
CPPUNIT_ASSERT(File::exists("testdir/existingFile"));
9598
File exists("testdir/existingFile", DEFAULT_UMASK);
9699
#else
100+
CPPUNIT_ASSERT(File::exists("testdir\\existingFile"));
97101
File exists("testdir\\existingFile", DEFAULT_UMASK);
98102
#endif
99103

0 commit comments

Comments
 (0)