4
4
#include <errno.h>
5
5
#include "../cache.h"
6
6
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
-
17
7
void * gitfakemmap (void * start , size_t length , int prot , int flags , int fd , off_t offset )
18
8
{
19
9
int n = 0 ;
20
10
21
- if (start != NULL )
11
+ if (start != NULL || !( flags & MAP_PRIVATE ) )
22
12
die ("Invalid usage of gitfakemmap." );
23
13
24
- if (lseek (fd , offset , SEEK_SET )< 0 ) {
14
+ if (lseek (fd , offset , SEEK_SET ) < 0 ) {
25
15
errno = EINVAL ;
26
16
return MAP_FAILED ;
27
17
}
28
18
29
19
start = xmalloc (length );
30
- if (start == NULL ) {
20
+ if (start == NULL ) {
31
21
errno = ENOMEM ;
32
22
return MAP_FAILED ;
33
23
}
34
24
35
- while (n < length ) {
25
+ while (n < length ) {
36
26
int count = read (fd , start + n , length - n );
37
27
38
- if (count == 0 ) {
28
+ if (count == 0 ) {
39
29
memset (start + n , 0 , length - n );
40
30
break ;
41
31
}
42
32
43
- if (count < 0 ) {
33
+ if (count < 0 ) {
44
34
free (start );
45
35
errno = EACCES ;
46
36
return MAP_FAILED ;
@@ -49,65 +39,12 @@ void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_
49
39
n += count ;
50
40
}
51
41
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
-
62
42
return start ;
63
43
}
64
44
65
45
int gitfakemunmap (void * start , size_t length )
66
46
{
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
-
109
47
free (start );
110
-
111
48
return 0 ;
112
49
}
113
50
0 commit comments