chiark / gitweb /
util: add files_same() helper function
authorHarald Hoyer <harald@redhat.com>
Thu, 6 Mar 2014 08:12:57 +0000 (09:12 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 11 Mar 2014 03:34:06 +0000 (04:34 +0100)
files_same() returns
     1, if the files are the same
     0, if the files have different inode/dev numbers
 errno, for any stat error

src/shared/util.c
src/shared/util.h

index d28caae6c2ebc0fd034acebf2d06fa77bd1c96cb..10f113bd240975d7f9fc810c21623ad54aba2e67 100644 (file)
@@ -3198,19 +3198,27 @@ bool on_tty(void) {
         return cached_on_tty;
 }
 
-int running_in_chroot(void) {
-        struct stat a = {}, b = {};
+int files_same(const char *filea, const char *fileb) {
+        struct stat a, b;
 
-        /* Only works as root */
-        if (stat("/proc/1/root", &a) < 0)
+        if (stat(filea, &a) < 0)
                 return -errno;
 
-        if (stat("/", &b) < 0)
+        if (stat(fileb, &b) < 0)
                 return -errno;
 
-        return
-                a.st_dev != b.st_dev ||
-                a.st_ino != b.st_ino;
+        return a.st_dev == b.st_dev &&
+               a.st_ino == b.st_ino;
+}
+
+int running_in_chroot(void) {
+        int ret;
+
+        ret = files_same("/proc/1/root", "/");
+        if (ret < 0)
+                return ret;
+
+        return ret == 0;
 }
 
 static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
index c2bc9771b9f424c4bc5f6d5eb3725cfd184975aa..11d28665b6ba5144d5b81fae22124ace9e1ceda5 100644 (file)
@@ -451,6 +451,8 @@ static inline const char *ansi_highlight_off(void) {
         return on_tty() ? ANSI_HIGHLIGHT_OFF : "";
 }
 
+int files_same(const char *filea, const char *fileb);
+
 int running_in_chroot(void);
 
 char *ellipsize(const char *s, size_t length, unsigned percent);