1 /* ---------------------------------------------------------------------------- 2 libconfig - A library for processing structured configuration files 3 Copyright (C) 2005-2018 Mark A Lindner 4 5 This file is part of libconfig. 6 7 This library is free software; you can redistribute it and/or 8 modify it under the terms of the GNU Lesser General Public License 9 as published by the Free Software Foundation; either version 2.1 of 10 the License, or (at your option) any later version. 11 12 This library is distributed in the hope that it will be useful, but 13 WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 Lesser General Public License for more details. 16 17 You should have received a copy of the GNU Library General Public 18 License along with this library; if not, see 19 <http://www.gnu.org/licenses/>. 20 ---------------------------------------------------------------------------- 21 */ 22 23 #include "wincompat.h" 24 25 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) \ 26 || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) 27 28 #include <errno.h> 29 #include <io.h> 30 fsync(int fd)31int fsync(int fd) 32 { 33 HANDLE h = (HANDLE)_get_osfhandle(fd); 34 if(h == INVALID_HANDLE_VALUE) 35 { 36 errno = EBADF; 37 return(-1); 38 } 39 40 if(!FlushFileBuffers(h)) 41 { 42 DWORD err = GetLastError(); 43 switch(err) 44 { 45 case ERROR_ACCESS_DENIED: 46 return(0); 47 48 case ERROR_INVALID_HANDLE: 49 errno = EINVAL; 50 break; 51 52 default: 53 errno = EIO; 54 } 55 return(-1); 56 } 57 58 return(0); 59 } 60 61 #endif // WIN32 || WIN64 62