Commit c79dded300f279a6d8660b13f4fca3c684533449

Ramsay Jones 2009-06-14T22:13:35

win32: Add an fsync() implementation for windows For information on FlushFileBuffers(), see the msdn document at msdn.microsoft.com/en-us/library/aa364439(VS.85).aspx Note that Windows 2000 is shown as the minimum windows version to support FlushFileBuffers(), so if we wish to support Win9X and NT4, we will need to add code to dynamically check if kernel32.dll contains the function. The only error return mentioned in the msdn document is ERROR_INVALID_HANDLE, which is returned if the file/device (eg console) is not buffered. The fsync(2) manpage says that EINVAL is returned in errno, if "fd is bound to a special file which does not support synchronization". Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>

diff --git a/src/fileops.h b/src/fileops.h
index 1753969..749a91d 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -23,11 +23,6 @@ static inline int link(const char *old, const char *new)
 	return -1;
 }
 
-static inline int fsync(int fd)
-{
-	return 0;
-}
-
 static inline int git__mkdir(const char *path, int mode)
 {
 	return mkdir(path);
@@ -35,11 +30,13 @@ static inline int git__mkdir(const char *path, int mode)
 
 extern int git__unlink(const char *path);
 extern int git__mkstemp(char *template);
+extern int git__fsync(int fd);
 
 # ifndef GIT__WIN32_NO_HIDE_FILEOPS
 #  define unlink(p) git__unlink(p)
 #  define mkstemp(t) git__mkstemp(t)
 #  define mkdir(p,m) git__mkdir(p,m)
+#  define fsync(fd) git__fsync(fd)
 # endif
 #endif  /* GIT_WIN32 */
 
diff --git a/src/win32/fileops.c b/src/win32/fileops.c
index c6039d8..d435e70 100644
--- a/src/win32/fileops.c
+++ b/src/win32/fileops.c
@@ -1,5 +1,6 @@
 #define GIT__WIN32_NO_HIDE_FILEOPS
 #include "fileops.h"
+#include <errno.h>
 
 int git__unlink(const char *path)
 {
@@ -15,3 +16,26 @@ int git__mkstemp(char *template)
 	return open(file, O_RDWR | O_CREAT | O_BINARY, 0600);
 }
 
+int git__fsync(int fd)
+{
+	HANDLE fh = (HANDLE)_get_osfhandle(fd);
+
+	if (fh == INVALID_HANDLE_VALUE) {
+		errno = EBADF;
+		return -1;
+	}
+
+	if (!FlushFileBuffers(fh)) {
+		DWORD code = GetLastError();
+
+		if (code == ERROR_INVALID_HANDLE)
+			errno = EINVAL;
+		else
+			errno = EIO;
+
+		return -1;
+	}
+
+	return 0;
+}
+