From 5b5cb69bf3e16e4170d4206621682a12d441c87f Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 21 Feb 2018 09:58:22 +0000 Subject: win32: import fsync(2) implementation from gnulib --- include/mingw.h | 2 +- include/platform.h | 1 + win32/Kbuild | 1 + win32/fsync.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 win32/fsync.c diff --git a/include/mingw.h b/include/mingw.h index 6f69913d6..a59ce6fc4 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -390,7 +390,7 @@ NOIMPL(getsid,pid_t pid UNUSED_PARAM); IMPL(getuid,int,DEFAULT_UID,void); int getlogin_r(char *buf, size_t len); int fcntl(int fd, int cmd, ...); -IMPL(fsync,int,0,int fd UNUSED_PARAM); +int fsync(int fd); int kill(pid_t pid, int sig); int link(const char *oldpath, const char *newpath); NOIMPL(mknod,const char *name UNUSED_PARAM, mode_t mode UNUSED_PARAM, dev_t device UNUSED_PARAM); diff --git a/include/platform.h b/include/platform.h index cc22f4fc9..ee13e1b1e 100644 --- a/include/platform.h +++ b/include/platform.h @@ -442,6 +442,7 @@ typedef unsigned smalluint; #endif #if ENABLE_PLATFORM_MINGW32 +# undef HAVE_FDATASYNC # undef HAVE_DPRINTF # undef HAVE_GETLINE # undef HAVE_MEMRCHR diff --git a/win32/Kbuild b/win32/Kbuild index b2b43b290..8248c994f 100644 --- a/win32/Kbuild +++ b/win32/Kbuild @@ -6,6 +6,7 @@ lib-y:= lib-$(CONFIG_PLATFORM_MINGW32) += env.o lib-$(CONFIG_PLATFORM_MINGW32) += fnmatch.o +lib-$(CONFIG_PLATFORM_MINGW32) += fsync.o lib-$(CONFIG_PLATFORM_MINGW32) += ioctl.o lib-$(CONFIG_PLATFORM_MINGW32) += mingw.o lib-$(CONFIG_PLATFORM_MINGW32) += process.o 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 @@ +/* Emulate fsync on platforms that lack it, primarily Windows and + cross-compilers like MinGW. + + This is derived from sqlite3 sources. + https://www.sqlite.org/src/finfo?name=src/os_win.c + https://www.sqlite.org/copyright.html + + Written by Richard W.M. Jones + + Copyright (C) 2008-2018 Free Software Foundation, Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "libbb.h" +#include + +/* FlushFileBuffers */ +# define WIN32_LEAN_AND_MEAN +# include + +# include + +/* Get _get_osfhandle. */ +# include + +int +fsync (int fd) +{ + HANDLE h = (HANDLE) _get_osfhandle (fd); + DWORD err; + + if (h == INVALID_HANDLE_VALUE) + { + errno = EBADF; + return -1; + } + + if (!FlushFileBuffers (h)) + { + /* Translate some Windows errors into rough approximations of Unix + * errors. MSDN is useless as usual - in this case it doesn't + * document the full range of errors. + */ + err = GetLastError (); + switch (err) + { + case ERROR_ACCESS_DENIED: + /* For a read-only handle, fsync should succeed, even though we have + no way to sync the access-time changes. */ + return 0; + + /* eg. Trying to fsync a tty. */ + case ERROR_INVALID_HANDLE: + errno = EINVAL; + break; + + default: + errno = EIO; + } + return -1; + } + + return 0; +} -- cgit v1.2.3-55-g6feb