Seek back to current filepos when mmap()ing with NO_MMAP
[alt-git.git] / compat / mmap.c
bloba4d2e507f73c5595a2ca76c0369349cc11e2426f
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)
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;
20 start = xmalloc(length);
21 if (start == NULL) {
22 errno = ENOMEM;
23 return MAP_FAILED;
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;
34 if (count < 0) {
35 free(start);
36 errno = EACCES;
37 return MAP_FAILED;
40 n += count;
43 if (current_offset != lseek(fd, current_offset, SEEK_SET)) {
44 errno = EINVAL;
45 return MAP_FAILED;
48 return start;
51 int gitfakemunmap(void *start, size_t length)
53 free(start);
54 return 0;