From: Tomas Mlcoch Date: Mon, 11 May 2015 07:54:03 +0000 (+0200) Subject: misc: Add cr_identical_files() function X-Git-Tag: upstream/0.10.0~72 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ca1660beabf627e696c324daeb17320991755ff5;p=services%2Fcreaterepo_c.git misc: Add cr_identical_files() function --- diff --git a/src/misc.c b/src/misc.c index b101f7f..ba9d2d6 100644 --- a/src/misc.c +++ b/src/misc.c @@ -19,6 +19,7 @@ #define _XOPEN_SOURCE 500 +#include #include #include #include @@ -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; +} + diff --git a/src/misc.h b/src/misc.h index d5cd146..f6c78a3 100644 --- a/src/misc.h +++ b/src/misc.h @@ -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