mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 5/6] libfile: add diff_file function
Date: Wed,  8 Oct 2014 16:24:14 +0200	[thread overview]
Message-ID: <1412778255-4153-5-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1412778255-4153-1-git-send-email-s.hauer@pengutronix.de>

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/libfile.h |  2 ++
 lib/libfile.c     | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)

diff --git a/include/libfile.h b/include/libfile.h
index 4a25a91..315adb2 100644
--- a/include/libfile.h
+++ b/include/libfile.h
@@ -15,4 +15,6 @@ int write_file(const char *filename, void *buf, size_t size);
 
 int copy_file(const char *src, const char *dst, int verbose);
 
+int diff_file(const char *f1, const char *f2);
+
 #endif /* __LIBFILE_H */
diff --git a/lib/libfile.c b/lib/libfile.c
index c626e2f..1d773e3 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -331,3 +331,74 @@ out:
 	return ret;
 }
 EXPORT_SYMBOL(copy_file);
+
+/**
+ * diff_file - Compare two files
+ * @f1:		The first file
+ * @f2:		The second file
+ *
+ * Return: 0 if both files are identical, 1 if they differ,
+ *         a negative error code if some error occured
+ */
+int diff_file(const char *f1, const char *f2)
+{
+	int fd1, fd2, ret;
+	struct stat s1, s2;
+	void *buf1, *buf2;
+	loff_t left;
+
+	fd1 = open(f1, O_RDONLY);
+	if (fd1 < 0)
+		return -errno;
+
+	fd2 = open(f2, O_RDONLY);
+	if (fd2 < 0) {
+		ret = -errno;
+		goto err_out1;
+	}
+
+	ret = fstat(fd1, &s1);
+	if (ret)
+		goto err_out2;
+
+	ret = fstat(fd2, &s2);
+	if (ret)
+		goto err_out2;
+
+	if (s1.st_size != s2.st_size)
+		return 1;
+
+	buf1 = xmalloc(RW_BUF_SIZE);
+	buf2 = xmalloc(RW_BUF_SIZE);
+
+	left = s1.st_size;
+	while (left) {
+		loff_t now = min(left, (loff_t)RW_BUF_SIZE);
+
+		ret = read_full(fd1, buf1, now);
+		if (ret < 0)
+			goto err_out3;
+
+		ret = read_full(fd2, buf2, now);
+		if (ret < 0)
+			goto err_out3;
+
+		if (memcmp(buf1, buf2, now)) {
+			ret = 1;
+			goto err_out3;
+		}
+
+		left -= now;
+	}
+
+	ret = 0;
+
+err_out3:
+	free(buf1);
+	free(buf2);
+err_out2:
+	close(fd2);
+err_out1:
+	close(fd1);
+	return ret;
+}
-- 
2.1.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2014-10-08 14:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-08 14:24 [PATCH 1/6] fs: store pointer to fsdev instead of dev in struct filep Sascha Hauer
2014-10-08 14:24 ` [PATCH 2/6] fs: do not lookup global FILE * when the file is already available Sascha Hauer
2014-10-08 14:24 ` [PATCH 3/6] fs: Store the path in struct filep Sascha Hauer
2014-10-08 14:24 ` [PATCH 4/6] fs: implement fstat Sascha Hauer
2014-10-08 14:24 ` Sascha Hauer [this message]
2014-10-08 14:24 ` [PATCH 6/6] commands: implement 'diff' command Sascha Hauer
2014-10-08 15:32   ` Antony Pavlov
2014-10-09  6:50     ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1412778255-4153-5-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox