]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mon/MonitorDBStore: {read,write}_meta
authorSage Weil <sage@redhat.com>
Mon, 2 May 2016 19:11:43 +0000 (15:11 -0400)
committerBoris Ranto <branto@redhat.com>
Fri, 6 May 2016 11:44:17 +0000 (13:44 +0200)
These are lifted from ObjectStore.

Signed-off-by: Sage Weil <sage@redhat.com>
src/mon/MonitorDBStore.h

index 73e91c6336b88ac84763de4546eec0b74a1dd378..95b14a33435e780192f09caf57b47d64c67355f4 100644 (file)
@@ -28,6 +28,7 @@
 #include "common/Finisher.h"
 #include "common/errno.h"
 #include "common/debug.h"
+#include "common/safe_io.h"
 
 class MonitorDBStore
 {
@@ -661,6 +662,57 @@ class MonitorDBStore
     return db->get_estimated_size(extras);
   }
 
+  /**
+   * write_meta - write a simple configuration key out-of-band
+   *
+   * Write a simple key/value pair for basic store configuration
+   * (e.g., a uuid or magic number) to an unopened/unmounted store.
+   * The default implementation writes this to a plaintext file in the
+   * path.
+   *
+   * A newline is appended.
+   *
+   * @param key key name (e.g., "fsid")
+   * @param value value (e.g., a uuid rendered as a string)
+   * @returns 0 for success, or an error code
+   */
+  int write_meta(const std::string& key,
+                const std::string& value) const {
+    string v = value;
+    v += "\n";
+    int r = safe_write_file(path.c_str(), key.c_str(),
+                           v.c_str(), v.length());
+    if (r < 0)
+      return r;
+    return 0;
+  }
+
+  /**
+   * read_meta - read a simple configuration key out-of-band
+   *
+   * Read a simple key value to an unopened/mounted store.
+   *
+   * Trailing whitespace is stripped off.
+   *
+   * @param key key name
+   * @param value pointer to value string
+   * @returns 0 for success, or an error code
+   */
+  int read_meta(const std::string& key,
+               std::string *value) const {
+    char buf[4096];
+    int r = safe_read_file(path.c_str(), key.c_str(),
+                          buf, sizeof(buf));
+    if (r <= 0)
+      return r;
+    // drop trailing newlines
+    while (r && isspace(buf[r-1])) {
+      --r;
+    }
+    *value = string(buf, r);
+    return 0;
+  }
+
   explicit MonitorDBStore(const string& path)
     : path(path),
       db(0),