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 |
||
47 |
do
|
|
48 |
{
|
|
49 |
#if defined(F_FULLFSYNC)
|
|
50 |
/*
|
|
51 |
In Mac OS X >= 10.3 this call is safer than fsync() (it forces the
|
|
52 |
disk's cache and guarantees ordered writes).
|
|
53 |
*/
|
|
54 |
if (!(res= fcntl(fd, F_FULLFSYNC, 0))) |
|
55 |
break; /* ok */ |
|
56 |
/* Some file systems don't support F_FULLFSYNC and fail above: */
|
|
57 |
#endif
|
|
58 |
#if defined(HAVE_FDATASYNC)
|
|
59 |
res= fdatasync(fd); |
|
60 |
#elif defined(HAVE_FSYNC)
|
|
61 |
res= fsync(fd); |
|
62 |
#else
|
|
63 |
#error Cannot find a way to sync a file, durability in danger
|
|
64 |
res= 0; /* No sync (strange OS) */ |
|
65 |
#endif
|
|
66 |
} while (res == -1 && errno == EINTR); |
|
67 |
||
68 |
if (res) |
|
69 |
{
|
|
70 |
int er= errno; |
|
71 |
if (!(my_errno= er)) |
|
72 |
my_errno= -1; /* Unknown error */ |
|
73 |
if ((my_flags & MY_IGNORE_BADFD) && |
|
74 |
(er == EBADF || er == EINVAL || er == EROFS)) |
|
75 |
{
|
|
76 |
res= 0; |
|
77 |
}
|
|
78 |
else if (my_flags & MY_WME) |
|
79 |
my_error(EE_SYNC, MYF(ME_BELL+ME_WAITTANG), my_filename(fd), my_errno); |
|
80 |
}
|
|
51.3.15
by Jay Pipes
Phase 3 removal of DBUG in mysys |
81 |
return(res); |
1
by brian
clean slate |
82 |
} /* my_sync */ |
83 |
||
84 |
||
85 |
static const char cur_dir_name[]= {FN_CURLIB, 0}; |
|
86 |
/*
|
|
87 |
Force directory information to disk.
|
|
88 |
||
89 |
SYNOPSIS
|
|
90 |
my_sync_dir()
|
|
91 |
dir_name the name of the directory
|
|
92 |
my_flags flags (MY_WME etc)
|
|
93 |
||
94 |
RETURN
|
|
95 |
0 if ok, !=0 if error
|
|
96 |
*/
|
|
632.1.11
by Monty Taylor
Fixed Sun Studio warnings in mysys. |
97 |
#ifdef NEED_EXPLICIT_SYNC_DIR
|
98 |
int my_sync_dir(const char *dir_name, myf my_flags) |
|
1
by brian
clean slate |
99 |
{
|
100 |
File dir_fd; |
|
101 |
int res= 0; |
|
102 |
const char *correct_dir_name; |
|
103 |
/* Sometimes the path does not contain an explicit directory */
|
|
104 |
correct_dir_name= (dir_name[0] == 0) ? cur_dir_name : dir_name; |
|
105 |
/*
|
|
106 |
Syncing a dir may give EINVAL on tmpfs on Linux, which is ok.
|
|
107 |
EIO on the other hand is very important. Hence MY_IGNORE_BADFD.
|
|
108 |
*/
|
|
109 |
if ((dir_fd= my_open(correct_dir_name, O_RDONLY, MYF(my_flags))) >= 0) |
|
110 |
{
|
|
111 |
if (my_sync(dir_fd, MYF(my_flags | MY_IGNORE_BADFD))) |
|
112 |
res= 2; |
|
113 |
if (my_close(dir_fd, MYF(my_flags))) |
|
114 |
res= 3; |
|
115 |
}
|
|
116 |
else
|
|
117 |
res= 1; |
|
51.3.15
by Jay Pipes
Phase 3 removal of DBUG in mysys |
118 |
return(res); |
632.1.11
by Monty Taylor
Fixed Sun Studio warnings in mysys. |
119 |
}
|
1
by brian
clean slate |
120 |
#else
|
632.1.11
by Monty Taylor
Fixed Sun Studio warnings in mysys. |
121 |
int my_sync_dir(const char *, myf) |
122 |
{
|
|
1
by brian
clean slate |
123 |
return 0; |
632.1.11
by Monty Taylor
Fixed Sun Studio warnings in mysys. |
124 |
}
|
1
by brian
clean slate |
125 |
#endif
|
126 |
||
127 |
||
128 |
/*
|
|
129 |
Force directory information to disk.
|
|
130 |
||
131 |
SYNOPSIS
|
|
132 |
my_sync_dir_by_file()
|
|
133 |
file_name the name of a file in the directory
|
|
134 |
my_flags flags (MY_WME etc)
|
|
135 |
||
136 |
RETURN
|
|
137 |
0 if ok, !=0 if error
|
|
138 |
*/
|
|
632.1.11
by Monty Taylor
Fixed Sun Studio warnings in mysys. |
139 |
#ifdef NEED_EXPLICIT_SYNC_DIR
|
140 |
int my_sync_dir_by_file(const char *file_name, myf my_flags) |
|
1
by brian
clean slate |
141 |
{
|
142 |
char dir_name[FN_REFLEN]; |
|
143 |
size_t dir_name_length; |
|
144 |
dirname_part(dir_name, file_name, &dir_name_length); |
|
145 |
return my_sync_dir(dir_name, my_flags); |
|
632.1.11
by Monty Taylor
Fixed Sun Studio warnings in mysys. |
146 |
}
|
1
by brian
clean slate |
147 |
#else
|
632.1.11
by Monty Taylor
Fixed Sun Studio warnings in mysys. |
148 |
int my_sync_dir_by_file(const char *, myf) |
149 |
{
|
|
1
by brian
clean slate |
150 |
return 0; |
632.1.11
by Monty Taylor
Fixed Sun Studio warnings in mysys. |
151 |
}
|
1
by brian
clean slate |
152 |
#endif
|
153 |