1 /* Copyright (c) 2017, 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_fallocator.cc
30 */
31
32 #include "my_config.h"
33
34 #include <errno.h>
35 #ifdef HAVE_POSIX_FALLOCATE
36 #include <fcntl.h>
37 #endif
38 #include <string.h>
39 #include <sys/types.h>
40 #include <limits>
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44
45 #include "my_dbug.h"
46 #include "my_inttypes.h"
47 #include "my_io.h"
48 #include "my_sys.h"
49 #include "my_thread_local.h"
50 #include "mysys_err.h"
51 #ifdef _WIN32
52 #include "mysys/mysys_priv.h"
53 #endif
54
55 /** Change size of the specified file. Forces the OS to reserve disk space for
56 the file, even when called to fill with zeros. The function also changes the
57 file position. Usually it points to the end of the file after execution.
58
59 @note Implementation based on, and mostly copied from, my_chsize. But instead
60 of ftruncate uses posix_fallocate. This implementation was needed because of
61 allocation in temptable. Probably this implementation should replace my_chsize
62 implementation.
63
64 @param[in] fd File descriptor
65 @param[in] newlength New file size
66 @param[in] filler Fill up all bytes after new_length with this character
67 @param[in] MyFlags Flags
68 @return 0 if OK, 1 otherwise
69 */
my_fallocator(File fd,my_off_t newlength,int filler,myf MyFlags)70 int my_fallocator(File fd, my_off_t newlength, int filler, myf MyFlags) {
71 my_off_t oldsize;
72 uchar buff[IO_SIZE];
73 DBUG_TRACE;
74
75 if ((oldsize = my_seek(fd, 0L, MY_SEEK_END, MYF(MY_WME + MY_FAE))) ==
76 newlength)
77 return 0;
78
79 if (oldsize > newlength) {
80 #ifdef _WIN32
81 if (my_win_chsize(fd, newlength)) {
82 set_my_errno(errno);
83 goto err;
84 }
85 return 0;
86 #elif defined(HAVE_POSIX_FALLOCATE)
87 if (posix_fallocate(fd, 0, newlength) != 0) {
88 set_my_errno(errno);
89 goto err;
90 }
91 return 0;
92 #else
93 /*
94 Fill space between requested length and true length with 'filler'
95 We should never come here on any modern machine
96 */
97 if (my_seek(fd, newlength, MY_SEEK_SET, MYF(MY_WME + MY_FAE)) ==
98 MY_FILEPOS_ERROR) {
99 goto err;
100 }
101 std::swap(newlength, oldsize);
102 #endif // _WIN32
103 }
104
105 /* Full file with 'filler' until it's as big as requested */
106 memset(buff, filler, IO_SIZE);
107 while (newlength - oldsize > IO_SIZE) {
108 if (my_write(fd, buff, IO_SIZE, MYF(MY_NABP))) goto err;
109 oldsize += IO_SIZE;
110 }
111
112 if (my_write(fd, buff, static_cast<size_t>(newlength - oldsize),
113 MYF(MY_NABP)))
114 goto err;
115 return 0;
116
117 err:
118 if (MyFlags & MY_WME) {
119 MyOsError(my_errno(), EE_CANT_CHSIZE, MYF(0));
120 }
121 return 1;
122 }
123