Commit 0731a5b4db086eefac1a842e37526ef7bdbaa7de

Carlos Martín Nieto 2014-05-14T19:12:48

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

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)