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 (flush_key_blocks(info->s->getKeyCache(), info->s->kfile, FLUSH_RELEASE))
00052   error=errno;
00053       if (info->opt_flag & WRITE_CACHE_USED)
00054   if (flush_io_cache(&info->rec_cache))
00055     error=errno;
00056       if (info->opt_flag & READ_CACHE_USED)
00057       {
00058   if (flush_io_cache(&info->rec_cache))
00059     error=errno;
00060         info->rec_cache.reinit_io_cache(internal::READ_CACHE,0, (bool) (info->lock_type != F_UNLCK),1);
00061       }
00062       if (info->lock_type != F_UNLCK && ! info->was_locked)
00063       {
00064   info->was_locked=info->lock_type;
00065   if (mi_lock_database(info,F_UNLCK))
00066     error=errno;
00067       }
00068 #ifdef CANT_OPEN_FILES_TWICE
00069       if (info->s->kfile >= 0 && internal::my_close(info->s->kfile,MYF(0)))
00070   error = errno;
00071       if (info->dfile >= 0 && internal::my_close(info->dfile,MYF(0)))
00072   error = errno;
00073       info->s->kfile=info->dfile= -1; /* Files aren't open anymore */
00074       break;
00075 #endif
00076     case HA_PANIC_READ:     /* Restore to before WRITE */
00077 #ifdef CANT_OPEN_FILES_TWICE
00078       {         /* Open closed files */
00079   char name_buff[FN_REFLEN];
00080   if (info->s->kfile < 0)
00081     if ((info->s->kfile= internal::my_open(internal::fn_format(name_buff,info->filename,"",
00082                 N_NAME_IEXT,4),info->mode,
00083             MYF(MY_WME))) < 0)
00084       error = errno;
00085   if (info->dfile < 0)
00086   {
00087     if ((info->dfile= internal::my_open(internal::fn_format(name_buff,info->filename,"",
00088                 N_NAME_DEXT,4),info->mode,
00089             MYF(MY_WME))) < 0)
00090       error = errno;
00091     info->rec_cache.file=info->dfile;
00092   }
00093       }
00094 #endif
00095       if (info->was_locked)
00096       {
00097   if (mi_lock_database(info, info->was_locked))
00098     error=errno;
00099   info->was_locked=0;
00100       }
00101       break;
00102     }
00103     ++it;
00104   }
00105   THR_LOCK_myisam.unlock();
00106   if (!error)
00107     return(0);
00108   return(errno=error);
00109 } /* mi_panic */