]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
file_utils: add same_file_lax()
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 11 Feb 2021 13:40:41 +0000 (14:40 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Thu, 11 Feb 2021 13:44:52 +0000 (14:44 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/file_utils.c
src/lxc/file_utils.h

index e046c2dd0040c5598d2af877a8b66151418c83e0..78fa85ed7029498bb5ebc91f7b5f3d0464942536 100644 (file)
@@ -725,3 +725,26 @@ char *read_file_at(int dfd, const char *fnam,
 
        return move_ptr(buf);
 }
+
+bool same_file_lax(int fda, int fdb)
+{
+       struct stat st_fda, st_fdb;
+
+
+        if (fda == fdb)
+                return true;
+
+        if (fstat(fda, &st_fda) < 0)
+                return false;
+
+        if (fstat(fdb, &st_fdb) < 0)
+                return false;
+
+       errno = EINVAL;
+       if ((st_fda.st_mode & S_IFMT) != (st_fdb.st_mode & S_IFMT))
+               return false;
+
+       errno = EINVAL;
+       return (st_fda.st_dev == st_fdb.st_dev) &&
+              (st_fda.st_ino == st_fdb.st_ino);
+}
index 76a48edb3749f767518c974bb2d231e0e00a8ede..36bd9b0c2f59f8c640de44863100603316a68692 100644 (file)
@@ -97,4 +97,12 @@ __hidden extern char *read_file_at(int dfd, const char *fnam,
 __hidden extern ssize_t lxc_read_try_buf_at(int dfd, const char *path,
                                             void *buf, size_t count);
 
+/*
+ * Check if two fds refer to the same file.
+ * The function is "lax" in so far, as it doesn't care whether fda and fdb have
+ * the same flags or whether they share the same device context when they refer
+ * to devices.
+ */
+__hidden extern bool same_file_lax(int fda, int fdb);
+
 #endif /* __LXC_FILE_UTILS_H */