Use ASSERT from cpputils instead of fspp_assert

This commit is contained in:
Sebastian Messmer 2015-07-22 13:48:34 +02:00
parent 7dcb29d515
commit d9cfa55b91
7 changed files with 52 additions and 162 deletions

View File

@ -3,9 +3,9 @@
#include <cassert>
#include "FuseErrnoException.h"
#include "../utils/IOException.h"
#include "Filesystem.h"
#include <iostream>
#include <messmer/cpp-utils/assert/assert.h>
using std::string;
@ -134,8 +134,8 @@ void* fusepp_init(fuse_conn_info *conn) {
void fusepp_destroy(void *userdata) {
auto f = FUSE_OBJ;
assert(userdata == f);
UNUSED(userdata); //In the Release build, the assert doesn't run
ASSERT(userdata == f, "Wrong userdata set");
UNUSED(userdata); //In case the assert is disabled
f->destroy();
}
@ -223,8 +223,8 @@ int Fuse::getattr(const bf::path &path, struct stat *stbuf) {
try {
_fs->lstat(path, stbuf);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::getattr: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::getattr: " << e.what() << std::endl;
return -EIO;
} catch(fspp::fuse::FuseErrnoException &e) {
return -e.getErrno();
@ -246,8 +246,8 @@ int Fuse::fgetattr(const bf::path &path, struct stat *stbuf, fuse_file_info *fil
try {
_fs->fstat(fileinfo->fh, stbuf);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::fgetattr: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::fgetattr: " << e.what() << std::endl;
return -EIO;
} catch(fspp::fuse::FuseErrnoException &e) {
return -e.getErrno();
@ -259,8 +259,8 @@ int Fuse::readlink(const bf::path &path, char *buf, size_t size) {
try {
_fs->readSymlink(path, buf, size);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::readlink: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::readlink: " << e.what() << std::endl;
return -EIO;
} catch (fspp::fuse::FuseErrnoException &e) {
return -e.getErrno();
@ -281,8 +281,8 @@ int Fuse::mkdir(const bf::path &path, mode_t mode) {
auto context = fuse_get_context();
_fs->mkdir(path, mode, context->uid, context->gid);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::mkdir: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::mkdir: " << e.what() << std::endl;
return -EIO;
} catch(fspp::fuse::FuseErrnoException &e) {
return -e.getErrno();
@ -294,8 +294,8 @@ int Fuse::unlink(const bf::path &path) {
try {
_fs->unlink(path);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::unlink: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::unlink: " << e.what() << std::endl;
return -EIO;
} catch(fspp::fuse::FuseErrnoException &e) {
return -e.getErrno();
@ -306,8 +306,8 @@ int Fuse::rmdir(const bf::path &path) {
try {
_fs->rmdir(path);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::rmdir: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::rmdir: " << e.what() << std::endl;
return -EIO;
} catch(fspp::fuse::FuseErrnoException &e) {
return -e.getErrno();
@ -320,8 +320,8 @@ int Fuse::symlink(const bf::path &from, const bf::path &to) {
auto context = fuse_get_context();
_fs->createSymlink(from, to, context->uid, context->gid);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::symlink: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::symlink: " << e.what() << std::endl;
return -EIO;
} catch(fspp::fuse::FuseErrnoException &e) {
return -e.getErrno();
@ -333,8 +333,8 @@ int Fuse::rename(const bf::path &from, const bf::path &to) {
try {
_fs->rename(from, to);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::rename: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::rename: " << e.what() << std::endl;
return -EIO;
} catch(fspp::fuse::FuseErrnoException &e) {
return -e.getErrno();
@ -355,8 +355,8 @@ int Fuse::chmod(const bf::path &path, mode_t mode) {
try {
_fs->chmod(path, mode);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::chmod: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::chmod: " << e.what() << std::endl;
return -EIO;
} catch (fspp::fuse::FuseErrnoException &e) {
return -e.getErrno();
@ -367,8 +367,8 @@ int Fuse::chown(const bf::path &path, uid_t uid, gid_t gid) {
try {
_fs->chown(path, uid, gid);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::chown: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::chown: " << e.what() << std::endl;
return -EIO;
} catch (fspp::fuse::FuseErrnoException &e) {
return -e.getErrno();
@ -380,8 +380,8 @@ int Fuse::truncate(const bf::path &path, off_t size) {
try {
_fs->truncate(path, size);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::truncate: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::truncate: " << e.what() << std::endl;
return -EIO;
} catch (FuseErrnoException &e) {
return -e.getErrno();
@ -394,8 +394,8 @@ int Fuse::ftruncate(const bf::path &path, off_t size, fuse_file_info *fileinfo)
try {
_fs->ftruncate(fileinfo->fh, size);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::ftruncate: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::ftruncate: " << e.what() << std::endl;
return -EIO;
} catch (FuseErrnoException &e) {
return -e.getErrno();
@ -408,8 +408,8 @@ int Fuse::utimens(const bf::path &path, const timespec times[2]) {
try {
_fs->utimens(path, times);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::utimens: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::utimens: " << e.what() << std::endl;
return -EIO;
} catch (FuseErrnoException &e) {
return -e.getErrno();
@ -421,8 +421,8 @@ int Fuse::open(const bf::path &path, fuse_file_info *fileinfo) {
try {
fileinfo->fh = _fs->openFile(path, fileinfo->flags);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::open: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::open: " << e.what() << std::endl;
return -EIO;
} catch (FuseErrnoException &e) {
return -e.getErrno();
@ -435,8 +435,8 @@ int Fuse::release(const bf::path &path, fuse_file_info *fileinfo) {
try {
_fs->closeFile(fileinfo->fh);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::release: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::release: " << e.what() << std::endl;
return -EIO;
} catch (FuseErrnoException &e) {
return -e.getErrno();
@ -448,8 +448,8 @@ int Fuse::read(const bf::path &path, char *buf, size_t size, off_t offset, fuse_
UNUSED(path);
try {
return _fs->read(fileinfo->fh, buf, size, offset);
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::read: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::read: " << e.what() << std::endl;
return -EIO;
} catch (FuseErrnoException &e) {
return -e.getErrno();
@ -462,8 +462,8 @@ int Fuse::write(const bf::path &path, const char *buf, size_t size, off_t offset
try {
_fs->write(fileinfo->fh, buf, size, offset);
return size;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::write: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::write: " << e.what() << std::endl;
return -EIO;
} catch (FuseErrnoException &e) {
return -e.getErrno();
@ -476,8 +476,8 @@ int Fuse::statfs(const bf::path &path, struct statvfs *fsstat) {
try {
_fs->statfs(path, fsstat);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::statfs: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::statfs: " << e.what() << std::endl;
return -EIO;
} catch (FuseErrnoException &e) {
return -e.getErrno();
@ -491,8 +491,8 @@ int Fuse::flush(const bf::path &path, fuse_file_info *fileinfo) {
try {
_fs->flush(fileinfo->fh);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::flush: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::flush: " << e.what() << std::endl;
return -EIO;
} catch (FuseErrnoException &e) {
return -e.getErrno();
@ -509,8 +509,8 @@ int Fuse::fsync(const bf::path &path, int datasync, fuse_file_info *fileinfo) {
_fs->fsync(fileinfo->fh);
}
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::fsync: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::fsync: " << e.what() << std::endl;
return -EIO;
} catch (FuseErrnoException &e) {
return -e.getErrno();
@ -544,15 +544,15 @@ int Fuse::readdir(const bf::path &path, void *buf, fuse_fill_dir_t filler, off_t
} else if (entry.type == Dir::EntryType::SYMLINK) {
stbuf.st_mode = S_IFLNK;
} else {
assert(false);
ASSERT(false, "Unknown entry type");
}
if (filler(buf, entry.name.c_str(), &stbuf, 0) != 0) {
return -ENOMEM;
}
}
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::readdir: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::readdir: " << e.what() << std::endl;
return -EIO;
} catch (FuseErrnoException &e) {
return -e.getErrno();
@ -592,8 +592,8 @@ int Fuse::access(const bf::path &path, int mask) {
try {
_fs->access(path, mask);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::access: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::access: " << e.what() << std::endl;
return -EIO;
} catch (FuseErrnoException &e) {
return -e.getErrno();
@ -606,8 +606,8 @@ int Fuse::create(const bf::path &path, mode_t mode, fuse_file_info *fileinfo) {
auto context = fuse_get_context();
fileinfo->fh = _fs->createAndOpenFile(path, mode, context->uid, context->gid);
return 0;
} catch(const fspp::IOException &e) {
std::cerr << "IOException in Fuse::create: " << e.what() << std::endl;
} catch(const cpputils::AssertFailed &e) {
std::cerr << "AssertFailed in Fuse::create: " << e.what() << std::endl;
return -EIO;
} catch (FuseErrnoException &e) {
return -e.getErrno();

View File

@ -4,6 +4,7 @@
#include <stdexcept>
#include <errno.h>
#include <messmer/cpp-utils/assert/assert.h>
namespace fspp {
namespace fuse{
@ -26,7 +27,7 @@ inline void CHECK_RETVAL(int retval) {
inline FuseErrnoException::FuseErrnoException(int errno_)
:runtime_error(strerror(errno_)), _errno(errno_) {
assert(_errno != 0);
ASSERT(_errno != 0, "Errno shouldn't be zero");
}
inline FuseErrnoException::~FuseErrnoException() {

View File

@ -1,26 +0,0 @@
#include <google/gtest/gtest.h>
#include <google/gmock/gmock.h>
//Include the fspp_assert macro for a debug build
#undef NDEBUG
#include "../../utils/assert.h"
using testing::MatchesRegex;
TEST(AssertTest_DebugBuild, DoesntDieIfTrue) {
fspp_assert(true, "bla");
}
TEST(AssertTest_DebugBuild, DiesIfFalse) {
EXPECT_DEATH(
fspp_assert(false, "bla"),
""
);
}
TEST(AssertTest_DebugBuild, AssertMessage) {
EXPECT_DEATH(
fspp_assert(2==5, "my message"),
"Assertion \\[2==5\\] failed in .*/assert_debug_test.cpp:25: my message"
);
}

View File

@ -1,30 +0,0 @@
#include <google/gtest/gtest.h>
#include <google/gmock/gmock.h>
//Include the fspp_assert macro for a release build
#define NDEBUG
#include "../../utils/assert.h"
using testing::MatchesRegex;
TEST(AssertTest_ReleaseBuild, DoesntThrowIfTrue) {
fspp_assert(true, "bla");
}
TEST(AssertTest_ReleaseBuild, ThrowsIfFalse) {
EXPECT_THROW(
fspp_assert(false, "bla"),
fspp::IOException
);
}
TEST(AssertTest_ReleaseBuild, AssertMessage) {
try {
fspp_assert(2==5, "my message");
FAIL();
} catch (const fspp::IOException &e) {
EXPECT_THAT(e.what(), MatchesRegex(
"Assertion \\[2==5\\] failed in .*/assert_release_test.cpp:23: my message"
));
}
}

View File

@ -1 +0,0 @@
#include "IOException.h"

View File

@ -1,22 +0,0 @@
#ifndef CRYFS_IOEXCEPTION_H
#define CRYFS_IOEXCEPTION_H
#include <stdexcept>
namespace fspp {
class IOException : public std::exception {
public:
IOException(const std::string &message) : _message(message) { }
const char *what() const throw() override {
return _message.c_str();
}
private:
std::string _message;
};
}
#endif

View File

@ -1,32 +0,0 @@
#ifndef CRYFS_ASSERT_H
#define CRYFS_ASSERT_H
#include "IOException.h"
#include <iostream>
#include <string>
namespace fspp {
namespace _assert {
inline std::string format(const char *expr, const char *message, const char *file, int line) {
return std::string()+"Assertion ["+expr+"] failed in "+file+":"+std::to_string(line)+": "+message;
}
inline void assert_fail_release(const char *expr, const char *message, const char *file, int line) {
throw IOException(format(expr, message, file, line));
}
inline void assert_fail_debug(const char *expr, const char *message, const char *file, int line) {
std::cerr << format(expr, message, file, line) << std::endl;
abort();
}
}
}
#ifdef NDEBUG
//TODO Check whether disabling assertions in prod affects speed.
# define fspp_assert(expr, msg) (void)((expr) || (fspp::_assert::assert_fail_release(#expr, msg, __FILE__, __LINE__),0))
#else
# define fspp_assert(expr, msg) (void)((expr) || (fspp::_assert::assert_fail_debug(#expr, msg, __FILE__, __LINE__),0))
#endif
#endif