Rename gitfakemmap to git_mmap.
[git/git.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 int n = 0;
6 off_t current_offset = lseek(fd, 0, SEEK_CUR);
7
8 if (start != NULL || !(flags & MAP_PRIVATE))
9 die("Invalid usage of mmap when built with NO_MMAP");
10
11 if (lseek(fd, offset, SEEK_SET) < 0) {
12 errno = EINVAL;
13 return MAP_FAILED;
14 }
15
16 start = xmalloc(length);
17 if (start == NULL) {
18 errno = ENOMEM;
19 return MAP_FAILED;
20 }
21
22 while (n < length) {
23 int count = read(fd, start+n, length-n);
24
25 if (count == 0) {
26 memset(start+n, 0, length-n);
27 break;
28 }
29
30 if (count < 0) {
31 free(start);
32 errno = EACCES;
33 return MAP_FAILED;
34 }
35
36 n += count;
37 }
38
39 if (current_offset != lseek(fd, current_offset, SEEK_SET)) {
40 errno = EINVAL;
41 return MAP_FAILED;
42 }
43
44 return start;
45 }
46
47 int git_munmap(void *start, size_t length)
48 {
49 free(start);
50 return 0;
51 }
52