]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
crimson: add buffer_io helpers
authorKefu Chai <kchai@redhat.com>
Fri, 14 Jun 2019 03:49:47 +0000 (11:49 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 14 Jun 2019 08:31:22 +0000 (16:31 +0800)
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/crimson/CMakeLists.txt
src/crimson/common/buffer_io.cc [new file with mode: 0644]
src/crimson/common/buffer_io.h [new file with mode: 0644]

index 9a8fcf8a2d87c2dfa513e56b48a034f3faee000a..fd3f99a1b70c7a08c2e2dd4b48f64f92b080132a 100644 (file)
@@ -4,6 +4,7 @@ set_target_properties(crimson::cflags PROPERTIES
   INTERFACE_LINK_LIBRARIES Seastar::seastar)
 
 set(crimson_common_srcs
+  common/buffer_io.cc
   common/config_proxy.cc
   common/perf_counters_collection.cc
   common/assert.cc
diff --git a/src/crimson/common/buffer_io.cc b/src/crimson/common/buffer_io.cc
new file mode 100644 (file)
index 0000000..8e33cab
--- /dev/null
@@ -0,0 +1,34 @@
+#include "buffer_io.h"
+
+#include <seastar/core/reactor.hh>
+#include <seastar/core/fstream.hh>
+#include <seastar/core/do_with.hh>
+
+#include "include/buffer.h"
+
+namespace ceph::buffer {
+
+seastar::future<> write_file(ceph::buffer::list&& bl,
+                             seastar::sstring fn,
+                             seastar::file_permissions permissions)
+{
+  const auto flags = (seastar::open_flags::wo |
+                      seastar::open_flags::create |
+                      seastar::open_flags::truncate);
+  seastar::file_open_options foo;
+  foo.create_permissions = permissions;
+  return seastar::open_file_dma(fn, flags, foo).then(
+    [bl=std::move(bl)](seastar::file f) {
+    return seastar::do_with(seastar::make_file_output_stream(f), std::move(f),
+                            [&bl](seastar::output_stream<char>& out,
+                                  seastar::file& f) {
+      return seastar::do_for_each(bl.buffers(), [&out](auto& buf) {
+        return out.write(buf.c_str(), buf.length());
+      }).then([&out] {
+        return out.close();
+      });
+    });
+  });
+}
+
+}
diff --git a/src/crimson/common/buffer_io.h b/src/crimson/common/buffer_io.h
new file mode 100644 (file)
index 0000000..5cc6a6e
--- /dev/null
@@ -0,0 +1,19 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#pragma once
+
+#include <seastar/core/future.hh>
+#include <seastar/core/file-types.hh>
+
+#include "include/buffer_fwd.h"
+
+namespace ceph::buffer {
+  seastar::future<> write_file(ceph::buffer::list&& bl,
+                               seastar::sstring fn,
+                               seastar::file_permissions= // 0644
+                                 (seastar::file_permissions::user_read |
+                                  seastar::file_permissions::user_write |
+                                  seastar::file_permissions::group_read |
+                                  seastar::file_permissions::others_read));
+}