Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions cpp/src/arrow/ipc/ipc-memory-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,42 @@ TEST_F(TestMemoryMappedSource, WriteRead) {
}
}

TEST_F(TestMemoryMappedSource, ReadOnly) {
const int64_t buffer_size = 1024;
std::vector<uint8_t> buffer(buffer_size);

test::random_bytes(1024, 0, buffer.data());

const int reps = 5;

std::string path = "ipc-read-only-test";
CreateFile(path, reps * buffer_size);

std::shared_ptr<MemoryMappedSource> rwmmap;
ASSERT_OK(MemoryMappedSource::Open(path, MemorySource::READ_WRITE, &rwmmap));

int64_t position = 0;
for (int i = 0; i < reps; ++i) {
ASSERT_OK(rwmmap->Write(position, buffer.data(), buffer_size));

position += buffer_size;
}
rwmmap->Close();

std::shared_ptr<MemoryMappedSource> rommap;
ASSERT_OK(MemoryMappedSource::Open(path, MemorySource::READ_WRITE, &rommap));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean to open this read only?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Sorry for my mistake.


position = 0;
std::shared_ptr<Buffer> out_buffer;
for (int i = 0; i < reps; ++i) {
ASSERT_OK(rommap->ReadAt(position, buffer_size, &out_buffer));

ASSERT_EQ(0, memcmp(out_buffer->data(), buffer.data(), buffer_size));
position += buffer_size;
}
rommap->Close();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be nice to test that writing to a the file opened in read only mode gives an appropriate error status.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the good suggestion.


TEST_F(TestMemoryMappedSource, InvalidFile) {
std::string non_existent_path = "invalid-file-name-asfd";

Expand Down
5 changes: 3 additions & 2 deletions cpp/src/arrow/ipc/memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ class MemoryMappedSource::Impl {
if (is_open_) { return Status::IOError("A file is already open"); }

path_ = path;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you deleting this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this variable is currently not used anywhere. In addition, current implementation expects a file exists in a given path, which in turn means the file is already created in some other parts of the program. So, I think MemoryMappedSource is not responsible for deleting files used for mmap.
In addition, if path is required later, we can add it again easily.

int prot_flag = PROT_READ;

if (mode == MemorySource::READ_WRITE) {
file_ = fopen(path.c_str(), "r+b");
prot_flag |= PROT_WRITE;
} else {
file_ = fopen(path.c_str(), "rb");
}
Expand All @@ -73,9 +75,8 @@ class MemoryMappedSource::Impl {
fseek(file_, 0L, SEEK_SET);
is_open_ = true;

// TODO(wesm): Add read-only version of this
data_ = reinterpret_cast<uint8_t*>(
mmap(nullptr, size_, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file_), 0));
mmap(nullptr, size_, prot_flag, MAP_SHARED, fileno(file_), 0));
if (data_ == nullptr) {
std::stringstream ss;
ss << "Memory mapping file failed, errno: " << errno;
Expand Down