xattr-util: support AT_EMPTY_PATH in fgetxattrat_fake()
authorLennart Poettering <lennart@poettering.net>
Tue, 20 Feb 2018 11:46:14 +0000 (12:46 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 20 Feb 2018 14:39:31 +0000 (15:39 +0100)
Let's expose fstatat() like behaviour if AT_EMPTY_PATH is defined.

Also, check the specified flags returning EINVAL on the flags we don't
emulate.

src/basic/xattr-util.c

index 12d04ee..6f568ff 100644 (file)
@@ -31,6 +31,7 @@
 #include "macro.h"
 #include "sparse-endian.h"
 #include "stdio-util.h"
+#include "string-util.h"
 #include "time-util.h"
 #include "xattr-util.h"
 
@@ -111,11 +112,21 @@ ssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute,
 
         /* The kernel doesn't have a fgetxattrat() command, hence let's emulate one */
 
-        fd = openat(dirfd, filename, O_CLOEXEC|O_PATH|(flags & AT_SYMLINK_NOFOLLOW ? O_NOFOLLOW : 0));
-        if (fd < 0)
-                return -errno;
+        if (flags & ~(AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH))
+                return -EINVAL;
+
+        if (isempty(filename)) {
+                if (!(flags & AT_EMPTY_PATH))
+                        return -EINVAL;
 
-        xsprintf(fn, "/proc/self/fd/%i", fd);
+                xsprintf(fn, "/proc/self/fd/%i", dirfd);
+        } else {
+                fd = openat(dirfd, filename, O_CLOEXEC|O_PATH|(flags & AT_SYMLINK_NOFOLLOW ? O_NOFOLLOW : 0));
+                if (fd < 0)
+                        return -errno;
+
+                xsprintf(fn, "/proc/self/fd/%i", fd);
+        }
 
         l = getxattr(fn, attribute, value, size);
         if (l < 0)