1
/* Copyright (C) 2000 MySQL AB
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "mysys_priv.h"
17
#include "mysys_err.h"
24
Read a chunk of bytes from a file from a given position
28
Filedes File decsriptor
29
Buffer Buffer to read data into
30
Count Number of bytes to read
31
offset Position to read from
35
This differs from the normal pread() call in that we don't care
36
to set the position in the file back to the original position
37
if the system doesn't support pread().
41
# Number of bytes read
44
size_t my_pread(File Filedes, uchar *Buffer, size_t Count, my_off_t offset,
49
DBUG_ENTER("my_pread");
50
DBUG_PRINT("my",("Fd: %d Seek: %lu Buffer: 0x%lx Count: %u MyFlags: %d",
51
Filedes, (ulong) offset, (long) Buffer, (uint) Count,
55
errno=0; /* Linux doesn't reset this */
57
pthread_mutex_lock(&my_file_info[Filedes].mutex);
59
error= (lseek(Filedes, offset, MY_SEEK_SET) == (my_off_t) -1 ||
60
(readbytes= read(Filedes, Buffer, Count)) != Count);
61
pthread_mutex_unlock(&my_file_info[Filedes].mutex);
63
if ((error= ((readbytes= pread(Filedes, Buffer, Count, offset)) != Count)))
64
my_errno= errno ? errno : -1;
66
if (error || readbytes != Count)
68
DBUG_PRINT("warning",("Read only %d bytes off %u from %d, errno: %d",
69
(int) readbytes, (uint) Count,Filedes,my_errno));
71
if ((readbytes == 0 || readbytes == (size_t) -1) && errno == EINTR)
73
DBUG_PRINT("debug", ("my_pread() was interrupted and returned %d",
75
continue; /* Interrupted */
78
if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
80
if (readbytes == (size_t) -1)
81
my_error(EE_READ, MYF(ME_BELL+ME_WAITTANG),
82
my_filename(Filedes),my_errno);
83
else if (MyFlags & (MY_NABP | MY_FNABP))
84
my_error(EE_EOFERR, MYF(ME_BELL+ME_WAITTANG),
85
my_filename(Filedes),my_errno);
87
if (readbytes == (size_t) -1 || (MyFlags & (MY_FNABP | MY_NABP)))
88
DBUG_RETURN(MY_FILE_ERROR); /* Return with error */
90
if (MyFlags & (MY_NABP | MY_FNABP))
91
DBUG_RETURN(0); /* Read went ok; Return 0 */
92
DBUG_RETURN(readbytes); /* purecov: inspected */
98
Write a chunk of bytes to a file at a given position
102
Filedes File decsriptor
103
Buffer Buffer to write data from
104
Count Number of bytes to write
105
offset Position to write to
109
This differs from the normal pwrite() call in that we don't care
110
to set the position in the file back to the original position
111
if the system doesn't support pwrite()
115
# Number of bytes read
118
size_t my_pwrite(int Filedes, const uchar *Buffer, size_t Count,
119
my_off_t offset, myf MyFlags)
121
size_t writenbytes, written;
123
DBUG_ENTER("my_pwrite");
124
DBUG_PRINT("my",("Fd: %d Seek: %lu Buffer: 0x%lx Count: %u MyFlags: %d",
125
Filedes, (ulong) offset, (long) Buffer, (uint) Count,
134
writenbytes= (size_t) -1;
135
pthread_mutex_lock(&my_file_info[Filedes].mutex);
136
error= (lseek(Filedes, offset, MY_SEEK_SET) != (my_off_t) -1 &&
137
(writenbytes = write(Filedes, Buffer, Count)) == Count);
138
pthread_mutex_unlock(&my_file_info[Filedes].mutex);
142
if ((writenbytes= pwrite(Filedes, Buffer, Count,offset)) == Count)
146
if (writenbytes != (size_t) -1)
148
written+=writenbytes;
153
DBUG_PRINT("error",("Write only %u bytes", (uint) writenbytes));
154
#ifndef NO_BACKGROUND
156
if (my_thread_var->abort)
157
MyFlags&= ~ MY_WAIT_IF_FULL; /* End if aborted by user */
159
if ((my_errno == ENOSPC || my_errno == EDQUOT) &&
160
(MyFlags & MY_WAIT_IF_FULL))
162
if (!(errors++ % MY_WAIT_GIVE_USER_A_MESSAGE))
163
my_error(EE_DISK_FULL,MYF(ME_BELL | ME_NOREFRESH),
164
my_filename(Filedes),my_errno,MY_WAIT_FOR_USER_TO_FIX_PANIC);
165
VOID(sleep(MY_WAIT_FOR_USER_TO_FIX_PANIC));
168
if ((writenbytes && writenbytes != (size_t) -1) || my_errno == EINTR)
169
continue; /* Retry */
171
if (MyFlags & (MY_NABP | MY_FNABP))
173
if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
175
my_error(EE_WRITE, MYF(ME_BELL | ME_WAITTANG),
176
my_filename(Filedes),my_errno);
178
DBUG_RETURN(MY_FILE_ERROR); /* Error on read */
181
break; /* Return bytes written */
183
DBUG_EXECUTE_IF("check", my_seek(Filedes, -1, SEEK_SET, MYF(0)););
184
if (MyFlags & (MY_NABP | MY_FNABP))
185
DBUG_RETURN(0); /* Want only errors */
186
DBUG_RETURN(writenbytes+written); /* purecov: inspected */