xattr-util: merge various getxattr()/listxattr() helpers into getxattr_at_malloc() + listxattr_at_malloc()
Unfortunately fgetxattr() and flistxattr() don't work via O_PATH fds. Let's thus add fallbacks to go via /proc/self/fd/ in these cases. Also, let's merge all the various flavours we have here into singular implementations that can do everything we need: 1. malloc() loop handling 2. by fd, by path, or combination (i.e. a proper openat() like API) 3. work on O_PATH
This commit is contained in:
@@ -149,7 +149,7 @@ static void test_copy_tree(void) {
|
||||
assert_se(read_full_file(f, &buf, &sz) == 0);
|
||||
assert_se(streq(buf, "file\n"));
|
||||
|
||||
k = getxattr_malloc(f, "user.testxattr", &c, false);
|
||||
k = lgetxattr_malloc(f, "user.testxattr", &c);
|
||||
assert_se(xattr_worked < 0 || ((k >= 0) == !!xattr_worked));
|
||||
|
||||
if (k >= 0) {
|
||||
|
||||
+12
-11
@@ -15,14 +15,12 @@
|
||||
#include "tmpfile-util.h"
|
||||
#include "xattr-util.h"
|
||||
|
||||
static void test_fgetxattrat_fake(void) {
|
||||
static void test_getxattr_at_malloc(void) {
|
||||
char t[] = "/var/tmp/xattrtestXXXXXX";
|
||||
_cleanup_free_ char *value = NULL;
|
||||
_cleanup_close_ int fd = -1;
|
||||
const char *x;
|
||||
char v[3];
|
||||
int r;
|
||||
size_t size;
|
||||
|
||||
log_info("/* %s */", __func__);
|
||||
|
||||
@@ -38,21 +36,24 @@ static void test_fgetxattrat_fake(void) {
|
||||
fd = open(t, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
|
||||
assert_se(fd >= 0);
|
||||
|
||||
assert_se(fgetxattrat_fake(fd, "test", "user.foo", v, 3, 0, &size) >= 0);
|
||||
assert_se(size == 3);
|
||||
assert_se(memcmp(v, "bar", 3) == 0);
|
||||
assert_se(getxattr_at_malloc(fd, "test", "user.foo", 0, &value) == 3);
|
||||
assert_se(memcmp(value, "bar", 3) == 0);
|
||||
value = mfree(value);
|
||||
|
||||
assert_se(getxattr_at_malloc(AT_FDCWD, x, "user.foo", 0, &value) == 3);
|
||||
assert_se(memcmp(value, "bar", 3) == 0);
|
||||
value = mfree(value);
|
||||
|
||||
safe_close(fd);
|
||||
fd = open("/", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
|
||||
assert_se(fd >= 0);
|
||||
r = fgetxattrat_fake(fd, "usr", "user.idontexist", v, 3, 0, &size);
|
||||
r = getxattr_at_malloc(fd, "usr", "user.idontexist", 0, &value);
|
||||
assert_se(r == -ENODATA || ERRNO_IS_NOT_SUPPORTED(r));
|
||||
|
||||
safe_close(fd);
|
||||
fd = open(x, O_PATH|O_CLOEXEC);
|
||||
assert_se(fd >= 0);
|
||||
r = fgetxattrat_fake_malloc(fd, NULL, "user.foo", AT_EMPTY_PATH, &value);
|
||||
assert_se(r == 3);
|
||||
assert_se(getxattr_at_malloc(fd, NULL, "user.foo", 0, &value) == 3);
|
||||
assert_se(streq(value, "bar"));
|
||||
|
||||
cleanup:
|
||||
@@ -68,7 +69,7 @@ static void test_getcrtime(void) {
|
||||
|
||||
log_info("/* %s */", __func__);
|
||||
|
||||
assert_se(tmp_dir(&vt) >= 0);
|
||||
assert_se(var_tmp_dir(&vt) >= 0);
|
||||
|
||||
fd = open_tmpfile_unlinkable(vt, O_RDWR);
|
||||
assert_se(fd >= 0);
|
||||
@@ -92,7 +93,7 @@ static void test_getcrtime(void) {
|
||||
int main(void) {
|
||||
test_setup_logging(LOG_DEBUG);
|
||||
|
||||
test_fgetxattrat_fake();
|
||||
test_getxattr_at_malloc();
|
||||
test_getcrtime();
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user