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);