util: added file_compare() utility function
authorAndrew Tridgell <tridge@samba.org>
Thu, 11 Feb 2010 09:19:40 +0000 (20:19 +1100)
committerAndrew Tridgell <tridge@samba.org>
Thu, 11 Feb 2010 10:04:13 +0000 (21:04 +1100)
file_compare() returns true if two files are the same. It is meant for
small files.

Pair-Programmed-With: Andrew Bartlett <abartlet@samba.org>

lib/util/util.h
lib/util/util_file.c

index 729190af6333f589a2e93d3b7ef7684ac93e8e25..da9776163adc3d0fead6fadb6e899932bc01c35d 100644 (file)
@@ -601,6 +601,11 @@ _PUBLIC_ int vfdprintf(int fd, const char *format, va_list ap) PRINTF_ATTRIBUTE(
 _PUBLIC_ int fdprintf(int fd, const char *format, ...) PRINTF_ATTRIBUTE(2,3);
 _PUBLIC_ bool large_file_support(const char *path);
 
+/*
+  compare two files, return true if the two files have the same content
+ */
+bool file_compare(const char *path1, const char *path2);
+
 /* The following definitions come from lib/util/util.c  */
 
 
index 7466004e5ce07f175608202d797d23d94f2f2274..aa0b2d5855782d025ccf9780e70711111c2602c2 100644 (file)
@@ -435,3 +435,25 @@ _PUBLIC_ bool large_file_support(const char *path)
 }
 
 
+/*
+  compare two files, return true if the two files have the same content
+ */
+bool file_compare(const char *path1, const char *path2)
+{
+       size_t size1, size2;
+       char *p1, *p2;
+       TALLOC_CTX *mem_ctx = talloc_new(NULL);
+
+       p1 = file_load(path1, &size1, 0, mem_ctx);
+       p2 = file_load(path2, &size2, 0, mem_ctx);
+       if (!p1 || !p2 || size1 != size2) {
+               talloc_free(mem_ctx);
+               return false;
+       }
+       if (memcmp(p1, p2, size1) != 0) {
+               talloc_free(mem_ctx);
+               return false;
+       }
+       talloc_free(mem_ctx);
+       return true;
+}