Drizzled Public API Documentation

my_read.cc

00001 /* Copyright (C) 2000 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 #include <config.h>
00017 
00018 #include <drizzled/internal/my_sys.h>
00019 #include <drizzled/error.h>
00020 #include <errno.h>
00021 
00022 namespace drizzled
00023 {
00024 namespace internal
00025 {
00026 
00027 
00028 /*
00029   Read a chunk of bytes from a file with retry's if needed
00030 
00031   The parameters are:
00032     File descriptor
00033     Buffer to hold at least Count bytes
00034     Bytes to read
00035     Flags on what to do on error
00036 
00037     Return:
00038       -1 on error
00039       0  if flag has bits MY_NABP or MY_FNABP set
00040       N  number of bytes read.
00041 */
00042 
00043 size_t my_read(int Filedes, unsigned char *Buffer, size_t Count, myf MyFlags)
00044 {
00045   size_t readbytes, save_count;
00046   save_count= Count;
00047 
00048   for (;;)
00049   {
00050     errno= 0;         /* Linux doesn't reset this */
00051     if ((readbytes= read(Filedes, Buffer, Count)) != Count)
00052     {
00053       errno= errno ? errno : -1;
00054       if ((readbytes == 0 || (int) readbytes == -1) && errno == EINTR)
00055       {
00056         continue;                              /* Interrupted */
00057       }
00058       if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
00059       {
00060         if (readbytes == (size_t) -1)
00061           my_error(EE_READ, MYF(ME_BELL+ME_WAITTANG),
00062                    "unknown", errno);
00063         else if (MyFlags & (MY_NABP | MY_FNABP))
00064           my_error(EE_EOFERR, MYF(ME_BELL+ME_WAITTANG),
00065                    "unknown", errno);
00066       }
00067       if (readbytes == (size_t) -1 ||
00068           ((MyFlags & (MY_FNABP | MY_NABP)) && !(MyFlags & MY_FULL_IO)))
00069         return(MY_FILE_ERROR);  /* Return with error */
00070       if (readbytes != (size_t) -1 && (MyFlags & MY_FULL_IO))
00071       {
00072         Buffer+= readbytes;
00073         Count-= readbytes;
00074         continue;
00075       }
00076     }
00077 
00078     if (MyFlags & (MY_NABP | MY_FNABP))
00079       readbytes= 0;     /* Ok on read */
00080     else if (MyFlags & MY_FULL_IO)
00081       readbytes= save_count;
00082     break;
00083   }
00084   return(readbytes);
00085 } /* my_read */
00086 
00087 } /* namespace internal */
00088 } /* namespace drizzled */