| 1 | /* Copyright (c) 2000, 2026, 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 designed to work 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 either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <errno.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <time.h> |
| 28 | |
| 29 | #ifdef HAVE_UNISTD_H |
| 30 | #include <unistd.h> |
| 31 | #endif |
| 32 | |
| 33 | #ifndef _WIN32 |
| 34 | #include <utime.h> |
| 35 | #else |
| 36 | #include <sys/utime.h> |
| 37 | #endif |
| 38 | |
| 39 | #include "my_config.h" |
| 40 | |
| 41 | #include "m_string.h" |
| 42 | #include "my_dbug.h" |
| 43 | #include "my_dir.h" |
| 44 | #include "my_inttypes.h" |
| 45 | #include "my_io.h" |
| 46 | #include "my_sys.h" |
| 47 | #include "my_systime.h" // get_date |
| 48 | #include "my_thread_local.h" |
| 49 | #include "mysys_err.h" |
| 50 | #include "storage/myisam/myisam_sys.h" |
| 51 | |
| 52 | /* |
| 53 | Rename with copy stat form old file |
| 54 | Copy stats from old file to new file, deletes original and |
| 55 | changes new file name to old file name |
| 56 | |
| 57 | if MY_REDEL_MAKE_COPY is given, then the original file |
| 58 | is renamed to org_name-'current_time'.BAK |
| 59 | |
| 60 | if MY_REDEL_NO_COPY_STAT is given, stats are not copied |
| 61 | from org_name to tmp_name. |
| 62 | */ |
| 63 | |
| 64 | #define REDEL_EXT ".BAK" |
| 65 | |
| 66 | int my_redel(const char *org_name, const char *tmp_name, myf MyFlags) { |
| 67 | int error = 1; |
| 68 | DBUG_TRACE; |
| 69 | DBUG_PRINT("my", ("org_name: '%s' tmp_name: '%s' MyFlags: %d", org_name, |
| 70 | tmp_name, MyFlags)); |
| 71 | |
| 72 | if (!(MyFlags & MY_REDEL_NO_COPY_STAT)) { |
| 73 | if (my_copystat(org_name, tmp_name, MyFlags) < 0) goto end; |
| 74 | } |
| 75 | if (MyFlags & MY_REDEL_MAKE_BACKUP) { |
| 76 | char name_buff[FN_REFLEN + 20]; |
| 77 | char ext[20]; |
| 78 | ext[0] = '-'; |
| 79 | get_date(ext + 1, 2 + 4, (time_t)0); |
| 80 | my_stpcpy(strend(ext), REDEL_EXT); |
| 81 | if (my_rename(org_name, fn_format(name_buff, org_name, "", ext, 2), |
| 82 | MyFlags)) |
| 83 | goto end; |
| 84 | } else if (my_delete_allow_opened(org_name, MyFlags)) |
| 85 | goto end; |
| 86 | if (my_rename(tmp_name, org_name, MyFlags)) goto end; |
| 87 | |
| 88 | error = 0; |
| 89 | end: |
| 90 | return error; |
| 91 | } /* my_redel */ |
| 92 | |
| 93 | /* Copy stat from one file to another */ |
| 94 | /* Return -1 if can't get stat, 1 if wrong type of file */ |
| 95 | |
| 96 | int my_copystat(const char *from, const char *to, int MyFlags) { |
| 97 | MY_STAT statbuf; |
| 98 | |
| 99 | if (my_stat(from, &statbuf, MyFlags) == nullptr) |
| 100 | return -1; /* Can't get stat on input file */ |
| 101 | |
| 102 | if ((statbuf.st_mode & S_IFMT) != S_IFREG) return 1; |
| 103 | |
| 104 | /* Copy modes */ |
| 105 | if (chmod(to, statbuf.st_mode & 07777)) { |
| 106 | set_my_errno(errno); |
| 107 | if (MyFlags & (MY_FAE + MY_WME)) { |
| 108 | char errbuf[MYSYS_STRERROR_SIZE]; |
| 109 | my_error(EE_CHANGE_PERMISSIONS, MYF(0), from, errno, |
| 110 | my_strerror(errbuf, sizeof(errbuf), errno)); |
| 111 | } |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | #if !defined(_WIN32) |
| 116 | if (statbuf.st_nlink > 1 && MyFlags & MY_LINK_WARNING) { |
| 117 | if (MyFlags & MY_LINK_WARNING) |
| 118 | my_error(EE_LINK_WARNING, MYF(0), from, statbuf.st_nlink); |
| 119 | } |
| 120 | /* Copy ownership */ |
| 121 | if (chown(to, statbuf.st_uid, statbuf.st_gid)) { |
| 122 | set_my_errno(errno); |
| 123 | if (MyFlags & (MY_FAE + MY_WME)) { |
| 124 | char errbuf[MYSYS_STRERROR_SIZE]; |
| 125 | my_error(EE_CHANGE_OWNERSHIP, MYF(0), from, errno, |
| 126 | my_strerror(errbuf, sizeof(errbuf), errno)); |
| 127 | } |
| 128 | return -1; |
| 129 | } |
| 130 | #endif /* !_WIN32 */ |
| 131 | |
| 132 | if (MyFlags & MY_COPYTIME) { |
| 133 | struct utimbuf timep; |
| 134 | timep.actime = statbuf.st_atime; |
| 135 | timep.modtime = statbuf.st_mtime; |
| 136 | (void)utime(to, &timep); /* Update last accessed and modified times */ |
| 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } /* my_copystat */ |