From c7db303028a43230bb0d9d8f029c29371ba2b466 Mon Sep 17 00:00:00 2001 From: Samuel Just Date: Fri, 8 Apr 2016 11:32:37 -0700 Subject: [PATCH] chain_xattr: add chain_getxattr_buf Otherwise callers need to implement buffer doubling in a lot of cases, which is error prone. Signed-off-by: Samuel Just --- src/os/filestore/chain_xattr.cc | 29 +++++++++++++++++++++++++++++ src/os/filestore/chain_xattr.h | 2 ++ 2 files changed, 31 insertions(+) diff --git a/src/os/filestore/chain_xattr.cc b/src/os/filestore/chain_xattr.cc index 01d28fc2fe7..0461c1953db 100644 --- a/src/os/filestore/chain_xattr.cc +++ b/src/os/filestore/chain_xattr.cc @@ -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; diff --git a/src/os/filestore/chain_xattr.h b/src/os/filestore/chain_xattr.h index f6b7997b417..54a85686b97 100644 --- a/src/os/filestore/chain_xattr.h +++ b/src/os/filestore/chain_xattr.h @@ -6,6 +6,7 @@ #include "common/xattr.h" #include "include/assert.h" +#include "include/buffer.h" #include #include @@ -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); -- 2.39.5