From e1fc085915298edaf9d3ec97bbd87c7ff371a196 Mon Sep 17 00:00:00 2001
From: Juan RP <xtraeme@gmail.com>
Date: Sat, 15 Dec 2012 09:51:05 +0100
Subject: [PATCH] xbps_file_hash: use madvise(2).

---
 lib/util_hash.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lib/util_hash.c b/lib/util_hash.c
index b07d157a..f9938299 100644
--- a/lib/util_hash.c
+++ b/lib/util_hash.c
@@ -23,7 +23,9 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#define _BSD_SOURCE /* for madvise(2) */
 #include <sys/mman.h>
+#undef _BSD_SOURCE
 
 #include <stdio.h>
 #include <stdbool.h>
@@ -72,7 +74,7 @@ xbps_file_hash(const char *file)
 
 	assert(file != NULL);
 
-	if ((fd = open(file, O_RDONLY)) == -1) {
+	if ((fd = open(file, O_RDONLY|O_CLOEXEC)) == -1) {
 		free(buf);
 		return NULL;
 	}
@@ -105,11 +107,14 @@ xbps_file_hash(const char *file)
 	if (buf == MAP_FAILED)
 		return NULL;
 
+	(void)madvise(buf, mapsize, MADV_SEQUENTIAL);
 	if (SHA256(buf, st.st_size, digest) == NULL) {
 		munmap(buf, mapsize);
 		return NULL;
 	}
+	(void)madvise(buf, mapsize, MADV_DONTNEED);
 	munmap(buf, mapsize);
+
 	digest2string(digest, hash, SHA256_DIGEST_LENGTH);
 
 	return strdup(hash);