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