builtin-mailinfo.c: check error status from rewind and ftruncate
[git/spearce.git] / compat / win32mmap.c
blob779d796cd5f017d4effbede360185a4690cf414e
1 #include "../git-compat-util.h"
3 /*
4 * Note that this doesn't return the actual pagesize, but
5 * the allocation granularity. If future Windows specific git code
6 * needs the real getpagesize function, we need to find another solution.
7 */
8 int mingw_getpagesize(void)
10 SYSTEM_INFO si;
11 GetSystemInfo(&si);
12 return si.dwAllocationGranularity;
15 void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
17 HANDLE hmap;
18 void *temp;
19 size_t len;
20 struct stat st;
21 uint64_t o = offset;
22 uint32_t l = o & 0xFFFFFFFF;
23 uint32_t h = (o >> 32) & 0xFFFFFFFF;
25 if (!fstat(fd, &st))
26 len = xsize_t(st.st_size);
27 else
28 die("mmap: could not determine filesize");
30 if ((length + offset) > len)
31 length = len - offset;
33 if (!(flags & MAP_PRIVATE))
34 die("Invalid usage of mmap when built with USE_WIN32_MMAP");
36 hmap = CreateFileMapping((HANDLE)_get_osfhandle(fd), 0, PAGE_WRITECOPY,
37 0, 0, 0);
39 if (!hmap)
40 return MAP_FAILED;
42 temp = MapViewOfFileEx(hmap, FILE_MAP_COPY, h, l, length, start);
44 if (!CloseHandle(hmap))
45 warning("unable to close file mapping handle\n");
47 return temp ? temp : MAP_FAILED;
50 int git_munmap(void *start, size_t length)
52 return !UnmapViewOfFile(start);