2005-12-05 20:54:29 +01:00
|
|
|
#include "../git-compat-util.h"
|
2005-10-09 00:54:36 +02:00
|
|
|
|
2006-12-24 06:45:37 +01:00
|
|
|
void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
|
2005-10-09 00:54:36 +02:00
|
|
|
{
|
2006-12-24 06:45:47 +01:00
|
|
|
size_t n = 0;
|
2005-10-09 00:54:36 +02:00
|
|
|
|
2018-10-23 14:35:19 +02:00
|
|
|
if (start != NULL || flags != MAP_PRIVATE || prot != PROT_READ)
|
2006-12-24 06:45:37 +01:00
|
|
|
die("Invalid usage of mmap when built with NO_MMAP");
|
2005-10-09 00:54:36 +02:00
|
|
|
|
2021-08-21 14:52:40 +02:00
|
|
|
if (length == 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return MAP_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
start = malloc(length);
|
2005-10-09 00:54:36 +02:00
|
|
|
if (start == NULL) {
|
2005-10-09 00:54:36 +02:00
|
|
|
errno = ENOMEM;
|
|
|
|
return MAP_FAILED;
|
|
|
|
}
|
|
|
|
|
2005-10-09 00:54:36 +02:00
|
|
|
while (n < length) {
|
2014-04-10 20:54:12 +02:00
|
|
|
ssize_t count = xpread(fd, (char *)start + n, length - n, offset + n);
|
2005-10-09 00:54:36 +02:00
|
|
|
|
2005-10-09 00:54:36 +02:00
|
|
|
if (count == 0) {
|
2006-12-24 06:45:47 +01:00
|
|
|
memset((char *)start+n, 0, length-n);
|
2005-10-09 00:54:36 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-10-09 00:54:36 +02:00
|
|
|
if (count < 0) {
|
2005-10-09 00:54:36 +02:00
|
|
|
free(start);
|
|
|
|
errno = EACCES;
|
|
|
|
return MAP_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
n += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2006-12-24 06:45:37 +01:00
|
|
|
int git_munmap(void *start, size_t length)
|
2005-10-09 00:54:36 +02:00
|
|
|
{
|
|
|
|
free(start);
|
|
|
|
return 0;
|
|
|
|
}
|