Drizzled Public API Documentation

my_redel.cc
00001 /* Copyright (C) 2000 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 #include <config.h>
00017 
00018 #include <drizzled/internal/my_sys.h>
00019 #include <drizzled/internal/m_string.h>
00020 #include <drizzled/error.h>
00021 #if defined(HAVE_UTIME_H)
00022 #include <utime.h>
00023 #elif defined(HAVE_SYS_UTIME_H)
00024 #include <sys/utime.h>
00025 #elif !defined(HPUX10)
00026 struct utimbuf {
00027   time_t actime;
00028   time_t modtime;
00029 };
00030 #endif
00031 #ifdef HAVE_SYS_STAT_H
00032 # include <sys/stat.h>
00033 #endif
00034 
00035 namespace drizzled
00036 {
00037 namespace internal
00038 {
00039 
00040   /*
00041     Rename with copy stat form old file
00042     Copy stats from old file to new file, deletes orginal and
00043     changes new file name to old file name
00044 
00045     if MY_REDEL_MAKE_COPY is given, then the orginal file
00046     is renamed to org_name-'current_time'.BAK
00047   */
00048 
00049 #define REDEL_EXT ".BAK"
00050 
00051 int my_redel(const char *org_name, const char *tmp_name, myf MyFlags)
00052 {
00053   int error=1;
00054 
00055   if (my_copystat(org_name,tmp_name,MyFlags) < 0)
00056     goto end;
00057   if (MyFlags & MY_REDEL_MAKE_BACKUP)
00058   {
00059     char name_buff[FN_REFLEN+20];
00060     char ext[20];
00061     ext[0]='-';
00062     get_date(ext+1,2+4,(time_t) 0);
00063     strcpy(strchr(ext, '\0'),REDEL_EXT);
00064     if (my_rename(org_name, fn_format(name_buff, org_name, "", ext, 2),
00065       MyFlags))
00066       goto end;
00067   }
00068   else if (my_delete(org_name, MyFlags))
00069       goto end;
00070   if (my_rename(tmp_name,org_name,MyFlags))
00071     goto end;
00072 
00073   error=0;
00074 end:
00075   return(error);
00076 } /* my_redel */
00077 
00078 
00079   /* Copy stat from one file to another */
00080   /* Return -1 if can't get stat, 1 if wrong type of file */
00081 
00082 int my_copystat(const char *from, const char *to, int MyFlags)
00083 {
00084   struct stat statbuf;
00085 
00086   if (stat((char*) from, &statbuf))
00087   {
00088     errno=errno;
00089     if (MyFlags & (MY_FAE+MY_WME))
00090       my_error(EE_STAT, MYF(ME_BELL+ME_WAITTANG),from,errno);
00091     return -1;        /* Can't get stat on input file */
00092   }
00093   if ((statbuf.st_mode & S_IFMT) != S_IFREG)
00094     return 1;
00095   chmod(to, statbuf.st_mode & 07777);   /* Copy modes */
00096 
00097   if (statbuf.st_nlink > 1 && MyFlags & MY_LINK_WARNING)
00098   {
00099     if (MyFlags & MY_LINK_WARNING)
00100       my_error(EE_LINK_WARNING,MYF(ME_BELL+ME_WAITTANG),from,statbuf.st_nlink);
00101   }
00102   if(chown(to, statbuf.st_uid, statbuf.st_gid)!=0)
00103     return 1;
00104 
00105 #ifndef __ZTC__
00106   if (MyFlags & MY_COPYTIME)
00107   {
00108     struct utimbuf timep;
00109     timep.actime  = statbuf.st_atime;
00110     timep.modtime = statbuf.st_mtime;
00111     utime((char*) to, &timep);/* Update last accessed and modified times */
00112   }
00113 #else
00114   if (MyFlags & MY_COPYTIME)
00115   {
00116     time_t time[2];
00117     time[0]= statbuf.st_atime;
00118     time[1]= statbuf.st_mtime;
00119     utime((char*) to, time);/* Update last accessed and modified times */
00120   }
00121 #endif
00122   return 0;
00123 } /* my_copystat */
00124 
00125 } /* namespace internal */
00126 } /* namespace drizzled */