From f25bff5eaf6881717e873f27c26f2e8264517c16 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 25 Sep 2020 16:40:02 +0200 Subject: [PATCH] fs-util: add new futimens_opath() helper futimens() that works for O_PATH fds. --- src/basic/fs-util.c | 19 +++++++++++++++++++ src/basic/fs-util.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index c20a29332a..587b3504ee 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -312,6 +312,25 @@ int fchmod_opath(int fd, mode_t m) { return 0; } +int futimens_opath(int fd, const struct timespec ts[2]) { + char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; + + /* Similar to fchmod_path() but for futimens() */ + + xsprintf(procfs_path, "/proc/self/fd/%i", fd); + if (utimensat(AT_FDCWD, procfs_path, ts, 0) < 0) { + if (errno != ENOENT) + return -errno; + + if (proc_mounted() == 0) + return -ENOSYS; /* if we have no /proc/, the concept is not implementable */ + + return -ENOENT; + } + + return 0; +} + int stat_warn_permissions(const char *path, const struct stat *st) { assert(path); assert(st); diff --git a/src/basic/fs-util.h b/src/basic/fs-util.h index eb6e1eee4f..cf87a1e4d5 100644 --- a/src/basic/fs-util.h +++ b/src/basic/fs-util.h @@ -38,6 +38,8 @@ int fchmod_and_chown(int fd, mode_t mode, uid_t uid, gid_t gid); int fchmod_umask(int fd, mode_t mode); int fchmod_opath(int fd, mode_t m); +int futimens_opath(int fd, const struct timespec ts[2]); + int fd_warn_permissions(const char *path, int fd); int stat_warn_permissions(const char *path, const struct stat *st); -- 2.25.1