Drizzled Public API Documentation

my_pread.cc
1 /* Copyright (C) 2000 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 "myisam_priv.h"
17 #include <drizzled/error.h>
18 #include <cerrno>
19 #include <unistd.h>
20 
21 using namespace drizzled;
22 
23 #define MY_WAIT_FOR_USER_TO_FIX_PANIC 60 /* in seconds */
24 #define MY_WAIT_GIVE_USER_A_MESSAGE 10 /* Every 10 times of prev */
25 
26 /*
27  Read a chunk of bytes from a file from a given position
28 
29  SYNOPSIOS
30  my_pread()
31  Filedes File decsriptor
32  Buffer Buffer to read data into
33  Count Number of bytes to read
34  offset Position to read from
35  MyFlags Flags
36 
37  NOTES
38  This differs from the normal pread() call in that we don't care
39  to set the position in the file back to the original position
40  if the system doesn't support pread().
41 
42  RETURN
43  (size_t) -1 Error
44  # Number of bytes read
45 */
46 
47 size_t my_pread(int Filedes, unsigned char *Buffer, size_t Count, internal::my_off_t offset,
48  myf MyFlags)
49 {
50  size_t readbytes;
51  int error= 0;
52  for (;;)
53  {
54  errno=0; /* Linux doesn't reset this */
55  if ((error= ((readbytes= pread(Filedes, Buffer, Count, offset)) != Count)))
56  errno= errno ? errno : -1;
57  if (error || readbytes != Count)
58  {
59  if ((readbytes == 0 || readbytes == (size_t) -1) && errno == EINTR)
60  {
61  continue; /* Interrupted */
62  }
63  if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
64  {
65  if (readbytes == (size_t) -1)
66  my_error(EE_READ, MYF(ME_BELL+ME_WAITTANG), "unknown", errno);
67  else if (MyFlags & (MY_NABP | MY_FNABP))
68  my_error(EE_EOFERR, MYF(ME_BELL+ME_WAITTANG), "unknown", errno);
69  }
70  if (readbytes == (size_t) -1 || (MyFlags & (MY_FNABP | MY_NABP)))
71  return(MY_FILE_ERROR); /* Return with error */
72  }
73  if (MyFlags & (MY_NABP | MY_FNABP))
74  return(0); /* Read went ok; Return 0 */
75  return(readbytes);
76  }
77 } /* my_pread */
78 
79 
80 /*
81  Write a chunk of bytes to a file at a given position
82 
83  SYNOPSIOS
84  my_pwrite()
85  Filedes File decsriptor
86  Buffer Buffer to write data from
87  Count Number of bytes to write
88  offset Position to write to
89  MyFlags Flags
90 
91  NOTES
92  This differs from the normal pwrite() call in that we don't care
93  to set the position in the file back to the original position
94  if the system doesn't support pwrite()
95 
96  RETURN
97  (size_t) -1 Error
98  # Number of bytes read
99 */
100 
101 size_t my_pwrite(int Filedes, const unsigned char *Buffer, size_t Count,
102  internal::my_off_t offset, myf MyFlags)
103 {
104  size_t writenbytes, written;
105  uint32_t errors;
106  errors= 0;
107  written= 0;
108 
109  for (;;)
110  {
111  if ((writenbytes= pwrite(Filedes, Buffer, Count,offset)) == Count)
112  break;
113  errno= errno;
114  if (writenbytes != (size_t) -1)
115  { /* Safegueard */
116  written+=writenbytes;
117  Buffer+=writenbytes;
118  Count-=writenbytes;
119  offset+=writenbytes;
120  }
121 #ifndef NO_BACKGROUND
122  if ((errno == ENOSPC || errno == EDQUOT) &&
123  (MyFlags & MY_WAIT_IF_FULL))
124  {
125  if (!(errors++ % MY_WAIT_GIVE_USER_A_MESSAGE))
126  my_error(EE_DISK_FULL,MYF(ME_BELL | ME_NOREFRESH),
127  "unknown", errno, MY_WAIT_FOR_USER_TO_FIX_PANIC);
128  sleep(MY_WAIT_FOR_USER_TO_FIX_PANIC);
129  continue;
130  }
131  if ((writenbytes && writenbytes != (size_t) -1) || errno == EINTR)
132  continue; /* Retry */
133 #endif
134  if (MyFlags & (MY_NABP | MY_FNABP))
135  {
136  if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
137  {
138  my_error(EE_WRITE, MYF(ME_BELL | ME_WAITTANG), "unknown", errno);
139  }
140  return(MY_FILE_ERROR); /* Error on read */
141  }
142  else
143  break; /* Return bytes written */
144  }
145  if (MyFlags & (MY_NABP | MY_FNABP))
146  return(0); /* Want only errors */
147  return(writenbytes+written);
148 } /* my_pwrite */