Merge branch 'ew/svn-pm'
[git/git-svn.git] / compat / mmap.c
blob0fd46e793d088e6567b20c7e81a5cde7023bdb58
1 #include "../git-compat-util.h"
3 void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset)
5 int n = 0;
6 off_t current_offset = lseek(fd, 0, SEEK_CUR);
8 if (start != NULL || !(flags & MAP_PRIVATE))
9 die("Invalid usage of gitfakemmap.");
11 if (lseek(fd, offset, SEEK_SET) < 0) {
12 errno = EINVAL;
13 return MAP_FAILED;
16 start = xmalloc(length);
17 if (start == NULL) {
18 errno = ENOMEM;
19 return MAP_FAILED;
22 while (n < length) {
23 int count = read(fd, start+n, length-n);
25 if (count == 0) {
26 memset(start+n, 0, length-n);
27 break;
30 if (count < 0) {
31 free(start);
32 errno = EACCES;
33 return MAP_FAILED;
36 n += count;
39 if (current_offset != lseek(fd, current_offset, SEEK_SET)) {
40 errno = EINVAL;
41 return MAP_FAILED;
44 return start;
47 int gitfakemunmap(void *start, size_t length)
49 free(start);
50 return 0;