From: Yu Watanabe Date: Thu, 30 Mar 2023 02:42:21 +0000 (+0900) Subject: fd-util: make fd_get_path() support AT_FDCWD X-Git-Tag: v254-rc1~869^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F27060%2Fhead;p=thirdparty%2Fsystemd.git fd-util: make fd_get_path() support AT_FDCWD --- diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index b968bf948c4..b58a080d90b 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -534,6 +534,11 @@ bool fdname_is_valid(const char *s) { int fd_get_path(int fd, char **ret) { int r; + assert(fd >= 0 || fd == AT_FDCWD); + + if (fd == AT_FDCWD) + return safe_getcwd(ret); + r = readlink_malloc(FORMAT_PROC_FD_PATH(fd), ret); if (r == -ENOENT) { /* ENOENT can mean two things: that the fd does not exist or that /proc is not mounted. Let's make diff --git a/src/test/test-fd-util.c b/src/test/test-fd-util.c index 797b774998d..adcd851d7f5 100644 --- a/src/test/test-fd-util.c +++ b/src/test/test-fd-util.c @@ -649,6 +649,11 @@ TEST(fd_get_path) { assert_se(safe_getcwd(&saved_cwd) >= 0); assert_se(chdir(t) >= 0); + assert_se(fd_get_path(AT_FDCWD, &p) >= 0); + assert_se(streq(p, t)); + + p = mfree(p); + assert_se(q = path_join(t, "regular")); assert_se(touch(q) >= 0); assert_se(mkdirat_parents(tfd, "subdir/symlink", 0755) >= 0);