Drizzled Public API Documentation

my_pread.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 "myisam_priv.h"
00017 #include <drizzled/error.h>
00018 #include <cerrno>
00019 #include <unistd.h>
00020 
00021 using namespace drizzled;
00022 
00023 #define MY_WAIT_FOR_USER_TO_FIX_PANIC 60  /* in seconds */
00024 #define MY_WAIT_GIVE_USER_A_MESSAGE 10  /* Every 10 times of prev */
00025 
00026 /*
00027   Read a chunk of bytes from a file from a given position
00028 
00029   SYNOPSIOS
00030     my_pread()
00031     Filedes File decsriptor
00032     Buffer  Buffer to read data into
00033     Count Number of bytes to read
00034     offset  Position to read from
00035     MyFlags Flags
00036 
00037   NOTES
00038     This differs from the normal pread() call in that we don't care
00039     to set the position in the file back to the original position
00040     if the system doesn't support pread().
00041 
00042   RETURN
00043     (size_t) -1   Error
00044     #             Number of bytes read
00045 */
00046 
00047 size_t my_pread(int Filedes, unsigned char *Buffer, size_t Count, internal::my_off_t offset,
00048                 myf MyFlags)
00049 {
00050   size_t readbytes;
00051   int error= 0;
00052   for (;;)
00053   {
00054     errno=0;          /* Linux doesn't reset this */
00055     if ((error= ((readbytes= pread(Filedes, Buffer, Count, offset)) != Count)))
00056       errno= errno ? errno : -1;
00057     if (error || readbytes != Count)
00058     {
00059       if ((readbytes == 0 || readbytes == (size_t) -1) && errno == EINTR)
00060       {
00061         continue;                              /* Interrupted */
00062       }
00063       if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
00064       {
00065   if (readbytes == (size_t) -1)
00066     my_error(EE_READ, MYF(ME_BELL+ME_WAITTANG), "unknown", errno);
00067   else if (MyFlags & (MY_NABP | MY_FNABP))
00068     my_error(EE_EOFERR, MYF(ME_BELL+ME_WAITTANG), "unknown", errno);
00069       }
00070       if (readbytes == (size_t) -1 || (MyFlags & (MY_FNABP | MY_NABP)))
00071   return(MY_FILE_ERROR);    /* Return with error */
00072     }
00073     if (MyFlags & (MY_NABP | MY_FNABP))
00074       return(0);        /* Read went ok; Return 0 */
00075     return(readbytes);
00076   }
00077 } /* my_pread */
00078 
00079 
00080 /*
00081   Write a chunk of bytes to a file at a given position
00082 
00083   SYNOPSIOS
00084     my_pwrite()
00085     Filedes File decsriptor
00086     Buffer  Buffer to write data from
00087     Count Number of bytes to write
00088     offset  Position to write to
00089     MyFlags Flags
00090 
00091   NOTES
00092     This differs from the normal pwrite() call in that we don't care
00093     to set the position in the file back to the original position
00094     if the system doesn't support pwrite()
00095 
00096   RETURN
00097     (size_t) -1   Error
00098     #             Number of bytes read
00099 */
00100 
00101 size_t my_pwrite(int Filedes, const unsigned char *Buffer, size_t Count,
00102                  internal::my_off_t offset, myf MyFlags)
00103 {
00104   size_t writenbytes, written;
00105   uint32_t errors;
00106   errors= 0;
00107   written= 0;
00108 
00109   for (;;)
00110   {
00111     if ((writenbytes= pwrite(Filedes, Buffer, Count,offset)) == Count)
00112       break;
00113     errno= errno;
00114     if (writenbytes != (size_t) -1)
00115     {         /* Safegueard */
00116       written+=writenbytes;
00117       Buffer+=writenbytes;
00118       Count-=writenbytes;
00119       offset+=writenbytes;
00120     }
00121 #ifndef NO_BACKGROUND
00122     if ((errno == ENOSPC || errno == EDQUOT) &&
00123         (MyFlags & MY_WAIT_IF_FULL))
00124     {
00125       if (!(errors++ % MY_WAIT_GIVE_USER_A_MESSAGE))
00126   my_error(EE_DISK_FULL,MYF(ME_BELL | ME_NOREFRESH),
00127      "unknown", errno, MY_WAIT_FOR_USER_TO_FIX_PANIC);
00128       sleep(MY_WAIT_FOR_USER_TO_FIX_PANIC);
00129       continue;
00130     }
00131     if ((writenbytes && writenbytes != (size_t) -1) || errno == EINTR)
00132       continue;         /* Retry */
00133 #endif
00134     if (MyFlags & (MY_NABP | MY_FNABP))
00135     {
00136       if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
00137       {
00138   my_error(EE_WRITE, MYF(ME_BELL | ME_WAITTANG), "unknown", errno);
00139       }
00140       return(MY_FILE_ERROR);    /* Error on read */
00141     }
00142     else
00143       break;          /* Return bytes written */
00144   }
00145   if (MyFlags & (MY_NABP | MY_FNABP))
00146     return(0);      /* Want only errors */
00147   return(writenbytes+written);
00148 } /* my_pwrite */