Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lib / fsync.c
blobb09a8e64949c129608d70d8d31c69e67ba50b949
1 /* Emulate fsync on platforms that lack it, primarily Windows and
2 cross-compilers like MinGW.
4 This is derived from sqlite3 sources.
5 http://www.sqlite.org/cvstrac/rlog?f=sqlite/src/os_win.c
6 http://www.sqlite.org/copyright.html
8 Written by Richard W.M. Jones <rjones.at.redhat.com>
10 Copyright (C) 2008-2014 Free Software Foundation, Inc.
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include <config.h>
26 #include <unistd.h>
28 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
30 /* FlushFileBuffers */
31 # define WIN32_LEAN_AND_MEAN
32 # include <windows.h>
34 # include <errno.h>
36 /* Get _get_osfhandle. */
37 # include "msvc-nothrow.h"
39 int
40 fsync (int fd)
42 HANDLE h = (HANDLE) _get_osfhandle (fd);
43 DWORD err;
45 if (h == INVALID_HANDLE_VALUE)
47 errno = EBADF;
48 return -1;
51 if (!FlushFileBuffers (h))
53 /* Translate some Windows errors into rough approximations of Unix
54 * errors. MSDN is useless as usual - in this case it doesn't
55 * document the full range of errors.
57 err = GetLastError ();
58 switch (err)
60 case ERROR_ACCESS_DENIED:
61 /* For a read-only handle, fsync should succeed, even though we have
62 no way to sync the access-time changes. */
63 return 0;
65 /* eg. Trying to fsync a tty. */
66 case ERROR_INVALID_HANDLE:
67 errno = EINVAL;
68 break;
70 default:
71 errno = EIO;
73 return -1;
76 return 0;
79 #else /* !Windows */
81 # error "This platform lacks fsync function, and Gnulib doesn't provide a replacement. This is a bug in Gnulib."
83 #endif /* !Windows */