/
sql_show.cc
10407 lines (9105 loc) · 340 KB
/
sql_show.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, 2015, Oracle and/or its affiliates.
Copyright (c) 2009, 2023, MariaDB
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-1335 USA */
/* Function with list databases, tables or fields */
#include "sql_plugin.h" // SHOW_MY_BOOL
#include "sql_priv.h"
#include "unireg.h"
#include "sql_acl.h" // fill_schema_*_privileges
#include "sql_select.h" // For select_describe
#include "sql_base.h" // close_tables_for_reopen
#include "create_options.h"
#include "sql_show.h"
#include "sql_table.h" // filename_to_tablename,
// primary_key_name,
// build_table_filename
#include "sql_view.h"
#include "repl_failsafe.h"
#include "sql_parse.h" // check_access, check_table_access
#include "sql_partition.h" // partition_element
#include "sql_derived.h" // mysql_derived_prepare,
// mysql_handle_derived,
#include "sql_db.h" // check_db_dir_existence, load_db_opt_by_name
#include "sql_time.h" // interval_type_to_name
#include "tztime.h" // struct Time_zone
#include "sql_acl.h" // TABLE_ACLS, check_grant, DB_ACLS, acl_get,
// check_grant_db
#include "sp.h"
#include "sp_head.h"
#include "sp_pcontext.h"
#include "set_var.h"
#include "sql_trigger.h"
#include "sql_derived.h"
#include "sql_statistics.h"
#include "sql_connect.h"
#include "authors.h"
#include "contributors.h"
#include "sql_partition.h"
#ifdef HAVE_EVENT_SCHEDULER
#include "events.h"
#include "event_data_objects.h"
#endif
#include <my_dir.h>
#include "lock.h" // MYSQL_OPEN_IGNORE_FLUSH
#include "debug_sync.h"
#include "keycaches.h"
#include "ha_sequence.h"
#ifdef WITH_PARTITION_STORAGE_ENGINE
#include "ha_partition.h"
#endif
#include "transaction.h"
#include "opt_trace.h"
#include "my_cpu.h"
#include "lex_symbol.h"
#define KEYWORD_SIZE 64
extern SYMBOL symbols[];
extern size_t symbols_length;
extern SYMBOL sql_functions[];
extern size_t sql_functions_length;
extern Native_func_registry_array native_func_registry_array;
enum enum_i_s_events_fields
{
ISE_EVENT_CATALOG= 0,
ISE_EVENT_SCHEMA,
ISE_EVENT_NAME,
ISE_DEFINER,
ISE_TIME_ZONE,
ISE_EVENT_BODY,
ISE_EVENT_DEFINITION,
ISE_EVENT_TYPE,
ISE_EXECUTE_AT,
ISE_INTERVAL_VALUE,
ISE_INTERVAL_FIELD,
ISE_SQL_MODE,
ISE_STARTS,
ISE_ENDS,
ISE_STATUS,
ISE_ON_COMPLETION,
ISE_CREATED,
ISE_LAST_ALTERED,
ISE_LAST_EXECUTED,
ISE_EVENT_COMMENT,
ISE_ORIGINATOR,
ISE_CLIENT_CS,
ISE_CONNECTION_CL,
ISE_DB_CL
};
static const LEX_CSTRING trg_action_time_type_names[]=
{
{ STRING_WITH_LEN("BEFORE") },
{ STRING_WITH_LEN("AFTER") }
};
static const LEX_CSTRING trg_event_type_names[]=
{
{ STRING_WITH_LEN("INSERT") },
{ STRING_WITH_LEN("UPDATE") },
{ STRING_WITH_LEN("DELETE") }
};
LEX_CSTRING DATA_clex_str= { STRING_WITH_LEN("DATA") };
LEX_CSTRING INDEX_clex_str= { STRING_WITH_LEN("INDEX") };
#ifndef NO_EMBEDDED_ACCESS_CHECKS
static const char *grant_names[]={
"select","insert","update","delete","create","drop","reload","shutdown",
"process","file","grant","references","index","alter"};
static TYPELIB grant_types = { sizeof(grant_names)/sizeof(char **),
"grant_types",
grant_names, NULL};
#endif
/* Match the values of enum ha_choice */
static const LEX_CSTRING ha_choice_values[]=
{
{ STRING_WITH_LEN("") },
{ STRING_WITH_LEN("0") },
{ STRING_WITH_LEN("1") }
};
static void store_key_options(THD *, String *, TABLE *, KEY *);
static int show_create_view(THD *thd, TABLE_LIST *table, String *buff);
static int show_create_sequence(THD *thd, TABLE_LIST *table_list,
String *packet);
static const LEX_CSTRING *view_algorithm(TABLE_LIST *table);
bool get_lookup_field_values(THD *, COND *, TABLE_LIST *, LOOKUP_FIELD_VALUES *);
/**
Try to lock a mutex, but give up after a short while to not cause deadlocks
The loop is short, as the mutex we are trying to lock are mutex the should
never be locked a long time, just over a few instructions.
@return 0 ok
@return 1 error
*/
static bool trylock_short(mysql_mutex_t *mutex)
{
uint i;
for (i= 0 ; i < 100 ; i++)
{
if (!mysql_mutex_trylock(mutex))
return 0;
LF_BACKOFF();
}
return 1;
}
/***************************************************************************
** List all table types supported
***************************************************************************/
static bool is_show_command(THD *thd)
{
return sql_command_flags[thd->lex->sql_command] & CF_STATUS_COMMAND;
}
static int make_version_string(char *buf, int buf_length, uint version)
{
return (int)my_snprintf(buf, buf_length, "%d.%d", version>>8,version&0xff);
}
static const LEX_CSTRING maturity_name[]={
{ STRING_WITH_LEN("Unknown") },
{ STRING_WITH_LEN("Experimental") },
{ STRING_WITH_LEN("Alpha") },
{ STRING_WITH_LEN("Beta") },
{ STRING_WITH_LEN("Gamma") },
{ STRING_WITH_LEN("Stable") }};
static my_bool show_plugins(THD *thd, plugin_ref plugin,
void *arg)
{
TABLE *table= (TABLE*) arg;
struct st_maria_plugin *plug= plugin_decl(plugin);
struct st_plugin_dl *plugin_dl= plugin_dlib(plugin);
CHARSET_INFO *cs= system_charset_info;
char version_buf[20];
restore_record(table, s->default_values);
table->field[0]->store(plugin_name(plugin)->str,
plugin_name(plugin)->length, cs);
table->field[1]->store(version_buf,
make_version_string(version_buf, sizeof(version_buf), plug->version),
cs);
switch (plugin_state(plugin)) {
case PLUGIN_IS_DELETED:
table->field[2]->store(STRING_WITH_LEN("DELETED"), cs);
break;
case PLUGIN_IS_UNINITIALIZED:
table->field[2]->store(STRING_WITH_LEN("INACTIVE"), cs);
break;
case PLUGIN_IS_READY:
table->field[2]->store(STRING_WITH_LEN("ACTIVE"), cs);
break;
case PLUGIN_IS_DISABLED:
table->field[2]->store(STRING_WITH_LEN("DISABLED"), cs);
break;
case PLUGIN_IS_DYING:
table->field[2]->store(STRING_WITH_LEN("INACTIVE"), cs);
break;
case PLUGIN_IS_FREED: // filtered in fill_plugins, used in fill_all_plugins
table->field[2]->store(STRING_WITH_LEN("NOT INSTALLED"), cs);
break;
default:
DBUG_ASSERT(0);
}
table->field[3]->store(plugin_type_names[plug->type].str,
plugin_type_names[plug->type].length,
cs);
table->field[4]->store(version_buf,
make_version_string(version_buf, sizeof(version_buf),
*(uint *)plug->info), cs);
if (plugin_dl)
{
table->field[5]->store(plugin_dl->dl.str, plugin_dl->dl.length, cs);
table->field[5]->set_notnull();
table->field[6]->store(version_buf,
make_version_string(version_buf, sizeof(version_buf),
plugin_dl->mariaversion),
cs);
table->field[6]->set_notnull();
}
else
{
table->field[5]->set_null();
table->field[6]->set_null();
}
if (plug->author)
{
table->field[7]->store(plug->author, strlen(plug->author), cs);
table->field[7]->set_notnull();
}
else
table->field[7]->set_null();
if (plug->descr)
{
table->field[8]->store(plug->descr, strlen(plug->descr), cs);
table->field[8]->set_notnull();
}
else
table->field[8]->set_null();
switch (plug->license) {
case PLUGIN_LICENSE_GPL:
table->field[9]->store(PLUGIN_LICENSE_GPL_STRING,
strlen(PLUGIN_LICENSE_GPL_STRING), cs);
break;
case PLUGIN_LICENSE_BSD:
table->field[9]->store(PLUGIN_LICENSE_BSD_STRING,
strlen(PLUGIN_LICENSE_BSD_STRING), cs);
break;
default:
table->field[9]->store(PLUGIN_LICENSE_PROPRIETARY_STRING,
strlen(PLUGIN_LICENSE_PROPRIETARY_STRING), cs);
break;
}
table->field[10]->store(
global_plugin_typelib_names[plugin_load_option(plugin)],
strlen(global_plugin_typelib_names[plugin_load_option(plugin)]),
cs);
if (plug->maturity <= MariaDB_PLUGIN_MATURITY_STABLE)
table->field[11]->store(maturity_name[plug->maturity].str,
maturity_name[plug->maturity].length,
cs);
else
table->field[11]->store("Unknown", 7, cs);
if (plug->version_info)
{
table->field[12]->store(plug->version_info,
strlen(plug->version_info), cs);
table->field[12]->set_notnull();
}
else
table->field[12]->set_null();
return schema_table_store_record(thd, table);
}
int fill_plugins(THD *thd, TABLE_LIST *tables, COND *cond)
{
DBUG_ENTER("fill_plugins");
TABLE *table= tables->table;
if (plugin_foreach_with_mask(thd, show_plugins, MYSQL_ANY_PLUGIN,
~PLUGIN_IS_FREED, table))
DBUG_RETURN(1);
DBUG_RETURN(0);
}
int fill_all_plugins(THD *thd, TABLE_LIST *tables, COND *cond)
{
DBUG_ENTER("fill_all_plugins");
TABLE *table= tables->table;
LOOKUP_FIELD_VALUES lookup;
if (get_lookup_field_values(thd, cond, tables, &lookup))
DBUG_RETURN(0);
if (lookup.db_value.str && !lookup.db_value.str[0])
DBUG_RETURN(0); // empty string never matches a valid SONAME
MY_DIR *dirp= my_dir(opt_plugin_dir, MY_THREAD_SPECIFIC);
if (!dirp)
{
my_error(ER_CANT_READ_DIR, MYF(0), opt_plugin_dir, my_errno);
DBUG_RETURN(1);
}
if (!lookup.db_value.str)
plugin_dl_foreach(thd, 0, show_plugins, table);
const char *wstr= lookup.db_value.str, *wend= wstr + lookup.db_value.length;
for (size_t i=0; i < dirp->number_of_files; i++)
{
FILEINFO *file= dirp->dir_entry+i;
LEX_CSTRING dl= { file->name, strlen(file->name) };
const char *dlend= dl.str + dl.length;
const size_t so_ext_len= sizeof(SO_EXT) - 1;
if (strcasecmp(dlend - so_ext_len, SO_EXT))
continue;
if (lookup.db_value.str)
{
if (lookup.wild_db_value)
{
if (files_charset_info->wildcmp(dl.str, dlend, wstr, wend,
wild_prefix, wild_one, wild_many))
continue;
}
else
{
if (files_charset_info->strnncoll(dl.str, dl.length,
lookup.db_value.str, lookup.db_value.length))
continue;
}
}
plugin_dl_foreach(thd, &dl, show_plugins, table);
thd->clear_error();
}
my_dirend(dirp);
DBUG_RETURN(0);
}
/***************************************************************************
** List all Authors.
** If you can update it, you get to be in it :)
***************************************************************************/
bool mysqld_show_authors(THD *thd)
{
List<Item> field_list;
Protocol *protocol= thd->protocol;
MEM_ROOT *mem_root= thd->mem_root;
DBUG_ENTER("mysqld_show_authors");
field_list.push_back(new (mem_root) Item_empty_string(thd, "Name", 40),
mem_root);
field_list.push_back(new (mem_root) Item_empty_string(thd, "Location", 40),
mem_root);
field_list.push_back(new (mem_root) Item_empty_string(thd, "Comment", 512),
mem_root);
if (protocol->send_result_set_metadata(&field_list,
Protocol::SEND_NUM_ROWS |
Protocol::SEND_EOF))
DBUG_RETURN(TRUE);
show_table_authors_st *authors;
for (authors= show_table_authors; authors->name; authors++)
{
protocol->prepare_for_resend();
protocol->store(authors->name, strlen(authors->name), system_charset_info);
protocol->store(authors->location, strlen(authors->location),
system_charset_info);
protocol->store(authors->comment, strlen(authors->comment),
system_charset_info);
if (protocol->write())
DBUG_RETURN(TRUE);
}
my_eof(thd);
DBUG_RETURN(FALSE);
}
/***************************************************************************
** List all Contributors.
** Please get permission before updating
***************************************************************************/
bool mysqld_show_contributors(THD *thd)
{
List<Item> field_list;
Protocol *protocol= thd->protocol;
MEM_ROOT *mem_root= thd->mem_root;
DBUG_ENTER("mysqld_show_contributors");
field_list.push_back(new (mem_root) Item_empty_string(thd, "Name", 40),
mem_root);
field_list.push_back(new (mem_root) Item_empty_string(thd, "Location", 40),
mem_root);
field_list.push_back(new (mem_root) Item_empty_string(thd, "Comment", 512),
mem_root);
if (protocol->send_result_set_metadata(&field_list,
Protocol::SEND_NUM_ROWS |
Protocol::SEND_EOF))
DBUG_RETURN(TRUE);
show_table_contributors_st *contributors;
for (contributors= show_table_contributors; contributors->name; contributors++)
{
protocol->prepare_for_resend();
protocol->store(contributors->name, strlen(contributors->name),
system_charset_info);
protocol->store(contributors->location, strlen(contributors->location),
system_charset_info);
protocol->store(contributors->comment, strlen(contributors->comment),
system_charset_info);
if (protocol->write())
DBUG_RETURN(TRUE);
}
my_eof(thd);
DBUG_RETURN(FALSE);
}
/***************************************************************************
List all privileges supported
***************************************************************************/
struct show_privileges_st {
const char *privilege;
const char *context;
const char *comment;
};
static struct show_privileges_st sys_privileges[]=
{
{"Alter", "Tables", "To alter the table"},
{"Alter routine", "Functions,Procedures", "To alter or drop stored functions/procedures"},
{"Create", "Databases,Tables,Indexes", "To create new databases and tables"},
{"Create routine","Databases","To use CREATE FUNCTION/PROCEDURE"},
{"Create temporary tables","Databases","To use CREATE TEMPORARY TABLE"},
{"Create view", "Tables", "To create new views"},
{"Create user", "Server Admin", "To create new users"},
{"Delete", "Tables", "To delete existing rows"},
{"Delete history", "Tables", "To delete versioning table historical rows"},
{"Drop", "Databases,Tables", "To drop databases, tables, and views"},
#ifdef HAVE_EVENT_SCHEDULER
{"Event","Server Admin","To create, alter, drop and execute events"},
#endif
{"Execute", "Functions,Procedures", "To execute stored routines"},
{"File", "File access on server", "To read and write files on the server"},
{"Grant option", "Databases,Tables,Functions,Procedures", "To give to other users those privileges you possess"},
{"Index", "Tables", "To create or drop indexes"},
{"Insert", "Tables", "To insert data into tables"},
{"Lock tables","Databases","To use LOCK TABLES (together with SELECT privilege)"},
{"Process", "Server Admin", "To view the plain text of currently executing queries"},
{"Proxy", "Server Admin", "To make proxy user possible"},
{"References", "Databases,Tables", "To have references on tables"},
{"Reload", "Server Admin", "To reload or refresh tables, logs and privileges"},
{"Binlog admin", "Server", "To purge binary logs"},
{"Binlog monitor", "Server", "To use SHOW BINLOG STATUS and SHOW BINARY LOG"},
{"Binlog replay", "Server", "To use BINLOG (generated by mariadb-binlog)"},
{"Replication master admin", "Server", "To monitor connected slaves"},
{"Replication slave admin", "Server", "To start/stop slave and apply binlog events"},
{"Slave monitor", "Server", "To use SHOW SLAVE STATUS and SHOW RELAYLOG EVENTS"},
{"Replication slave","Server Admin","To read binary log events from the master"},
{"Select", "Tables", "To retrieve rows from table"},
{"Show databases","Server Admin","To see all databases with SHOW DATABASES"},
{"Show view","Tables","To see views with SHOW CREATE VIEW"},
{"Shutdown","Server Admin", "To shut down the server"},
{"Super","Server Admin","To use KILL thread, SET GLOBAL, CHANGE MASTER, etc."},
{"Trigger","Tables", "To use triggers"},
{"Create tablespace", "Server Admin", "To create/alter/drop tablespaces"},
{"Update", "Tables", "To update existing rows"},
{"Set user","Server", "To create views and stored routines with a different definer"},
{"Federated admin", "Server", "To execute the CREATE SERVER, ALTER SERVER, DROP SERVER statements"},
{"Connection admin", "Server", "To bypass connection limits and kill other users' connections"},
{"Read_only admin", "Server", "To perform write operations even if @@read_only=ON"},
{"Usage","Server Admin","No privileges - allow connect only"},
{NullS, NullS, NullS}
};
bool mysqld_show_privileges(THD *thd)
{
List<Item> field_list;
Protocol *protocol= thd->protocol;
MEM_ROOT *mem_root= thd->mem_root;
DBUG_ENTER("mysqld_show_privileges");
field_list.push_back(new (mem_root) Item_empty_string(thd, "Privilege", 10),
mem_root);
field_list.push_back(new (mem_root) Item_empty_string(thd, "Context", 15),
mem_root);
field_list.push_back(new (mem_root) Item_empty_string(thd, "Comment",
NAME_CHAR_LEN),
mem_root);
if (protocol->send_result_set_metadata(&field_list,
Protocol::SEND_NUM_ROWS |
Protocol::SEND_EOF))
DBUG_RETURN(TRUE);
show_privileges_st *privilege= sys_privileges;
for (privilege= sys_privileges; privilege->privilege ; privilege++)
{
protocol->prepare_for_resend();
protocol->store(privilege->privilege, strlen(privilege->privilege),
system_charset_info);
protocol->store(privilege->context, strlen(privilege->context),
system_charset_info);
protocol->store(privilege->comment, strlen(privilege->comment),
system_charset_info);
if (protocol->write())
DBUG_RETURN(TRUE);
}
my_eof(thd);
DBUG_RETURN(FALSE);
}
/** Hash of LEX_STRINGs used to search for ignored db directories. */
static HASH ignore_db_dirs_hash;
/**
An array of LEX_STRING pointers to collect the options at
option parsing time.
*/
static DYNAMIC_ARRAY ignore_db_dirs_array;
/**
A value for the read only system variable to show a list of
ignored directories.
*/
char *opt_ignore_db_dirs= NULL;
/**
This flag is ON if:
- the list of ignored directories is not empty
- and some of the ignored directory names
need no tablename-to-filename conversion.
Otherwise, if the name of the directory contains
unconditional characters like '+' or '.', they
never can match the database directory name. So the
db_name_is_in_ignore_db_dirs_list() can just return at once.
*/
static bool skip_ignored_dir_check= TRUE;
/**
Sets up the data structures for collection of directories at option
processing time.
We need to collect the directories in an array first, because
we need the character sets initialized before setting up the hash.
@return state
@retval TRUE failed
@retval FALSE success
*/
bool
ignore_db_dirs_init()
{
return my_init_dynamic_array(key_memory_ignored_db, &ignore_db_dirs_array,
sizeof(LEX_STRING *), 0, 0, MYF(0));
}
/**
Retrieves the key (the string itself) from the LEX_STRING hash members.
Needed by hash_init().
@param data the data element from the hash
@param out len_ret Placeholder to return the length of the key
@param unused
@return a pointer to the key
*/
static uchar *
db_dirs_hash_get_key(const uchar *data, size_t *len_ret,
my_bool __attribute__((unused)))
{
LEX_CSTRING *e= (LEX_CSTRING *) data;
*len_ret= e->length;
return (uchar *) e->str;
}
/**
Wrap a directory name into a LEX_STRING and push it to the array.
Called at option processing time for each --ignore-db-dir option.
@param path the name of the directory to push
@return state
@retval TRUE failed
@retval FALSE success
*/
bool push_ignored_db_dir(const char *path)
{
LEX_CSTRING *new_elt;
char *new_elt_buffer;
size_t path_len= strlen(path);
if (!path_len || path_len >= FN_REFLEN)
return true;
// No need to normalize, it's only a directory name, not a path.
if (!my_multi_malloc(key_memory_ignored_db, MYF(0),
&new_elt, sizeof(LEX_STRING),
&new_elt_buffer, path_len + 1,
NullS))
return true;
new_elt->str= new_elt_buffer;
memcpy(new_elt_buffer, path, path_len);
new_elt_buffer[path_len]= 0;
new_elt->length= path_len;
return insert_dynamic(&ignore_db_dirs_array, (uchar*) &new_elt);
}
/**
Clean up the directory ignore options accumulated so far.
Called at option processing time for each --ignore-db-dir option
with an empty argument.
*/
void
ignore_db_dirs_reset()
{
LEX_CSTRING **elt;
while (NULL!= (elt= (LEX_CSTRING **) pop_dynamic(&ignore_db_dirs_array)))
if (elt && *elt)
my_free(*elt);
}
/**
Free the directory ignore option variables.
Called at server shutdown.
*/
void
ignore_db_dirs_free()
{
if (opt_ignore_db_dirs)
{
my_free(opt_ignore_db_dirs);
opt_ignore_db_dirs= NULL;
}
ignore_db_dirs_reset();
delete_dynamic(&ignore_db_dirs_array);
my_hash_free(&ignore_db_dirs_hash);
}
/**
Initialize the ignore db directories hash and status variable from
the options collected in the array.
Called when option processing is over and the server's in-memory
structures are fully initialized.
@return state
@retval TRUE failed
@retval FALSE success
*/
static void dispose_db_dir(void *ptr)
{
my_free(ptr);
}
/*
Append an element into @@ignore_db_dirs
This is a function to be called after regular option processing has been
finalized.
*/
void ignore_db_dirs_append(const char *dirname_arg)
{
char *new_entry_buf;
LEX_STRING *new_entry;
size_t len= strlen(dirname_arg);
if (!my_multi_malloc(PSI_INSTRUMENT_ME, MYF(0),
&new_entry, sizeof(LEX_STRING),
&new_entry_buf, len + 1,
NullS))
return;
memcpy(new_entry_buf, dirname_arg, len+1);
new_entry->str = new_entry_buf;
new_entry->length= len;
if (my_hash_insert(&ignore_db_dirs_hash, (uchar *)new_entry))
{
// Either the name is already there or out-of-memory.
my_free(new_entry);
return;
}
// Append the name to the option string.
size_t curlen= strlen(opt_ignore_db_dirs);
// Add one for comma and one for \0.
size_t newlen= curlen + len + 1 + 1;
char *new_db_dirs;
if (!(new_db_dirs= (char*)my_malloc(PSI_INSTRUMENT_ME, newlen, MYF(0))))
{
// This is not a critical condition
return;
}
memcpy(new_db_dirs, opt_ignore_db_dirs, curlen);
if (curlen != 0)
new_db_dirs[curlen]=',';
memcpy(new_db_dirs + (curlen + ((curlen!=0)?1:0)), dirname_arg, len+1);
if (opt_ignore_db_dirs)
my_free(opt_ignore_db_dirs);
opt_ignore_db_dirs= new_db_dirs;
}
bool
ignore_db_dirs_process_additions()
{
ulong i;
size_t len;
char *ptr;
LEX_CSTRING *dir;
skip_ignored_dir_check= TRUE;
if (my_hash_init(key_memory_ignored_db, &ignore_db_dirs_hash,
lower_case_table_names ? character_set_filesystem :
&my_charset_bin, 0, 0, 0, db_dirs_hash_get_key,
dispose_db_dir, HASH_UNIQUE))
return true;
/* len starts from 1 because of the terminating zero. */
len= 1;
for (i= 0; i < ignore_db_dirs_array.elements; i++)
{
get_dynamic(&ignore_db_dirs_array, (uchar *) &dir, i);
len+= dir->length + 1; // +1 for the comma
if (skip_ignored_dir_check)
{
char buff[FN_REFLEN];
(void) tablename_to_filename(dir->str, buff, sizeof(buff));
skip_ignored_dir_check= strcmp(dir->str, buff) != 0;
}
}
/* No delimiter for the last directory. */
if (len > 1)
len--;
/* +1 the terminating zero */
ptr= opt_ignore_db_dirs= (char *) my_malloc(key_memory_ignored_db, len + 1,
MYF(0));
if (!ptr)
return true;
/* Make sure we have an empty string to start with. */
*ptr= 0;
for (i= 0; i < ignore_db_dirs_array.elements; i++)
{
get_dynamic(&ignore_db_dirs_array, (uchar *) &dir, i);
if (my_hash_insert(&ignore_db_dirs_hash, (uchar *)dir))
{
/* ignore duplicates from the config file */
if (my_hash_search(&ignore_db_dirs_hash, (uchar *)dir->str, dir->length))
{
sql_print_warning("Duplicate ignore-db-dir directory name '%.*s' "
"found in the config file(s). Ignoring the duplicate.",
(int) dir->length, dir->str);
my_free(dir);
goto continue_loop;
}
return true;
}
ptr= strnmov(ptr, dir->str, dir->length);
*(ptr++)= ',';
continue_loop:
/*
Set the transferred array element to NULL to avoid double free
in case of error.
*/
dir= NULL;
set_dynamic(&ignore_db_dirs_array, (uchar *) &dir, i);
}
if (ptr > opt_ignore_db_dirs)
{
ptr--;
DBUG_ASSERT(*ptr == ',');
}
/* make sure the string is terminated */
DBUG_ASSERT(ptr - opt_ignore_db_dirs <= (ptrdiff_t) len);
*ptr= 0;
/*
It's OK to empty the array here as the allocated elements are
referenced through the hash now.
*/
reset_dynamic(&ignore_db_dirs_array);
return false;
}
/**
Check if a directory name is in the hash of ignored directories.
@return search result
@retval TRUE found
@retval FALSE not found
*/
static inline bool
is_in_ignore_db_dirs_list(const char *directory)
{
return ignore_db_dirs_hash.records &&
NULL != my_hash_search(&ignore_db_dirs_hash, (const uchar *) directory,
strlen(directory));
}
/**
Check if a database name is in the hash of ignored directories.
@return search result
@retval TRUE found
@retval FALSE not found
*/
bool
db_name_is_in_ignore_db_dirs_list(const char *directory)
{
char buff[FN_REFLEN];
uint buff_len;
if (skip_ignored_dir_check)
return 0;
buff_len= tablename_to_filename(directory, buff, sizeof(buff));
return my_hash_search(&ignore_db_dirs_hash, (uchar *) buff, buff_len)!=NULL;
}
enum find_files_result {
FIND_FILES_OK,
FIND_FILES_OOM,
FIND_FILES_DIR
};
/*
find_files() - find files in a given directory.
SYNOPSIS
find_files()
thd thread handler
files put found files in this list
db database name to search tables in
or NULL to search for databases
path path to database
wild filter for found files
RETURN
FIND_FILES_OK success
FIND_FILES_OOM out of memory error
FIND_FILES_DIR no such directory, or directory can't be read
*/
static find_files_result
find_files(THD *thd, Dynamic_array<LEX_CSTRING*> *files, LEX_CSTRING *db,
const char *path, const LEX_CSTRING *wild)
{
MY_DIR *dirp;
Discovered_table_list tl(thd, files, wild);
DBUG_ENTER("find_files");
if (!(dirp = my_dir(path, MY_THREAD_SPECIFIC | (db ? 0 : MY_WANT_STAT))))
{
if (my_errno == ENOENT)
my_error(ER_BAD_DB_ERROR, MYF(0), db->str);
else
my_error(ER_CANT_READ_DIR, MYF(0), path, my_errno);
DBUG_RETURN(FIND_FILES_DIR);
}
if (!db) /* Return databases */
{
for (size_t i=0; i < dirp->number_of_files; i++)
{
FILEINFO *file= dirp->dir_entry+i;
#ifdef USE_SYMDIR
char *ext;
char buff[FN_REFLEN];
if (my_use_symdir && !strcmp(ext=fn_ext(file->name), ".sym"))
{
/* Only show the sym file if it points to a directory */
char *end;
*ext=0; /* Remove extension */
unpack_dirname(buff, file->name);
end= strend(buff);
if (end != buff && end[-1] == FN_LIBCHAR)
end[-1]= 0; // Remove end FN_LIBCHAR
if (!mysql_file_stat(key_file_misc, buff, file->mystat, MYF(0)))
continue;
}
#endif
if (!MY_S_ISDIR(file->mystat->st_mode))
continue;
if (is_in_ignore_db_dirs_list(file->name))
continue;
if (tl.add_file(file->name))
goto err;
}
}
else
{
if (ha_discover_table_names(thd, db, dirp, &tl, false))
goto err;
}
if (is_show_command(thd))
tl.sort();
#ifndef DBUG_OFF
else
{
/*
sort_desc() is used to find easier unstable mtr tests that query
INFORMATION_SCHEMA.{SCHEMATA|TABLES} without a proper ORDER BY.
This can be removed in some release after 10.3 (e.g. in 10.4).