diff options
Diffstat (limited to 'win32/fsync.c')
| -rw-r--r-- | win32/fsync.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/win32/fsync.c b/win32/fsync.c new file mode 100644 index 000000000..6ab44d434 --- /dev/null +++ b/win32/fsync.c | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | /* Emulate fsync on platforms that lack it, primarily Windows and | ||
| 2 | cross-compilers like MinGW. | ||
| 3 | |||
| 4 | This is derived from sqlite3 sources. | ||
| 5 | https://www.sqlite.org/src/finfo?name=src/os_win.c | ||
| 6 | https://www.sqlite.org/copyright.html | ||
| 7 | |||
| 8 | Written by Richard W.M. Jones <rjones.at.redhat.com> | ||
| 9 | |||
| 10 | Copyright (C) 2008-2018 Free Software Foundation, Inc. | ||
| 11 | |||
| 12 | This library is free software; you can redistribute it and/or | ||
| 13 | modify it under the terms of the GNU Lesser General Public | ||
| 14 | License as published by the Free Software Foundation; either | ||
| 15 | version 2.1 of the License, or (at your option) any later version. | ||
| 16 | |||
| 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 | Lesser General Public License for more details. | ||
| 21 | |||
| 22 | You should have received a copy of the GNU General Public License | ||
| 23 | along with this program. If not, see <https://www.gnu.org/licenses/>. */ | ||
| 24 | |||
| 25 | #include "libbb.h" | ||
| 26 | #include <unistd.h> | ||
| 27 | |||
| 28 | /* FlushFileBuffers */ | ||
| 29 | # define WIN32_LEAN_AND_MEAN | ||
| 30 | # include <windows.h> | ||
| 31 | |||
| 32 | # include <errno.h> | ||
| 33 | |||
| 34 | /* Get _get_osfhandle. */ | ||
| 35 | # include <io.h> | ||
| 36 | |||
| 37 | int | ||
| 38 | fsync (int fd) | ||
| 39 | { | ||
| 40 | HANDLE h = (HANDLE) _get_osfhandle (fd); | ||
| 41 | DWORD err; | ||
| 42 | |||
| 43 | if (h == INVALID_HANDLE_VALUE) | ||
| 44 | { | ||
| 45 | errno = EBADF; | ||
| 46 | return -1; | ||
| 47 | } | ||
| 48 | |||
| 49 | if (!FlushFileBuffers (h)) | ||
| 50 | { | ||
| 51 | /* Translate some Windows errors into rough approximations of Unix | ||
| 52 | * errors. MSDN is useless as usual - in this case it doesn't | ||
| 53 | * document the full range of errors. | ||
| 54 | */ | ||
| 55 | err = GetLastError (); | ||
| 56 | switch (err) | ||
| 57 | { | ||
| 58 | case ERROR_ACCESS_DENIED: | ||
| 59 | /* For a read-only handle, fsync should succeed, even though we have | ||
| 60 | no way to sync the access-time changes. */ | ||
| 61 | return 0; | ||
| 62 | |||
| 63 | /* eg. Trying to fsync a tty. */ | ||
| 64 | case ERROR_INVALID_HANDLE: | ||
| 65 | errno = EINVAL; | ||
| 66 | break; | ||
| 67 | |||
| 68 | default: | ||
| 69 | errno = EIO; | ||
| 70 | } | ||
| 71 | return -1; | ||
| 72 | } | ||
| 73 | |||
| 74 | return 0; | ||
| 75 | } | ||
