diff --git a/storage/csv/ha_tina.cc b/storage/csv/ha_tina.cc index 75d7b42673d..19cc104e552 100644 --- a/storage/csv/ha_tina.cc +++ b/storage/csv/ha_tina.cc @@ -186,6 +186,16 @@ static int tina_done_func(void *p) return 0; } +static File open_meta_file(const char *table_name) +{ + char meta_file_name[FN_REFLEN]; + fn_format(meta_file_name, table_name, "", CSM_EXT, + MY_REPLACE_EXT|MY_UNPACK_FILENAME); + return mysql_file_open(csv_key_file_metadata, + meta_file_name, + O_RDWR|O_CREAT, + MYF(MY_WME)); +} /* Simple lock controls. @@ -193,7 +203,6 @@ static int tina_done_func(void *p) static TINA_SHARE *get_share(const char *table_name, TABLE *table) { TINA_SHARE *share; - char meta_file_name[FN_REFLEN]; MY_STAT file_stat; /* Stat information for the data file */ char *tmp_name; uint length; @@ -231,8 +240,6 @@ static TINA_SHARE *get_share(const char *table_name, TABLE *table) my_stpcpy(share->table_name, table_name); fn_format(share->data_file_name, table_name, "", CSV_EXT, MY_REPLACE_EXT|MY_UNPACK_FILENAME); - fn_format(meta_file_name, table_name, "", CSM_EXT, - MY_REPLACE_EXT|MY_UNPACK_FILENAME); if (mysql_file_stat(csv_key_file_data, share->data_file_name, &file_stat, MYF(MY_WME)) == NULL) @@ -251,10 +258,7 @@ static TINA_SHARE *get_share(const char *table_name, TABLE *table) Usually this will result in auto-repair, and we will get a good meta-file in the end. */ - if (((share->meta_file= mysql_file_open(csv_key_file_metadata, - meta_file_name, - O_RDWR|O_CREAT, - MYF(MY_WME))) == -1) || + if (((share->meta_file= open_meta_file(table_name)) == -1) || read_meta_file(share->meta_file, &share->rows_recorded)) share->crashed= TRUE; } @@ -429,11 +433,15 @@ static int free_share(TINA_SHARE *share) mysql_mutex_lock(&tina_mutex); int result_code= 0; if (!--share->use_count){ - /* Write the meta file. Mark it as crashed if needed. */ - (void)write_meta_file(share->meta_file, share->rows_recorded, - share->crashed ? TRUE :FALSE); - if (mysql_file_close(share->meta_file, MYF(0))) - result_code= 1; + if (share->meta_file != -1) + { + /* Write the meta file. Mark it as crashed if needed. */ + (void)write_meta_file(share->meta_file, share->rows_recorded, + share->crashed ? TRUE :FALSE); + if (mysql_file_close(share->meta_file, MYF(0))) + result_code= 1; + } + if (share->tina_write_opened) { if (mysql_file_close(share->tina_write_filedes, MYF(0))) @@ -1501,6 +1509,12 @@ int ha_tina::repair(THD* thd, HA_CHECK_OPT* check_opt) my_off_t write_begin= 0, write_end; DBUG_ENTER("ha_tina::repair"); + /* reopen meta file */ + if ((share->meta_file != -1) && (mysql_file_close(share->meta_file, MYF(0)))) + DBUG_RETURN(-1); + if ((share->meta_file= open_meta_file(share->table_name)) == -1) + DBUG_RETURN(HA_ERR_CRASHED_ON_REPAIR); + /* empty file */ if (!share->saved_data_file_length) { @@ -1724,6 +1738,13 @@ int ha_tina::check(THD* thd, HA_CHECK_OPT* check_opt) table->s->reclength, MYF(MY_WME)))) DBUG_RETURN(HA_ERR_OUT_OF_MEM); + /* check meta file */ + if (share->meta_file == -1) + { + share->crashed= TRUE; + DBUG_RETURN(HA_ADMIN_CORRUPT); + } + /* position buffer to the start of the file */ if (init_data_file()) DBUG_RETURN(HA_ERR_CRASHED);