my_pread.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:5k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Library General Public
  5.    License as published by the Free Software Foundation; either
  6.    version 2 of the License, or (at your option) any later version.
  7.    
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU Library General Public
  14.    License along with this library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16.    MA 02111-1307, USA */
  17. #include "mysys_priv.h"
  18. #include "mysys_err.h"
  19. #include <errno.h>
  20. #ifdef HAVE_PREAD
  21. #include <unistd.h>
  22. #endif
  23. /* Read a chunk of bytes from a file  */
  24. uint my_pread(File Filedes, byte *Buffer, uint Count, my_off_t offset,
  25.       myf MyFlags)
  26. {
  27.   uint readbytes;
  28.   int error;
  29.   DBUG_ENTER("my_pread");
  30.   DBUG_PRINT("my",("Fd: %d  Seek: %lu  Buffer: %lx  Count: %u  MyFlags: %d",
  31.    Filedes, (ulong) offset, Buffer, Count, MyFlags));
  32.   for (;;)
  33.   {
  34. #ifndef __WIN__
  35.     errno=0; /* Linux doesn't reset this */
  36. #endif
  37. #ifndef HAVE_PREAD
  38.     pthread_mutex_lock(&my_file_info[Filedes].mutex);
  39.     readbytes= (uint) -1;
  40.     error= (lseek(Filedes, offset, MY_SEEK_SET) == -1L ||
  41.     (readbytes = (uint) read(Filedes, Buffer, Count)) != Count);
  42.     pthread_mutex_unlock(&my_file_info[Filedes].mutex);
  43. #else
  44.     error=((readbytes = (uint) pread(Filedes, Buffer, Count, offset)) != Count);
  45. #endif
  46.     if (error)
  47.     {
  48.       my_errno=errno;
  49.       DBUG_PRINT("warning",("Read only %ld bytes off %ld from %d, errno: %d",
  50.     readbytes,Count,Filedes,my_errno));
  51. #ifdef THREAD
  52.       if (readbytes == 0 && errno == EINTR)
  53. continue; /* Interrupted */
  54. #endif
  55.       if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
  56.       {
  57. if ((int) readbytes == -1)
  58.   my_error(EE_READ, MYF(ME_BELL+ME_WAITTANG),
  59.    my_filename(Filedes),my_errno);
  60. else if (MyFlags & (MY_NABP | MY_FNABP))
  61.   my_error(EE_EOFERR, MYF(ME_BELL+ME_WAITTANG),
  62.    my_filename(Filedes),my_errno);
  63.       }
  64.       if ((int) readbytes == -1 || (MyFlags & (MY_FNABP | MY_NABP)))
  65. DBUG_RETURN(MY_FILE_ERROR); /* Return with error */
  66.     }
  67.     if (MyFlags & (MY_NABP | MY_FNABP))
  68.       DBUG_RETURN(0); /* Ok vid l{sning */
  69.     DBUG_RETURN(readbytes); /* purecov: inspected */
  70.   }
  71. } /* my_pread */
  72. /* Write a chunk of bytes to a file */
  73. uint my_pwrite(int Filedes, const byte *Buffer, uint Count, my_off_t offset,
  74.        myf MyFlags)
  75. {
  76.   uint writenbytes,errors;
  77.   ulong written;
  78.   DBUG_ENTER("my_pwrite");
  79.   DBUG_PRINT("my",("Fd: %d  Seek: %lu  Buffer: %lx  Count: %d  MyFlags: %d",
  80.    Filedes, (ulong) offset,Buffer, Count, MyFlags));
  81.   errors=0; written=0L;
  82.   for (;;)
  83.   {
  84. #ifndef HAVE_PREAD
  85.     int error;
  86.     writenbytes= (uint) -1;
  87.     pthread_mutex_lock(&my_file_info[Filedes].mutex);
  88.     error=(lseek(Filedes, offset, MY_SEEK_SET) != -1L &&
  89.    (writenbytes = (uint) write(Filedes, Buffer, Count)) == Count);
  90.     pthread_mutex_unlock(&my_file_info[Filedes].mutex);
  91.     if (error)
  92.       break;
  93. #else
  94.     if ((writenbytes = (uint) pwrite(Filedes, Buffer, Count,offset)) == Count)
  95.       break;
  96. #endif
  97.     if ((int) writenbytes != -1)
  98.     { /* Safegueard */
  99.       written+=writenbytes;
  100.       Buffer+=writenbytes;
  101.       Count-=writenbytes;
  102.       offset+=writenbytes;
  103.     }
  104.     my_errno=errno;
  105.     DBUG_PRINT("error",("Write only %d bytes",writenbytes));
  106. #ifndef NO_BACKGROUND
  107. #ifdef THREAD
  108.     if (my_thread_var->abort)
  109.       MyFlags&= ~ MY_WAIT_IF_FULL; /* End if aborted by user */
  110. #endif
  111.     if (my_errno == ENOSPC && (MyFlags & MY_WAIT_IF_FULL))
  112.     {
  113.       if (!(errors++ % MY_WAIT_GIVE_USER_A_MESSAGE))
  114. my_error(EE_DISK_FULL,MYF(ME_BELL | ME_NOREFRESH),
  115.  my_filename(Filedes));
  116.       VOID(sleep(MY_WAIT_FOR_USER_TO_FIX_PANIC));
  117.       continue;
  118.     }
  119.     if ((writenbytes == 0 && my_errno == EINTR) ||
  120. (writenbytes > 0 && (uint) writenbytes != (uint) -1))
  121.       continue; /* Retry */
  122. #endif
  123.     if (MyFlags & (MY_NABP | MY_FNABP))
  124.     {
  125.       if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
  126.       {
  127. my_error(EE_WRITE, MYF(ME_BELL+ME_WAITTANG),
  128.  my_filename(Filedes),my_errno);
  129.       }
  130.       DBUG_RETURN(MY_FILE_ERROR); /* Error on read */
  131.     }
  132.     else
  133.       break; /* Return bytes written */
  134.   }
  135.   if (MyFlags & (MY_NABP | MY_FNABP))
  136.     DBUG_RETURN(0); /* Want only errors */
  137.   DBUG_RETURN(writenbytes+written); /* purecov: inspected */
  138. } /* my_write */