1
/* Copyright (C) 2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "mysys_priv.h"
17
#include "mysys_err.h"
21
Sync data in file to disk
25
fd File descritor to sync
26
my_flags Flags (now only MY_WME is supported)
29
If file system supports its, only file data is synced, not inode data.
31
MY_IGNORE_BADFD is useful when fd is "volatile" - not protected by a
32
mutex. In this case by the time of fsync(), fd may be already closed by
33
another thread, or even reassigned to a different file. With this flag -
34
MY_IGNORE_BADFD - such a situation will not be considered an error.
35
(which is correct behaviour, if we know that the other thread synced the
43
int my_sync(File fd, myf my_flags)
46
DBUG_ENTER("my_sync");
47
DBUG_PRINT("my",("Fd: %d my_flags: %d", fd, my_flags));
51
#if defined(F_FULLFSYNC)
53
In Mac OS X >= 10.3 this call is safer than fsync() (it forces the
54
disk's cache and guarantees ordered writes).
56
if (!(res= fcntl(fd, F_FULLFSYNC, 0)))
58
/* Some file systems don't support F_FULLFSYNC and fail above: */
59
DBUG_PRINT("info",("fcntl(F_FULLFSYNC) failed, falling back"));
61
#if defined(HAVE_FDATASYNC)
63
#elif defined(HAVE_FSYNC)
66
#error Cannot find a way to sync a file, durability in danger
67
res= 0; /* No sync (strange OS) */
69
} while (res == -1 && errno == EINTR);
75
my_errno= -1; /* Unknown error */
76
if ((my_flags & MY_IGNORE_BADFD) &&
77
(er == EBADF || er == EINVAL || er == EROFS))
79
DBUG_PRINT("info", ("ignoring errno %d", er));
82
else if (my_flags & MY_WME)
83
my_error(EE_SYNC, MYF(ME_BELL+ME_WAITTANG), my_filename(fd), my_errno);
89
static const char cur_dir_name[]= {FN_CURLIB, 0};
91
Force directory information to disk.
95
dir_name the name of the directory
96
my_flags flags (MY_WME etc)
101
int my_sync_dir(const char *dir_name, myf my_flags)
103
#ifdef NEED_EXPLICIT_SYNC_DIR
104
DBUG_ENTER("my_sync_dir");
105
DBUG_PRINT("my",("Dir: '%s' my_flags: %d", dir_name, my_flags));
108
const char *correct_dir_name;
109
/* Sometimes the path does not contain an explicit directory */
110
correct_dir_name= (dir_name[0] == 0) ? cur_dir_name : dir_name;
112
Syncing a dir may give EINVAL on tmpfs on Linux, which is ok.
113
EIO on the other hand is very important. Hence MY_IGNORE_BADFD.
115
if ((dir_fd= my_open(correct_dir_name, O_RDONLY, MYF(my_flags))) >= 0)
117
if (my_sync(dir_fd, MYF(my_flags | MY_IGNORE_BADFD)))
119
if (my_close(dir_fd, MYF(my_flags)))
132
Force directory information to disk.
135
my_sync_dir_by_file()
136
file_name the name of a file in the directory
137
my_flags flags (MY_WME etc)
140
0 if ok, !=0 if error
142
int my_sync_dir_by_file(const char *file_name, myf my_flags)
144
#ifdef NEED_EXPLICIT_SYNC_DIR
145
char dir_name[FN_REFLEN];
146
size_t dir_name_length;
147
dirname_part(dir_name, file_name, &dir_name_length);
148
return my_sync_dir(dir_name, my_flags);