~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/myisam/mi_panic.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2001, 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include "myisam_priv.h"
 
17
 
 
18
using namespace std;
 
19
using namespace drizzled;
 
20
 
 
21
        /* if flag == HA_PANIC_CLOSE then all misam files are closed */
 
22
        /* if flag == HA_PANIC_WRITE then all misam files are unlocked and
 
23
           all changed data in single user misam is written to file */
 
24
        /* if flag == HA_PANIC_READ then all misam files that was locked when
 
25
           mi_panic(HA_PANIC_WRITE) was done is locked. A mi_readinfo() is
 
26
           done for all single user files to get changes in database */
 
27
 
 
28
 
 
29
int mi_panic(enum ha_panic_function flag)
 
30
{
 
31
  int error=0;
 
32
  MI_INFO *info;
 
33
 
 
34
  pthread_mutex_lock(&THR_LOCK_myisam);
 
35
  list<MI_INFO *>::iterator it= myisam_open_list.begin();
 
36
  while (it != myisam_open_list.end())
 
37
  {
 
38
    info= *it;
 
39
    switch (flag) {
 
40
    case HA_PANIC_CLOSE:
 
41
      pthread_mutex_unlock(&THR_LOCK_myisam);   /* Not exactly right... */
 
42
      if (mi_close(info))
 
43
        error=errno;
 
44
      pthread_mutex_lock(&THR_LOCK_myisam);
 
45
      break;
 
46
    case HA_PANIC_WRITE:                /* Do this to free databases */
 
47
#ifdef CANT_OPEN_FILES_TWICE
 
48
      if (info->s->options & HA_OPTION_READ_ONLY_DATA)
 
49
        break;
 
50
#endif
 
51
      if (flush_key_blocks(info->s->key_cache, info->s->kfile, FLUSH_RELEASE))
 
52
        error=errno;
 
53
      if (info->opt_flag & WRITE_CACHE_USED)
 
54
        if (flush_io_cache(&info->rec_cache))
 
55
          error=errno;
 
56
      if (info->opt_flag & READ_CACHE_USED)
 
57
      {
 
58
        if (flush_io_cache(&info->rec_cache))
 
59
          error=errno;
 
60
        reinit_io_cache(&info->rec_cache,internal::READ_CACHE,0,
 
61
                       (bool) (info->lock_type != F_UNLCK),1);
 
62
      }
 
63
      if (info->lock_type != F_UNLCK && ! info->was_locked)
 
64
      {
 
65
        info->was_locked=info->lock_type;
 
66
        if (mi_lock_database(info,F_UNLCK))
 
67
          error=errno;
 
68
      }
 
69
#ifdef CANT_OPEN_FILES_TWICE
 
70
      if (info->s->kfile >= 0 && internal::my_close(info->s->kfile,MYF(0)))
 
71
        error = errno;
 
72
      if (info->dfile >= 0 && internal::my_close(info->dfile,MYF(0)))
 
73
        error = errno;
 
74
      info->s->kfile=info->dfile= -1;   /* Files aren't open anymore */
 
75
      break;
 
76
#endif
 
77
    case HA_PANIC_READ:                 /* Restore to before WRITE */
 
78
#ifdef CANT_OPEN_FILES_TWICE
 
79
      {                                 /* Open closed files */
 
80
        char name_buff[FN_REFLEN];
 
81
        if (info->s->kfile < 0)
 
82
          if ((info->s->kfile= internal::my_open(internal::fn_format(name_buff,info->filename,"",
 
83
                                              N_NAME_IEXT,4),info->mode,
 
84
                                    MYF(MY_WME))) < 0)
 
85
            error = errno;
 
86
        if (info->dfile < 0)
 
87
        {
 
88
          if ((info->dfile= internal::my_open(internal::fn_format(name_buff,info->filename,"",
 
89
                                              N_NAME_DEXT,4),info->mode,
 
90
                                    MYF(MY_WME))) < 0)
 
91
            error = errno;
 
92
          info->rec_cache.file=info->dfile;
 
93
        }
 
94
      }
 
95
#endif
 
96
      if (info->was_locked)
 
97
      {
 
98
        if (mi_lock_database(info, info->was_locked))
 
99
          error=errno;
 
100
        info->was_locked=0;
 
101
      }
 
102
      break;
 
103
    }
 
104
    ++it;
 
105
  }
 
106
  pthread_mutex_unlock(&THR_LOCK_myisam);
 
107
  if (!error)
 
108
    return(0);
 
109
  return(errno=error);
 
110
} /* mi_panic */