2154
2154
broadcast_refresh();
2157
static int send_check_errmsg(Session *session, TableList* table,
2158
const char* operator_name, const char* errmsg)
2161
Protocol *protocol= session->protocol;
2162
protocol->prepareForResend();
2163
protocol->store(table->alias);
2164
protocol->store((char*) operator_name);
2165
protocol->store(STRING_WITH_LEN("error"));
2166
protocol->store(errmsg);
2167
session->clear_error();
2168
if (protocol->write())
2174
static int prepare_for_repair(Session *session, TableList *table_list,
2175
HA_CHECK_OPT *check_opt)
2178
Table tmp_table, *table;
2180
char from[FN_REFLEN],tmp[FN_REFLEN+32];
2182
struct stat stat_info;
2184
if (!(check_opt->use_frm))
2187
if (!(table= table_list->table)) /* if open_ltable failed */
2189
char key[MAX_DBKEY_LENGTH];
2190
uint32_t key_length;
2192
key_length= table_list->create_table_def_key(key);
2193
pthread_mutex_lock(&LOCK_open); /* Lock table for repair */
2194
if (!(share= (get_table_share(session, table_list, key, key_length, 0,
2197
pthread_mutex_unlock(&LOCK_open);
2199
return 0; // Can't open frm file
2202
if (open_table_from_share(session, share, "", 0, 0, 0, &tmp_table, OTM_OPEN))
2204
release_table_share(share);
2205
pthread_mutex_unlock(&LOCK_open);
2207
return 0; // Out of memory
2210
pthread_mutex_unlock(&LOCK_open);
2214
REPAIR Table ... USE_FRM for temporary tables makes little sense.
2216
if (table->s->tmp_table)
2218
error= send_check_errmsg(session, table_list, "repair",
2219
"Cannot repair temporary table from .frm file");
2224
User gave us USE_FRM which means that the header in the index file is
2226
In this case we will try to fix the table the following way:
2227
- Rename the data file to a temporary name
2228
- Truncate the table
2229
- Replace the new data file with the old one
2230
- Run a normal repair using the new index file and the old data file
2234
Check if this is a table type that stores index and data separately,
2235
like ISAM or MyISAM. We assume fixed order of engine file name
2236
extentions array. First element of engine file name extentions array
2237
is meta/index file extention. Second element - data file extention.
2239
ext= table->file->engine->bas_ext();
2240
if (!ext[0] || !ext[1])
2241
goto end; // No data file
2243
// Name of data file
2244
sprintf(from,"%s%s", table->s->normalized_path.str, ext[1]);
2245
if (stat(from, &stat_info))
2246
goto end; // Can't use USE_FRM flag
2248
snprintf(tmp, sizeof(tmp), "%s-%lx_%"PRIx64,
2249
from, (unsigned long)current_pid, session->thread_id);
2251
/* If we could open the table, close it */
2252
if (table_list->table)
2254
pthread_mutex_lock(&LOCK_open); /* Close for repair table */
2255
session->close_cached_table(table);
2256
pthread_mutex_unlock(&LOCK_open);
2258
if (lock_and_wait_for_table_name(session,table_list))
2263
if (my_rename(from, tmp, MYF(MY_WME)))
2265
pthread_mutex_lock(&LOCK_open); /* Lock during rename of table (aka we go to unlock ) */
2266
unlock_table_name(table_list);
2267
pthread_mutex_unlock(&LOCK_open);
2268
error= send_check_errmsg(session, table_list, "repair",
2269
"Failed renaming data file");
2272
if (mysql_truncate(session, table_list, 1))
2274
pthread_mutex_lock(&LOCK_open); /* Lock during truncate of table during repair operation. */
2275
unlock_table_name(table_list);
2276
pthread_mutex_unlock(&LOCK_open);
2277
error= send_check_errmsg(session, table_list, "repair",
2278
"Failed generating table from .frm file");
2281
if (my_rename(tmp, from, MYF(MY_WME)))
2283
pthread_mutex_lock(&LOCK_open); /* Final repair of table for rename */
2284
unlock_table_name(table_list);
2285
pthread_mutex_unlock(&LOCK_open);
2286
error= send_check_errmsg(session, table_list, "repair",
2287
"Failed restoring .MYD file");
2292
Now we should be able to open the partially repaired table
2293
to finish the repair in the handler later on.
2295
pthread_mutex_lock(&LOCK_open); /* Lock for opening partially repaired table */
2296
if (session->reopen_name_locked_table(table_list, true))
2298
unlock_table_name(table_list);
2299
pthread_mutex_unlock(&LOCK_open);
2300
error= send_check_errmsg(session, table_list, "repair",
2301
"Failed to open partially repaired table");
2304
pthread_mutex_unlock(&LOCK_open);
2307
if (table == &tmp_table)
2309
pthread_mutex_lock(&LOCK_open); /* Lock to close table after repair operation */
2310
table->closefrm(true); // Free allocated memory
2311
pthread_mutex_unlock(&LOCK_open);
2320
2159
false Message sent to net (admin operation went ok)