]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fs-util: try AT_EMPTY_PATH first for futimens_opath
authorMike Yuan <me@yhndnzj.com>
Mon, 29 Apr 2024 08:22:37 +0000 (16:22 +0800)
committerMike Yuan <me@yhndnzj.com>
Mon, 29 Apr 2024 08:33:17 +0000 (16:33 +0800)
src/basic/fs-util.c

index 7f0b5814be540189366bba72c97b5c496674c2da..43836f082ff65b931dd8166624bf800b119e4105 100644 (file)
@@ -325,12 +325,22 @@ int fchmod_opath(int fd, mode_t m) {
 int futimens_opath(int fd, const struct timespec ts[2]) {
         /* Similar to fchmod_opath() but for futimens() */
 
-        if (utimensat(AT_FDCWD, FORMAT_PROC_FD_PATH(fd), ts, 0) < 0) {
+        assert(fd >= 0);
+
+        if (utimensat(fd, "", ts, AT_EMPTY_PATH) >= 0)
+                return 0;
+        if (errno != EINVAL)
+                return -errno;
+
+        /* Support for AT_EMPTY_PATH is added rather late (kernel 5.8), so fall back to going through /proc/
+         * if unavailable. */
+
+        if (utimensat(AT_FDCWD, FORMAT_PROC_FD_PATH(fd), ts, /* flags = */ 0) < 0) {
                 if (errno != ENOENT)
                         return -errno;
 
                 if (proc_mounted() == 0)
-                        return -ENOSYS; /* if we have no /proc/, the concept is not implementable */
+                        return -ENOSYS;
 
                 return -ENOENT;
         }