Commit 7a6cf8153799563464d6f1761e55352c327b4122

Ramsay Jones 2009-03-16T17:08:45

win32: Open or create files in binary mode On windows, unless we use the O_BINARY flag in the open() call, the file I/O routines will perform line ending conversion (\r\n => \n on input, \n => \r\n on output). In addition to the performance penalty, most files in the object database are binary and will, therefore, become corrupted by this conversion. Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

diff --git a/src/fileops.c b/src/fileops.c
index c7f0591..e2ec615 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -3,13 +3,13 @@
 
 int gitfo_open(const char *path, int flags)
 {
-	int fd = open(path, flags);
+	int fd = open(path, flags | O_BINARY);
 	return fd >= 0 ? fd : git_os_error();
 }
 
 int gitfo_creat(const char *path, int mode)
 {
-	int fd = creat(path, mode);
+	int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
 	return fd >= 0 ? fd : git_os_error();
 }
 
diff --git a/src/fileops.h b/src/fileops.h
index 963dd0f..3cc1e16 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -15,6 +15,10 @@
 #include <time.h>
 #include <dirent.h>
 
+#if !defined(O_BINARY)
+#define O_BINARY 0
+#endif
+
 #define GITFO_BUF_INIT {NULL, 0}
 
 typedef int git_file;