Drizzled Public API Documentation

mi_panic.cc
00001 /* Copyright (C) 2000-2001, 2003 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 "myisam_priv.h"
00017 
00018 using namespace std;
00019 using namespace drizzled;
00020 
00021   /* if flag == HA_PANIC_CLOSE then all misam files are closed */
00022   /* if flag == HA_PANIC_WRITE then all misam files are unlocked and
00023      all changed data in single user misam is written to file */
00024   /* if flag == HA_PANIC_READ then all misam files that was locked when
00025      mi_panic(HA_PANIC_WRITE) was done is locked. A mi_readinfo() is
00026      done for all single user files to get changes in database */
00027 
00028 
00029 int mi_panic(enum ha_panic_function flag)
00030 {
00031   int error=0;
00032   MI_INFO *info;
00033 
00034   THR_LOCK_myisam.lock();
00035   list<MI_INFO *>::iterator it= myisam_open_list.begin();
00036   while (it != myisam_open_list.end())
00037   {
00038     info= *it;
00039     switch (flag) {
00040     case HA_PANIC_CLOSE:
00041       THR_LOCK_myisam.unlock(); /* Not exactly right... */
00042       if (mi_close(info))
00043   error=errno;
00044       THR_LOCK_myisam.lock();
00045       break;
00046     case HA_PANIC_WRITE:    /* Do this to free databases */
00047 #ifdef CANT_OPEN_FILES_TWICE
00048       if (info->s->options & HA_OPTION_READ_ONLY_DATA)
00049   break;
00050 #endif
00051       if (info->opt_flag & WRITE_CACHE_USED)
00052   if (info->rec_cache.flush())
00053     error=errno;
00054       if (info->opt_flag & READ_CACHE_USED)
00055       {
00056   if (info->rec_cache.flush())
00057     error=errno;
00058         info->rec_cache.reinit_io_cache(internal::READ_CACHE,0, (bool) (info->lock_type != F_UNLCK),1);
00059       }
00060       if (info->lock_type != F_UNLCK && ! info->was_locked)
00061       {
00062   info->was_locked=info->lock_type;
00063   if (mi_lock_database(info,F_UNLCK))
00064     error=errno;
00065       }
00066 #ifdef CANT_OPEN_FILES_TWICE
00067       if (info->s->kfile >= 0 && internal::my_close(info->s->kfile,MYF(0)))
00068   error = errno;
00069       if (info->dfile >= 0 && internal::my_close(info->dfile,MYF(0)))
00070   error = errno;
00071       info->s->kfile=info->dfile= -1; /* Files aren't open anymore */
00072       break;
00073 #endif
00074     case HA_PANIC_READ:     /* Restore to before WRITE */
00075 #ifdef CANT_OPEN_FILES_TWICE
00076       {         /* Open closed files */
00077   char name_buff[FN_REFLEN];
00078   if (info->s->kfile < 0)
00079     if ((info->s->kfile= internal::my_open(internal::fn_format(name_buff,info->filename,"",
00080                 N_NAME_IEXT,4),info->mode,
00081             MYF(MY_WME))) < 0)
00082       error = errno;
00083   if (info->dfile < 0)
00084   {
00085     if ((info->dfile= internal::my_open(internal::fn_format(name_buff,info->filename,"",
00086                 N_NAME_DEXT,4),info->mode,
00087             MYF(MY_WME))) < 0)
00088       error = errno;
00089     info->rec_cache.file=info->dfile;
00090   }
00091       }
00092 #endif
00093       if (info->was_locked)
00094       {
00095   if (mi_lock_database(info, info->was_locked))
00096     error=errno;
00097   info->was_locked=0;
00098       }
00099       break;
00100     }
00101     ++it;
00102   }
00103   THR_LOCK_myisam.unlock();
00104   if (!error)
00105     return(0);
00106   return(errno=error);
00107 } /* mi_panic */