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