]> git.proxmox.com Git - libgit2.git/blob - src/unix/map.c
Cleanup legal data
[libgit2.git] / src / unix / map.c
1 /*
2 * Copyright (C) 2009-2011 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 #include <sys/mman.h>
13 #include <errno.h>
14
15 int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
16 {
17 int mprot = 0;
18 int mflag = 0;
19
20 assert((out != NULL) && (len > 0));
21
22 if ((out == NULL) || (len == 0)) {
23 errno = EINVAL;
24 return git__throw(GIT_ERROR, "Failed to mmap. No map or zero length");
25 }
26
27 out->data = NULL;
28 out->len = 0;
29
30 if (prot & GIT_PROT_WRITE)
31 mprot = PROT_WRITE;
32 else if (prot & GIT_PROT_READ)
33 mprot = PROT_READ;
34 else {
35 errno = EINVAL;
36 return git__throw(GIT_ERROR, "Failed to mmap. Invalid protection parameters");
37 }
38
39 if ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)
40 mflag = MAP_SHARED;
41 else if ((flags & GIT_MAP_TYPE) == GIT_MAP_PRIVATE)
42 mflag = MAP_PRIVATE;
43
44 if (flags & GIT_MAP_FIXED) {
45 errno = EINVAL;
46 return git__throw(GIT_ERROR, "Failed to mmap. FIXED not set");
47 }
48
49 out->data = mmap(NULL, len, mprot, mflag, fd, offset);
50 if (!out->data || out->data == MAP_FAILED)
51 return git__throw(GIT_EOSERR, "Failed to mmap. Could not write data");
52 out->len = len;
53
54 return GIT_SUCCESS;
55 }
56
57 int p_munmap(git_map *map)
58 {
59 assert(map != NULL);
60
61 if (!map)
62 return git__throw(GIT_ERROR, "Failed to munmap. Map does not exist");
63
64 munmap(map->data, map->len);
65
66 return GIT_SUCCESS;
67 }
68
69 #endif
70