compat / mmap.con commit git-branch -D: make it work even when on a yet-to-be-born branch (67affd5)
   1#include <stdio.h>
   2#include <stdlib.h>
   3#include <unistd.h>
   4#include <errno.h>
   5#include "../git-compat-util.h"
   6
   7void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset)
   8{
   9        int n = 0;
  10        off_t current_offset = lseek(fd, 0, SEEK_CUR);
  11
  12        if (start != NULL || !(flags & MAP_PRIVATE))
  13                die("Invalid usage of gitfakemmap.");
  14
  15        if (lseek(fd, offset, SEEK_SET) < 0) {
  16                errno = EINVAL;
  17                return MAP_FAILED;
  18        }
  19
  20        start = xmalloc(length);
  21        if (start == NULL) {
  22                errno = ENOMEM;
  23                return MAP_FAILED;
  24        }
  25
  26        while (n < length) {
  27                int count = read(fd, start+n, length-n);
  28
  29                if (count == 0) {
  30                        memset(start+n, 0, length-n);
  31                        break;
  32                }
  33
  34                if (count < 0) {
  35                        free(start);
  36                        errno = EACCES;
  37                        return MAP_FAILED;
  38                }
  39
  40                n += count;
  41        }
  42
  43        if (current_offset != lseek(fd, current_offset, SEEK_SET)) {
  44                errno = EINVAL;
  45                return MAP_FAILED;
  46        }
  47
  48        return start;
  49}
  50
  51int gitfakemunmap(void *start, size_t length)
  52{
  53        free(start);
  54        return 0;
  55}
  56