Yank writing-back support from gitfakemmap.
We do not write through our use of mmap(), so make sure callers pass MAP_PRIVATE and remove support for writing changes back. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Oct 8, 2005 at 15:54 UTC
f48000fcbe1009c18f1cc46e56cde2cb632071fa
1 file changed
+6
-69
compat/mmap.c
+6
-69
@@ -4,43 +4,33 @@
4
#include <errno.h>
5
#include "../cache.h"
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
-
7
void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset)
8
{
9
int n = 0;
10
21
- if(start != NULL)
11
+ if (start != NULL || !(flags & MAP_PRIVATE))
12
die("Invalid usage of gitfakemmap.");
13
24
- if(lseek(fd, offset, SEEK_SET)<0) {
14
+ if (lseek(fd, offset, SEEK_SET) < 0) {
15
errno = EINVAL;
16
return MAP_FAILED;
17
}
18
19
start = xmalloc(length);
30
- if(start == NULL) {
20
+ if (start == NULL) {
21
errno = ENOMEM;
22
return MAP_FAILED;
23
}
24
35
- while(n < length) {
25
+ while (n < length) {
26
int count = read(fd, start+n, length-n);
27
38
- if(count == 0) {
28
+ if (count == 0) {
29
memset(start+n, 0, length-n);
30
break;
31
}
32
43
- if(count < 0) {
33
+ if (count < 0) {
34
free(start);
35
errno = EACCES;
36
return MAP_FAILED;
@@ -49,65 +39,12 @@ void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_
39
n += count;
40
}
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
-
42
return start;
43
}
44
45
int gitfakemunmap(void *start, size_t length)
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
-
47
free(start);
110
-
48
return 0;
49
}
50