fs-util: move link_fd() from tmpfile-util.c into generic fs-util.c
authorLennart Poettering <lennart@poettering.net>
Wed, 13 Mar 2024 16:48:04 +0000 (17:48 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 14 Mar 2024 08:17:46 +0000 (09:17 +0100)
It's a generically useful call, let's move it so that we can use it at
more places.

src/basic/fs-util.c
src/basic/fs-util.h
src/basic/tmpfile-util.c

index 5bc7d2f95beb568c89c7dafe0d24c21739c9d3ba..999713d243b3f66879027f7274239e2f15dbee51 100644 (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));
+}
index 6a1e2e76d14929fc246d988e511c33d4ddd45b38..86fc67115861b803d1aa43a0e8fa8aea0b1a65ff 100644 (file)
@@ -146,3 +146,5 @@ int xopenat_lock_full(int dir_fd, const char *path, int open_flags, XOpenFlags x
 static inline int xopenat_lock(int dir_fd, const char *path, int open_flags, LockType locktype, int operation) {
         return xopenat_lock_full(dir_fd, path, open_flags, 0, 0, locktype, operation);
 }
+
+int link_fd(int fd, int newdirfd, const char *newpath);
index e77ca9424892d7a113b258c388a04edabfa9282e..4cae5cbd0b70c6e71fb6c54c1d85a5c501b64d51 100644 (file)
@@ -330,26 +330,6 @@ int fopen_tmpfile_linkable(const char *target, int flags, char **ret_path, FILE
         return 0;
 }
 
-static int link_fd(int fd, int newdirfd, const char *newpath) {
-        int r;
-
-        assert(fd >= 0);
-        assert(newdirfd >= 0 || newdirfd == AT_FDCWD);
-        assert(newpath);
-
-        /* Try symlinking via /proc/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));
-}
-
 int link_tmpfile_at(int fd, int dir_fd, const char *path, const char *target, LinkTmpfileFlags flags) {
         _cleanup_free_ char *tmp = NULL;
         int r;