Merge branch 'js/compat-mkdir'
[git] / compat / mmap.c
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))
8                 die("Invalid usage of mmap when built with NO_MMAP");
9
10         start = xmalloc(length);
11         if (start == NULL) {
12                 errno = ENOMEM;
13                 return MAP_FAILED;
14         }
15
16         while (n < length) {
17                 ssize_t count = pread(fd, (char *)start + n, length - n, offset + n);
18
19                 if (count == 0) {
20                         memset((char *)start+n, 0, length-n);
21                         break;
22                 }
23
24                 if (count < 0) {
25                         if (errno == EAGAIN || errno == EINTR)
26                                 continue;
27                         free(start);
28                         errno = EACCES;
29                         return MAP_FAILED;
30                 }
31
32                 n += count;
33         }
34
35         return start;
36 }
37
38 int git_munmap(void *start, size_t length)
39 {
40         free(start);
41         return 0;
42 }