~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
	my_error(EE_LINK, MYF(ME_BELL+ME_WAITTANG),from,to,my_errno);
41
      DBUG_RETURN(error);
42
    }
43
    my_errno=save_errno;
44
  }
45
#endif
46
#if defined(HAVE_RENAME)
47
  if (rename(from,to))
48
#else
49
  if (link(from, to) || unlink(from))
50
#endif
51
  {
52
    my_errno=errno;
53
    error = -1;
54
    if (MyFlags & (MY_FAE+MY_WME))
55
      my_error(EE_LINK, MYF(ME_BELL+ME_WAITTANG),from,to,my_errno);
56
  }
57
  else if (MyFlags & MY_SYNC_DIR)
58
  {
59
#ifdef NEED_EXPLICIT_SYNC_DIR
60
    /* do only the needed amount of syncs: */
61
    char dir_from[FN_REFLEN], dir_to[FN_REFLEN];
62
    size_t dir_from_length, dir_to_length;
63
    dirname_part(dir_from, from, &dir_from_length);
64
    dirname_part(dir_to, to, &dir_to_length);
65
    if (my_sync_dir(dir_from, MyFlags) ||
66
        (strcmp(dir_from, dir_to) &&
67
         my_sync_dir(dir_to, MyFlags)))
68
      error= -1;
69
#endif
70
  }
71
  DBUG_RETURN(error);
72
} /* my_rename */