~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to mysys/my_redel.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <m_string.h>
 
19
#include "mysys_err.h"
 
20
#if defined(HAVE_UTIME_H)
 
21
#include <utime.h>
 
22
#elif defined(HAVE_SYS_UTIME_H)
 
23
#include <sys/utime.h>
 
24
#elif !defined(HPUX10)
 
25
struct utimbuf {
 
26
  time_t actime;
 
27
  time_t modtime;
 
28
};
 
29
#endif
 
30
 
 
31
        /*
 
32
          Rename with copy stat form old file
 
33
          Copy stats from old file to new file, deletes orginal and
 
34
          changes new file name to old file name
 
35
 
 
36
          if MY_REDEL_MAKE_COPY is given, then the orginal file
 
37
          is renamed to org_name-'current_time'.BAK
 
38
        */
 
39
 
 
40
#define REDEL_EXT ".BAK"
 
41
 
 
42
int my_redel(const char *org_name, const char *tmp_name, myf MyFlags)
 
43
{
 
44
  int error=1;
 
45
  DBUG_ENTER("my_redel");
 
46
  DBUG_PRINT("my",("org_name: '%s' tmp_name: '%s'  MyFlags: %d",
 
47
                   org_name,tmp_name,MyFlags));
 
48
 
 
49
  if (my_copystat(org_name,tmp_name,MyFlags) < 0)
 
50
    goto end;
 
51
  if (MyFlags & MY_REDEL_MAKE_BACKUP)
 
52
  {
 
53
    char name_buff[FN_REFLEN+20];    
 
54
    char ext[20];
 
55
    ext[0]='-';
 
56
    get_date(ext+1,2+4,(time_t) 0);
 
57
    strmov(strend(ext),REDEL_EXT);
 
58
    if (my_rename(org_name, fn_format(name_buff, org_name, "", ext, 2),
 
59
                  MyFlags))
 
60
      goto end;
 
61
  }
 
62
  else if (my_delete_allow_opened(org_name, MyFlags))
 
63
      goto end;
 
64
  if (my_rename(tmp_name,org_name,MyFlags))
 
65
    goto end;
 
66
 
 
67
  error=0;
 
68
end:
 
69
  DBUG_RETURN(error);
 
70
} /* my_redel */
 
71
 
 
72
 
 
73
        /* Copy stat from one file to another */
 
74
        /* Return -1 if can't get stat, 1 if wrong type of file */
 
75
 
 
76
int my_copystat(const char *from, const char *to, int MyFlags)
 
77
{
 
78
  struct stat statbuf;
 
79
#if !defined(__WIN__) && !defined(__NETWARE__)
 
80
  int res;
 
81
#endif
 
82
 
 
83
  if (stat((char*) from, &statbuf))
 
84
  {
 
85
    my_errno=errno;
 
86
    if (MyFlags & (MY_FAE+MY_WME))
 
87
      my_error(EE_STAT, MYF(ME_BELL+ME_WAITTANG),from,errno);
 
88
    return -1;                          /* Can't get stat on input file */
 
89
  }
 
90
  if ((statbuf.st_mode & S_IFMT) != S_IFREG)
 
91
    return 1;
 
92
  VOID(chmod(to, statbuf.st_mode & 07777));             /* Copy modes */
 
93
 
 
94
#if !defined(__WIN__) && !defined(__NETWARE__)
 
95
  if (statbuf.st_nlink > 1 && MyFlags & MY_LINK_WARNING)
 
96
  {
 
97
    if (MyFlags & MY_LINK_WARNING)
 
98
      my_error(EE_LINK_WARNING,MYF(ME_BELL+ME_WAITTANG),from,statbuf.st_nlink);
 
99
  }
 
100
  res= chown(to, statbuf.st_uid, statbuf.st_gid);       /* Copy ownership */
 
101
#endif /* !__WIN__ && !__NETWARE__ */
 
102
 
 
103
#ifndef VMS
 
104
#ifndef __ZTC__
 
105
  if (MyFlags & MY_COPYTIME)
 
106
  {
 
107
    struct utimbuf timep;
 
108
    timep.actime  = statbuf.st_atime;
 
109
    timep.modtime = statbuf.st_mtime;
 
110
    VOID(utime((char*) to, &timep));/* Update last accessed and modified times */
 
111
  }
 
112
#else
 
113
  if (MyFlags & MY_COPYTIME)
 
114
  {
 
115
    time_t time[2];
 
116
    time[0]= statbuf.st_atime;
 
117
    time[1]= statbuf.st_mtime;
 
118
    VOID(utime((char*) to, time));/* Update last accessed and modified times */
 
119
  }
 
120
#endif
 
121
#endif
 
122
  return 0;
 
123
} /* my_copystat */