1 /* Copyright (c) 2000, 2019, 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, 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 /**
29 @file mysys/my_write.cc
30 */
31
32 #include "my_config.h"
33
34 #include <errno.h>
35 #include <stddef.h>
36 #include <sys/types.h>
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #include "my_compiler.h"
42 #include "my_dbug.h"
43 #include "my_inttypes.h"
44 #include "my_io.h"
45 #include "my_sys.h"
46 #include "my_thread_local.h"
47 #include "mysys_err.h"
48 #if defined(_WIN32)
49 #include "mysys/mysys_priv.h"
50 #endif
51
52 #include <algorithm>
53
54 extern PSI_stage_info stage_waiting_for_disk_space;
55
56 #ifndef _WIN32
57 // Mock away write() for unit testing.
58 ssize_t (*mock_write)(int fd, const void *buf, size_t count) = nullptr;
59 #endif
60
61 /**
62 Write a chunk of bytes to a file
63
64 if (MyFlags & (MY_NABP | MY_FNABP))
65 returns
66 0 if Count == 0
67 On succes, 0
68 On failure, (size_t)-1 == MY_FILE_ERROR
69
70 otherwise
71 returns
72 0 if Count == 0
73 On success, the number of bytes written.
74 On partial success (if less than Count bytes could be written),
75 the actual number of bytes written.
76 On failure, (size_t)-1 == MY_FILE_ERROR
77 */
my_write(File Filedes,const uchar * Buffer,size_t Count,myf MyFlags)78 size_t my_write(File Filedes, const uchar *Buffer, size_t Count, myf MyFlags) {
79 int64_t sum_written = 0;
80 uint errors = 0;
81 const size_t initial_count = Count;
82 size_t ToWriteCount;
83
84 DBUG_TRACE;
85
86 /* The behavior of write(fd, buf, 0) is not portable */
87 if (unlikely(!Count)) return 0;
88
89 DBUG_EXECUTE_IF("simulate_no_free_space_error",
90 { DBUG_SET("+d,simulate_file_write_error"); });
91 for (;;) {
92 errno = 0;
93
94 ToWriteCount = Count;
95 DBUG_EXECUTE_IF("force_wait_for_disk_space", { ToWriteCount = 1; });
96 DBUG_EXECUTE_IF("simulate_io_thd_wait_for_disk_space", {
97 if (strstr(my_filename(Filedes), "slave-relay-bin.0")) ToWriteCount = 1;
98 });
99 DBUG_EXECUTE_IF("simulate_random_io_thd_wait_for_disk_space", {
100 if (strstr(my_filename(Filedes), "slave-relay-bin.0")) {
101 if (rand() % 3 == 0)
102 ToWriteCount = Count;
103 else
104 ToWriteCount = std::min(Count, 1 + (Count * (rand() % 100) / 100));
105 }
106 });
107
108 int64_t writtenbytes =
109 #ifdef _WIN32
110 my_win_write(Filedes, Buffer, ToWriteCount);
111 #else
112 (mock_write ? mock_write(Filedes, Buffer, ToWriteCount)
113 : write(Filedes, Buffer, ToWriteCount));
114 #endif
115 DBUG_EXECUTE_IF("simulate_file_write_error", {
116 errno = ENOSPC;
117 writtenbytes = -1;
118 });
119 DBUG_EXECUTE_IF("force_wait_for_disk_space", { errno = ENOSPC; });
120 DBUG_EXECUTE_IF("simulate_io_thd_wait_for_disk_space", {
121 if (strstr(my_filename(Filedes), "slave-relay-bin.0")) errno = ENOSPC;
122 });
123 DBUG_EXECUTE_IF("simulate_random_io_thd_wait_for_disk_space", {
124 if (strstr(my_filename(Filedes), "slave-relay-bin.0") &&
125 ToWriteCount != Count)
126 errno = ENOSPC;
127 });
128 if (writtenbytes == static_cast<int64_t>(Count)) {
129 sum_written += writtenbytes;
130 break;
131 }
132 if (writtenbytes != -1) { /* Safeguard */
133 sum_written += writtenbytes;
134 Buffer += writtenbytes;
135 Count -= writtenbytes;
136 }
137 set_my_errno(errno);
138 if (is_killed_hook(nullptr))
139 MyFlags &= ~MY_WAIT_IF_FULL; /* End if aborted by user */
140
141 if ((my_errno() == ENOSPC || my_errno() == EDQUOT) &&
142 (MyFlags & MY_WAIT_IF_FULL)) {
143 PSI_stage_info old_stage;
144 if (MyFlags & MY_REPORT_WAITING_IF_FULL) {
145 set_waiting_for_disk_space_hook(nullptr, true);
146 enter_stage_hook(nullptr, &stage_waiting_for_disk_space, &old_stage,
147 __func__, __FILE__, __LINE__);
148 }
149 wait_for_free_space(my_filename(Filedes), errors);
150 if (MyFlags & MY_REPORT_WAITING_IF_FULL) {
151 enter_stage_hook(nullptr, &old_stage, nullptr, __func__, __FILE__,
152 __LINE__);
153 set_waiting_for_disk_space_hook(nullptr, false);
154 }
155
156 errors++;
157 DBUG_EXECUTE_IF("simulate_no_free_space_error",
158 { DBUG_SET("-d,simulate_file_write_error"); });
159 if (is_killed_hook(nullptr)) break;
160 continue;
161 }
162
163 if (writtenbytes != 0 && writtenbytes != -1 && !is_killed_hook(nullptr))
164 continue; /* Retry if something written */
165
166 if (my_errno() == EINTR) {
167 continue; /* Interrupted, retry */
168 } else if (writtenbytes == 0 && !errors++) /* Retry once */
169 {
170 /* We may come here if the file quota is exeeded */
171 continue;
172 }
173 break;
174 }
175 if (MyFlags & (MY_NABP | MY_FNABP)) {
176 if (sum_written == static_cast<int64_t>(initial_count))
177 return 0; /* Want only errors, not bytes written */
178 if (MyFlags & (MY_WME | MY_FAE | MY_FNABP)) {
179 MyOsError(my_errno(), EE_WRITE, MYF(0), my_filename(Filedes));
180 }
181 return MY_FILE_ERROR;
182 }
183
184 if (sum_written == 0) return MY_FILE_ERROR;
185
186 return sum_written;
187 }
188