1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-18 06:03:42 +03:00

xattr-util: add flistxattr_malloc() that returns a NULSTR

This commit is contained in:
Lennart Poettering 2019-11-14 14:51:04 +01:00
parent 6ac99d9d5f
commit 7de2d2e17d
2 changed files with 36 additions and 0 deletions

View File

@ -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;
}
}

View File

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