~drizzle-trunk/drizzle/development

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
*/
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
97
int my_sync_dir(const char *dir_name __attribute__((unused)),
98
                myf my_flags __attribute__((unused)))
1 by brian
clean slate
99
{
100
#ifdef NEED_EXPLICIT_SYNC_DIR
101
  File dir_fd;
102
  int res= 0;
103
  const char *correct_dir_name;
104
  /* Sometimes the path does not contain an explicit directory */
105
  correct_dir_name= (dir_name[0] == 0) ? cur_dir_name : dir_name;
106
  /*
107
    Syncing a dir may give EINVAL on tmpfs on Linux, which is ok.
108
    EIO on the other hand is very important. Hence MY_IGNORE_BADFD.
109
  */
110
  if ((dir_fd= my_open(correct_dir_name, O_RDONLY, MYF(my_flags))) >= 0)
111
  {
112
    if (my_sync(dir_fd, MYF(my_flags | MY_IGNORE_BADFD)))
113
      res= 2;
114
    if (my_close(dir_fd, MYF(my_flags)))
115
      res= 3;
116
  }
117
  else
118
    res= 1;
51.3.15 by Jay Pipes
Phase 3 removal of DBUG in mysys
119
  return(res);
1 by brian
clean slate
120
#else
121
  return 0;
122
#endif
123
}
124
125
126
/*
127
  Force directory information to disk.
128
129
  SYNOPSIS
130
    my_sync_dir_by_file()
131
    file_name            the name of a file in the directory
132
    my_flags             flags (MY_WME etc)
133
134
  RETURN
135
    0 if ok, !=0 if error
136
*/
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
137
int my_sync_dir_by_file(const char *file_name __attribute__((unused)),
138
                        myf my_flags __attribute__((unused)))
1 by brian
clean slate
139
{
140
#ifdef NEED_EXPLICIT_SYNC_DIR
141
  char dir_name[FN_REFLEN];
142
  size_t dir_name_length;
143
  dirname_part(dir_name, file_name, &dir_name_length);
144
  return my_sync_dir(dir_name, my_flags);
145
#else
146
  return 0;
147
#endif
148
}
149