drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
my_sync.cc
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16#include <config.h>
17#include <drizzled/internal/my_sys.h>
18
19#include <errno.h>
20#include <fcntl.h>
21
22#include <drizzled/error.h>
23
24namespace drizzled
25{
26namespace internal
27{
28
29/*
30 Sync data in file to disk
31
32 SYNOPSIS
33 my_sync()
34 fd File descritor to sync
35 my_flags Flags (now only MY_WME is supported)
36
37 NOTE
38 If file system supports its, only file data is synced, not inode data.
39
40 MY_IGNORE_BADFD is useful when fd is "volatile" - not protected by a
41 mutex. In this case by the time of fsync(), fd may be already closed by
42 another thread, or even reassigned to a different file. With this flag -
43 MY_IGNORE_BADFD - such a situation will not be considered an error.
44 (which is correct behaviour, if we know that the other thread synced the
45 file before closing)
46
47 RETURN
48 0 ok
49 -1 error
50*/
51
52int my_sync(int fd, myf my_flags)
53{
54 int res;
55
56 do
57 {
58#if defined(F_FULLFSYNC)
59 /*
60 In Mac OS X >= 10.3 this call is safer than fsync() (it forces the
61 disk's cache and guarantees ordered writes).
62 */
63 if (!(res= fcntl(fd, F_FULLFSYNC, 0)))
64 break; /* ok */
65 /* Some file systems don't support F_FULLFSYNC and fail above: */
66#endif
67 res= fdatasync(fd);
68 } while (res == -1 && errno == EINTR);
69
70 if (res)
71 {
72 int er= errno;
73 if (!(errno= er))
74 errno= -1; /* Unknown error */
75 if ((my_flags & MY_IGNORE_BADFD) &&
76 (er == EBADF || er == EINVAL || er == EROFS))
77 {
78 res= 0;
79 }
80 else if (my_flags & MY_WME)
81 my_error(EE_SYNC, MYF(ME_BELL+ME_WAITTANG), "unknown", errno);
82 }
83 return res;
84} /* my_sync */
85
86
87static const char cur_dir_name[]= {FN_CURLIB, 0};
88/*
89 Force directory information to disk.
90
91 SYNOPSIS
92 my_sync_dir()
93 dir_name the name of the directory
94 my_flags flags (MY_WME etc)
95
96 RETURN
97 0 if ok, !=0 if error
98*/
99#ifdef NEED_EXPLICIT_SYNC_DIR
100int my_sync_dir(const char *dir_name, myf my_flags)
101{
102 int dir_fd;
103 int res= 0;
104 const char *correct_dir_name;
105 /* Sometimes the path does not contain an explicit directory */
106 correct_dir_name= (dir_name[0] == 0) ? cur_dir_name : dir_name;
107 /*
108 Syncing a dir may give EINVAL on tmpfs on Linux, which is ok.
109 EIO on the other hand is very important. Hence MY_IGNORE_BADFD.
110 */
111 if ((dir_fd= my_open(correct_dir_name, O_RDONLY, MYF(my_flags))) >= 0)
112 {
113 if (my_sync(dir_fd, MYF(my_flags | MY_IGNORE_BADFD)))
114 res= 2;
115 if (my_close(dir_fd, MYF(my_flags)))
116 res= 3;
117 }
118 else
119 res= 1;
120 return res;
121}
122#else
123int my_sync_dir(const char *, myf)
124{
125 return 0;
126}
127#endif
128
129
130/*
131 Force directory information to disk.
132
133 SYNOPSIS
134 my_sync_dir_by_file()
135 file_name the name of a file in the directory
136 my_flags flags (MY_WME etc)
137
138 RETURN
139 0 if ok, !=0 if error
140*/
141#ifdef NEED_EXPLICIT_SYNC_DIR
142int my_sync_dir_by_file(const char *file_name, myf my_flags)
143{
144 char dir_name[FN_REFLEN];
145 size_t dir_name_length;
146 dirname_part(dir_name, file_name, &dir_name_length);
147 return my_sync_dir(dir_name, my_flags);
148}
149#else
150int my_sync_dir_by_file(const char *, myf)
151{
152 return 0;
153}
154#endif
155
156} /* namespace internal */
157} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.