aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2018-02-21 09:58:22 +0000
committerRon Yorston <rmy@pobox.com>2018-02-21 09:58:22 +0000
commit5b5cb69bf3e16e4170d4206621682a12d441c87f (patch)
treee17c977154aa2e0a54dd1acef5c8d6a8ada72624
parenta1401e340043ac2829757be5c5f546d0ce7eadc7 (diff)
downloadbusybox-w32-5b5cb69bf3e16e4170d4206621682a12d441c87f.tar.gz
busybox-w32-5b5cb69bf3e16e4170d4206621682a12d441c87f.tar.bz2
busybox-w32-5b5cb69bf3e16e4170d4206621682a12d441c87f.zip
win32: import fsync(2) implementation from gnulib
-rw-r--r--include/mingw.h2
-rw-r--r--include/platform.h1
-rw-r--r--win32/Kbuild1
-rw-r--r--win32/fsync.c75
4 files changed, 78 insertions, 1 deletions
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);
390IMPL(getuid,int,DEFAULT_UID,void); 390IMPL(getuid,int,DEFAULT_UID,void);
391int getlogin_r(char *buf, size_t len); 391int getlogin_r(char *buf, size_t len);
392int fcntl(int fd, int cmd, ...); 392int fcntl(int fd, int cmd, ...);
393IMPL(fsync,int,0,int fd UNUSED_PARAM); 393int fsync(int fd);
394int kill(pid_t pid, int sig); 394int kill(pid_t pid, int sig);
395int link(const char *oldpath, const char *newpath); 395int link(const char *oldpath, const char *newpath);
396NOIMPL(mknod,const char *name UNUSED_PARAM, mode_t mode UNUSED_PARAM, dev_t device UNUSED_PARAM); 396NOIMPL(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;
442#endif 442#endif
443 443
444#if ENABLE_PLATFORM_MINGW32 444#if ENABLE_PLATFORM_MINGW32
445# undef HAVE_FDATASYNC
445# undef HAVE_DPRINTF 446# undef HAVE_DPRINTF
446# undef HAVE_GETLINE 447# undef HAVE_GETLINE
447# undef HAVE_MEMRCHR 448# 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:=
6 6
7lib-$(CONFIG_PLATFORM_MINGW32) += env.o 7lib-$(CONFIG_PLATFORM_MINGW32) += env.o
8lib-$(CONFIG_PLATFORM_MINGW32) += fnmatch.o 8lib-$(CONFIG_PLATFORM_MINGW32) += fnmatch.o
9lib-$(CONFIG_PLATFORM_MINGW32) += fsync.o
9lib-$(CONFIG_PLATFORM_MINGW32) += ioctl.o 10lib-$(CONFIG_PLATFORM_MINGW32) += ioctl.o
10lib-$(CONFIG_PLATFORM_MINGW32) += mingw.o 11lib-$(CONFIG_PLATFORM_MINGW32) += mingw.o
11lib-$(CONFIG_PLATFORM_MINGW32) += process.o 12lib-$(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 @@
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
37int
38fsync (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}