]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/buffer: fix pread_file to generate local bufferlist
authorYuan Zhou <yuan.zhou@intel.com>
Fri, 18 Jan 2019 06:26:25 +0000 (14:26 +0800)
committerYuan Zhou <yuan.zhou@intel.com>
Thu, 21 Mar 2019 16:16:27 +0000 (00:16 +0800)
Signed-off-by: Yuan Zhou <yuan.zhou@intel.com>
src/common/buffer.cc
src/test/immutable_object_cache/CMakeLists.txt
src/test/immutable_object_cache/test_ObjectCacheFile.cc [new file with mode: 0644]
src/test/immutable_object_cache/test_sync_file.cc [deleted file]
src/tools/immutable_object_cache/ObjectCacheFile.cc
src/tools/immutable_object_cache/ObjectCacheFile.h

index 8633ca407b5edb7ccc1be140f2b100a28b69782b..5f3f948ff9848b8fc74a32e3dd3868999c2ecd9a 100644 (file)
@@ -1814,8 +1814,12 @@ int buffer::list::pread_file(const char *fn, uint64_t off, uint64_t len, std::st
   if (len > st.st_size - off) {
     len = st.st_size - off;
   }
-  lseek(fd, off, SEEK_SET);
-  ssize_t ret = safe_read(fd, (void*)this->c_str(), len);
+  ssize_t ret = lseek64(fd, off, SEEK_SET);
+  if (ret != off) {
+    return -errno;
+  }
+
+  ret = read_fd(fd, len);
   if (ret < 0) {
     std::ostringstream oss;
     oss << "bufferlist::read_file(" << fn << "): read error:"
index aade49afba88bfb5862e1518e27ab8fe8917d472..7733ca2ddd6a68886859410006c6ba3b36e64df2 100644 (file)
@@ -2,7 +2,7 @@
 add_executable(unittest_ceph_immutable_obj_cache
   test_main.cc
   test_SimplePolicy.cc
-  test_sync_file.cc
+  test_ObjectCacheFile.cc
   test_DomainSocket.cc
   test_multi_session.cc
   test_object_store.cc
diff --git a/src/test/immutable_object_cache/test_ObjectCacheFile.cc b/src/test/immutable_object_cache/test_ObjectCacheFile.cc
new file mode 100644 (file)
index 0000000..29d90b2
--- /dev/null
@@ -0,0 +1,120 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "gtest/gtest.h"
+#include "include/Context.h"
+#include "include/buffer_fwd.h"
+#include "common/Mutex.h"
+#include "common/Cond.h"
+#include "global/global_init.h"
+#include "common/ceph_argparse.h"
+#include "global/global_context.h"
+#include <experimental/filesystem>
+
+#include "tools/immutable_object_cache/ObjectCacheFile.h"
+
+using namespace ceph::immutable_obj_cache;
+namespace efs = std::experimental::filesystem;
+
+class TestObjectCacheFile :public ::testing::Test {
+public:
+  std::string m_cache_root_dir;
+
+  TestObjectCacheFile(){}
+  ~TestObjectCacheFile(){}
+  static void SetUpTestCase() {}
+  static void TearDownTestCase() {}
+
+  void SetUp() override {
+    m_cache_root_dir = g_ceph_context->_conf.get_val<std::string>("immutable_object_cache_path")
+      + "/ceph_immutable_obj_cache/";
+
+    if (efs::exists(m_cache_root_dir)) {
+      efs::remove_all(m_cache_root_dir);
+    }
+    efs::create_directories(m_cache_root_dir);
+  }
+
+  void TearDown() override {
+    efs::remove_all(m_cache_root_dir);
+  }
+
+};
+
+TEST_F(TestObjectCacheFile, test_write_object_to_file) {
+  ObjectCacheFile* m_cache_file_1 = new ObjectCacheFile(g_ceph_context, "test_sync_file_1");
+  ObjectCacheFile* m_cache_file_2 = new ObjectCacheFile(g_ceph_context, "test_sync_file_2");
+  ObjectCacheFile* m_cache_file_3 = new ObjectCacheFile(g_ceph_context, "test_sync_file_3");
+  ASSERT_TRUE(m_cache_file_1->get_file_size() == -1);
+  ASSERT_TRUE(m_cache_file_2->get_file_size() == -1);
+  ASSERT_TRUE(m_cache_file_3->get_file_size() == -1);
+
+  bufferlist* buf_1 = new ceph::bufferlist();
+  bufferlist* buf_2 = new ceph::bufferlist();
+  bufferlist* buf_3 = new ceph::bufferlist();
+  buf_1->append(std::string(1024, '0'));
+  buf_2->append(std::string(4096, '0'));
+  buf_3->append(std::string(0, '0'));
+
+  ASSERT_TRUE(m_cache_file_1->write_object_to_file(*buf_1, 1024) == 1024);
+  ASSERT_TRUE(m_cache_file_2->write_object_to_file(*buf_2, 4096) == 4096);
+  ASSERT_TRUE(m_cache_file_3->write_object_to_file(*buf_3, 0) == 0);
+  ASSERT_TRUE(m_cache_file_1->get_file_size() == 1024);
+  ASSERT_TRUE(m_cache_file_2->get_file_size() == 4096);
+  ASSERT_TRUE(m_cache_file_3->get_file_size() == 0);
+
+  delete m_cache_file_1;
+  delete m_cache_file_2;
+  delete m_cache_file_3;
+  delete buf_1;
+  delete buf_2;
+  delete buf_3;
+}
+
+TEST_F(TestObjectCacheFile, test_read_object_from_file) {
+  ObjectCacheFile* m_cache_file_1 = new ObjectCacheFile(g_ceph_context, "test_sync_file_1");
+  ObjectCacheFile* m_cache_file_2 = new ObjectCacheFile(g_ceph_context, "test_sync_file_2");
+  bufferlist* buf_1 = new ceph::bufferlist();
+  bufferlist* buf_2 = new ceph::bufferlist();
+
+  ASSERT_TRUE(m_cache_file_1->get_file_size() == -1);
+  ASSERT_TRUE(m_cache_file_2->get_file_size() == -1);
+  ASSERT_EQ(m_cache_file_1->read_object_from_file(buf_1, 0, 1024), -1);
+  ASSERT_EQ(m_cache_file_2->read_object_from_file(buf_2, 0, 1024), -1);
+
+  buf_1->append(std::string("helloworld"));
+  ASSERT_TRUE(m_cache_file_1->write_object_to_file(*buf_1, 10) == 10);
+  ASSERT_TRUE(m_cache_file_1->get_file_size() == 10);
+
+  bufferlist* buf_3 = new ceph::bufferlist();
+  bufferlist* buf_4 = new ceph::bufferlist();
+  bufferlist* buf_5 = new ceph::bufferlist();
+  bufferlist* buf_6 = new ceph::bufferlist();
+
+  ASSERT_EQ(m_cache_file_1->read_object_from_file(buf_3, 0, 10), 10);
+  ASSERT_EQ(10, buf_3->length());
+  ASSERT_EQ(0, (strncmp(buf_1->c_str(), buf_3->c_str(), 10)));
+
+  ASSERT_EQ(m_cache_file_1->read_object_from_file(buf_4, 0, 4096), 10);
+  ASSERT_EQ(10, buf_4->length());
+  ASSERT_EQ(0, (strncmp(buf_1->c_str(), buf_4->c_str(), 10)));
+
+  ASSERT_EQ(m_cache_file_1->read_object_from_file(buf_5, 2, 4), 4);
+  ASSERT_EQ(4, buf_5->length());
+  bufferlist sub_bl;
+  sub_bl.substr_of(*buf_1, 2, 4);
+  ASSERT_EQ(0, (strncmp(sub_bl.c_str(), buf_5->c_str(), 4)));
+
+  ASSERT_EQ(m_cache_file_1->read_object_from_file(buf_6, 12, 4), -1);
+  ASSERT_EQ(0, buf_6->length());
+
+
+  delete m_cache_file_1;
+  delete m_cache_file_2;
+  delete buf_1;
+  delete buf_2;
+  delete buf_3;
+  delete buf_4;
+  delete buf_5;
+  delete buf_6;
+}
diff --git a/src/test/immutable_object_cache/test_sync_file.cc b/src/test/immutable_object_cache/test_sync_file.cc
deleted file mode 100644 (file)
index 1358ac6..0000000
+++ /dev/null
@@ -1,86 +0,0 @@
-// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
-// vim: ts=8 sw=2 smarttab
-
-#include "gtest/gtest.h"
-#include "include/Context.h"
-#include "include/buffer_fwd.h"
-#include "common/Mutex.h"
-#include "common/Cond.h"
-#include "global/global_init.h"
-#include "common/ceph_argparse.h"
-#include "global/global_context.h"
-#include <experimental/filesystem>
-
-#include "tools/immutable_object_cache/ObjectCacheFile.h"
-
-using namespace ceph::immutable_obj_cache;
-namespace efs = std::experimental::filesystem;
-
-class TestObjectCacheFile :public ::testing::Test {
-public:
-  std::string m_cache_root_dir;
-
-  TestObjectCacheFile(){}
-  ~TestObjectCacheFile(){}
-  static void SetUpTestCase() {}
-  static void TearDownTestCase() {}
-
-  void SetUp() override {
-    m_cache_root_dir = g_ceph_context->_conf.get_val<std::string>("immutable_object_cache_path")
-      + "/ceph_immutable_obj_cache/";
-
-    if (efs::exists(m_cache_root_dir)) {
-      efs::remove_all(m_cache_root_dir);
-    }
-    efs::create_directories(m_cache_root_dir);
-  }
-
-  void TearDown() override {
-    efs::remove_all(m_cache_root_dir);
-  }
-
-};
-
-TEST_F(TestObjectCacheFile, test_write_object_to_file) {
-  ObjectCacheFile* m_sync_file_1 = new ObjectCacheFile(g_ceph_context, "test_sync_file_1");
-  ObjectCacheFile* m_sync_file_2 = new ObjectCacheFile(g_ceph_context, "test_sync_file_2");
-  ASSERT_TRUE(m_sync_file_1->get_file_size() == -1);
-  ASSERT_TRUE(m_sync_file_2->get_file_size() == -1);
-  bufferlist* buf_1 = new ceph::bufferlist();
-  bufferlist* buf_2 = new ceph::bufferlist();
-  buf_1->append(std::string(1024, '0'));
-  buf_2->append(std::string(4096, '0'));
-  ASSERT_TRUE(m_sync_file_1->write_object_to_file(*buf_1, 1024) == 1024);
-  ASSERT_TRUE(m_sync_file_2->write_object_to_file(*buf_2, 4096) == 4096);
-  ASSERT_TRUE(m_sync_file_1->get_file_size() == 1024);
-  ASSERT_TRUE(m_sync_file_2->get_file_size() == 4096);
-  delete m_sync_file_1;
-  delete m_sync_file_2;
-  delete buf_1;
-  delete buf_2;
-}
-
-TEST_F(TestObjectCacheFile, test_read_object_from_file) {
-  ObjectCacheFile* m_sync_file_1 = new ObjectCacheFile(g_ceph_context, "test_sync_file_1");
-  ObjectCacheFile* m_sync_file_2 = new ObjectCacheFile(g_ceph_context, "test_sync_file_2");
-  bufferlist* buf_1 = new ceph::bufferlist();
-  bufferlist* buf_2 = new ceph::bufferlist();
-  ASSERT_EQ(m_sync_file_1->read_object_from_file(buf_1, 0, 1024), -1);
-  ASSERT_EQ(m_sync_file_2->read_object_from_file(buf_2, 0, 1024), -1);
-  ASSERT_TRUE(m_sync_file_1->get_file_size() == -1);
-  ASSERT_TRUE(m_sync_file_2->get_file_size() == -1);
-  ASSERT_EQ(m_sync_file_1->read_object_from_file(buf_1, 0, 1024), -1);
-  ASSERT_EQ(m_sync_file_2->read_object_from_file(buf_2, 0, 1024), -1);
-  buf_1->append(std::string(1024, '0'));
-  buf_2->append(std::string(4096, '2'));
-  ASSERT_TRUE(m_sync_file_1->write_object_to_file(*buf_1, 1024) == 1024);
-  ASSERT_TRUE(m_sync_file_2->write_object_to_file(*buf_2, 4096) == 4096);
-  ASSERT_TRUE(m_sync_file_1->get_file_size() == 1024);
-  ASSERT_TRUE(m_sync_file_2->get_file_size() == 4096);
-  ASSERT_EQ(m_sync_file_1->read_object_from_file(buf_1, 0, 1024), 1024);
-  ASSERT_EQ(m_sync_file_2->read_object_from_file(buf_2, 0, 4096), 4096);
-  delete m_sync_file_1;
-  delete m_sync_file_2;
-  delete buf_1;
-  delete buf_2;
-}
index a85d7f5f13c798120bb35186896af6f2a93eb34d..1d7f0be9f9adaaced5bf324ebba972277f51e540 100644 (file)
@@ -67,7 +67,7 @@ int ObjectCacheFile::read_object_from_file(ceph::bufferlist* read_buf, uint64_t
   return read_buf->length();
 }
 
-uint64_t ObjectCacheFile::get_file_size() {
+int ObjectCacheFile::get_file_size() {
   struct stat buf;
   int temp_fd = ::open(m_name.c_str(), O_RDONLY);
   if(temp_fd < 0) {
index 3bbdaa2d8287a35aa662a069a10f45b3b7ab9673..6357a1f592753198ce1d1dff30fca052c412828a 100644 (file)
@@ -29,7 +29,7 @@ public:
 
   int write_object_to_file(ceph::bufferlist read_buf, uint64_t object_len);
   int read_object_from_file(ceph::bufferlist* read_buf, uint64_t object_off, uint64_t object_len);
-  uint64_t get_file_size();
+  int get_file_size();
 
 private:
   CephContext *cct;