1 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    Without limiting anything contained in the foregoing, this file,
15    which is part of C Driver for MySQL (Connector/C), is also subject to the
16    Universal FOSS Exception, version 1.0, a copy of which can be found at
17    http://oss.oracle.com/licenses/universal-foss-exception.
18 
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License, version 2.0, for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
27 
28 #include "mysys_priv.h"
29 #include "my_sys.h"
30 #include "mysys_err.h"
31 #include <errno.h>
32 #include "my_thread_local.h"
33 
34 
35 /**
36   Write a chunk of bytes to a file
37 
38   if (MyFlags & (MY_NABP | MY_FNABP))
39   @returns
40     0  if Count == 0
41     On succes, 0
42     On failure, (size_t)-1 == MY_FILE_ERROR
43 
44   otherwise
45   @returns
46     0  if Count == 0
47     On success, the number of bytes written.
48     On partial success (if less than Count bytes could be written),
49        the actual number of bytes written.
50     On failure, (size_t)-1 == MY_FILE_ERROR
51 */
my_write(File Filedes,const uchar * Buffer,size_t Count,myf MyFlags)52 size_t my_write(File Filedes, const uchar *Buffer, size_t Count, myf MyFlags)
53 {
54   size_t writtenbytes;
55   size_t sum_written= 0;
56   uint errors= 0;
57   const size_t initial_count= Count;
58 
59   DBUG_ENTER("my_write");
60   DBUG_PRINT("my",("fd: %d  Buffer: %p  Count: %lu  MyFlags: %d",
61 		   Filedes, Buffer, (ulong) Count, MyFlags));
62 
63   /* The behavior of write(fd, buf, 0) is not portable */
64   if (unlikely(!Count))
65     DBUG_RETURN(0);
66 
67   DBUG_EXECUTE_IF ("simulate_no_free_space_error",
68                    { DBUG_SET("+d,simulate_file_write_error");});
69   for (;;)
70   {
71     errno= 0;
72 #ifdef _WIN32
73     writtenbytes= my_win_write(Filedes, Buffer, Count);
74 #else
75     writtenbytes= write(Filedes, Buffer, Count);
76 #endif
77     DBUG_EXECUTE_IF("simulate_file_write_error",
78                     {
79                       errno= ENOSPC;
80                       writtenbytes= (size_t) -1;
81                     });
82     if (writtenbytes == Count)
83     {
84       sum_written+= writtenbytes;
85       break;
86     }
87     if (writtenbytes != (size_t) -1)
88     {						/* Safeguard */
89       sum_written+= writtenbytes;
90       Buffer+= writtenbytes;
91       Count-= writtenbytes;
92     }
93     set_my_errno(errno);
94     DBUG_PRINT("error",("Write only %ld bytes, error: %d",
95 			(long) writtenbytes, my_errno()));
96     if (is_killed_hook(NULL))
97       MyFlags&= ~ MY_WAIT_IF_FULL;		/* End if aborted by user */
98 
99     if ((my_errno() == ENOSPC || my_errno() == EDQUOT) &&
100         (MyFlags & MY_WAIT_IF_FULL))
101     {
102       wait_for_free_space(my_filename(Filedes), errors);
103       errors++;
104       DBUG_EXECUTE_IF("simulate_no_free_space_error",
105                       { DBUG_SET("-d,simulate_file_write_error");});
106       continue;
107     }
108 
109     if (writtenbytes != 0 && writtenbytes != (size_t) -1)
110       continue;                                 /* Retry if something written */
111     else if (my_errno() == EINTR)
112     {
113       DBUG_PRINT("debug", ("my_write() was interrupted and returned %ld",
114                            (long) writtenbytes));
115       continue;                                 /* Interrupted, retry */
116     }
117     else if (writtenbytes == 0 && !errors++)    /* Retry once */
118     {
119       /* We may come here if the file quota is exeeded */
120       continue;
121     }
122     break;
123   }
124   if (MyFlags & (MY_NABP | MY_FNABP))
125   {
126     if (sum_written == initial_count)
127       DBUG_RETURN(0);        /* Want only errors, not bytes written */
128     if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
129     {
130       char errbuf[MYSYS_STRERROR_SIZE];
131       my_error(EE_WRITE, MYF(0), my_filename(Filedes),
132                my_errno(), my_strerror(errbuf, sizeof(errbuf), my_errno()));
133     }
134     DBUG_RETURN(MY_FILE_ERROR);
135   }
136 
137   if (sum_written == 0)
138     DBUG_RETURN(MY_FILE_ERROR);
139 
140   DBUG_RETURN(sum_written);
141 } /* my_write */
142