From 8e4075e6b1911679ae3da837b5f13fb85c8f01f5 Mon Sep 17 00:00:00 2001 From: Timo Teras Date: Thu, 30 Jul 2009 09:07:35 +0300 Subject: io: fix mmap writing to actually work apparently it needs to have both PROT_READ and PROT_WRITE. and it needs to be MAP_SHARED for the writing to be effective. oh, and the data needs to be preallocated with ftruncate; otherwise, one gets SIGBUS. --- src/io.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/io.c') diff --git a/src/io.c b/src/io.c index c78cda8..0fa1f88 100644 --- a/src/io.c +++ b/src/io.c @@ -113,13 +113,15 @@ size_t apk_istream_splice(void *stream, int fd, size_t size, apk_progress_cb cb, void *cb_ctx) { struct apk_istream *is = (struct apk_istream *) stream; - unsigned char *buf; + unsigned char *buf = MAP_FAILED; size_t bufsz, done = 0, r, togo, mmapped = 0; bufsz = size; - if (size > 256 * 1024) { - buf = mmap(NULL, size, PROT_WRITE, 0, fd, 0); - if (buf != NULL) { + if (size > 128 * 1024) { + if (ftruncate(fd, size) == 0) + buf = mmap(NULL, size, PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); + if (buf != MAP_FAILED) { mmapped = 1; if (bufsz > 2*1024*1024) bufsz = 2*1024*1024; -- cgit v1.2.3