44#include <errno.h>
55#include "../cache.h"
66
7- typedef struct fakemmapwritable {
8- void * start ;
9- size_t length ;
10- int fd ;
11- off_t offset ;
12- struct fakemmapwritable * next ;
13- } fakemmapwritable ;
14-
15- static fakemmapwritable * writablelist = NULL ;
16-
177void * gitfakemmap (void * start , size_t length , int prot , int flags , int fd , off_t offset )
188{
199 int n = 0 ;
2010
21- if (start != NULL )
11+ if (start != NULL || !( flags & MAP_PRIVATE ) )
2212 die ("Invalid usage of gitfakemmap." );
2313
24- if (lseek (fd , offset , SEEK_SET )< 0 ) {
14+ if (lseek (fd , offset , SEEK_SET ) < 0 ) {
2515 errno = EINVAL ;
2616 return MAP_FAILED ;
2717 }
2818
2919 start = xmalloc (length );
30- if (start == NULL ) {
20+ if (start == NULL ) {
3121 errno = ENOMEM ;
3222 return MAP_FAILED ;
3323 }
3424
35- while (n < length ) {
25+ while (n < length ) {
3626 int count = read (fd , start + n , length - n );
3727
38- if (count == 0 ) {
28+ if (count == 0 ) {
3929 memset (start + n , 0 , length - n );
4030 break ;
4131 }
4232
43- if (count < 0 ) {
33+ if (count < 0 ) {
4434 free (start );
4535 errno = EACCES ;
4636 return MAP_FAILED ;
@@ -49,65 +39,12 @@ void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_
4939 n += count ;
5040 }
5141
52- if (prot & PROT_WRITE ) {
53- fakemmapwritable * next = xmalloc (sizeof (fakemmapwritable ));
54- next -> start = start ;
55- next -> length = length ;
56- next -> fd = dup (fd );
57- next -> offset = offset ;
58- next -> next = writablelist ;
59- writablelist = next ;
60- }
61-
6242 return start ;
6343}
6444
6545int gitfakemunmap (void * start , size_t length )
6646{
67- fakemmapwritable * writable = writablelist , * before = NULL ;
68-
69- while (writable && (writable -> start > start + length
70- || writable -> start + writable -> length < start )) {
71- before = writable ;
72- writable = writable -> next ;
73- }
74-
75- if (writable ) {
76- /* need to write back the contents */
77- int n = 0 ;
78-
79- if (writable -> start != start || writable -> length != length )
80- die ("fakemmap does not support partial write back." );
81-
82- if (lseek (writable -> fd , writable -> offset , SEEK_SET ) < 0 ) {
83- free (start );
84- errno = EBADF ;
85- return -1 ;
86- }
87-
88- while (n < length ) {
89- int count = write (writable -> fd , start + n , length - n );
90-
91- if (count < 0 ) {
92- errno = EINVAL ;
93- return -1 ;
94- }
95-
96- n += count ;
97- }
98-
99- close (writable -> fd );
100-
101- if (before )
102- before -> next = writable -> next ;
103- else
104- writablelist = writable -> next ;
105-
106- free (writable );
107- }
108-
10947 free (start );
110-
11148 return 0 ;
11249}
11350
0 commit comments