summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2010-02-11 20:19:40 +1100
committerAndrew Tridgell <tridge@samba.org>2010-02-11 21:04:13 +1100
commitae95d611b6e0754f130707f23fa6ae0997c40b66 (patch)
treedc15a920a3e0a97e3dd13f1e0e8c5c99926d90a8
parentd6fb64c51244529388b1f79ba8220ff608e1e4de (diff)
downloadsamba-ae95d611b6e0754f130707f23fa6ae0997c40b66.tar.gz
samba-ae95d611b6e0754f130707f23fa6ae0997c40b66.tar.bz2
samba-ae95d611b6e0754f130707f23fa6ae0997c40b66.zip
util: added file_compare() utility function
file_compare() returns true if two files are the same. It is meant for small files. Pair-Programmed-With: Andrew Bartlett <abartlet@samba.org>
-rw-r--r--lib/util/util.h5
-rw-r--r--lib/util/util_file.c22
2 files changed, 27 insertions, 0 deletions
diff --git a/lib/util/util.h b/lib/util/util.h
index 729190af63..da9776163a 100644
--- a/lib/util/util.h
+++ b/lib/util/util.h
@@ -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 */
diff --git a/lib/util/util_file.c b/lib/util/util_file.c
index 7466004e5c..aa0b2d5855 100644
--- a/lib/util/util_file.c
+++ b/lib/util/util_file.c
@@ -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;
+}