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