diff --git a/cmdline/mingw.c b/cmdline/mingw.c index b45411bd..41f61172 100644 --- a/cmdline/mingw.c +++ b/cmdline/mingw.c @@ -567,7 +567,10 @@ static void windows_errno(DWORD error) { switch (error) { case ERROR_INVALID_HANDLE : - errno = EBADF; + /* we check for a bad handle calling _get_osfhandle() */ + /* and in such case we return EBADF */ + /* Other cases are here identified with EINVAL */ + errno = EINVAL; break; case ERROR_FILE_NOT_FOUND : case ERROR_PATH_NOT_FOUND : /* in GetFileAttributeW() if internal path not found */ @@ -791,6 +794,29 @@ int windows_ftruncate(int fd, off64_t off) return 0; } +int windows_fsync(int fd) +{ + HANDLE h; + + if (fd == -1) { + errno = EBADF; + return -1; + } + + h = (HANDLE)_get_osfhandle(fd); + if (h == INVALID_HANDLE_VALUE) { + errno = EBADF; + return -1; + } + + if (!FlushFileBuffers(h)) { + windows_errno(GetLastError()); + return -1; + } + + return 0; +} + int windows_futimens(int fd, struct windows_timespec tv[2]) { HANDLE h; diff --git a/cmdline/mingw.h b/cmdline/mingw.h index 351e4327..0b2a5dc2 100644 --- a/cmdline/mingw.h +++ b/cmdline/mingw.h @@ -54,7 +54,7 @@ #define ftruncate windows_ftruncate #define HAVE_FSYNC 1 #undef fsync -#define fsync _commit +#define fsync windows_fsync #undef rename #define rename windows_rename #undef remove @@ -191,6 +191,11 @@ int lstat_ex(const char* file, struct windows_stat* st); */ int windows_ftruncate(int fd, off64_t off); +/** + * Like the C fsync(). + */ +int windows_fsync(int fd); + /** * Like the C futimes(). */