diff --git a/block-cache/io_engine.cc b/block-cache/io_engine.cc index 7837c33..a4211cb 100644 --- a/block-cache/io_engine.cc +++ b/block-cache/io_engine.cc @@ -75,9 +75,11 @@ aio_engine::~aio_engine() } aio_engine::handle -aio_engine::open_file(std::string const &path, mode m) +aio_engine::open_file(std::string const &path, mode m, sharing s) { int flags = (m == READ_ONLY) ? O_RDONLY : O_RDWR; + if (s == EXCLUSIVE) + flags |= O_EXCL; int fd = ::open(path.c_str(), O_DIRECT | flags); if (fd < 0) { ostringstream out; diff --git a/block-cache/io_engine.h b/block-cache/io_engine.h index 55b556d..f9e4c9c 100644 --- a/block-cache/io_engine.h +++ b/block-cache/io_engine.h @@ -27,12 +27,17 @@ namespace bcache { WRITE }; + enum sharing { + EXCLUSIVE, + SHARED + }; + io_engine() {} virtual ~io_engine() {} using handle = unsigned; - virtual handle open_file(std::string const &path, mode m) = 0; + virtual handle open_file(std::string const &path, mode m, sharing s = EXCLUSIVE) = 0; virtual void close_file(handle h) = 0; // returns false if there are insufficient resources to @@ -79,8 +84,7 @@ namespace bcache { using handle = unsigned; - // FIXME: open exclusive? - virtual handle open_file(std::string const &path, mode m); + virtual handle open_file(std::string const &path, mode m, sharing s = EXCLUSIVE); virtual void close_file(handle h); // Returns false if queueing the io failed diff --git a/unit-tests/copier_t.cc b/unit-tests/copier_t.cc index e95819f..0185d9e 100644 --- a/unit-tests/copier_t.cc +++ b/unit-tests/copier_t.cc @@ -33,7 +33,7 @@ using namespace testing; namespace { class io_engine_mock : public io_engine { public: - MOCK_METHOD2(open_file, handle(string const &, mode)); + MOCK_METHOD3(open_file, handle(string const &, mode, sharing)); MOCK_METHOD1(close_file, void(handle)); MOCK_METHOD6(issue_io, bool(handle, dir, sector_t, sector_t, void *, unsigned)); @@ -51,9 +51,9 @@ namespace { } unique_ptr make_copier() { - EXPECT_CALL(engine_, open_file(src_file_, io_engine::READ_ONLY)). + EXPECT_CALL(engine_, open_file(src_file_, io_engine::READ_ONLY, io_engine::EXCLUSIVE)). WillOnce(Return(SRC_HANDLE)); - EXPECT_CALL(engine_, open_file(dest_file_, io_engine::READ_WRITE)). + EXPECT_CALL(engine_, open_file(dest_file_, io_engine::READ_WRITE, io_engine::EXCLUSIVE)). WillOnce(Return(DEST_HANDLE)); EXPECT_CALL(engine_, close_file(SRC_HANDLE)).Times(1);