# Define NEEDS_SOCKET if linking with libc is not enough (SunOS,
# Patrick Mauritz).
#
+# Define NO_MMAP if you want to avoid mmap.
+#
# Define WITH_OWN_SUBPROCESS_PY if you want to use with python 2.3.
#
# Define NO_IPV6 if you lack IPv6 support and getaddrinfo().
DEFINES += -Dstrcasestr=gitstrcasestr
LIB_OBJS += compat/strcasestr.o
endif
+ifdef NO_MMAP
+ DEFINES += -Dmmap=gitfakemmap -Dmunmap=gitfakemunmap -DNO_MMAP
+ LIB_OBJS += compat/mmap.o
+endif
ifdef NO_IPV6
DEFINES += -DNO_IPV6 -Dsockaddr_storage=sockaddr_in
endif
#include <string.h>
#include <errno.h>
#include <limits.h>
+#ifndef NO_MMAP
#include <sys/mman.h>
+#endif
#include <sys/param.h>
#include <netinet/in.h>
#include <sys/types.h>
/* Dumb servers support */
extern int update_server_info(int);
+#ifdef NO_MMAP
+
+#ifndef PROT_READ
+#define PROT_READ 1
+#define PROT_WRITE 2
+#define MAP_PRIVATE 1
+#define MAP_FAILED ((void*)-1)
+#endif
+
+extern void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);
+extern int gitfakemunmap(void *start, size_t length);
+
+#endif
+
#endif /* CACHE_H */
--- /dev/null
+#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;
+}
+