indexer: mmap fixes for Windows Windows has its own ftruncate() called _chsize_s(). p_mkstemp() is changed to use p_open() so we can make sure we open for writing; the addition of exclusive create is a good thing to do regardless, as we want a temporary path for ourselves. Lastly, MSVC doesn't quite know how to add two numbers if one of them is a void pointer, so let's alias it to unsigned char.C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
diff --git a/src/indexer.c b/src/indexer.c
index 11268e0..ebfdffb 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -429,6 +429,7 @@ static int write_at(git_indexer *idx, const void *data, git_off_t offset, size_t
git_file fd = idx->pack->mwf.fd;
long page_size = git__page_size();
git_off_t page_start, page_offset;
+ unsigned char *map_data;
git_map map;
int error;
@@ -439,7 +440,8 @@ static int write_at(git_indexer *idx, const void *data, git_off_t offset, size_t
if ((error = p_mmap(&map, page_offset + size, GIT_PROT_WRITE, GIT_MAP_SHARED, fd, page_start)) < 0)
return error;
- memcpy(map.data + page_offset, data, size);
+ map_data = (unsigned char *)map.data;
+ memcpy(map_data + page_offset, data, size);
p_munmap(&map);
return 0;
diff --git a/src/posix.h b/src/posix.h
index 745e4af..965cd98 100644
--- a/src/posix.h
+++ b/src/posix.h
@@ -60,7 +60,6 @@ extern int p_write(git_file fd, const void *buf, size_t cnt);
#define p_lseek(f,n,w) lseek(f, n, w)
#define p_close(fd) close(fd)
#define p_umask(m) umask(m)
-#define p_ftruncate(fd, sz) ftruncate(fd, sz)
extern int p_open(const char *path, int flags, ...);
extern int p_creat(const char *path, mode_t mode);
@@ -74,6 +73,7 @@ extern int p_rename(const char *from, const char *to);
#define p_rmdir(p) rmdir(p)
#define p_chmod(p,m) chmod(p, m)
#define p_access(p,m) access(p,m)
+#define p_ftruncate(fd, sz) ftruncate(fd, sz)
#define p_recv(s,b,l,f) recv(s,b,l,f)
#define p_send(s,b,l,f) send(s,b,l,f)
typedef int GIT_SOCKET;
diff --git a/src/win32/posix.h b/src/win32/posix.h
index 7f9d57c..2cbea18 100644
--- a/src/win32/posix.h
+++ b/src/win32/posix.h
@@ -19,6 +19,12 @@
# define EAFNOSUPPORT (INT_MAX-1)
#endif
+#ifdef _MSC_VER
+# define p_ftruncate(fd, sz) _chsize_s(fd, sz)
+#else /* MinGW */
+# define p_ftruncate(fd, sz) _chsize(fd, sz)
+#endif
+
GIT_INLINE(int) p_link(const char *old, const char *new)
{
GIT_UNUSED(old);
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index 73bf925..3493843 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -578,7 +578,7 @@ int p_mkstemp(char *tmp_path)
return -1;
#endif
- return p_creat(tmp_path, 0744); //-V536
+ return p_open(tmp_path, O_RDWR | O_CREAT | O_EXCL, 0744); //-V536
}
int p_access(const char* path, mode_t mode)