Drizzled Public API Documentation

my_rename.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/error.h>
00020 #include <drizzled/internal/m_string.h>
00021 
00022 namespace drizzled
00023 {
00024 namespace internal
00025 {
00026 
00027   /* On unix rename deletes to file if it exists */
00028 
00029 int my_rename(const char *from, const char *to, myf MyFlags)
00030 {
00031   int error = 0;
00032 
00033 #if defined(HAVE_FILE_VERSIONS)
00034   {       /* Check that there isn't a old file */
00035     int save_errno;
00036     MY_STAT my_stat_result;
00037     save_errno=errno;
00038     if (my_stat(to,&my_stat_result,MYF(0)))
00039     {
00040       errno=EEXIST;
00041       error= -1;
00042       if (MyFlags & MY_FAE+MY_WME)
00043   my_error(EE_LINK, MYF(ME_BELL+ME_WAITTANG),from,to,errno);
00044       return(error);
00045     }
00046     errno=save_errno;
00047   }
00048 #endif
00049   if (rename(from,to))
00050   {
00051     errno=errno;
00052     error = -1;
00053     if (MyFlags & (MY_FAE+MY_WME))
00054       my_error(EE_LINK, MYF(ME_BELL+ME_WAITTANG),from,to,errno);
00055   }
00056   else if (MyFlags & MY_SYNC_DIR)
00057   {
00058 #ifdef NEED_EXPLICIT_SYNC_DIR
00059     /* do only the needed amount of syncs: */
00060     char dir_from[FN_REFLEN], dir_to[FN_REFLEN];
00061     size_t dir_from_length, dir_to_length;
00062     dirname_part(dir_from, from, &dir_from_length);
00063     dirname_part(dir_to, to, &dir_to_length);
00064     if (my_sync_dir(dir_from, MyFlags) ||
00065         (strcmp(dir_from, dir_to) &&
00066          my_sync_dir(dir_to, MyFlags)))
00067       error= -1;
00068 #endif
00069   }
00070   return(error);
00071 } /* my_rename */
00072 
00073 } /* namespace internal */
00074 } /* namespace drizzled */