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