Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/lib/object_store/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
5 changes: 5 additions & 0 deletions src/lib/object_store/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
7 changes: 7 additions & 0 deletions src/lib/object_store/Generation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/lib/object_store/test/FileTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,18 @@ void FileTests::testExistNotExist()
// Test pre-condition
CPPUNIT_ASSERT(!exists("nonExistentFile"));

// Attempt to open a file known not to exist
// Check the 'exists' static routine, then attempt to open a file known not to exist
#ifndef _WIN32
CPPUNIT_ASSERT(!File::exists("testdir/nonExistentFile"));
File doesntExist("testdir/nonExistentFile", DEFAULT_UMASK);
#else
CPPUNIT_ASSERT(!File::exists("testdir\\nonExistentFile"));
File doesntExist("testdir\\nonExistentFile", DEFAULT_UMASK);
#endif

CPPUNIT_ASSERT(!doesntExist.isValid());

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

#ifndef _WIN32
CPPUNIT_ASSERT(File::exists("testdir/existingFile"));
File exists("testdir/existingFile", DEFAULT_UMASK);
#else
CPPUNIT_ASSERT(File::exists("testdir\\existingFile"));
File exists("testdir\\existingFile", DEFAULT_UMASK);
#endif

Expand Down
Loading