]> git.proxmox.com Git - libgit2.git/blob - src/unix/map.c
Actually do the mmap... unsurprisingly, this makes the indexer work on SFS
[libgit2.git] / src / unix / map.c
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #include <git2/common.h>
8
9 #ifndef GIT_WIN32
10
11 #include "map.h"
12 #ifndef __amigaos4__
13 #include <sys/mman.h>
14 #endif
15 #include <errno.h>
16
17 int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
18 {
19 int mprot = 0;
20 int mflag = 0;
21
22 GIT_MMAP_VALIDATE(out, len, prot, flags);
23
24 out->data = NULL;
25 out->len = 0;
26
27 #ifndef __amigaos4__
28 if (prot & GIT_PROT_WRITE)
29 mprot = PROT_WRITE;
30 else if (prot & GIT_PROT_READ)
31 mprot = PROT_READ;
32
33 if ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)
34 mflag = MAP_SHARED;
35 else if ((flags & GIT_MAP_TYPE) == GIT_MAP_PRIVATE)
36 mflag = MAP_PRIVATE;
37
38 out->data = mmap(NULL, len, mprot, mflag, fd, offset);
39 #else
40 if ((prot & GIT_PROT_WRITE) && ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)) {
41 printf("Trying to map shared-writeable file!!!\n");
42 }
43
44 if(out->data = malloc(len)) {
45 lseek(fd, offset, SEEK_SET);
46 p_read(fd, out->data, len);
47 }
48 #endif
49
50 if (!out->data || out->data == MAP_FAILED) {
51 giterr_set(GITERR_OS, "Failed to mmap. Could not write data");
52 return -1;
53 }
54
55 out->len = len;
56
57 return 0;
58 }
59
60 int p_munmap(git_map *map)
61 {
62 assert(map != NULL);
63 #ifndef __amigaos4__
64 munmap(map->data, map->len);
65 #else
66 free(map->data);
67 #endif
68 return 0;
69 }
70
71 #endif
72