From 59bd300b4356cb8cee396032e8eb8324376fb630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 8 Apr 2015 16:24:10 +0200 Subject: [PATCH] use sqlite itself to lock the database file rather than those unportable bsd locks --- test/storage/database.cpp | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/test/storage/database.cpp b/test/storage/database.cpp index c60388be4..1a2b618a5 100644 --- a/test/storage/database.cpp +++ b/test/storage/database.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include class ScopedTest { public: @@ -96,31 +96,40 @@ TEST_F(Storage, DatabaseCreate) { class FileLock { public: FileLock(const std::string& path) { - fd = open(path.c_str(), O_RDONLY | O_NOCTTY | O_CREAT, 0666); - if (fd <= 0) { - throw std::runtime_error("Could not open file"); + const int err = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, nullptr); + if (err != SQLITE_OK) { + throw std::runtime_error("Could not open db"); } lock(); } void lock() { - if (0 != flock(fd, LOCK_EX)) { - throw std::runtime_error("Could not lock file"); + assert(!locked); + const int err = sqlite3_exec(db, "begin exclusive transaction", nullptr, nullptr, nullptr); + if (err != SQLITE_OK) { + throw std::runtime_error("Could not lock db"); } + locked = true; } void unlock() { - if (0 != flock(fd, LOCK_UN)) { - throw std::runtime_error("Could not unlock file"); + assert(locked); + const int err = sqlite3_exec(db, "commit", nullptr, nullptr, nullptr); + if (err != SQLITE_OK) { + throw std::runtime_error("Could not unlock db"); } + locked = false; } ~FileLock() { - unlock(); + if (locked) { + unlock(); + } } private: - int fd; + sqlite3* db; + bool locked = false; }; TEST_F(Storage, DatabaseLockedRead) {