Mmap reads should not return error if reading past file

Summary:
Currently, mmap returns IOError when user tries to read data past the end of the file. This diff changes the behavior. Now, we return just the bytes that we can, and report the size we returned via a Slice result. This is consistent with non-mmap behavior and also pread() system call.

This diff is taken out of D45123.

Test Plan: make check

Reviewers: sdong

Reviewed By: sdong

Subscribers: dhruba, leveldb

Differential Revision: https://reviews.facebook.net/D45645
main
Igor Canadi 10 years ago
parent d286b5df90
commit 16ebe3a2a9
  1. 9
      util/env_posix.cc

@ -313,12 +313,13 @@ class PosixMmapReadableFile: public RandomAccessFile {
virtual Status Read(uint64_t offset, size_t n, Slice* result, virtual Status Read(uint64_t offset, size_t n, Slice* result,
char* scratch) const override { char* scratch) const override {
Status s; Status s;
if (offset + n > length_) { if (offset > length_) {
*result = Slice(); *result = Slice();
s = IOError(filename_, EINVAL); return IOError(filename_, EINVAL);
} else { } else if (offset + n > length_) {
*result = Slice(reinterpret_cast<char*>(mmapped_region_) + offset, n); n = length_ - offset;
} }
*result = Slice(reinterpret_cast<char*>(mmapped_region_) + offset, n);
return s; return s;
} }
virtual Status InvalidateCache(size_t offset, size_t length) override { virtual Status InvalidateCache(size_t offset, size_t length) override {

Loading…
Cancel
Save