From 57461fba8c39103de8ebd20778bc7e64baae63cc Mon Sep 17 00:00:00 2001 From: Andrew Kryczka Date: Fri, 27 May 2016 12:10:26 -0700 Subject: [PATCH] In-memory environment read beyond EOF Summary: Made it consistent with posix Env, which uses pread() that returns 0 (success) when an offset is given beyond EOF. The purpose of making these Envs behave consistently is I am repurposing the in-memory Envs' tests for the basic Env tests in D58635. Test Plan: ran mock_env_test and memenv_test Reviewers: IslamAbdelRahman, lightmark, sdong Reviewed By: sdong Subscribers: andrewkr, dhruba, leveldb Differential Revision: https://reviews.facebook.net/D58845 --- util/memenv.cc | 16 ++++++++-------- util/memenv_test.cc | 2 +- util/mock_env.cc | 5 +---- util/mock_env_test.cc | 2 +- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/util/memenv.cc b/util/memenv.cc index 5737370230..835de6e881 100644 --- a/util/memenv.cc +++ b/util/memenv.cc @@ -2,14 +2,17 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. +#include + +#include +#include +#include +#include + #include "rocksdb/env.h" #include "rocksdb/status.h" #include "port/port.h" #include "util/mutexlock.h" -#include -#include -#include -#include namespace rocksdb { @@ -70,10 +73,7 @@ class FileState { uint64_t Size() const { return size_; } Status Read(uint64_t offset, size_t n, Slice* result, char* scratch) const { - if (offset > size_) { - return Status::IOError("Offset greater than file size."); - } - const uint64_t available = size_ - offset; + const uint64_t available = size_ - std::min(size_, offset); if (n > available) { n = available; } diff --git a/util/memenv_test.cc b/util/memenv_test.cc index 2b872d266c..0df5eb71c1 100644 --- a/util/memenv_test.cc +++ b/util/memenv_test.cc @@ -124,7 +124,7 @@ TEST_F(MemEnvTest, ReadWrite) { ASSERT_EQ(0, result.compare("d")); // Too high offset. - ASSERT_TRUE(!rand_file->Read(1000, 5, &result, scratch).ok()); + ASSERT_TRUE(rand_file->Read(1000, 5, &result, scratch).ok()); } TEST_F(MemEnvTest, Locks) { diff --git a/util/mock_env.cc b/util/mock_env.cc index 5001dfc395..cbc0e282b4 100644 --- a/util/mock_env.cc +++ b/util/mock_env.cc @@ -99,10 +99,7 @@ class MemFile { Status Read(uint64_t offset, size_t n, Slice* result, char* scratch) const { MutexLock lock(&mutex_); - if (offset > Size()) { - return Status::IOError("Offset greater than file size."); - } - const uint64_t available = Size() - offset; + const uint64_t available = Size() - std::min(Size(), offset); if (n > available) { n = available; } diff --git a/util/mock_env_test.cc b/util/mock_env_test.cc index 710881b553..5437060510 100644 --- a/util/mock_env_test.cc +++ b/util/mock_env_test.cc @@ -123,7 +123,7 @@ TEST_F(MockEnvTest, ReadWrite) { ASSERT_EQ(0, result.compare("d")); // Too high offset. - ASSERT_TRUE(!rand_file->Read(1000, 5, &result, scratch).ok()); + ASSERT_TRUE(rand_file->Read(1000, 5, &result, scratch).ok()); } TEST_F(MockEnvTest, Locks) {