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 int my_redel(const char *org_name, const char *tmp_name, myf MyFlags)
00050 {
00051   int error=1;
00052 
00053   if (my_copystat(org_name,tmp_name,MyFlags) < 0)
00054     goto end;
00055   if (my_delete(org_name, MyFlags))
00056       goto end;
00057   if (my_rename(tmp_name,org_name,MyFlags))
00058     goto end;
00059 
00060   error=0;
00061 end:
00062   return(error);
00063 } /* my_redel */
00064 
00065 
00066   /* Copy stat from one file to another */
00067   /* Return -1 if can't get stat, 1 if wrong type of file */
00068 
00069 int my_copystat(const char *from, const char *to, int MyFlags)
00070 {
00071   struct stat statbuf;
00072 
00073   if (stat((char*) from, &statbuf))
00074   {
00075     errno=errno;
00076     if (MyFlags & (MY_FAE+MY_WME))
00077       my_error(EE_STAT, MYF(ME_BELL+ME_WAITTANG),from,errno);
00078     return -1;        /* Can't get stat on input file */
00079   }
00080   if ((statbuf.st_mode & S_IFMT) != S_IFREG)
00081     return 1;
00082   chmod(to, statbuf.st_mode & 07777);   /* Copy modes */
00083 
00084   if (statbuf.st_nlink > 1 && MyFlags & MY_LINK_WARNING)
00085   {
00086     if (MyFlags & MY_LINK_WARNING)
00087       my_error(EE_LINK_WARNING,MYF(ME_BELL+ME_WAITTANG),from,statbuf.st_nlink);
00088   }
00089   if(chown(to, statbuf.st_uid, statbuf.st_gid)!=0)
00090     return 1;
00091 
00092 #ifndef __ZTC__
00093   if (MyFlags & MY_COPYTIME)
00094   {
00095     struct utimbuf timep;
00096     timep.actime  = statbuf.st_atime;
00097     timep.modtime = statbuf.st_mtime;
00098     utime((char*) to, &timep);/* Update last accessed and modified times */
00099   }
00100 #else
00101   if (MyFlags & MY_COPYTIME)
00102   {
00103     time_t time[2];
00104     time[0]= statbuf.st_atime;
00105     time[1]= statbuf.st_mtime;
00106     utime((char*) to, time);/* Update last accessed and modified times */
00107   }
00108 #endif
00109   return 0;
00110 } /* my_copystat */
00111 
00112 } /* namespace internal */
00113 } /* namespace drizzled */