1/* Copyright (c) 2000, 2016, Oracle and/or its affiliates.
2 Copyright (c) 2011, 2016, MariaDB
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
17#include "mysys_priv.h"
18#include "mysys_err.h"
19#include <errno.h>
20
21
22 /* Write a chunk of bytes to a file */
23
24size_t my_write(File Filedes, const uchar *Buffer, size_t Count, myf MyFlags)
25{
26 size_t writtenbytes, written;
27 uint errors;
28 DBUG_ENTER("my_write");
29 DBUG_PRINT("my",("fd: %d Buffer: %p Count: %lu MyFlags: %lu",
30 Filedes, Buffer, (ulong) Count, MyFlags));
31 errors= 0; written= 0;
32 if (!(MyFlags & (MY_WME | MY_FAE | MY_FNABP)))
33 MyFlags|= my_global_flags;
34
35 /* The behavior of write(fd, buf, 0) is not portable */
36 if (unlikely(!Count))
37 DBUG_RETURN(0);
38
39 for (;;)
40 {
41#ifdef _WIN32
42 if(Filedes < 0)
43 {
44 my_errno= errno= EBADF;
45 DBUG_RETURN((size_t)-1);
46 }
47 writtenbytes= my_win_write(Filedes, Buffer, Count);
48#else
49 writtenbytes= write(Filedes, Buffer, Count);
50#endif
51
52 /**
53 To simulate the write error set the errno = error code
54 and the number pf written bytes to -1.
55 */
56 DBUG_EXECUTE_IF ("simulate_file_write_error",
57 if (!errors) {
58 errno= ENOSPC;
59 writtenbytes= (size_t) -1;
60 });
61
62 if (writtenbytes == Count)
63 break;
64 if (writtenbytes != (size_t) -1)
65 { /* Safeguard */
66 written+= writtenbytes;
67 Buffer+= writtenbytes;
68 Count-= writtenbytes;
69 }
70 my_errno= errno;
71 DBUG_PRINT("error",("Write only %ld bytes, error: %d",
72 (long) writtenbytes, my_errno));
73#ifndef NO_BACKGROUND
74 if (my_thread_var->abort)
75 MyFlags&= ~ MY_WAIT_IF_FULL; /* End if aborted by user */
76
77 if ((my_errno == ENOSPC || my_errno == EDQUOT) &&
78 (MyFlags & MY_WAIT_IF_FULL))
79 {
80 wait_for_free_space(my_filename(Filedes), errors);
81 errors++;
82 continue;
83 }
84
85 if ((writtenbytes == 0 || writtenbytes == (size_t) -1))
86 {
87 if (my_errno == EINTR)
88 {
89 DBUG_PRINT("debug", ("my_write() was interrupted and returned %ld",
90 (long) writtenbytes));
91 continue; /* Interrupted */
92 }
93
94 if (!writtenbytes && !errors++) /* Retry once */
95 {
96 /* We may come here if the file quota is exeeded */
97 errno= EFBIG; /* Assume this is the error */
98 continue;
99 }
100 }
101 else
102 continue; /* Retry */
103#endif
104
105 /* Don't give a warning if it's ok that we only write part of the data */
106 if (MyFlags & (MY_NABP | MY_FNABP))
107 {
108 if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
109 {
110 my_error(EE_WRITE, MYF(ME_BELL | ME_WAITTANG | (MyFlags & (ME_JUST_INFO | ME_NOREFRESH))),
111 my_filename(Filedes),my_errno);
112 }
113 DBUG_RETURN(MY_FILE_ERROR); /* Error on read */
114 }
115 break; /* Return bytes written */
116 }
117 if (MyFlags & (MY_NABP | MY_FNABP))
118 DBUG_RETURN(0); /* Want only errors */
119 DBUG_RETURN(writtenbytes+written);
120} /* my_write */
121