size_t my_fwrite(FILE *stream, const uchar *Buffer, size_t Count, myf MyFlags) { size_t writtenbytes =0; my_off_t seekptr; #if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM) uint errors; #endif DBUG_ENTER("my_fwrite"); DBUG_PRINT("my",("stream: 0x%lx Buffer: 0x%lx Count: %u MyFlags: %d", (long) stream, (long) Buffer, (uint) Count, MyFlags)); #if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM) errors=0; #endif seekptr= ftell(stream); for (;;) { size_t written; if ((written = (size_t) fwrite((char*) Buffer,sizeof(char), Count, stream)) != Count) { DBUG_PRINT("error",("Write only %d bytes", (int) writtenbytes)); my_errno=errno; if (written != (size_t) -1) { seekptr+=written; Buffer+=written; writtenbytes+=written; Count-=written; } #ifdef EINTR if (errno == EINTR) { VOID(my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0))); continue; } #endif #if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM) #ifdef THREAD if (my_thread_var->abort) MyFlags&= ~ MY_WAIT_IF_FULL; /* End if aborted by user */ #endif if ((errno == ENOSPC || errno == EDQUOT) && (MyFlags & MY_WAIT_IF_FULL)) { wait_for_free_space("[stream]", errors); errors++; VOID(my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0))); continue; } #endif if (ferror(stream) || (MyFlags & (MY_NABP | MY_FNABP))) { if (MyFlags & (MY_WME | MY_FAE | MY_FNABP)) { my_error(EE_WRITE, MYF(ME_BELL+ME_WAITTANG), my_filename(fileno(stream)),errno); } writtenbytes= (size_t) -1; /* Return that we got error */ break; } } if (MyFlags & (MY_NABP | MY_FNABP)) writtenbytes= 0; /* Everything OK */ else writtenbytes+= written; break; } DBUG_RETURN(writtenbytes); } /* my_fwrite */
size_t my_pwrite(int Filedes, const uchar *Buffer, size_t Count, my_off_t offset, myf MyFlags) { size_t writenbytes, written; uint errors; DBUG_ENTER("my_pwrite"); DBUG_PRINT("my",("Fd: %d Seek: %lu Buffer: 0x%lx Count: %u MyFlags: %d", Filedes, (ulong) offset, (long) Buffer, (uint) Count, MyFlags)); errors= 0; written= 0; for (;;) { #ifndef HAVE_PREAD int error; writenbytes= (size_t) -1; pthread_mutex_lock(&my_file_info[Filedes].mutex); error= (lseek(Filedes, offset, MY_SEEK_SET) != (my_off_t) -1 && (writenbytes = write(Filedes, Buffer, (uint) Count)) == Count); pthread_mutex_unlock(&my_file_info[Filedes].mutex); if (error) break; #else if ((writenbytes= pwrite(Filedes, Buffer, Count,offset)) == Count) break; my_errno= errno; #endif if (writenbytes != (size_t) -1) { /* Safegueard */ written+=writenbytes; Buffer+=writenbytes; Count-=writenbytes; offset+=writenbytes; } DBUG_PRINT("error",("Write only %u bytes", (uint) writenbytes)); #ifndef NO_BACKGROUND #ifdef THREAD if (my_thread_var->abort) MyFlags&= ~ MY_WAIT_IF_FULL; /* End if aborted by user */ #endif if ((my_errno == ENOSPC || my_errno == EDQUOT) && (MyFlags & MY_WAIT_IF_FULL)) { wait_for_free_space(my_filename(Filedes), errors); errors++; continue; } if ((writenbytes && writenbytes != (size_t) -1) || my_errno == EINTR) continue; /* Retry */ #endif if (MyFlags & (MY_NABP | MY_FNABP)) { if (MyFlags & (MY_WME | MY_FAE | MY_FNABP)) { my_error(EE_WRITE, MYF(ME_BELL | ME_WAITTANG), my_filename(Filedes),my_errno); } DBUG_RETURN(MY_FILE_ERROR); /* Error on read */ } else break; /* Return bytes written */ } DBUG_EXECUTE_IF("check", my_seek(Filedes, -1, SEEK_SET, MYF(0)););
size_t my_pwrite(File Filedes, const uchar *Buffer, size_t Count, my_off_t offset, myf MyFlags) { size_t writtenbytes; size_t sum_written= 0; uint errors= 0; const size_t initial_count= Count; DBUG_ENTER("my_pwrite"); DBUG_PRINT("my",("fd: %d Seek: %llu Buffer: %p Count: %lu MyFlags: %d", Filedes, offset, Buffer, (ulong)Count, MyFlags)); for (;;) { errno= 0; #if defined (_WIN32) writtenbytes= my_win_pwrite(Filedes, Buffer, Count, offset); #else writtenbytes= pwrite(Filedes, Buffer, Count, offset); #endif if(writtenbytes == Count) { sum_written+= writtenbytes; break; } my_errno= errno; if (writtenbytes != (size_t) -1) { sum_written+= writtenbytes; Buffer+= writtenbytes; Count-= writtenbytes; offset+= writtenbytes; } DBUG_PRINT("error",("Write only %u bytes", (uint) writtenbytes)); if (my_thread_var->abort) MyFlags&= ~ MY_WAIT_IF_FULL; /* End if aborted by user */ if ((my_errno == ENOSPC || my_errno == EDQUOT) && (MyFlags & MY_WAIT_IF_FULL)) { wait_for_free_space(my_filename(Filedes), errors); errors++; continue; } if (writtenbytes != 0 && writtenbytes != (size_t) -1) continue; else if (my_errno == EINTR) { continue; /* Retry */ } else if (writtenbytes == 0 && !errors++) /* Retry once */ { /* We may come here if the file quota is exeeded */ continue; } break; /* Return bytes written */ } if (MyFlags & (MY_NABP | MY_FNABP)) { if (sum_written == initial_count) DBUG_RETURN(0); /* Want only errors, not bytes written */ if (MyFlags & (MY_WME | MY_FAE | MY_FNABP)) { char errbuf[MYSYS_STRERROR_SIZE]; my_error(EE_WRITE, MYF(ME_BELL | ME_WAITTANG), my_filename(Filedes), my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno)); } DBUG_RETURN(MY_FILE_ERROR); } DBUG_EXECUTE_IF("check", my_seek(Filedes, -1, SEEK_SET, MYF(0)););