misc: Add cr_identical_files() function
authorTomas Mlcoch <tmlcoch@redhat.com>
Mon, 11 May 2015 07:54:03 +0000 (09:54 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Mon, 11 May 2015 07:54:03 +0000 (09:54 +0200)
src/misc.c
src/misc.h

index b101f7fbfc8864bd4ca0301127a7f4019de0221b..ba9d2d6d668bfcc6fd7eee9d90edc6147cee79fb 100644 (file)
@@ -19,6 +19,7 @@
 
 #define _XOPEN_SOURCE 500
 
+#include <glib/gstdio.h>
 #include <glib.h>
 #include <arpa/inet.h>
 #include <assert.h>
@@ -1432,3 +1433,44 @@ cr_spawn_check_exit_status(gint exit_status, GError **err)
 
     return FALSE;
 }
+
+gboolean
+cr_identical_files(const gchar *fn1,
+                   const gchar *fn2,
+                   gboolean *identical,
+                   GError **err)
+{
+    int rc;
+    GStatBuf buf1, buf2;
+
+    *identical = FALSE;
+
+    // Stat old file
+    rc = g_stat(fn1, &buf1);
+    if (rc == -1) {
+        if (errno == ENOENT) // The first file doesn't exist
+            return TRUE;
+
+        g_set_error(err, CREATEREPO_C_ERROR, CRE_IO,
+                    "Cannot stat %s: %s", fn1, g_strerror(errno));
+        return FALSE;
+    }
+
+    // Stat new file
+    rc = g_stat(fn2, &buf2);
+    if (rc == -1) {
+        if (errno == ENOENT) // The second file doesn't exist
+            return TRUE;
+
+        g_set_error(err, CREATEREPO_C_ERROR, CRE_IO,
+                    "Cannot stat %s: %s", fn2, g_strerror(errno));
+        return FALSE;
+    }
+
+    // Check if both paths point to the same file
+    if (buf1.st_ino == buf2.st_ino)
+        *identical = TRUE;
+
+    return TRUE;
+}
+
index d5cd14694c2abeb320f3baa0440c4383bfefc979..f6c78a350beff61bf76b3080c9183350fef31e36 100644 (file)
@@ -506,6 +506,23 @@ cr_str_to_nevra(const char *str);
 void
 cr_nevra_free(cr_NEVRA *nevra);
 
+/** Are the files identical?
+ * Different paths could point to the same file.
+ * This functions checks if both paths point to the same file or not.
+ * If one of the files doesn't exists, the funcion doesn't fail
+ * and just put FALSE into "indentical" value and returns.
+ * @param fn1           First path
+ * @param fn2           Second path
+ * @param identical     Are the files same or not
+ * @param err           GError **
+ * @return              FALSE if an error was encountered, TRUE otherwise
+ */
+gboolean
+cr_identical_files(const gchar *fn1,
+                   const gchar *fn2,
+                   gboolean *identical,
+                   GError **err);
+
 /** @} */
 
 #ifdef __cplusplus