[io_engine] Add exclusive flag to io_engine

This commit is contained in:
Joe Thornber 2016-06-07 13:45:27 +01:00
parent df9f4d479d
commit 07f44e9c77
3 changed files with 13 additions and 7 deletions

View File

@ -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;

View File

@ -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

View File

@ -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<copier> 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);