forked from twitter-forks/mysql
/
table.cc
5361 lines (4636 loc) · 158 KB
/
table.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2000, 2011, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/* Some general useful functions */
#include "my_global.h" /* NO_EMBEDDED_ACCESS_CHECKS */
#include "sql_priv.h"
#include "unireg.h" // REQUIRED: for other includes
#include "table.h"
#include "frm_crypt.h" // get_crypt_for_frm
#include "key.h" // find_ref_key
#include "sql_table.h" // build_table_filename,
// primary_key_name
#include "sql_trigger.h"
#include "sql_parse.h" // free_items
#include "strfunc.h" // unhex_type2
#include "sql_partition.h" // mysql_unpack_partition,
// fix_partition_func, partition_info
#include "sql_acl.h" // *_ACL, acl_getroot_no_password
#include "sql_base.h" // release_table_share
#include <m_ctype.h>
#include "my_md5.h"
#include "sql_select.h"
#include "mdl.h" // MDL_wait_for_graph_visitor
/* INFORMATION_SCHEMA name */
LEX_STRING INFORMATION_SCHEMA_NAME= {C_STRING_WITH_LEN("information_schema")};
/* PERFORMANCE_SCHEMA name */
LEX_STRING PERFORMANCE_SCHEMA_DB_NAME= {C_STRING_WITH_LEN("performance_schema")};
/* MYSQL_SCHEMA name */
LEX_STRING MYSQL_SCHEMA_NAME= {C_STRING_WITH_LEN("mysql")};
/* GENERAL_LOG name */
LEX_STRING GENERAL_LOG_NAME= {C_STRING_WITH_LEN("general_log")};
/* SLOW_LOG name */
LEX_STRING SLOW_LOG_NAME= {C_STRING_WITH_LEN("slow_log")};
/* Functions defined in this file */
void open_table_error(TABLE_SHARE *share, int error, int db_errno,
myf errortype, int errarg);
static int open_binary_frm(THD *thd, TABLE_SHARE *share,
uchar *head, File file);
static void fix_type_pointers(const char ***array, TYPELIB *point_to_type,
uint types, char **names);
static uint find_field(Field **fields, uchar *record, uint start, uint length);
inline bool is_system_table_name(const char *name, uint length);
static ulong get_form_pos(File file, uchar *head);
/**************************************************************************
Object_creation_ctx implementation.
**************************************************************************/
Object_creation_ctx *Object_creation_ctx::set_n_backup(THD *thd)
{
Object_creation_ctx *backup_ctx;
DBUG_ENTER("Object_creation_ctx::set_n_backup");
backup_ctx= create_backup_ctx(thd);
change_env(thd);
DBUG_RETURN(backup_ctx);
}
void Object_creation_ctx::restore_env(THD *thd, Object_creation_ctx *backup_ctx)
{
if (!backup_ctx)
return;
backup_ctx->change_env(thd);
delete backup_ctx;
}
/**************************************************************************
Default_object_creation_ctx implementation.
**************************************************************************/
Default_object_creation_ctx::Default_object_creation_ctx(THD *thd)
: m_client_cs(thd->variables.character_set_client),
m_connection_cl(thd->variables.collation_connection)
{ }
Default_object_creation_ctx::Default_object_creation_ctx(
CHARSET_INFO *client_cs, CHARSET_INFO *connection_cl)
: m_client_cs(client_cs),
m_connection_cl(connection_cl)
{ }
Object_creation_ctx *
Default_object_creation_ctx::create_backup_ctx(THD *thd) const
{
return new Default_object_creation_ctx(thd);
}
void Default_object_creation_ctx::change_env(THD *thd) const
{
thd->variables.character_set_client= m_client_cs;
thd->variables.collation_connection= m_connection_cl;
thd->update_charset();
}
/**************************************************************************
View_creation_ctx implementation.
**************************************************************************/
View_creation_ctx *View_creation_ctx::create(THD *thd)
{
View_creation_ctx *ctx= new (thd->mem_root) View_creation_ctx(thd);
return ctx;
}
/*************************************************************************/
View_creation_ctx * View_creation_ctx::create(THD *thd,
TABLE_LIST *view)
{
View_creation_ctx *ctx= new (thd->mem_root) View_creation_ctx(thd);
/* Throw a warning if there is NULL cs name. */
if (!view->view_client_cs_name.str ||
!view->view_connection_cl_name.str)
{
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
ER_VIEW_NO_CREATION_CTX,
ER(ER_VIEW_NO_CREATION_CTX),
(const char *) view->db,
(const char *) view->table_name);
ctx->m_client_cs= system_charset_info;
ctx->m_connection_cl= system_charset_info;
return ctx;
}
/* Resolve cs names. Throw a warning if there is unknown cs name. */
bool invalid_creation_ctx;
invalid_creation_ctx= resolve_charset(view->view_client_cs_name.str,
system_charset_info,
&ctx->m_client_cs);
invalid_creation_ctx= resolve_collation(view->view_connection_cl_name.str,
system_charset_info,
&ctx->m_connection_cl) ||
invalid_creation_ctx;
if (invalid_creation_ctx)
{
sql_print_warning("View '%s'.'%s': there is unknown charset/collation "
"names (client: '%s'; connection: '%s').",
(const char *) view->db,
(const char *) view->table_name,
(const char *) view->view_client_cs_name.str,
(const char *) view->view_connection_cl_name.str);
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
ER_VIEW_INVALID_CREATION_CTX,
ER(ER_VIEW_INVALID_CREATION_CTX),
(const char *) view->db,
(const char *) view->table_name);
}
return ctx;
}
/*************************************************************************/
/* Get column name from column hash */
static uchar *get_field_name(Field **buff, size_t *length,
my_bool not_used __attribute__((unused)))
{
*length= (uint) strlen((*buff)->field_name);
return (uchar*) (*buff)->field_name;
}
/*
Returns pointer to '.frm' extension of the file name.
SYNOPSIS
fn_rext()
name file name
DESCRIPTION
Checks file name part starting with the rightmost '.' character,
and returns it if it is equal to '.frm'.
TODO
It is a good idea to get rid of this function modifying the code
to garantee that the functions presently calling fn_rext() always
get arguments in the same format: either with '.frm' or without '.frm'.
RETURN VALUES
Pointer to the '.frm' extension. If there is no extension,
or extension is not '.frm', pointer at the end of file name.
*/
char *fn_rext(char *name)
{
char *res= strrchr(name, '.');
if (res && !strcmp(res, reg_ext))
return res;
return name + strlen(name);
}
TABLE_CATEGORY get_table_category(const LEX_STRING *db, const LEX_STRING *name)
{
DBUG_ASSERT(db != NULL);
DBUG_ASSERT(name != NULL);
if (is_infoschema_db(db->str, db->length))
return TABLE_CATEGORY_INFORMATION;
if ((db->length == PERFORMANCE_SCHEMA_DB_NAME.length) &&
(my_strcasecmp(system_charset_info,
PERFORMANCE_SCHEMA_DB_NAME.str,
db->str) == 0))
return TABLE_CATEGORY_PERFORMANCE;
if ((db->length == MYSQL_SCHEMA_NAME.length) &&
(my_strcasecmp(system_charset_info,
MYSQL_SCHEMA_NAME.str,
db->str) == 0))
{
if (is_system_table_name(name->str, name->length))
return TABLE_CATEGORY_SYSTEM;
if ((name->length == GENERAL_LOG_NAME.length) &&
(my_strcasecmp(system_charset_info,
GENERAL_LOG_NAME.str,
name->str) == 0))
return TABLE_CATEGORY_LOG;
if ((name->length == SLOW_LOG_NAME.length) &&
(my_strcasecmp(system_charset_info,
SLOW_LOG_NAME.str,
name->str) == 0))
return TABLE_CATEGORY_LOG;
}
return TABLE_CATEGORY_USER;
}
/*
Allocate a setup TABLE_SHARE structure
SYNOPSIS
alloc_table_share()
TABLE_LIST Take database and table name from there
key Table cache key (db \0 table_name \0...)
key_length Length of key
RETURN
0 Error (out of memory)
# Share
*/
TABLE_SHARE *alloc_table_share(TABLE_LIST *table_list, char *key,
uint key_length)
{
MEM_ROOT mem_root;
TABLE_SHARE *share;
char *key_buff, *path_buff;
char path[FN_REFLEN];
uint path_length;
DBUG_ENTER("alloc_table_share");
DBUG_PRINT("enter", ("table: '%s'.'%s'",
table_list->db, table_list->table_name));
path_length= build_table_filename(path, sizeof(path) - 1,
table_list->db,
table_list->table_name, "", 0);
init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
if (multi_alloc_root(&mem_root,
&share, sizeof(*share),
&key_buff, key_length,
&path_buff, path_length + 1,
NULL))
{
bzero((char*) share, sizeof(*share));
share->set_table_cache_key(key_buff, key, key_length);
share->path.str= path_buff;
share->path.length= path_length;
strmov(share->path.str, path);
share->normalized_path.str= share->path.str;
share->normalized_path.length= path_length;
share->version= refresh_version;
/*
Since alloc_table_share() can be called without any locking (for
example, ha_create_table... functions), we do not assign a table
map id here. Instead we assign a value that is not used
elsewhere, and then assign a table map id inside open_table()
under the protection of the LOCK_open mutex.
*/
share->table_map_id= ~0UL;
share->cached_row_logging_check= -1;
share->used_tables.empty();
share->free_tables.empty();
share->m_flush_tickets.empty();
memcpy((char*) &share->mem_root, (char*) &mem_root, sizeof(mem_root));
mysql_mutex_init(key_TABLE_SHARE_LOCK_ha_data,
&share->LOCK_ha_data, MY_MUTEX_INIT_FAST);
}
DBUG_RETURN(share);
}
/*
Initialize share for temporary tables
SYNOPSIS
init_tmp_table_share()
thd thread handle
share Share to fill
key Table_cache_key, as generated from create_table_def_key.
must start with db name.
key_length Length of key
table_name Table name
path Path to file (possible in lower case) without .frm
NOTES
This is different from alloc_table_share() because temporary tables
don't have to be shared between threads or put into the table def
cache, so we can do some things notable simpler and faster
If table is not put in thd->temporary_tables (happens only when
one uses OPEN TEMPORARY) then one can specify 'db' as key and
use key_length= 0 as neither table_cache_key or key_length will be used).
*/
void init_tmp_table_share(THD *thd, TABLE_SHARE *share, const char *key,
uint key_length, const char *table_name,
const char *path)
{
DBUG_ENTER("init_tmp_table_share");
DBUG_PRINT("enter", ("table: '%s'.'%s'", key, table_name));
bzero((char*) share, sizeof(*share));
init_sql_alloc(&share->mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
share->table_category= TABLE_CATEGORY_TEMPORARY;
share->tmp_table= INTERNAL_TMP_TABLE;
share->db.str= (char*) key;
share->db.length= strlen(key);
share->table_cache_key.str= (char*) key;
share->table_cache_key.length= key_length;
share->table_name.str= (char*) table_name;
share->table_name.length= strlen(table_name);
share->path.str= (char*) path;
share->normalized_path.str= (char*) path;
share->path.length= share->normalized_path.length= strlen(path);
share->frm_version= FRM_VER_TRUE_VARCHAR;
share->cached_row_logging_check= -1;
/*
table_map_id is also used for MERGE tables to suppress repeated
compatibility checks.
*/
share->table_map_id= (ulong) thd->query_id;
share->used_tables.empty();
share->free_tables.empty();
share->m_flush_tickets.empty();
DBUG_VOID_RETURN;
}
/**
Release resources (plugins) used by the share and free its memory.
TABLE_SHARE is self-contained -- it's stored in its own MEM_ROOT.
Free this MEM_ROOT.
*/
void TABLE_SHARE::destroy()
{
uint idx;
KEY *info_it;
/* The mutex is initialized only for shares that are part of the TDC */
if (tmp_table == NO_TMP_TABLE)
mysql_mutex_destroy(&LOCK_ha_data);
my_hash_free(&name_hash);
plugin_unlock(NULL, db_plugin);
db_plugin= NULL;
/* Release fulltext parsers */
info_it= key_info;
for (idx= keys; idx; idx--, info_it++)
{
if (info_it->flags & HA_USES_PARSER)
{
plugin_unlock(NULL, info_it->parser);
info_it->flags= 0;
}
}
if (ha_data_destroy)
{
ha_data_destroy(ha_data);
ha_data_destroy= NULL;
}
#ifdef WITH_PARTITION_STORAGE_ENGINE
if (ha_part_data_destroy)
{
ha_part_data_destroy(ha_part_data);
ha_part_data_destroy= NULL;
}
#endif /* WITH_PARTITION_STORAGE_ENGINE */
/*
Make a copy since the share is allocated in its own root,
and free_root() updates its argument after freeing the memory.
*/
MEM_ROOT own_root= mem_root;
free_root(&own_root, MYF(0));
}
/*
Free table share and memory used by it
SYNOPSIS
free_table_share()
share Table share
*/
void free_table_share(TABLE_SHARE *share)
{
DBUG_ENTER("free_table_share");
DBUG_PRINT("enter", ("table: %s.%s", share->db.str, share->table_name.str));
DBUG_ASSERT(share->ref_count == 0);
if (share->m_flush_tickets.is_empty())
{
/*
No threads are waiting for this share to be flushed (the
share is not old, is for a temporary table, or just nobody
happens to be waiting for it). Destroy it.
*/
share->destroy();
}
else
{
Wait_for_flush_list::Iterator it(share->m_flush_tickets);
Wait_for_flush *ticket;
/*
We're about to iterate over a list that is used
concurrently. Make sure this never happens without a lock.
*/
mysql_mutex_assert_owner(&LOCK_open);
while ((ticket= it++))
(void) ticket->get_ctx()->m_wait.set_status(MDL_wait::GRANTED);
/*
If there are threads waiting for this share to be flushed,
the last one to receive the notification will destroy the
share. At this point the share is removed from the table
definition cache, so is OK to proceed here without waiting
for this thread to do the work.
*/
}
DBUG_VOID_RETURN;
}
/**
Return TRUE if a table name matches one of the system table names.
Currently these are:
help_category, help_keyword, help_relation, help_topic,
proc, event
time_zone, time_zone_leap_second, time_zone_name, time_zone_transition,
time_zone_transition_type
This function trades accuracy for speed, so may return false
positives. Presumably mysql.* database is for internal purposes only
and should not contain user tables.
*/
inline bool is_system_table_name(const char *name, uint length)
{
CHARSET_INFO *ci= system_charset_info;
return (
/* mysql.proc table */
(length == 4 &&
my_tolower(ci, name[0]) == 'p' &&
my_tolower(ci, name[1]) == 'r' &&
my_tolower(ci, name[2]) == 'o' &&
my_tolower(ci, name[3]) == 'c') ||
(length > 4 &&
(
/* one of mysql.help* tables */
(my_tolower(ci, name[0]) == 'h' &&
my_tolower(ci, name[1]) == 'e' &&
my_tolower(ci, name[2]) == 'l' &&
my_tolower(ci, name[3]) == 'p') ||
/* one of mysql.time_zone* tables */
(my_tolower(ci, name[0]) == 't' &&
my_tolower(ci, name[1]) == 'i' &&
my_tolower(ci, name[2]) == 'm' &&
my_tolower(ci, name[3]) == 'e') ||
/* mysql.event table */
(my_tolower(ci, name[0]) == 'e' &&
my_tolower(ci, name[1]) == 'v' &&
my_tolower(ci, name[2]) == 'e' &&
my_tolower(ci, name[3]) == 'n' &&
my_tolower(ci, name[4]) == 't')
)
)
);
}
/**
Check if a string contains path elements
*/
static inline bool has_disabled_path_chars(const char *str)
{
for (; *str; str++)
switch (*str)
{
case FN_EXTCHAR:
case '/':
case '\\':
case '~':
case '@':
return TRUE;
}
return FALSE;
}
/*
Read table definition from a binary / text based .frm file
SYNOPSIS
open_table_def()
thd Thread handler
share Fill this with table definition
db_flags Bit mask of the following flags: OPEN_VIEW
NOTES
This function is called when the table definition is not cached in
table_def_cache
The data is returned in 'share', which is alloced by
alloc_table_share().. The code assumes that share is initialized.
RETURN VALUES
0 ok
1 Error (see open_table_error)
2 Error (see open_table_error)
3 Wrong data in .frm file
4 Error (see open_table_error)
5 Error (see open_table_error: charset unavailable)
6 Unknown .frm version
*/
int open_table_def(THD *thd, TABLE_SHARE *share, uint db_flags)
{
int error, table_type;
bool error_given;
File file;
uchar head[64];
char path[FN_REFLEN];
MEM_ROOT **root_ptr, *old_root;
DBUG_ENTER("open_table_def");
DBUG_PRINT("enter", ("table: '%s'.'%s' path: '%s'", share->db.str,
share->table_name.str, share->normalized_path.str));
error= 1;
error_given= 0;
strxmov(path, share->normalized_path.str, reg_ext, NullS);
if ((file= mysql_file_open(key_file_frm,
path, O_RDONLY | O_SHARE, MYF(0))) < 0)
{
/*
We don't try to open 5.0 unencoded name, if
- non-encoded name contains '@' signs,
because '@' can be misinterpreted.
It is not clear if '@' is escape character in 5.1,
or a normal character in 5.0.
- non-encoded db or table name contain "#mysql50#" prefix.
This kind of tables must have been opened only by the
mysql_file_open() above.
*/
if (has_disabled_path_chars(share->table_name.str) ||
has_disabled_path_chars(share->db.str) ||
!strncmp(share->db.str, MYSQL50_TABLE_NAME_PREFIX,
MYSQL50_TABLE_NAME_PREFIX_LENGTH) ||
!strncmp(share->table_name.str, MYSQL50_TABLE_NAME_PREFIX,
MYSQL50_TABLE_NAME_PREFIX_LENGTH))
goto err_not_open;
/* Try unencoded 5.0 name */
uint length;
strxnmov(path, sizeof(path)-1,
mysql_data_home, "/", share->db.str, "/",
share->table_name.str, reg_ext, NullS);
length= unpack_filename(path, path) - reg_ext_length;
/*
The following is a safety test and should never fail
as the old file name should never be longer than the new one.
*/
DBUG_ASSERT(length <= share->normalized_path.length);
/*
If the old and the new names have the same length,
then table name does not have tricky characters,
so no need to check the old file name.
*/
if (length == share->normalized_path.length ||
((file= mysql_file_open(key_file_frm,
path, O_RDONLY | O_SHARE, MYF(0))) < 0))
goto err_not_open;
/* Unencoded 5.0 table name found */
path[length]= '\0'; // Remove .frm extension
strmov(share->normalized_path.str, path);
share->normalized_path.length= length;
}
error= 4;
if (mysql_file_read(file, head, 64, MYF(MY_NABP)))
goto err;
if (head[0] == (uchar) 254 && head[1] == 1)
{
if (head[2] == FRM_VER || head[2] == FRM_VER+1 ||
(head[2] >= FRM_VER+3 && head[2] <= FRM_VER+4))
{
/* Open view only */
if (db_flags & OPEN_VIEW_ONLY)
{
error_given= 1;
goto err;
}
table_type= 1;
}
else
{
error= 6; // Unkown .frm version
goto err;
}
}
else if (memcmp(head, STRING_WITH_LEN("TYPE=")) == 0)
{
error= 5;
if (memcmp(head+5,"VIEW",4) == 0)
{
share->is_view= 1;
if (db_flags & OPEN_VIEW)
error= 0;
}
goto err;
}
else
goto err;
/* No handling of text based files yet */
if (table_type == 1)
{
root_ptr= my_pthread_getspecific_ptr(MEM_ROOT**, THR_MALLOC);
old_root= *root_ptr;
*root_ptr= &share->mem_root;
error= open_binary_frm(thd, share, head, file);
*root_ptr= old_root;
error_given= 1;
}
share->table_category= get_table_category(& share->db, & share->table_name);
if (!error)
thd->status_var.opened_shares++;
err:
mysql_file_close(file, MYF(MY_WME));
err_not_open:
if (error && !error_given)
{
share->error= error;
open_table_error(share, error, (share->open_errno= my_errno), 0);
}
DBUG_RETURN(error);
}
/*
Read data from a binary .frm file from MySQL 3.23 - 5.0 into TABLE_SHARE
*/
static int open_binary_frm(THD *thd, TABLE_SHARE *share, uchar *head,
File file)
{
int error, errarg= 0;
uint new_frm_ver, field_pack_length, new_field_pack_flag;
uint interval_count, interval_parts, read_length, int_length;
uint db_create_options, keys, key_parts, n_length;
uint key_info_length, com_length, null_bit_pos;
uint extra_rec_buf_length;
uint i,j;
bool use_hash;
char *keynames, *names, *comment_pos;
uchar forminfo[288];
uchar *record;
uchar *disk_buff, *strpos, *null_flags, *null_pos;
ulong pos, record_offset, *rec_per_key, rec_buff_length;
handler *handler_file= 0;
KEY *keyinfo;
KEY_PART_INFO *key_part;
SQL_CRYPT *crypted=0;
Field **field_ptr, *reg_field;
const char **interval_array;
enum legacy_db_type legacy_db_type;
my_bitmap_map *bitmaps;
uchar *extra_segment_buff= 0;
const uint format_section_header_size= 8;
uchar *format_section_fields= 0;
DBUG_ENTER("open_binary_frm");
new_field_pack_flag= head[27];
new_frm_ver= (head[2] - FRM_VER);
field_pack_length= new_frm_ver < 2 ? 11 : 17;
disk_buff= 0;
error= 3;
/* Position of the form in the form file. */
if (!(pos= get_form_pos(file, head)))
goto err; /* purecov: inspected */
mysql_file_seek(file,pos,MY_SEEK_SET,MYF(0));
if (mysql_file_read(file, forminfo,288,MYF(MY_NABP)))
goto err;
share->frm_version= head[2];
/*
Check if .frm file created by MySQL 5.0. In this case we want to
display CHAR fields as CHAR and not as VARCHAR.
We do it this way as we want to keep the old frm version to enable
MySQL 4.1 to read these files.
*/
if (share->frm_version == FRM_VER_TRUE_VARCHAR -1 && head[33] == 5)
share->frm_version= FRM_VER_TRUE_VARCHAR;
#ifdef WITH_PARTITION_STORAGE_ENGINE
if (*(head+61) &&
!(share->default_part_db_type=
ha_checktype(thd, (enum legacy_db_type) (uint) *(head+61), 1, 0)))
goto err;
DBUG_PRINT("info", ("default_part_db_type = %u", head[61]));
#endif
legacy_db_type= (enum legacy_db_type) (uint) *(head+3);
DBUG_ASSERT(share->db_plugin == NULL);
/*
if the storage engine is dynamic, no point in resolving it by its
dynamically allocated legacy_db_type. We will resolve it later by name.
*/
if (legacy_db_type > DB_TYPE_UNKNOWN &&
legacy_db_type < DB_TYPE_FIRST_DYNAMIC)
share->db_plugin= ha_lock_engine(NULL,
ha_checktype(thd, legacy_db_type, 0, 0));
share->db_create_options= db_create_options= uint2korr(head+30);
share->db_options_in_use= share->db_create_options;
share->mysql_version= uint4korr(head+51);
share->null_field_first= 0;
if (!head[32]) // New frm file in 3.23
{
share->avg_row_length= uint4korr(head+34);
share->row_type= (row_type) head[40];
share->table_charset= get_charset((((uint) head[41]) << 8) +
(uint) head[38],MYF(0));
share->null_field_first= 1;
}
if (!share->table_charset)
{
/* unknown charset in head[38] or pre-3.23 frm */
if (use_mb(default_charset_info))
{
/* Warn that we may be changing the size of character columns */
sql_print_warning("'%s' had no or invalid character set, "
"and default character set is multi-byte, "
"so character column sizes may have changed",
share->path.str);
}
share->table_charset= default_charset_info;
}
share->db_record_offset= 1;
if (db_create_options & HA_OPTION_LONG_BLOB_PTR)
share->blob_ptr_size= portable_sizeof_char_ptr;
/* Set temporarily a good value for db_low_byte_first */
share->db_low_byte_first= test(legacy_db_type != DB_TYPE_ISAM);
error=4;
share->max_rows= uint4korr(head+18);
share->min_rows= uint4korr(head+22);
/* Read keyinformation */
key_info_length= (uint) uint2korr(head+28);
mysql_file_seek(file, (ulong) uint2korr(head+6), MY_SEEK_SET, MYF(0));
if (read_string(file,(uchar**) &disk_buff,key_info_length))
goto err; /* purecov: inspected */
if (disk_buff[0] & 0x80)
{
share->keys= keys= (disk_buff[1] << 7) | (disk_buff[0] & 0x7f);
share->key_parts= key_parts= uint2korr(disk_buff+2);
}
else
{
share->keys= keys= disk_buff[0];
share->key_parts= key_parts= disk_buff[1];
}
share->keys_for_keyread.init(0);
share->keys_in_use.init(keys);
n_length=keys*sizeof(KEY)+key_parts*sizeof(KEY_PART_INFO);
if (!(keyinfo = (KEY*) alloc_root(&share->mem_root,
n_length + uint2korr(disk_buff+4))))
goto err; /* purecov: inspected */
bzero((char*) keyinfo,n_length);
share->key_info= keyinfo;
key_part= reinterpret_cast<KEY_PART_INFO*>(keyinfo+keys);
strpos=disk_buff+6;
if (!(rec_per_key= (ulong*) alloc_root(&share->mem_root,
sizeof(ulong)*key_parts)))
goto err;
for (i=0 ; i < keys ; i++, keyinfo++)
{
keyinfo->table= 0; // Updated in open_frm
if (new_frm_ver >= 3)
{
keyinfo->flags= (uint) uint2korr(strpos) ^ HA_NOSAME;
keyinfo->key_length= (uint) uint2korr(strpos+2);
keyinfo->key_parts= (uint) strpos[4];
keyinfo->algorithm= (enum ha_key_alg) strpos[5];
keyinfo->block_size= uint2korr(strpos+6);
strpos+=8;
}
else
{
keyinfo->flags= ((uint) strpos[0]) ^ HA_NOSAME;
keyinfo->key_length= (uint) uint2korr(strpos+1);
keyinfo->key_parts= (uint) strpos[3];
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
strpos+=4;
}
keyinfo->key_part= key_part;
keyinfo->rec_per_key= rec_per_key;
for (j=keyinfo->key_parts ; j-- ; key_part++)
{
*rec_per_key++=0;
key_part->fieldnr= (uint16) (uint2korr(strpos) & FIELD_NR_MASK);
key_part->offset= (uint) uint2korr(strpos+2)-1;
key_part->key_type= (uint) uint2korr(strpos+5);
// key_part->field= (Field*) 0; // Will be fixed later
if (new_frm_ver >= 1)
{
key_part->key_part_flag= *(strpos+4);
key_part->length= (uint) uint2korr(strpos+7);
strpos+=9;
}
else
{
key_part->length= *(strpos+4);
key_part->key_part_flag=0;
if (key_part->length > 128)
{
key_part->length&=127; /* purecov: inspected */
key_part->key_part_flag=HA_REVERSE_SORT; /* purecov: inspected */
}
strpos+=7;
}
key_part->store_length=key_part->length;
}
}
keynames=(char*) key_part;
strpos+= (strmov(keynames, (char *) strpos) - keynames)+1;
//reading index comments
for (keyinfo= share->key_info, i=0; i < keys; i++, keyinfo++)
{
if (keyinfo->flags & HA_USES_COMMENT)
{
keyinfo->comment.length= uint2korr(strpos);
keyinfo->comment.str= strmake_root(&share->mem_root, (char*) strpos+2,
keyinfo->comment.length);
strpos+= 2 + keyinfo->comment.length;
}
DBUG_ASSERT(test(keyinfo->flags & HA_USES_COMMENT) ==
(keyinfo->comment.length > 0));
}
share->reclength = uint2korr((head+16));
if (*(head+26) == 1)
share->system= 1; /* one-record-database */
#ifdef HAVE_CRYPTED_FRM
else if (*(head+26) == 2)
{
crypted= get_crypt_for_frm();
share->crypted= 1;
}
#endif
record_offset= (ulong) (uint2korr(head+6)+
((uint2korr(head+14) == 0xffff ?
uint4korr(head+47) : uint2korr(head+14))));
if ((n_length= uint4korr(head+55)))
{
/* Read extra data segment */
uchar *next_chunk, *buff_end;
DBUG_PRINT("info", ("extra segment size is %u bytes", n_length));
if (!(extra_segment_buff= (uchar*) my_malloc(n_length, MYF(MY_WME))))
goto err;
next_chunk= extra_segment_buff;
if (mysql_file_pread(file, extra_segment_buff,
n_length, record_offset + share->reclength,
MYF(MY_NABP)))
{
goto err;
}
share->connect_string.length= uint2korr(next_chunk);
if (!(share->connect_string.str= strmake_root(&share->mem_root,
(char*) next_chunk + 2,
share->connect_string.
length)))
{
goto err;
}
next_chunk+= share->connect_string.length + 2;
buff_end= extra_segment_buff + n_length;
if (next_chunk + 2 < buff_end)
{
uint str_db_type_length= uint2korr(next_chunk);
LEX_STRING name;
name.str= (char*) next_chunk + 2;
name.length= str_db_type_length;
plugin_ref tmp_plugin= ha_resolve_by_name(thd, &name);
if (tmp_plugin != NULL && !plugin_equals(tmp_plugin, share->db_plugin))
{
if (legacy_db_type > DB_TYPE_UNKNOWN &&
legacy_db_type < DB_TYPE_FIRST_DYNAMIC &&
legacy_db_type != ha_legacy_type(
plugin_data(tmp_plugin, handlerton *)))
{
/* bad file, legacy_db_type did not match the name */
goto err;
}
/*
tmp_plugin is locked with a local lock.
we unlock the old value of share->db_plugin before
replacing it with a globally locked version of tmp_plugin
*/
plugin_unlock(NULL, share->db_plugin);
share->db_plugin= my_plugin_lock(NULL, &tmp_plugin);
DBUG_PRINT("info", ("setting dbtype to '%.*s' (%d)",
str_db_type_length, next_chunk + 2,
ha_legacy_type(share->db_type())));
}
#ifdef WITH_PARTITION_STORAGE_ENGINE
else if (str_db_type_length == 9 &&