MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_write.c
1 /* Copyright (c) 2000, 2013, 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 #include "mysys_priv.h"
17 #include "mysys_err.h"
18 #include <errno.h>
19 
20 
21  /* Write a chunk of bytes to a file */
22 
23 size_t my_write(File Filedes, const uchar *Buffer, size_t Count, myf MyFlags)
24 {
25  size_t writtenbytes, written;
26  uint errors;
27  DBUG_ENTER("my_write");
28  DBUG_PRINT("my",("fd: %d Buffer: %p Count: %lu MyFlags: %d",
29  Filedes, Buffer, (ulong) Count, MyFlags));
30  errors= 0; written= 0;
31 
32  /* The behavior of write(fd, buf, 0) is not portable */
33  if (unlikely(!Count))
34  DBUG_RETURN(0);
35 
36  DBUG_EXECUTE_IF ("simulate_no_free_space_error",
37  { DBUG_SET("+d,simulate_file_write_error");});
38  for (;;)
39  {
40 #ifdef _WIN32
41  writtenbytes= my_win_write(Filedes, Buffer, Count);
42 #else
43  writtenbytes= write(Filedes, Buffer, Count);
44 #endif
45  DBUG_EXECUTE_IF("simulate_file_write_error",
46  {
47  errno= ENOSPC;
48  writtenbytes= (size_t) -1;
49  });
50  if (writtenbytes == Count)
51  break;
52  if (writtenbytes != (size_t) -1)
53  { /* Safeguard */
54  written+= writtenbytes;
55  Buffer+= writtenbytes;
56  Count-= writtenbytes;
57  }
58  my_errno= errno;
59  DBUG_PRINT("error",("Write only %ld bytes, error: %d",
60  (long) writtenbytes, my_errno));
61 #ifndef NO_BACKGROUND
62  if (my_thread_var->abort)
63  MyFlags&= ~ MY_WAIT_IF_FULL; /* End if aborted by user */
64 
65  if ((my_errno == ENOSPC || my_errno == EDQUOT) &&
66  (MyFlags & MY_WAIT_IF_FULL))
67  {
68  wait_for_free_space(my_filename(Filedes), errors);
69  errors++;
70  DBUG_EXECUTE_IF("simulate_no_free_space_error",
71  { DBUG_SET("-d,simulate_file_write_error");});
72  continue;
73  }
74 
75  if ((writtenbytes == 0 || writtenbytes == (size_t) -1))
76  {
77  if (my_errno == EINTR)
78  {
79  DBUG_PRINT("debug", ("my_write() was interrupted and returned %ld",
80  (long) writtenbytes));
81  continue; /* Interrupted */
82  }
83 
84  if (!writtenbytes && !errors++) /* Retry once */
85  {
86  /* We may come here if the file quota is exeeded */
87  errno= EFBIG; /* Assume this is the error */
88  continue;
89  }
90  }
91  else
92  continue; /* Retry */
93 #endif
94  if (MyFlags & (MY_NABP | MY_FNABP))
95  {
96  if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
97  {
98  char errbuf[MYSYS_STRERROR_SIZE];
99  my_error(EE_WRITE, MYF(ME_BELL+ME_WAITTANG), my_filename(Filedes),
100  my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno));
101  }
102  DBUG_RETURN(MY_FILE_ERROR); /* Error on read */
103  }
104  else
105  break; /* Return bytes written */
106  }
107  if (MyFlags & (MY_NABP | MY_FNABP))
108  DBUG_RETURN(0); /* Want only errors */
109  DBUG_RETURN(writtenbytes+written);
110 } /* my_write */