fs-util: move link_fd() from tmpfile-util.c into generic fs-util.c

It's a generically useful call, let's move it so that we can use it at
more places.
This commit is contained in:
Lennart Poettering
2024-03-14 09:17:46 +01:00
parent dd92ba8a7a
commit 0b8e36f064
3 changed files with 22 additions and 20 deletions
+20
View File
@@ -1236,3 +1236,23 @@ int xopenat_lock_full(
return TAKE_FD(fd);
}
int link_fd(int fd, int newdirfd, const char *newpath) {
int r;
assert(fd >= 0);
assert(newdirfd >= 0 || newdirfd == AT_FDCWD);
assert(newpath);
/* Try linking via /proc/self/fd/ first. */
r = RET_NERRNO(linkat(AT_FDCWD, FORMAT_PROC_FD_PATH(fd), newdirfd, newpath, AT_SYMLINK_FOLLOW));
if (r != -ENOENT)
return r;
/* Fall back to symlinking via AT_EMPTY_PATH as fallback (this requires CAP_DAC_READ_SEARCH and a
* more recent kernel, but does not require /proc/ mounted) */
if (proc_mounted() != 0)
return r;
return RET_NERRNO(linkat(fd, "", newdirfd, newpath, AT_EMPTY_PATH));
}