Merge branch 'ds/midx-expire-repack'
[git/git.git] / compat / mmap.c
CommitLineData
4050c0df 1#include "../git-compat-util.h"
730d48a2 2
d6779124 3void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
730d48a2 4{
8e554429 5 size_t n = 0;
730d48a2 6
bf1e6da7 7 if (start != NULL || flags != MAP_PRIVATE || prot != PROT_READ)
d6779124 8 die("Invalid usage of mmap when built with NO_MMAP");
730d48a2 9
730d48a2 10 start = xmalloc(length);
f48000fc 11 if (start == NULL) {
730d48a2
JS
12 errno = ENOMEM;
13 return MAP_FAILED;
14 }
15
f48000fc 16 while (n < length) {
9aa91af0 17 ssize_t count = xpread(fd, (char *)start + n, length - n, offset + n);
730d48a2 18
f48000fc 19 if (count == 0) {
8e554429 20 memset((char *)start+n, 0, length-n);
730d48a2
JS
21 break;
22 }
23
f48000fc 24 if (count < 0) {
730d48a2
JS
25 free(start);
26 errno = EACCES;
27 return MAP_FAILED;
28 }
29
30 n += count;
31 }
32
730d48a2
JS
33 return start;
34}
35
d6779124 36int git_munmap(void *start, size_t length)
730d48a2 37{
730d48a2 38 free(start);
730d48a2
JS
39 return 0;
40}