1 /* Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
2 Copyright (c) 2009, 2020, MariaDB Corporation.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
16
17
18 /**
19 @file
20
21 @brief
22 Functions for discover of frm file from handler
23 */
24
25 #include "mariadb.h"
26 #include "sql_priv.h"
27 #include "unireg.h"
28 #include "discover.h"
29 #include <my_dir.h>
30
31 /**
32 Read the contents of a .frm file.
33
34 frmdata and len are set to 0 on error.
35
36 @param name path to table-file "db/name"
37 @param frmdata frm data
38 @param len length of the read frmdata
39
40 @retval
41 0 ok
42 @retval
43 1 Could not open file
44 @retval
45 2 Could not stat file
46 @retval
47 3 Could not allocate data for read. Could not read file
48 */
49
readfrm(const char * name,const uchar ** frmdata,size_t * len)50 int readfrm(const char *name, const uchar **frmdata, size_t *len)
51 {
52 int error;
53 char index_file[FN_REFLEN];
54 File file;
55 size_t read_len;
56 uchar *read_data;
57 MY_STAT state;
58 DBUG_ENTER("readfrm");
59 DBUG_PRINT("enter",("name: '%s'",name));
60
61 *frmdata= NULL; // In case of errors
62 *len= 0;
63 error= 1;
64 if ((file= mysql_file_open(key_file_frm,
65 fn_format(index_file, name, "", reg_ext,
66 MY_UNPACK_FILENAME|MY_APPEND_EXT),
67 O_RDONLY | O_SHARE,
68 MYF(0))) < 0)
69 goto err_end;
70
71 // Get length of file
72 error= 2;
73 if (mysql_file_fstat(file, &state, MYF(0)))
74 goto err;
75 read_len= (size_t)MY_MIN(FRM_MAX_SIZE, state.st_size); // safety
76
77 // Read whole frm file
78 error= 3;
79 if (!(read_data= (uchar*)my_malloc(key_memory_frm_string, read_len,
80 MYF(MY_WME))))
81 goto err;
82 if (mysql_file_read(file, read_data, read_len, MYF(MY_NABP)))
83 {
84 my_free(read_data);
85 goto err;
86 }
87
88 // Setup return data
89 *frmdata= (uchar*) read_data;
90 *len= read_len;
91 error= 0;
92
93 err:
94 (void) mysql_file_close(file, MYF(MY_WME));
95
96 err_end: /* Here when no file */
97 DBUG_RETURN (error);
98 } /* readfrm */
99
100
101 /*
102 Write the content of a frm data pointer to a frm or par file.
103
104 @param path full path to table-file "db/name.frm" or .par
105 @param db Database name. Only used for my_error()
106 @param table Table name. Only used for my_error()
107 @param data data to write to file
108 @param len length of the data
109
110 @retval
111 0 ok
112 @retval
113 <> 0 Could not write file. In this case the file is not created
114 */
115
writefile(const char * path,const char * db,const char * table,bool tmp_table,const uchar * data,size_t len)116 int writefile(const char *path, const char *db, const char *table,
117 bool tmp_table, const uchar *data, size_t len)
118 {
119 int error;
120 int create_flags= O_RDWR | O_TRUNC;
121 DBUG_ENTER("writefile");
122 DBUG_PRINT("enter",("name: '%s' len: %lu ",path, (ulong) len));
123
124 if (tmp_table)
125 create_flags|= O_EXCL | O_NOFOLLOW;
126
127 File file= mysql_file_create(key_file_frm, path,
128 CREATE_MODE, create_flags, MYF(0));
129
130 if (unlikely((error= file < 0)))
131 {
132 if (my_errno == ENOENT)
133 my_error(ER_BAD_DB_ERROR, MYF(0), db);
134 else
135 my_error(ER_CANT_CREATE_TABLE, MYF(0), db, table, my_errno);
136 }
137 else
138 {
139 error= (int)mysql_file_write(file, data, len, MYF(MY_WME | MY_NABP));
140
141 if (!error && !tmp_table && opt_sync_frm)
142 error= mysql_file_sync(file, MYF(MY_WME)) ||
143 my_sync_dir_by_file(path, MYF(MY_WME));
144
145 error|= mysql_file_close(file, MYF(MY_WME));
146 if (error)
147 my_delete(path, MYF(0));
148 }
149 DBUG_RETURN(error);
150 } /* writefile */
151
152
advance(FILEINFO * & from,FILEINFO * & to,FILEINFO * cur,bool & skip)153 static inline void advance(FILEINFO* &from, FILEINFO* &to,
154 FILEINFO* cur, bool &skip)
155 {
156 if (skip) // if not copying
157 from= cur; // just advance the start pointer
158 else // if copying
159 if (to == from) // but to the same place, not shifting the data
160 from= to= cur; // advance both pointers
161 else // otherwise
162 while (from < cur) // have to copy [from...cur) to [to...)
163 *to++ = *from++;
164 skip= false;
165 }
166
167 /**
168 Go through the directory listing looking for files with a specified
169 extension and add them to the result list
170
171 @details
172 This function may be called many times on the same directory listing
173 but with different extensions. To avoid discovering the same table twice,
174 whenever a table file is discovered, all files with the same name
175 (independently from the extensions) are removed from the list.
176
177 Example: the list contained
178 { "db.opt", "t1.MYD", "t1.MYI", "t1.frm", "t2.ARZ", "t3.ARZ", "t3.frm" }
179 on discovering all ".frm" files, tables "t1" and "t3" will be found,
180 and list will become
181 { "db.opt", "t2.ARZ" }
182 and now ".ARZ" discovery can discover the table "t2"
183
184 @note
185 This function assumes that the directory listing is sorted alphabetically.
186
187 @note Partitioning makes this more complicated. A partitioned table t1 might
188 have files, like t1.frm, t1#P#part1.ibd, t1#P#foo.ibd, etc.
189 That means we need to compare file names only up to the first '#' or '.'
190 whichever comes first.
191 */
extension_based_table_discovery(MY_DIR * dirp,const char * ext_meta,handlerton::discovered_list * result)192 int extension_based_table_discovery(MY_DIR *dirp, const char *ext_meta,
193 handlerton::discovered_list *result)
194 {
195 CHARSET_INFO *cs= character_set_filesystem;
196 size_t ext_meta_len= strlen(ext_meta);
197 FILEINFO *from, *to, *cur, *end;
198 bool skip= false;
199
200 from= to= cur= dirp->dir_entry;
201 end= cur + dirp->number_of_files;
202 while (cur < end)
203 {
204 char *octothorp= strchr(cur->name + 1, '#');
205 char *ext= strchr(octothorp ? octothorp : cur->name, FN_EXTCHAR);
206
207 if (ext)
208 {
209 size_t len= (octothorp ? octothorp : ext) - cur->name;
210 if (from != cur &&
211 (strlen(from->name) <= len ||
212 cs->strnncoll(from->name, len, cur->name, len) ||
213 (from->name[len] != FN_EXTCHAR && from->name[len] != '#')))
214 advance(from, to, cur, skip);
215
216 if (cs->strnncoll(ext, strlen(ext),
217 ext_meta, ext_meta_len) == 0)
218 {
219 *ext = 0;
220 if (result->add_file(cur->name))
221 return 1;
222 *ext = FN_EXTCHAR;
223 skip= true; // table discovered, skip all files with the same name
224 }
225 }
226 else
227 {
228 advance(from, to, cur, skip);
229 from++;
230 }
231
232 cur++;
233 }
234 advance(from, to, cur, skip);
235 dirp->number_of_files= (uint)(to - dirp->dir_entry);
236 return 0;
237 }
238
239 /**
240 Simple, not reusable file-based table discovery
241
242 @details
243 simplified version of extension_based_table_discovery(), that does not
244 modify the list of files. It cannot be called many times for the same
245 directory listing, otherwise it'll produce duplicate results.
246 */
ext_table_discovery_simple(MY_DIR * dirp,handlerton::discovered_list * result)247 int ext_table_discovery_simple(MY_DIR *dirp,
248 handlerton::discovered_list *result)
249 {
250 CHARSET_INFO *cs= character_set_filesystem;
251 FILEINFO *cur, *end;
252
253 cur= dirp->dir_entry;
254 end= cur + dirp->number_of_files;
255 while (cur < end)
256 {
257 char *ext= strrchr(cur->name, FN_EXTCHAR);
258
259 if (ext)
260 {
261 if (cs->strnncoll(ext, strlen(ext),
262 reg_ext, reg_ext_length) == 0)
263 {
264 *ext = 0;
265 if (result->add_file(cur->name))
266 return 1;
267 }
268 }
269 cur++;
270 }
271 return 0;
272 }
273
274