]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
chain_xattr: add chain_getxattr_buf
authorSamuel Just <sjust@redhat.com>
Fri, 8 Apr 2016 18:32:37 +0000 (11:32 -0700)
committerSamuel Just <sjust@redhat.com>
Fri, 8 Apr 2016 22:39:13 +0000 (15:39 -0700)
Otherwise callers need to implement buffer doubling in a lot of cases,
which is error prone.

Signed-off-by: Samuel Just <sjust@redhat.com>
src/os/filestore/chain_xattr.cc
src/os/filestore/chain_xattr.h

index 01d28fc2fe7510779db54f93cc4595bc257603f0..0461c1953db022cf1a01afa79eb4fed83dc17517 100644 (file)
@@ -173,6 +173,35 @@ int chain_getxattr(const char *fn, const char *name, void *val, size_t size)
   return ret;
 }
 
+int chain_getxattr_buf(const char *fn, const char *name, bufferptr *bp)
+{
+  size_t size = 1024; // Initial
+  while (1) {
+    bufferptr buf(size);
+    int r = chain_getxattr(
+      fn,
+      name,
+      buf.c_str(),
+      size);
+    if (r > 0) {
+      buf.set_length(r);
+      if (bp)
+       bp->swap(buf);
+      return r;
+    } else if (r == 0) {
+      return 0;
+    } else {
+      if (r == -ERANGE) {
+       size *= 2;
+      } else {
+       return r;
+      }
+    }
+  }
+  assert(0 == "unreachable");
+  return 0;
+}
+
 static int chain_fgetxattr_len(int fd, const char *name)
 {
   int i = 0, total = 0;
index f6b7997b417240ba56ca3d217dac56e6559e262f..54a85686b9706ed251c469772a92e66a064746dd 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "common/xattr.h"
 #include "include/assert.h"
+#include "include/buffer.h"
 #include <string.h>
 #include <stdio.h>
 
@@ -80,6 +81,7 @@ static inline int sys_fremovexattr(int fd, const char *name)
 // wrappers to chain large values across multiple xattrs
 
 int chain_getxattr(const char *fn, const char *name, void *val, size_t size);
+int chain_getxattr_buf(const char *fn, const char *name, bufferptr *bp);
 int chain_fgetxattr(int fd, const char *name, void *val, size_t size);
 
 int get_xattr_block_size(size_t size);