Drizzled Public API Documentation

my_redel.cc
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include <config.h>
17 
18 #include <drizzled/internal/my_sys.h>
19 #include <drizzled/internal/m_string.h>
20 #include <drizzled/error.h>
21 #if defined(HAVE_UTIME_H)
22 #include <utime.h>
23 #elif defined(HAVE_SYS_UTIME_H)
24 #include <sys/utime.h>
25 #elif !defined(HPUX10)
26 struct utimbuf {
27  time_t actime;
28  time_t modtime;
29 };
30 #endif
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 
35 namespace drizzled
36 {
37 namespace internal
38 {
39 
40  /*
41  Rename with copy stat form old file
42  Copy stats from old file to new file, deletes orginal and
43  changes new file name to old file name
44 
45  if MY_REDEL_MAKE_COPY is given, then the orginal file
46  is renamed to org_name-'current_time'.BAK
47  */
48 
49 int my_redel(const char *org_name, const char *tmp_name, myf MyFlags)
50 {
51  int error=1;
52 
53  if (my_copystat(org_name,tmp_name,MyFlags) < 0)
54  goto end;
55  if (my_delete(org_name, MyFlags))
56  goto end;
57  if (my_rename(tmp_name,org_name,MyFlags))
58  goto end;
59 
60  error=0;
61 end:
62  return(error);
63 } /* my_redel */
64 
65 
66  /* Copy stat from one file to another */
67  /* Return -1 if can't get stat, 1 if wrong type of file */
68 
69 int my_copystat(const char *from, const char *to, int MyFlags)
70 {
71  struct stat statbuf;
72 
73  if (stat((char*) from, &statbuf))
74  {
75  errno=errno;
76  if (MyFlags & (MY_FAE+MY_WME))
77  my_error(EE_STAT, MYF(ME_BELL+ME_WAITTANG),from,errno);
78  return -1; /* Can't get stat on input file */
79  }
80  if ((statbuf.st_mode & S_IFMT) != S_IFREG)
81  return 1;
82  chmod(to, statbuf.st_mode & 07777); /* Copy modes */
83 
84  if (statbuf.st_nlink > 1 && MyFlags & MY_LINK_WARNING)
85  {
86  if (MyFlags & MY_LINK_WARNING)
87  my_error(EE_LINK_WARNING,MYF(ME_BELL+ME_WAITTANG),from,statbuf.st_nlink);
88  }
89  if(chown(to, statbuf.st_uid, statbuf.st_gid)!=0)
90  return 1;
91 
92 #ifndef __ZTC__
93  if (MyFlags & MY_COPYTIME)
94  {
95  struct utimbuf timep;
96  timep.actime = statbuf.st_atime;
97  timep.modtime = statbuf.st_mtime;
98  utime((char*) to, &timep);/* Update last accessed and modified times */
99  }
100 #else
101  if (MyFlags & MY_COPYTIME)
102  {
103  time_t time[2];
104  time[0]= statbuf.st_atime;
105  time[1]= statbuf.st_mtime;
106  utime((char*) to, time);/* Update last accessed and modified times */
107  }
108 #endif
109  return 0;
110 } /* my_copystat */
111 
112 } /* namespace internal */
113 } /* namespace drizzled */
TODO: Rename this file - func.h is stupid.