1 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 /* Support rutiner with are using with dbug */
24 
25 #include "myisamdef.h"
26 
27 	/* Print a key in user understandable format */
28 
_mi_print_key(FILE * stream,HA_KEYSEG * keyseg,const uchar * key,uint length)29 void _mi_print_key(FILE *stream, HA_KEYSEG *keyseg,
30 		   const uchar *key, uint length)
31 {
32   int flag;
33   short int s_1;
34   long	int l_1;
35   float f_1;
36   double d_1;
37   const uchar *end;
38   const uchar *key_end=key+length;
39 
40   (void) fputs("Key: \"",stream);
41   flag=0;
42   for (; keyseg->type && key < key_end ;keyseg++)
43   {
44     if (flag++)
45       (void) putc('-',stream);
46     if (keyseg->flag & HA_NULL_PART)
47     {
48       /* A NULL value is encoded by a 1-byte flag. Zero means NULL. */
49       if (! *(key++))
50       {
51 	fprintf(stream,"NULL");
52 	continue;
53       }
54     }
55     end= key + keyseg->length;
56 
57     switch (keyseg->type) {
58     case HA_KEYTYPE_BINARY:
59       if (!(keyseg->flag & HA_SPACE_PACK) && keyseg->length == 1)
60       {						/* packed binary digit */
61 	(void) fprintf(stream,"%d",(uint) *key++);
62 	break;
63       }
64       /* fall through */
65     case HA_KEYTYPE_TEXT:
66     case HA_KEYTYPE_NUM:
67       if (keyseg->flag & HA_SPACE_PACK)
68       {
69 	(void) fprintf(stream,"%.*s",(int) *key,key+1);
70 	key+= (int) *key+1;
71       }
72       else
73       {
74 	(void) fprintf(stream,"%.*s",(int) keyseg->length,key);
75 	key=end;
76       }
77       break;
78     case HA_KEYTYPE_INT8:
79       (void) fprintf(stream,"%d",(int) *((signed char*) key));
80       key=end;
81       break;
82     case HA_KEYTYPE_SHORT_INT:
83       s_1= mi_sint2korr(key);
84       (void) fprintf(stream,"%d",(int) s_1);
85       key=end;
86       break;
87     case HA_KEYTYPE_USHORT_INT:
88       {
89 	ushort u_1;
90 	u_1= mi_uint2korr(key);
91 	(void) fprintf(stream,"%u",(uint) u_1);
92 	key=end;
93 	break;
94       }
95     case HA_KEYTYPE_LONG_INT:
96       l_1=mi_sint4korr(key);
97       (void) fprintf(stream,"%ld",l_1);
98       key=end;
99       break;
100     case HA_KEYTYPE_ULONG_INT:
101       l_1=mi_sint4korr(key);
102       (void) fprintf(stream,"%lu",(ulong) l_1);
103       key=end;
104       break;
105     case HA_KEYTYPE_INT24:
106       (void) fprintf(stream,"%ld",(long) mi_sint3korr(key));
107       key=end;
108       break;
109     case HA_KEYTYPE_UINT24:
110       (void) fprintf(stream,"%lu",(ulong) mi_uint3korr(key));
111       key=end;
112       break;
113     case HA_KEYTYPE_FLOAT:
114       mi_float4get(f_1,key);
115       (void) fprintf(stream,"%g",(double) f_1);
116       key=end;
117       break;
118     case HA_KEYTYPE_DOUBLE:
119       mi_float8get(d_1,key);
120       (void) fprintf(stream,"%g",d_1);
121       key=end;
122       break;
123     case HA_KEYTYPE_LONGLONG:
124     {
125       char buff[21];
126       longlong2str(mi_sint8korr(key),buff,-10);
127       (void) fprintf(stream,"%s",buff);
128       key=end;
129       break;
130     }
131     case HA_KEYTYPE_ULONGLONG:
132     {
133       char buff[21];
134       longlong2str(mi_sint8korr(key),buff,10);
135       (void) fprintf(stream,"%s",buff);
136       key=end;
137       break;
138     }
139     case HA_KEYTYPE_BIT:
140     {
141       uint i;
142       fputs("0x",stream);
143       for (i=0 ; i < keyseg->length ; i++)
144         fprintf(stream, "%02x", (uint) *key++);
145       key= end;
146       break;
147     }
148 
149     case HA_KEYTYPE_VARTEXT1:                   /* VARCHAR and TEXT */
150     case HA_KEYTYPE_VARTEXT2:                   /* VARCHAR and TEXT */
151     case HA_KEYTYPE_VARBINARY1:                 /* VARBINARY and BLOB */
152     case HA_KEYTYPE_VARBINARY2:                 /* VARBINARY and BLOB */
153     {
154       uint tmp_length;
155       get_key_length(tmp_length,key);
156       /*
157 	The following command sometimes gives a warning from valgrind.
158 	Not yet sure if the bug is in valgrind, glibc or mysqld
159       */
160       (void) fprintf(stream,"%.*s",(int) tmp_length,key);
161       key+=tmp_length;
162       break;
163     }
164     default: break;			/* This never happens */
165     }
166   }
167   (void) fputs("\"\n",stream);
168   return;
169 } /* print_key */
170 
171 
172 #ifdef EXTRA_DEBUG
173 /**
174   Check if the named table is in the open list.
175 
176   @param[in]    name    table path as in MYISAM_SHARE::unique_file_name
177   @param[in]    where   verbal description of caller
178 
179   @retval       TRUE    table is in open list
180   @retval       FALSE   table is not in open list
181 
182   @note This function takes THR_LOCK_myisam. Do not call it when
183   this mutex is locked by this thread already.
184 */
185 
check_table_is_closed(const char * name,const char * where)186 my_bool check_table_is_closed(const char *name, const char *where)
187 {
188   char filename[FN_REFLEN];
189   LIST *pos;
190   DBUG_ENTER("check_table_is_closed");
191 
192   (void) fn_format(filename,name,"",MI_NAME_IEXT,4+16+32);
193   mysql_mutex_lock(&THR_LOCK_myisam);
194   for (pos=myisam_open_list ; pos ; pos=pos->next)
195   {
196     MI_INFO *info=(MI_INFO*) pos->data;
197     MYISAM_SHARE *share=info->s;
198     if (!strcmp(share->unique_file_name,filename))
199     {
200       if (share->last_version)
201       {
202         mysql_mutex_unlock(&THR_LOCK_myisam);
203         my_message_local(WARNING_LEVEL,
204                          "Table: %s is open on %s", name,where);
205         DBUG_PRINT("warning",("Table: %s is open on %s", name,where));
206         DBUG_RETURN(1);
207       }
208     }
209   }
210   mysql_mutex_unlock(&THR_LOCK_myisam);
211   DBUG_RETURN(0);
212 }
213 #endif /* EXTRA_DEBUG */
214