summaryrefslogtreecommitdiff
path: root/compat/mmap.c
blob: fca6321ce01152c826bf977cd6b4d6b047ec57fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "../cache.h"
 
typedef struct fakemmapwritable {
	void *start;
	size_t length;
	int fd;
	off_t offset;
	struct fakemmapwritable *next;
} fakemmapwritable;
 
static fakemmapwritable *writablelist = NULL;
 
void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset)
{
	int n = 0;
 
	if(start != NULL)
		die("Invalid usage of gitfakemmap.");
 
	if(lseek(fd, offset, SEEK_SET)<0) {
		errno = EINVAL;
		return MAP_FAILED;
	}
 
	start = xmalloc(length);
	if(start == NULL) {
		errno = ENOMEM;
		return MAP_FAILED;
	}
 
	while(n < length) {
		int count = read(fd, start+n, length-n);
 
		if(count == 0) {
			memset(start+n, 0, length-n);
			break;
		}
 
		if(count < 0) {
			free(start);
			errno = EACCES;
			return MAP_FAILED;
		}
 
		n += count;
	}
 
	if(prot & PROT_WRITE) {
		fakemmapwritable *next = xmalloc(sizeof(fakemmapwritable));
		next->start = start;
		next->length = length;
		next->fd = dup(fd);
		next->offset = offset;
		next->next = writablelist;
		writablelist = next;
	}
 
	return start;
}
 
int gitfakemunmap(void *start, size_t length)
{
	fakemmapwritable *writable = writablelist, *before = NULL;
 
	while(writable && (writable->start > start + length
			|| writable->start + writable->length < start)) {
		before = writable;
		writable = writable->next;
	}
 
	if(writable) {
		/* need to write back the contents */
		int n = 0;
 
		if(writable->start != start || writable->length != length)
			die("fakemmap does not support partial write back.");
 
		if(lseek(writable->fd, writable->offset, SEEK_SET) < 0) {
			free(start);
			errno = EBADF;
			return -1;
		}
 
		while(n < length) {
			int count = write(writable->fd, start + n, length - n);
 
			if(count < 0) {
				errno = EINVAL;
				return -1;
			}
 
			n += count;
		}
 
		close(writable->fd);
 
		if(before)
			before->next = writable->next;
		else
			writablelist = writable->next;
 
		free(writable);
	}
 
	free(start);
 
	return 0;
}