| 1 | #include "../git-compat-util.h" |
| 2 | |
| 3 | void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset) |
| 4 | { |
| 5 | size_t n = 0; |
| 6 | |
| 7 | if (start != NULL || flags != MAP_PRIVATE || prot != PROT_READ) |
| 8 | die("Invalid usage of mmap when built with NO_MMAP"); |
| 9 | |
| 10 | if (length == 0) { |
| 11 | errno = EINVAL; |
| 12 | return MAP_FAILED; |
| 13 | } |
| 14 | |
| 15 | start = malloc(length); |
| 16 | if (!start) { |
| 17 | errno = ENOMEM; |
| 18 | return MAP_FAILED; |
| 19 | } |
| 20 | |
| 21 | while (n < length) { |
| 22 | ssize_t count = xpread(fd, (char *)start + n, length - n, offset + n); |
| 23 | |
| 24 | if (count == 0) { |
| 25 | memset((char *)start+n, 0, length-n); |
| 26 | break; |
| 27 | } |
| 28 | |
| 29 | if (count < 0) { |
| 30 | free(start); |
| 31 | errno = EACCES; |
| 32 | return MAP_FAILED; |
| 33 | } |
| 34 | |
| 35 | n += count; |
| 36 | } |
| 37 | |
| 38 | return start; |
| 39 | } |
| 40 | |
| 41 | int git_munmap(void *start, size_t length UNUSED) |
| 42 | { |
| 43 | free(start); |
| 44 | return 0; |
| 45 | } |