Drizzled Public API Documentation

my_read.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 <config.h>
17 
18 #include <drizzled/internal/my_sys.h>
19 #include <drizzled/error.h>
20 #include <errno.h>
21 
22 namespace drizzled
23 {
24 namespace internal
25 {
26 
27 
28 /*
29  Read a chunk of bytes from a file with retry's if needed
30 
31  The parameters are:
32  File descriptor
33  Buffer to hold at least Count bytes
34  Bytes to read
35  Flags on what to do on error
36 
37  Return:
38  -1 on error
39  0 if flag has bits MY_NABP or MY_FNABP set
40  N number of bytes read.
41 */
42 
43 size_t my_read(int Filedes, unsigned char *Buffer, size_t Count, myf MyFlags)
44 {
45  size_t readbytes, save_count;
46  save_count= Count;
47 
48  for (;;)
49  {
50  errno= 0; /* Linux doesn't reset this */
51  if ((readbytes= read(Filedes, Buffer, Count)) != Count)
52  {
53  errno= errno ? errno : -1;
54  if ((readbytes == 0 || (int) readbytes == -1) && errno == EINTR)
55  {
56  continue; /* Interrupted */
57  }
58  if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
59  {
60  if (readbytes == (size_t) -1)
61  my_error(EE_READ, MYF(ME_BELL+ME_WAITTANG),
62  "unknown", errno);
63  else if (MyFlags & (MY_NABP | MY_FNABP))
64  my_error(EE_EOFERR, MYF(ME_BELL+ME_WAITTANG),
65  "unknown", errno);
66  }
67  if (readbytes == (size_t) -1 ||
68  ((MyFlags & (MY_FNABP | MY_NABP)) && !(MyFlags & MY_FULL_IO)))
69  return(MY_FILE_ERROR); /* Return with error */
70  if (readbytes != (size_t) -1 && (MyFlags & MY_FULL_IO))
71  {
72  Buffer+= readbytes;
73  Count-= readbytes;
74  continue;
75  }
76  }
77 
78  if (MyFlags & (MY_NABP | MY_FNABP))
79  readbytes= 0; /* Ok on read */
80  else if (MyFlags & MY_FULL_IO)
81  readbytes= save_count;
82  break;
83  }
84  return(readbytes);
85 } /* my_read */
86 
87 } /* namespace internal */
88 } /* namespace drizzled */