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-1301 USA */
15
16#include "mysys_priv.h"
17#include <my_dir.h>
18#include "mysys_err.h"
19#include "m_string.h"
20#undef my_rename
21
22 /* On unix rename deletes to file if it exists */
23
24int my_rename(const char *from, const char *to, myf MyFlags)
25{
26 int error = 0;
27 DBUG_ENTER("my_rename");
28 DBUG_PRINT("my",("from %s to %s MyFlags %lu", from, to, MyFlags));
29
30#if defined(__WIN__)
31 if (!MoveFileEx(from, to, MOVEFILE_COPY_ALLOWED |
32 MOVEFILE_REPLACE_EXISTING))
33 {
34 my_osmaperr(GetLastError());
35#elif defined(HAVE_RENAME)
36 if (rename(from,to))
37 {
38#else
39 if (link(from, to) || unlink(from))
40 {
41#endif
42 my_errno=errno;
43 error = -1;
44 if (MyFlags & (MY_FAE+MY_WME))
45 my_error(EE_LINK, MYF(ME_BELL+ME_WAITTANG),from,to,my_errno);
46 }
47 else if (MyFlags & MY_SYNC_DIR)
48 {
49#ifdef NEED_EXPLICIT_SYNC_DIR
50 /* do only the needed amount of syncs: */
51 char dir_from[FN_REFLEN], dir_to[FN_REFLEN];
52 size_t dir_from_length, dir_to_length;
53 dirname_part(dir_from, from, &dir_from_length);
54 dirname_part(dir_to, to, &dir_to_length);
55 if (my_sync_dir(dir_from, MyFlags) ||
56 (strcmp(dir_from, dir_to) &&
57 my_sync_dir(dir_to, MyFlags)))
58 error= -1;
59#endif
60 }
61 DBUG_RETURN(error);
62} /* my_rename */
63