diff --git a/src/blobstore/implementations/ondisk/CMakeLists.txt b/src/blobstore/implementations/ondisk/CMakeLists.txt index 305291a4..53ef89f8 100644 --- a/src/blobstore/implementations/ondisk/CMakeLists.txt +++ b/src/blobstore/implementations/ondisk/CMakeLists.txt @@ -1,3 +1,3 @@ -add_library(blobstore_ondisk Data.cpp OnDiskBlob.cpp OnDiskBlobStore.cpp FileAlreadyExistsException.cpp) +add_library(blobstore_ondisk Data.cpp OnDiskBlob.cpp OnDiskBlobStore.cpp FileAlreadyExistsException.cpp FileDoesntExistException.cpp) target_link_libraries(blobstore_ondisk boost_filesystem boost_system) diff --git a/src/blobstore/implementations/ondisk/Data.cpp b/src/blobstore/implementations/ondisk/Data.cpp index a067eabb..9be9fd20 100644 --- a/src/blobstore/implementations/ondisk/Data.cpp +++ b/src/blobstore/implementations/ondisk/Data.cpp @@ -1,4 +1,6 @@ -#include +#include "Data.h" + +#include "FileDoesntExistException.h" #include #include @@ -56,6 +58,7 @@ void Data::StoreToFile(const bf::path &filepath) const { Data Data::LoadFromFile(const bf::path &filepath) { ifstream file(filepath.c_str(), ios::binary); + _assertFileExists(file, filepath); size_t size = _getStreamSize(file); Data result(size); @@ -63,6 +66,12 @@ Data Data::LoadFromFile(const bf::path &filepath) { return result; } +void Data::_assertFileExists(const ifstream &file, const bf::path &filepath) { + if (!file.good()) { + throw FileDoesntExistException(filepath); + } +} + size_t Data::_getStreamSize(istream &stream) { auto current_pos = stream.tellg(); diff --git a/src/blobstore/implementations/ondisk/Data.h b/src/blobstore/implementations/ondisk/Data.h index 1e3fa4d6..d1ab56c1 100644 --- a/src/blobstore/implementations/ondisk/Data.h +++ b/src/blobstore/implementations/ondisk/Data.h @@ -32,6 +32,7 @@ private: size_t _size; void *_data; + static void _assertFileExists(const std::ifstream &file, const boost::filesystem::path &filepath); static size_t _getStreamSize(std::istream &stream); void _readFromStream(std::istream &stream); diff --git a/src/blobstore/implementations/ondisk/FileDoesntExistException.cpp b/src/blobstore/implementations/ondisk/FileDoesntExistException.cpp new file mode 100644 index 00000000..c891c3cf --- /dev/null +++ b/src/blobstore/implementations/ondisk/FileDoesntExistException.cpp @@ -0,0 +1,19 @@ +#include + +namespace bf = boost::filesystem; + +using std::runtime_error; +using std::string; + +namespace blobstore { +namespace ondisk { + +FileDoesntExistException::FileDoesntExistException(const bf::path &filepath) +: runtime_error(string("The file ")+filepath.c_str()+" doesn't exist") { +} + +FileDoesntExistException::~FileDoesntExistException() { +} + +} /* namespace ondisk */ +} /* namespace blobstore */ diff --git a/src/blobstore/implementations/ondisk/FileDoesntExistException.h b/src/blobstore/implementations/ondisk/FileDoesntExistException.h new file mode 100644 index 00000000..db9ef6f6 --- /dev/null +++ b/src/blobstore/implementations/ondisk/FileDoesntExistException.h @@ -0,0 +1,21 @@ +#pragma once +#ifndef BLOBSTORE_IMPLEMENTATIONS_ONDISK_FILEDOESNTEXISTEXCEPTION_H_ +#define BLOBSTORE_IMPLEMENTATIONS_ONDISK_FILEDOESNTEXISTEXCEPTION_H_ + +#include + +#include + +namespace blobstore { +namespace ondisk { + +class FileDoesntExistException: public std::runtime_error { +public: + FileDoesntExistException(const boost::filesystem::path &filepath); + virtual ~FileDoesntExistException(); +}; + +} /* namespace ondisk */ +} /* namespace blobstore */ + +#endif diff --git a/src/test/blobstore/ondisk/OnDiskBlobLoadTest.cpp b/src/test/blobstore/ondisk/OnDiskBlobLoadTest.cpp index b4465c45..d0e7b810 100644 --- a/src/test/blobstore/ondisk/OnDiskBlobLoadTest.cpp +++ b/src/test/blobstore/ondisk/OnDiskBlobLoadTest.cpp @@ -5,6 +5,7 @@ #include "blobstore/implementations/ondisk/OnDiskBlob.h" #include "blobstore/implementations/ondisk/Data.h" +#include "blobstore/implementations/ondisk/FileDoesntExistException.h" #include @@ -64,4 +65,10 @@ TEST_P(OnDiskBlobLoadTest, LoadedDataIsCorrect) { EXPECT_BLOB_DATA_EQ(randomData, *blob); } -//TODO Test file doesn't exist +TEST_F(OnDiskBlobLoadTest, LoadNotExistingBlob) { + TempFile file2(false); // Pass false, so the file isn't created. + EXPECT_THROW( + OnDiskBlob::LoadFromDisk(file2.path()), + FileDoesntExistException + ); +}