MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_rename.c
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 
24 int 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 %d", from, to, MyFlags));
29 
30 #if defined(HAVE_FILE_VERSIONS)
31  { /* Check that there isn't a old file */
32  int save_errno;
33  MY_STAT my_stat_result;
34  save_errno=my_errno;
35  if (my_stat(to,&my_stat_result,MYF(0)))
36  {
37  my_errno=EEXIST;
38  error= -1;
39  if (MyFlags & MY_FAE+MY_WME)
40  {
41  char errbuf[MYSYS_STRERROR_SIZE];
42  my_error(EE_LINK, MYF(ME_BELL+ME_WAITTANG), from, to,
43  my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno));
44  }
45  DBUG_RETURN(error);
46  }
47  my_errno=save_errno;
48  }
49 #endif
50 #if defined(__WIN__)
51  if(!MoveFileEx(from, to, MOVEFILE_COPY_ALLOWED|
52  MOVEFILE_REPLACE_EXISTING))
53  {
54  my_osmaperr(GetLastError());
55 #else
56  if (rename(from,to))
57  {
58 #endif
59  my_errno=errno;
60  error = -1;
61  if (MyFlags & (MY_FAE+MY_WME))
62  {
63  char errbuf[MYSYS_STRERROR_SIZE];
64  my_error(EE_LINK, MYF(ME_BELL+ME_WAITTANG), from, to,
65  my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno));
66  }
67  }
68  else if (MyFlags & MY_SYNC_DIR)
69  {
70 #ifdef NEED_EXPLICIT_SYNC_DIR
71  /* do only the needed amount of syncs: */
72  char dir_from[FN_REFLEN], dir_to[FN_REFLEN];
73  size_t dir_from_length, dir_to_length;
74  dirname_part(dir_from, from, &dir_from_length);
75  dirname_part(dir_to, to, &dir_to_length);
76  if (my_sync_dir(dir_from, MyFlags) ||
77  (strcmp(dir_from, dir_to) &&
78  my_sync_dir(dir_to, MyFlags)))
79  error= -1;
80 #endif
81  }
82  DBUG_RETURN(error);
83 } /* my_rename */