1
by brian
clean slate |
1 |
/* Copyright (C) 2003 MySQL AB
|
2 |
||
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.
|
|
6 |
||
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.
|
|
11 |
||
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 */
|
|
15 |
||
16 |
#include "mysys_priv.h" |
|
17 |
#include "mysys_err.h" |
|
18 |
#include <errno.h> |
|
19 |
||
20 |
/*
|
|
21 |
Sync data in file to disk
|
|
22 |
||
23 |
SYNOPSIS
|
|
24 |
my_sync()
|
|
25 |
fd File descritor to sync
|
|
26 |
my_flags Flags (now only MY_WME is supported)
|
|
27 |
||
28 |
NOTE
|
|
29 |
If file system supports its, only file data is synced, not inode data.
|
|
30 |
||
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
|
|
36 |
file before closing)
|
|
37 |
||
38 |
RETURN
|
|
39 |
0 ok
|
|
40 |
-1 error
|
|
41 |
*/
|
|
42 |
||
43 |
int my_sync(File fd, myf my_flags) |
|
44 |
{
|
|
45 |
int res; |
|
46 |
DBUG_ENTER("my_sync"); |
|
47 |
DBUG_PRINT("my",("Fd: %d my_flags: %d", fd, my_flags)); |
|
48 |
||
49 |
do
|
|
50 |
{
|
|
51 |
#if defined(F_FULLFSYNC)
|
|
52 |
/*
|
|
53 |
In Mac OS X >= 10.3 this call is safer than fsync() (it forces the
|
|
54 |
disk's cache and guarantees ordered writes).
|
|
55 |
*/
|
|
56 |
if (!(res= fcntl(fd, F_FULLFSYNC, 0))) |
|
57 |
break; /* ok */ |
|
58 |
/* Some file systems don't support F_FULLFSYNC and fail above: */
|
|
59 |
DBUG_PRINT("info",("fcntl(F_FULLFSYNC) failed, falling back")); |
|
60 |
#endif
|
|
61 |
#if defined(HAVE_FDATASYNC)
|
|
62 |
res= fdatasync(fd); |
|
63 |
#elif defined(HAVE_FSYNC)
|
|
64 |
res= fsync(fd); |
|
65 |
#else
|
|
66 |
#error Cannot find a way to sync a file, durability in danger
|
|
67 |
res= 0; /* No sync (strange OS) */ |
|
68 |
#endif
|
|
69 |
} while (res == -1 && errno == EINTR); |
|
70 |
||
71 |
if (res) |
|
72 |
{
|
|
73 |
int er= errno; |
|
74 |
if (!(my_errno= er)) |
|
75 |
my_errno= -1; /* Unknown error */ |
|
76 |
if ((my_flags & MY_IGNORE_BADFD) && |
|
77 |
(er == EBADF || er == EINVAL || er == EROFS)) |
|
78 |
{
|
|
79 |
DBUG_PRINT("info", ("ignoring errno %d", er)); |
|
80 |
res= 0; |
|
81 |
}
|
|
82 |
else if (my_flags & MY_WME) |
|
83 |
my_error(EE_SYNC, MYF(ME_BELL+ME_WAITTANG), my_filename(fd), my_errno); |
|
84 |
}
|
|
85 |
DBUG_RETURN(res); |
|
86 |
} /* my_sync */ |
|
87 |
||
88 |
||
89 |
static const char cur_dir_name[]= {FN_CURLIB, 0}; |
|
90 |
/*
|
|
91 |
Force directory information to disk.
|
|
92 |
||
93 |
SYNOPSIS
|
|
94 |
my_sync_dir()
|
|
95 |
dir_name the name of the directory
|
|
96 |
my_flags flags (MY_WME etc)
|
|
97 |
||
98 |
RETURN
|
|
99 |
0 if ok, !=0 if error
|
|
100 |
*/
|
|
101 |
int my_sync_dir(const char *dir_name, myf my_flags) |
|
102 |
{
|
|
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)); |
|
106 |
File dir_fd; |
|
107 |
int res= 0; |
|
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; |
|
111 |
/*
|
|
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.
|
|
114 |
*/
|
|
115 |
if ((dir_fd= my_open(correct_dir_name, O_RDONLY, MYF(my_flags))) >= 0) |
|
116 |
{
|
|
117 |
if (my_sync(dir_fd, MYF(my_flags | MY_IGNORE_BADFD))) |
|
118 |
res= 2; |
|
119 |
if (my_close(dir_fd, MYF(my_flags))) |
|
120 |
res= 3; |
|
121 |
}
|
|
122 |
else
|
|
123 |
res= 1; |
|
124 |
DBUG_RETURN(res); |
|
125 |
#else
|
|
126 |
return 0; |
|
127 |
#endif
|
|
128 |
}
|
|
129 |
||
130 |
||
131 |
/*
|
|
132 |
Force directory information to disk.
|
|
133 |
||
134 |
SYNOPSIS
|
|
135 |
my_sync_dir_by_file()
|
|
136 |
file_name the name of a file in the directory
|
|
137 |
my_flags flags (MY_WME etc)
|
|
138 |
||
139 |
RETURN
|
|
140 |
0 if ok, !=0 if error
|
|
141 |
*/
|
|
142 |
int my_sync_dir_by_file(const char *file_name, myf my_flags) |
|
143 |
{
|
|
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); |
|
149 |
#else
|
|
150 |
return 0; |
|
151 |
#endif
|
|
152 |
}
|
|
153 |