teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0258-util-add-files_same-helper-function.patch

84b277
From b6fbffe3d4a9435cc8560ee0271c7e6e763e89c1 Mon Sep 17 00:00:00 2001
84b277
From: Harald Hoyer <harald@redhat.com>
84b277
Date: Thu, 6 Mar 2014 09:12:57 +0100
84b277
Subject: [PATCH] util: add files_same() helper function
84b277
84b277
files_same() returns
84b277
     1, if the files are the same
84b277
     0, if the files have different inode/dev numbers
84b277
 errno, for any stat error
84b277
84b277
 (cherry picked from commit 9d9951a460a90ef0e1e0384742cefdcf85193f8c)
84b277
84b277
 Related: #1111199
84b277
---
84b277
 src/shared/util.c | 24 ++++++++++++++++--------
84b277
 src/shared/util.h |  2 ++
84b277
 2 files changed, 18 insertions(+), 8 deletions(-)
84b277
84b277
diff --git a/src/shared/util.c b/src/shared/util.c
84b277
index 2974c2a..fb1e6d1 100644
84b277
--- a/src/shared/util.c
84b277
+++ b/src/shared/util.c
84b277
@@ -3311,19 +3311,27 @@ bool on_tty(void) {
84b277
         return cached_on_tty;
84b277
 }
84b277
 
84b277
-int running_in_chroot(void) {
84b277
-        struct stat a = {}, b = {};
84b277
+int files_same(const char *filea, const char *fileb) {
84b277
+        struct stat a, b;
84b277
 
84b277
-        /* Only works as root */
84b277
-        if (stat("/proc/1/root", &a) < 0)
84b277
+        if (stat(filea, &a) < 0)
84b277
                 return -errno;
84b277
 
84b277
-        if (stat("/", &b) < 0)
84b277
+        if (stat(fileb, &b) < 0)
84b277
                 return -errno;
84b277
 
84b277
-        return
84b277
-                a.st_dev != b.st_dev ||
84b277
-                a.st_ino != b.st_ino;
84b277
+        return a.st_dev == b.st_dev &&
84b277
+               a.st_ino == b.st_ino;
84b277
+}
84b277
+
84b277
+int running_in_chroot(void) {
84b277
+        int ret;
84b277
+
84b277
+        ret = files_same("/proc/1/root", "/");
84b277
+        if (ret < 0)
84b277
+                return ret;
84b277
+
84b277
+        return ret == 0;
84b277
 }
84b277
 
84b277
 char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
84b277
diff --git a/src/shared/util.h b/src/shared/util.h
84b277
index e577ef0..631a385 100644
84b277
--- a/src/shared/util.h
84b277
+++ b/src/shared/util.h
84b277
@@ -403,6 +403,8 @@ static inline const char *ansi_highlight_off(void) {
84b277
         return on_tty() ? ANSI_HIGHLIGHT_OFF : "";
84b277
 }
84b277
 
84b277
+int files_same(const char *filea, const char *fileb);
84b277
+
84b277
 int running_in_chroot(void);
84b277
 
84b277
 char *ellipsize(const char *s, size_t length, unsigned percent);