MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_fstream.c
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2 
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.
6 
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.
11 
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 /* USE_MY_STREAM isn't set because we can't thrust my_fclose! */
17 
18 #include "mysys_priv.h"
19 #include "mysys_err.h"
20 #include <errno.h>
21 #include <stdio.h>
22 
23 #ifdef HAVE_FSEEKO
24 #undef ftell
25 #undef fseek
26 #define ftell(A) ftello(A)
27 #define fseek(A,B,C) fseeko((A),(B),(C))
28 #endif
29 
30 /*
31  Read a chunk of bytes from a FILE
32 
33  SYNOPSIS
34  my_fread()
35  stream File descriptor
36  Buffer Buffer to read to
37  Count Number of bytes to read
38  MyFlags Flags on what to do on error
39 
40  RETURN
41  (size_t) -1 Error
42  # Number of bytes read
43  */
44 
45 size_t my_fread(FILE *stream, uchar *Buffer, size_t Count, myf MyFlags)
46 {
47  size_t readbytes;
48  DBUG_ENTER("my_fread");
49  DBUG_PRINT("my",("stream: 0x%lx Buffer: 0x%lx Count: %u MyFlags: %d",
50  (long) stream, (long) Buffer, (uint) Count, MyFlags));
51 
52  if ((readbytes= fread(Buffer, sizeof(char), Count, stream)) != Count)
53  {
54  DBUG_PRINT("error",("Read only %d bytes", (int) readbytes));
55  if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
56  {
57  if (ferror(stream))
58  {
59  char errbuf[MYSYS_STRERROR_SIZE];
60  my_error(EE_READ, MYF(ME_BELL+ME_WAITTANG),
61  my_filename(my_fileno(stream)),
62  errno, my_strerror(errbuf, sizeof(errbuf), errno));
63  }
64  else
65  if (MyFlags & (MY_NABP | MY_FNABP))
66  {
67  char errbuf[MYSYS_STRERROR_SIZE];
68  my_error(EE_EOFERR, MYF(ME_BELL+ME_WAITTANG),
69  my_filename(my_fileno(stream)), errno,
70  my_strerror(errbuf, sizeof(errbuf), errno));
71  }
72  }
73  my_errno=errno ? errno : -1;
74  if (ferror(stream) || MyFlags & (MY_NABP | MY_FNABP))
75  DBUG_RETURN((size_t) -1); /* Return with error */
76  }
77  if (MyFlags & (MY_NABP | MY_FNABP))
78  DBUG_RETURN(0); /* Read ok */
79  DBUG_RETURN(readbytes);
80 } /* my_fread */
81 
82 
83 /*
84  Write a chunk of bytes to a stream
85 
86  my_fwrite()
87  stream File descriptor
88  Buffer Buffer to write from
89  Count Number of bytes to write
90  MyFlags Flags on what to do on error
91 
92  RETURN
93  (size_t) -1 Error
94  # Number of bytes written
95 */
96 
97 size_t my_fwrite(FILE *stream, const uchar *Buffer, size_t Count, myf MyFlags)
98 {
99  size_t writtenbytes =0;
100  my_off_t seekptr;
101 #if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
102  uint errors;
103 #endif
104  DBUG_ENTER("my_fwrite");
105  DBUG_PRINT("my",("stream: 0x%lx Buffer: 0x%lx Count: %u MyFlags: %d",
106  (long) stream, (long) Buffer, (uint) Count, MyFlags));
107 
108 #if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
109  errors=0;
110 #endif
111  seekptr= ftell(stream);
112  for (;;)
113  {
114  size_t written;
115  if ((written = (size_t) fwrite((char*) Buffer,sizeof(char),
116  Count, stream)) != Count)
117  {
118  DBUG_PRINT("error",("Write only %d bytes", (int) writtenbytes));
119  my_errno=errno;
120  if (written != (size_t) -1)
121  {
122  seekptr+=written;
123  Buffer+=written;
124  writtenbytes+=written;
125  Count-=written;
126  }
127 #ifdef EINTR
128  if (errno == EINTR)
129  {
130  (void) my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0));
131  continue;
132  }
133 #endif
134 #if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
135  if (my_thread_var->abort)
136  MyFlags&= ~ MY_WAIT_IF_FULL; /* End if aborted by user */
137 
138  if ((errno == ENOSPC || errno == EDQUOT) &&
139  (MyFlags & MY_WAIT_IF_FULL))
140  {
141  wait_for_free_space("[stream]", errors);
142  errors++;
143  (void) my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0));
144  continue;
145  }
146 #endif
147  if (ferror(stream) || (MyFlags & (MY_NABP | MY_FNABP)))
148  {
149  if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
150  {
151  char errbuf[MYSYS_STRERROR_SIZE];
152  my_error(EE_WRITE, MYF(ME_BELL+ME_WAITTANG),
153  my_filename(my_fileno(stream)),
154  errno, my_strerror(errbuf, sizeof(errbuf), errno));
155  }
156  writtenbytes= (size_t) -1; /* Return that we got error */
157  break;
158  }
159  }
160  if (MyFlags & (MY_NABP | MY_FNABP))
161  writtenbytes= 0; /* Everything OK */
162  else
163  writtenbytes+= written;
164  break;
165  }
166  DBUG_RETURN(writtenbytes);
167 } /* my_fwrite */
168 
169 
170 /* Seek to position in file */
171 
172 my_off_t my_fseek(FILE *stream, my_off_t pos, int whence,
173  myf MyFlags __attribute__((unused)))
174 {
175  DBUG_ENTER("my_fseek");
176  DBUG_PRINT("my",("stream: 0x%lx pos: %lu whence: %d MyFlags: %d",
177  (long) stream, (long) pos, whence, MyFlags));
178  DBUG_RETURN(fseek(stream, (off_t) pos, whence) ?
179  MY_FILEPOS_ERROR : (my_off_t) ftell(stream));
180 } /* my_seek */
181 
182 
183 /* Tell current position of file */
184 
185 my_off_t my_ftell(FILE *stream, myf MyFlags __attribute__((unused)))
186 {
187  off_t pos;
188  DBUG_ENTER("my_ftell");
189  DBUG_PRINT("my",("stream: 0x%lx MyFlags: %d", (long) stream, MyFlags));
190  pos=ftell(stream);
191  DBUG_PRINT("exit",("ftell: %lu",(ulong) pos));
192  DBUG_RETURN((my_off_t) pos);
193 } /* my_ftell */
194 
195 
196 /* Get a File corresponding to the stream*/
197 int my_fileno(FILE *f)
198 {
199 #ifdef _WIN32
200  return my_win_fileno(f);
201 #else
202  return fileno(f);
203 #endif
204 }