summaryrefslogtreecommitdiff
path: root/src/basic/xattr-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2019-11-14 14:51:04 +0100
committerLennart Poettering <lennart@poettering.net>2019-11-19 15:44:58 +0100
commit7de2d2e17d50c87a9fa9163980a11b15ecb14c55 (patch)
tree9aaf53704209e38eb8e9c6baff83c2b3380990fe /src/basic/xattr-util.c
parent6ac99d9d5ffb0e8abb7311e2cb0abe8a4adbb38a (diff)
downloadsystemd-7de2d2e17d50c87a9fa9163980a11b15ecb14c55.tar.gz
xattr-util: add flistxattr_malloc() that returns a NULSTR
Diffstat (limited to 'src/basic/xattr-util.c')
-rw-r--r--src/basic/xattr-util.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/basic/xattr-util.c b/src/basic/xattr-util.c
index aae693e7b1..0125a9c2c0 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;
+ }
+}