| 1 | /* Copyright (c) 2000, 2010, 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-1335 USA */ |
| 15 | |
| 16 | #include "mysys_priv.h"cxx . . $ `<file>/mysys/mysys_priv.h`/ |
| 17 | #include <my_dir.h>cxx . . $ `<file>/include/my_dir.h`/ |
| 18 | #include "mysys_err.h"cxx . . $ `<file>/include/mysys_err.h`/ |
| 19 | #include "m_string.h"cxx . . $ `<file>/include/m_string.h`/ |
| 20 | #undef my_rename |
| 21 | |
| 22 | |
| 23 | #ifdef _WIN32 |
| 24 | |
| 25 | #define RENAME_MAX_RETRIES 50 |
| 26 | |
| 27 | /* |
| 28 | On Windows, bad 3rd party programs (backup or anitivirus, or something else) |
| 29 | can have file open with a sharing mode incompatible with renaming, i.e they |
| 30 | won't use FILE_SHARE_DELETE when opening file. |
| 31 | |
| 32 | The following function will do a couple of retries, in case MoveFileEx returns |
| 33 | ERROR_SHARING_VIOLATION. |
| 34 | */ |
| 35 | static BOOL win_rename_with_retries(const char *from, const char *to) |
| 36 | { |
| 37 | DBUG_INJECT_FILE_SHARING_VIOLATION(from); |
| 38 | |
| 39 | for (int retry= FILE_SHARING_VIOLATION_RETRIES; retry--;) |
| 40 | { |
| 41 | BOOL ret= MoveFileEx(from, to, |
| 42 | MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING); |
| 43 | |
| 44 | DBUG_CLEAR_FILE_SHARING_VIOLATION(); |
| 45 | |
| 46 | if (ret) |
| 47 | return ret; |
| 48 | |
| 49 | DWORD last_error= GetLastError(); |
| 50 | |
| 51 | if (last_error == ERROR_SHARING_VIOLATION || |
| 52 | last_error == ERROR_ACCESS_DENIED) |
| 53 | { |
| 54 | Sleep(FILE_SHARING_VIOLATION_DELAY_MS); |
| 55 | } |
| 56 | else |
| 57 | return ret; |
| 58 | } |
| 59 | return FALSE; |
| 60 | } |
| 61 | #endif |
| 62 | |
| 63 | /* On unix rename deletes to file if it exists */ |
| 64 | int my_renamecxx . . $ my_rename(fd278af2f295fa2e).(const char *from, const char *to, myfcxx . . $ myf# MyFlags) |
| 65 | { |
| 66 | int error = 0; |
| 67 | DBUG_ENTERcxx . . $ `include/my_dbug.h:165:9`!("my_rename"); |
| 68 | DBUG_PRINTcxx . . $ `include/my_dbug.h:174:9`!("my",("from %s to %s MyFlags %lu", from, to, MyFlags)); |
| 69 | |
| 70 | #if defined(_WIN32) |
| 71 | if (!win_rename_with_retries(from, to)) |
| 72 | { |
| 73 | my_osmaperr(GetLastError()); |
| 74 | #elif defined(HAVE_RENAMEcxx . . $ `/build/tmp.NxIY9EeyJN/package/build/include/my_config.h:212:9`!) |
| 75 | if (renamecxx . . $ rename(5ae29d726ae57f88).(from,to)) |
| 76 | { |
| 77 | #else |
| 78 | if (link(from, to) || unlink(from)) |
| 79 | { |
| 80 | #endif |
| 81 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`! && !accesscxx . . $ access(cb7559238bb26814).(from, F_OKcxx . . $ `<external>/12628407106812493757/unistd.h:284:9`!)) |
| 82 | my_errnocxx . . $ `include/my_pthread.h:720:9`!= ENOTDIRcxx . . $ `<external>/8987895939074593727/errno-base.h:24:9`!; |
| 83 | else |
| 84 | my_errnocxx . . $ `include/my_pthread.h:720:9`!= errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 85 | error = -1; |
| 86 | if (MyFlags & (MY_FAEcxx . . $ `include/my_sys.h:56:9`!+MY_WMEcxx . . $ `include/my_sys.h:57:9`!)) |
| 87 | my_errorcxx . . $ my_error(21e90ace5e448e7a).(EE_LINKcxx . . $ `include/mysys_err.h:44:9`!, MYFcxx . . $ `include/my_global.h:986:9`!(ME_BELLcxx . . $ `include/my_sys.h:105:9`!),from,to,my_errnocxx . . $ `include/my_pthread.h:720:9`!); |
| 88 | } |
| 89 | else if (MyFlags & MY_SYNC_DIRcxx . . $ `include/my_sys.h:93:9`!) |
| 90 | { |
| 91 | #ifdef NEED_EXPLICIT_SYNC_DIRcxx . . $ `include/my_global.h:1117:9`! |
| 92 | /* do only the needed amount of syncs: */ |
| 93 | char dir_from[FN_REFLENcxx . . $ `include/my_global.h:603:9`!], dir_to[FN_REFLENcxx . . $ `include/my_global.h:603:9`!]; |
| 94 | size_t dir_from_length, dir_to_length; |
| 95 | dirname_partcxx . . $ dirname_part(b0ced3dc79a746a2).(dir_from, from, &dir_from_length); |
| 96 | dirname_partcxx . . $ dirname_part(b0ced3dc79a746a2).(dir_to, to, &dir_to_length); |
| 97 | if (my_sync_dircxx . . $ my_sync_dir(54dd70b47e85a2fc).(dir_from, MyFlags) || |
| 98 | (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(dir_from, dir_to) && |
| 99 | my_sync_dircxx . . $ my_sync_dir(54dd70b47e85a2fc).(dir_to, MyFlags))) |
| 100 | error= -1; |
| 101 | #endif |
| 102 | } |
| 103 | DBUG_RETURNcxx . . $ `include/my_dbug.h:168:9`!(error); |
| 104 | } /* my_rename */ |