fd-util: make fd_get_path() support AT_FDCWD

This commit is contained in:
Yu Watanabe
2023-03-30 12:03:33 +09:00
parent 8067fe86ff
commit 46693a7925
2 changed files with 10 additions and 0 deletions
+5
View File
@@ -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
+5
View File
@@ -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);