Code

a4d2e507f73c5595a2ca76c0369349cc11e2426f
[git.git] / compat / mmap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include "../git-compat-util.h"
7 void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset)
8 {
9         int n = 0;
10         off_t current_offset = lseek(fd, 0, SEEK_CUR);
12         if (start != NULL || !(flags & MAP_PRIVATE))
13                 die("Invalid usage of gitfakemmap.");
15         if (lseek(fd, offset, SEEK_SET) < 0) {
16                 errno = EINVAL;
17                 return MAP_FAILED;
18         }
20         start = xmalloc(length);
21         if (start == NULL) {
22                 errno = ENOMEM;
23                 return MAP_FAILED;
24         }
26         while (n < length) {
27                 int count = read(fd, start+n, length-n);
29                 if (count == 0) {
30                         memset(start+n, 0, length-n);
31                         break;
32                 }
34                 if (count < 0) {
35                         free(start);
36                         errno = EACCES;
37                         return MAP_FAILED;
38                 }
40                 n += count;
41         }
43         if (current_offset != lseek(fd, current_offset, SEEK_SET)) {
44                 errno = EINVAL;
45                 return MAP_FAILED;
46         }
48         return start;
49 }
51 int gitfakemunmap(void *start, size_t length)
52 {
53         free(start);
54         return 0;
55 }