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>
This commit is contained in:
Ramsay Jones 2009-06-14 22:13:35 +01:00 committed by Andreas Ericsson
parent 8a086f872a
commit c79dded300
2 changed files with 26 additions and 5 deletions

View File

@ -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 */

View File

@ -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;
}