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