]>
Commit | Line | Data |
---|---|---|
4050c0df | 1 | #include "../git-compat-util.h" |
730d48a2 | 2 | |
d6779124 | 3 | void *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 | |
95b4ff39 RS |
10 | if (length == 0) { |
11 | errno = EINVAL; | |
12 | return MAP_FAILED; | |
13 | } | |
14 | ||
15 | start = malloc(length); | |
afe8a907 | 16 | if (!start) { |
730d48a2 JS |
17 | errno = ENOMEM; |
18 | return MAP_FAILED; | |
19 | } | |
20 | ||
f48000fc | 21 | while (n < length) { |
9aa91af0 | 22 | ssize_t count = xpread(fd, (char *)start + n, length - n, offset + n); |
730d48a2 | 23 | |
f48000fc | 24 | if (count == 0) { |
8e554429 | 25 | memset((char *)start+n, 0, length-n); |
730d48a2 JS |
26 | break; |
27 | } | |
28 | ||
f48000fc | 29 | if (count < 0) { |
730d48a2 JS |
30 | free(start); |
31 | errno = EACCES; | |
32 | return MAP_FAILED; | |
33 | } | |
34 | ||
35 | n += count; | |
36 | } | |
37 | ||
730d48a2 JS |
38 | return start; |
39 | } | |
40 | ||
d6779124 | 41 | int git_munmap(void *start, size_t length) |
730d48a2 | 42 | { |
730d48a2 | 43 | free(start); |
730d48a2 JS |
44 | return 0; |
45 | } |