datadict.cc 6.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

16
#include "mariadb.h"
17 18 19 20
#include "datadict.h"
#include "sql_priv.h"
#include "sql_class.h"
#include "sql_table.h"
21
#include "ha_sequence.h"
22

23 24 25 26
static int read_string(File file, uchar**to, size_t length)
{
  DBUG_ENTER("read_string");

27
  /* This can't use MY_THREAD_SPECIFIC as it's used on server start */
28 29 30 31 32 33 34 35 36 37 38
  if (!(*to= (uchar*) my_malloc(length+1,MYF(MY_WME))) ||
      mysql_file_read(file, *to, length, MYF(MY_NABP)))
  {
     my_free(*to);
    *to= 0;
    DBUG_RETURN(1);
  }
  *((char*) *to+length)= '\0'; // C-style safety
  DBUG_RETURN (0);
}

39 40 41 42

/**
  Check type of .frm if we are not going to parse it.

43 44 45 46
  @param[in]  thd               The current session.
  @param[in]  path              path to FRM file.
  @param[in/out] engine_name    table engine name (length < NAME_CHAR_LEN)

47
  engine_name is a LEX_CSTRING, where engine_name->str must point to
48
  a buffer of at least NAME_CHAR_LEN+1 bytes.
49 50
  If engine_name is 0, then the function will only test if the file is a
  view or not
51

52 53
  @param[out] is_sequence  1 if table is a SEQUENCE, 0 otherwise

54
  @retval  TABLE_TYPE_UNKNOWN   error  - file can't be opened
55
  @retval  TABLE_TYPE_NORMAL    table
56 57
  @retval  TABLE_TYPE_SEQUENCE  sequence table
  @retval  TABLE_TYPE_VIEW      view
58 59
*/

60
Table_type dd_frm_type(THD *thd, char *path, LEX_CSTRING *engine_name,
61
                       bool *is_sequence)
62 63
{
  File file;
64
  uchar header[40];     //"TYPE=VIEW\n" it is 10 characters
65
  size_t error;
66
  Table_type type= TABLE_TYPE_UNKNOWN;
67
  uchar dbt;
68 69
  DBUG_ENTER("dd_frm_type");

70 71 72 73 74
  *is_sequence= 0;

  if ((file= mysql_file_open(key_file_frm, path, O_RDONLY | O_SHARE, MYF(0)))
      < 0)
    DBUG_RETURN(TABLE_TYPE_UNKNOWN);
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  /*
    We return TABLE_TYPE_NORMAL if we can open the .frm file. This allows us
    to drop a bad .frm file with DROP TABLE
  */
  type= TABLE_TYPE_NORMAL;

  /*
    Initialize engine name in case we are not able to find it out
    The cast is safe, as engine_name->str points to a usable buffer.
   */
  if (engine_name)
  {
    engine_name->length= 0;
    ((char*) (engine_name->str))[0]= 0;
  }

92
  if (unlikely((error= mysql_file_read(file, (uchar*) header, sizeof(header), MYF(MY_NABP)))))
93
    goto err;
94

95
  if (unlikely((!strncmp((char*) header, "TYPE=VIEW\n", 10))))
96
  {
97
    type= TABLE_TYPE_VIEW;
98 99 100
    goto err;
  }

101 102
  /* engine_name is 0 if we only want to know if table is view or not */
  if (!engine_name)
103
    goto err;
104

105 106 107
  if (!is_binary_frm_header(header))
    goto err;

108 109
  dbt= header[3];

110 111 112 113 114 115
  if (((header[39] >> 4) & 3) == HA_CHOICE_YES)
  {
    DBUG_PRINT("info", ("Sequence found"));
    *is_sequence= 1;
  }

116 117 118 119 120 121 122 123 124 125
  /* cannot use ha_resolve_by_legacy_type without a THD */
  if (thd && dbt < DB_TYPE_FIRST_DYNAMIC)
  {
    handlerton *ht= ha_resolve_by_legacy_type(thd, (enum legacy_db_type)dbt);
    if (ht)
    {
      *engine_name= hton2plugin[ht->slot]->name;
      goto err;
    }
  }
126

127
  /* read the true engine name */
128 129 130 131 132 133 134 135 136 137 138
  {
    MY_STAT state;  
    uchar *frm_image= 0;
    uint n_length;

    if (mysql_file_fstat(file, &state, MYF(MY_WME)))
      goto err;

    if (mysql_file_seek(file, 0, SEEK_SET, MYF(MY_WME)))
      goto err;

139
    if (read_string(file, &frm_image, (size_t)state.st_size))
140 141 142 143
      goto err;

    if ((n_length= uint4korr(frm_image+55)))
    {
144
      uint record_offset= uint2korr(frm_image+6)+
145
                      ((uint2korr(frm_image+14) == 0xffff ?
146
                        uint4korr(frm_image+47) : uint2korr(frm_image+14)));
147 148 149 150 151 152 153 154
      uint reclength= uint2korr(frm_image+16);

      uchar *next_chunk= frm_image + record_offset + reclength;
      uchar *buff_end= next_chunk + n_length;
      uint connect_string_length= uint2korr(next_chunk);
      next_chunk+= connect_string_length + 2;
      if (next_chunk + 2 < buff_end)
      {
155 156
        uint len= uint2korr(next_chunk);
        if (len <= NAME_CHAR_LEN)
157 158 159 160 161 162
        {
          /*
            The following cast is safe as the caller has allocated buffer
            and it's up to this function to generate the name.
          */
          strmake((char*) engine_name->str, (char*)next_chunk + 2,
163
                  engine_name->length= len);
164
        }
165 166 167 168 169 170
      }
    }

    my_free(frm_image);
  }

171
  /* Probably a table. */
172 173 174
err:
  mysql_file_close(file, MYF(MY_WME));
  DBUG_RETURN(type);
175 176 177 178 179 180 181 182 183
}


/*
  Regenerate a metadata locked table.

  @param  thd   Thread context.
  @param  db    Name of the database to which the table belongs to.
  @param  name  Table name.
Sergei Golubchik's avatar
Sergei Golubchik committed
184 185
  @param  path  For temporary tables only - path to table files.
                Otherwise NULL (the path is calculated from db and table names).
186 187 188 189 190

  @retval  FALSE  Success.
  @retval  TRUE   Error.
*/

Sergei Golubchik's avatar
Sergei Golubchik committed
191 192
bool dd_recreate_table(THD *thd, const char *db, const char *table_name,
                       const char *path)
193 194 195
{
  bool error= TRUE;
  HA_CREATE_INFO create_info;
Sergei Golubchik's avatar
Sergei Golubchik committed
196
  char path_buf[FN_REFLEN + 1];
197 198 199 200
  DBUG_ENTER("dd_recreate_table");

  memset(&create_info, 0, sizeof(create_info));

Sergei Golubchik's avatar
Sergei Golubchik committed
201 202 203 204 205 206 207 208 209 210 211 212
  if (path)
    create_info.options|= HA_LEX_CREATE_TMP_TABLE;
  else
  {
    build_table_filename(path_buf, sizeof(path_buf) - 1,
                         db, table_name, "", 0);
    path= path_buf;

    /* There should be a exclusive metadata lock on the table. */
    DBUG_ASSERT(thd->mdl_context.is_lock_owner(MDL_key::TABLE, db, table_name,
                                               MDL_EXCLUSIVE));
  }
213 214

  /* Attempt to reconstruct the table. */
215
  error= ha_create_table(thd, path, db, table_name, &create_info, NULL);
216 217 218 219

  DBUG_RETURN(error);
}