~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
994.2.4 by Monty Taylor
Blast. Fixed some make distcheck issues.
16
#include "mysys/mysys_priv.h"
17
#include "mysys/mysys_err.h"
1 by brian
clean slate
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);
1192.3.42 by Monty Taylor
Removed a bunch of checks for things that are, in fact, everywhere.
60
#else
1 by brian
clean slate
61
    res= fsync(fd);
62
#endif
63
  } while (res == -1 && errno == EINTR);
64
65
  if (res)
66
  {
67
    int er= errno;
68
    if (!(my_errno= er))
69
      my_errno= -1;                             /* Unknown error */
70
    if ((my_flags & MY_IGNORE_BADFD) &&
71
        (er == EBADF || er == EINVAL || er == EROFS))
72
    {
73
      res= 0;
74
    }
75
    else if (my_flags & MY_WME)
1034.1.8 by Brian Aker
Remove locks around my_open(). Open file counts are now "best effort" (not
76
      my_error(EE_SYNC, MYF(ME_BELL+ME_WAITTANG), "unknown", my_errno);
1 by brian
clean slate
77
  }
51.3.15 by Jay Pipes
Phase 3 removal of DBUG in mysys
78
  return(res);
1 by brian
clean slate
79
} /* my_sync */
80
81
82
static const char cur_dir_name[]= {FN_CURLIB, 0};
83
/*
84
  Force directory information to disk.
85
86
  SYNOPSIS
87
    my_sync_dir()
88
    dir_name             the name of the directory
89
    my_flags             flags (MY_WME etc)
90
91
  RETURN
92
    0 if ok, !=0 if error
93
*/
632.1.11 by Monty Taylor
Fixed Sun Studio warnings in mysys.
94
#ifdef NEED_EXPLICIT_SYNC_DIR
95
int my_sync_dir(const char *dir_name, myf my_flags)
1 by brian
clean slate
96
{
97
  File dir_fd;
98
  int res= 0;
99
  const char *correct_dir_name;
100
  /* Sometimes the path does not contain an explicit directory */
101
  correct_dir_name= (dir_name[0] == 0) ? cur_dir_name : dir_name;
102
  /*
103
    Syncing a dir may give EINVAL on tmpfs on Linux, which is ok.
104
    EIO on the other hand is very important. Hence MY_IGNORE_BADFD.
105
  */
106
  if ((dir_fd= my_open(correct_dir_name, O_RDONLY, MYF(my_flags))) >= 0)
107
  {
108
    if (my_sync(dir_fd, MYF(my_flags | MY_IGNORE_BADFD)))
109
      res= 2;
110
    if (my_close(dir_fd, MYF(my_flags)))
111
      res= 3;
112
  }
113
  else
114
    res= 1;
51.3.15 by Jay Pipes
Phase 3 removal of DBUG in mysys
115
  return(res);
632.1.11 by Monty Taylor
Fixed Sun Studio warnings in mysys.
116
}
1 by brian
clean slate
117
#else
632.1.11 by Monty Taylor
Fixed Sun Studio warnings in mysys.
118
int my_sync_dir(const char *, myf)
119
{
1 by brian
clean slate
120
  return 0;
632.1.11 by Monty Taylor
Fixed Sun Studio warnings in mysys.
121
}
1 by brian
clean slate
122
#endif
123
124
125
/*
126
  Force directory information to disk.
127
128
  SYNOPSIS
129
    my_sync_dir_by_file()
130
    file_name            the name of a file in the directory
131
    my_flags             flags (MY_WME etc)
132
133
  RETURN
134
    0 if ok, !=0 if error
135
*/
632.1.11 by Monty Taylor
Fixed Sun Studio warnings in mysys.
136
#ifdef NEED_EXPLICIT_SYNC_DIR
137
int my_sync_dir_by_file(const char *file_name, myf my_flags)
1 by brian
clean slate
138
{
139
  char dir_name[FN_REFLEN];
140
  size_t dir_name_length;
141
  dirname_part(dir_name, file_name, &dir_name_length);
142
  return my_sync_dir(dir_name, my_flags);
632.1.11 by Monty Taylor
Fixed Sun Studio warnings in mysys.
143
}
1 by brian
clean slate
144
#else
632.1.11 by Monty Taylor
Fixed Sun Studio warnings in mysys.
145
int my_sync_dir_by_file(const char *, myf)
146
{
1 by brian
clean slate
147
  return 0;
632.1.11 by Monty Taylor
Fixed Sun Studio warnings in mysys.
148
}
1 by brian
clean slate
149
#endif
150