xbps_file_hash: use madvise(2).

This commit is contained in:
Juan RP 2012-12-15 09:51:05 +01:00
parent 4095290189
commit e1fc085915

View File

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