From: Lennart Poettering Date: Thu, 14 Nov 2019 13:51:04 +0000 (+0100) Subject: xattr-util: add flistxattr_malloc() that returns a NULSTR X-Git-Tag: v244-rc1~6^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7de2d2e17d50c87a9fa9163980a11b15ecb14c55;p=thirdparty%2Fsystemd.git xattr-util: add flistxattr_malloc() that returns a NULSTR --- diff --git a/src/basic/xattr-util.c b/src/basic/xattr-util.c index aae693e7b1b..0125a9c2c08 100644 --- a/src/basic/xattr-util.c +++ b/src/basic/xattr-util.c @@ -234,3 +234,37 @@ int fd_setcrtime(int fd, usec_t usec) { return 0; } + +int flistxattr_malloc(int fd, char **ret) { + size_t l = 100; + + assert(fd >= 0); + assert(ret); + + for (;;) { + _cleanup_free_ char *v = NULL; + ssize_t n; + + v = new(char, l+1); + if (!v) + return -ENOMEM; + + n = flistxattr(fd, v, l); + if (n < 0) { + if (errno != ERANGE) + return -errno; + } else { + v[n] = 0; /* NUL terminate */ + *ret = TAKE_PTR(v); + return (int) n; + } + + n = flistxattr(fd, NULL, 0); + if (n < 0) + return -errno; + if (n > INT_MAX) /* We couldn't return this as 'int' anymore */ + return -E2BIG; + + l = (size_t) n; + } +} diff --git a/src/basic/xattr-util.h b/src/basic/xattr-util.h index 9fa85d71296..a69e913b7f3 100644 --- a/src/basic/xattr-util.h +++ b/src/basic/xattr-util.h @@ -23,3 +23,5 @@ int fd_setcrtime(int fd, usec_t usec); int fd_getcrtime(int fd, usec_t *usec); int path_getcrtime(const char *p, usec_t *usec); int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags); + +int flistxattr_malloc(int fd, char **ret);