16 #include <my_global.h>
19 #include "rpl_info_file.h"
23 int init_ulongvar_from_file(ulong* var,
IO_CACHE* f, ulong default_val);
24 int init_strvar_from_file(
char *var,
int max_size,
IO_CACHE *f,
25 const char *default_val);
26 int init_intvar_from_file(
int* var,
IO_CACHE* f,
int default_val);
27 int init_floatvar_from_file(
float* var,
IO_CACHE* f,
float default_val);
28 bool init_dynarray_intvar_from_file(
char *buffer,
size_t size,
31 Rpl_info_file::Rpl_info_file(
const int nparam,
32 const char* param_pattern_fname,
33 const char* param_info_fname,
35 :Rpl_info_handler(nparam), info_fd(-1), name_indexed(indexed_arg)
37 DBUG_ENTER(
"Rpl_info_file::Rpl_info_file");
39 memset(&info_file, 0,
sizeof(info_file));
40 fn_format(pattern_fname, param_pattern_fname, mysql_data_home,
"", 4 + 32);
41 fn_format(info_fname, param_info_fname, mysql_data_home,
"", 4 + 32);
46 Rpl_info_file::~Rpl_info_file()
48 DBUG_ENTER(
"Rpl_info_file::~Rpl_info_file");
55 int Rpl_info_file::do_init_info(uint instance)
57 DBUG_ENTER(
"Rpl_info_file::do_init_info(uint)");
59 char fname_local[FN_REFLEN];
60 char *pos= strmov(fname_local, pattern_fname);
62 sprintf(pos,
"%u", instance);
64 fn_format(info_fname, fname_local, mysql_data_home,
"", 4 + 32);
65 DBUG_RETURN(do_init_info());
68 int Rpl_info_file::do_init_info()
71 DBUG_ENTER(
"Rpl_info_file::do_init_info");
74 enum_return_check ret_check= do_check_info();
75 if (ret_check == REPOSITORY_DOES_NOT_EXIST)
83 if (my_b_inited(&info_file))
84 end_io_cache(&info_file);
85 my_close(info_fd, MYF(MY_WME));
87 if ((info_fd = my_open(info_fname, O_CREAT|O_RDWR|O_BINARY, MYF(MY_WME))) < 0)
89 sql_print_error(
"Failed to create a new info file (\
90 file '%s', errno %d)", info_fname, my_errno);
93 else if (init_io_cache(&info_file, info_fd, IO_SIZE*2, READ_CACHE, 0L,0,
96 sql_print_error(
"Failed to create a cache on info file (\
97 file '%s')", info_fname);
103 my_close(info_fd, MYF(0));
108 else if (ret_check == REPOSITORY_EXISTS)
111 reinit_io_cache(&info_file, READ_CACHE, 0L,0,0);
114 if ((info_fd = my_open(info_fname, O_RDWR|O_BINARY, MYF(MY_WME))) < 0 )
116 sql_print_error(
"Failed to open the existing info file (\
117 file '%s', errno %d)", info_fname, my_errno);
120 else if (init_io_cache(&info_file, info_fd, IO_SIZE*2, READ_CACHE, 0L,
123 sql_print_error(
"Failed to create a cache on info file (\
124 file '%s')", info_fname);
130 my_close(info_fd, MYF(0));
140 int Rpl_info_file::do_prepare_info_for_read()
144 return (reinit_io_cache(&info_file, READ_CACHE, 0L, 0, 0));
147 int Rpl_info_file::do_prepare_info_for_write()
151 return (reinit_io_cache(&info_file, WRITE_CACHE, 0L, 0, 1));
154 inline enum_return_check do_check_repository_file(
const char *fname)
156 if (my_access(fname, F_OK))
157 return REPOSITORY_DOES_NOT_EXIST;
159 if (my_access(fname, F_OK | R_OK | W_OK))
160 return ERROR_CHECKING_REPOSITORY;
162 return REPOSITORY_EXISTS;
175 enum_return_check Rpl_info_file::do_check_info(uint instance)
178 enum_return_check last_check= REPOSITORY_EXISTS;
179 char fname_local[FN_REFLEN];
182 for (i= 1; i <= instance && last_check == REPOSITORY_EXISTS; i++)
184 pos= strmov(fname_local, pattern_fname);
186 sprintf(pos,
"%u", i);
187 fn_format(fname_local, fname_local, mysql_data_home,
"", 4 + 32);
188 last_check= do_check_repository_file(fname_local);
193 enum_return_check Rpl_info_file::do_check_info()
195 return do_check_repository_file(info_fname);
215 bool Rpl_info_file::do_count_info(
const int nparam,
216 const char* param_pattern,
221 Rpl_info_file* info= NULL;
223 char fname_local[FN_REFLEN];
225 enum_return_check last_check= REPOSITORY_EXISTS;
227 DBUG_ENTER(
"Rpl_info_file::do_count_info");
229 if (!(info=
new Rpl_info_file(nparam, param_pattern,
"", indexed)))
232 for (i= 1; last_check == REPOSITORY_EXISTS; i++)
234 pos= strmov(fname_local, param_pattern);
237 sprintf(pos,
"%u", i);
239 fn_format(fname_local, fname_local, mysql_data_home,
"", 4 + 32);
240 if ((last_check= do_check_repository_file(fname_local)) == REPOSITORY_EXISTS)
251 int Rpl_info_file::do_flush_info(
const bool force)
255 DBUG_ENTER(
"Rpl_info_file::do_flush_info");
257 if (flush_io_cache(&info_file))
259 if (!error && (force ||
261 ++(sync_counter) >= sync_period)))
263 if (my_sync(info_fd, MYF(MY_WME)))
271 void Rpl_info_file::do_end_info()
273 DBUG_ENTER(
"Rpl_info_file::do_end_info");
277 if (my_b_inited(&info_file))
278 end_io_cache(&info_file);
279 my_close(info_fd, MYF(MY_WME));
286 int Rpl_info_file::do_remove_info()
291 DBUG_ENTER(
"Rpl_info_file::do_remove_info");
293 if (my_stat(info_fname, &stat_area, MYF(0)) && my_delete(info_fname, MYF(MY_WME)))
299 int Rpl_info_file::do_clean_info()
308 int Rpl_info_file::do_reset_info(
const int nparam,
309 const char* param_pattern,
314 Rpl_info_file* info= NULL;
315 char fname_local[FN_REFLEN];
317 enum_return_check last_check= REPOSITORY_EXISTS;
319 DBUG_ENTER(
"Rpl_info_file::do_count_info");
321 if (!(info=
new Rpl_info_file(nparam, param_pattern,
"", indexed)))
324 for (i= 1; last_check == REPOSITORY_EXISTS; i++)
326 pos= strmov(fname_local, param_pattern);
329 sprintf(pos,
"%u", i);
331 fn_format(fname_local, fname_local, mysql_data_home,
"", 4 + 32);
332 if ((last_check= do_check_repository_file(fname_local)) == REPOSITORY_EXISTS)
333 if (my_delete(fname_local, MYF(MY_WME)))
344 bool Rpl_info_file::do_set_info(
const int pos,
const char *value)
346 return (my_b_printf(&info_file,
"%s\n", value) > (
size_t) 0 ?
350 bool Rpl_info_file::do_set_info(
const int pos,
const uchar *value,
353 return (my_b_write(&info_file, value, size));
356 bool Rpl_info_file::do_set_info(
const int pos,
const ulong value)
358 return (my_b_printf(&info_file,
"%lu\n", value) > (
size_t) 0 ?
362 bool Rpl_info_file::do_set_info(
const int pos,
const int value)
364 return (my_b_printf(&info_file,
"%d\n", value) > (
size_t) 0 ?
368 bool Rpl_info_file::do_set_info(
const int pos,
const float value)
383 sprintf(buffer,
"%.3f", value);
385 return (my_b_printf(&info_file,
"%s\n", buffer) > (
size_t) 0 ?
389 bool Rpl_info_file::do_set_info(
const int pos,
const Dynamic_ids *value)
397 if (const_cast<Dynamic_ids *>(value)->pack_dynamic_ids(&buffer))
400 error= (my_b_printf(&info_file,
"%s\n", buffer.c_ptr_safe()) >
401 (
size_t) 0 ? FALSE : TRUE);
406 bool Rpl_info_file::do_get_info(
const int pos,
char *value,
const size_t size,
407 const char *default_value)
409 return (init_strvar_from_file(value, size, &info_file,
413 bool Rpl_info_file::do_get_info(
const int pos, uchar *value,
const size_t size,
414 const uchar *default_value)
416 return(my_b_read(&info_file, value, size));
419 bool Rpl_info_file::do_get_info(
const int pos, ulong *value,
420 const ulong default_value)
422 return (init_ulongvar_from_file(value, &info_file,
426 bool Rpl_info_file::do_get_info(
const int pos,
int *value,
427 const int default_value)
429 return (init_intvar_from_file((
int *) value, &info_file,
430 (
int) default_value));
433 bool Rpl_info_file::do_get_info(
const int pos,
float *value,
434 const float default_value)
436 return (init_floatvar_from_file(value, &info_file,
440 bool Rpl_info_file::do_get_info(
const int pos,
Dynamic_ids *value,
441 const Dynamic_ids *default_value __attribute__((unused)))
447 const int array_size= 16 * (
sizeof(long) * 3 + 1);
448 char buffer[array_size];
449 char *buffer_act= buffer;
451 bool error= init_dynarray_intvar_from_file(buffer,
sizeof(buffer),
455 value->unpack_dynamic_ids(buffer_act);
457 if (buffer != buffer_act)
469 char* Rpl_info_file::do_get_description_info()
474 bool Rpl_info_file::do_is_transactional()
479 bool Rpl_info_file::do_update_is_transactional()
484 uint Rpl_info_file::do_get_rpl_info_type()
486 return INFO_REPOSITORY_FILE;
489 int init_strvar_from_file(
char *var,
int max_size,
IO_CACHE *f,
490 const char *default_val)
493 DBUG_ENTER(
"init_strvar_from_file");
495 if ((length=my_b_gets(f,var, max_size)))
497 char* last_p = var + length -1;
507 while (((c=my_b_get(f)) !=
'\n' && c != my_b_EOF)) ;
511 else if (default_val)
513 strmake(var, default_val, max_size-1);
519 int init_intvar_from_file(
int* var,
IO_CACHE* f,
int default_val)
528 DBUG_ENTER(
"init_intvar_from_file");
530 if (my_b_gets(f, buf,
sizeof(buf)))
535 else if (default_val)
543 int init_ulongvar_from_file(ulong* var,
IO_CACHE* f, ulong default_val)
552 DBUG_ENTER(
"init_ulongvar_from_file");
554 if (my_b_gets(f, buf,
sizeof(buf)))
556 *var = strtoul(buf, 0, 10);
559 else if (default_val)
567 int init_floatvar_from_file(
float* var,
IO_CACHE* f,
float default_val)
581 DBUG_ENTER(
"init_floatvar_from_file");
583 if (my_b_gets(f, buf,
sizeof(buf)))
585 if (sscanf(buf,
"%f", var) != 1)
590 else if (default_val != 0.0)
617 bool init_dynarray_intvar_from_file(
char *buffer,
size_t size,
621 char *buf_act= buffer;
626 DBUG_ENTER(
"init_dynarray_intvar_from_file");
628 if ((read_size= my_b_gets(f, buf_act, size)) == 0)
632 if (read_size + 1 == size && buf[size - 2] !=
'\n')
637 char buf_work[(
sizeof(long) * 3 + 1) * 16];
638 memcpy(buf_work, buf,
sizeof(buf_work));
639 num_items= atoi(strtok_r(buf_work,
" ", &last));
646 size_t max_size= (1 + num_items) * (
sizeof(
long) * 3 + 1) + 1;
647 if (! (buf_act= (
char*) my_malloc(max_size, MYF(MY_WME))))
649 *buffer_act= buf_act;
650 memcpy(buf_act, buf, read_size);
651 snd_size= my_b_gets(f, buf_act + read_size, max_size - read_size);
653 ((snd_size + 1 == max_size - read_size) && buf[max_size - 2] !=
'\n'))