diff options
-rw-r--r-- | include/my_sys.h | 8 | ||||
-rw-r--r-- | include/mysql_com.h | 3 | ||||
-rw-r--r-- | libmysql/Makefile.shared | 3 | ||||
-rw-r--r-- | libmysql/libmysql.def | 3 | ||||
-rw-r--r-- | libmysqld/libmysqld.def | 1 | ||||
-rw-r--r-- | mysys/Makefile.am | 3 | ||||
-rw-r--r-- | mysys/default.c | 135 | ||||
-rw-r--r-- | mysys/default_modify.c | 164 | ||||
-rw-r--r-- | server-tools/instance-manager/commands.cc | 4 | ||||
-rw-r--r-- | sql/mysqld.cc | 2 |
10 files changed, 180 insertions, 146 deletions
diff --git a/include/my_sys.h b/include/my_sys.h index a744a71ba6e..9c07fc2e923 100644 --- a/include/my_sys.h +++ b/include/my_sys.h @@ -778,15 +778,13 @@ extern void reset_root_defaults(MEM_ROOT *mem_root, uint block_size, extern char *strdup_root(MEM_ROOT *root,const char *str); extern char *strmake_root(MEM_ROOT *root,const char *str,uint len); extern char *memdup_root(MEM_ROOT *root,const char *str,uint len); -extern int my_correct_defaults_file(const char *file_location, - const char *option, - const char *option_value, - const char *section_name, - int remove_option); extern void get_defaults_files(int argc, char **argv, char **defaults, char **extra_defaults); extern int load_defaults(const char *conf_file, const char **groups, int *argc, char ***argv); +extern uint modify_defaults_file(const char *file_location, const char *option, + const char *option_value, + const char *section_name, int remove_option); extern int my_search_option_files(const char *conf_file, int *argc, char ***argv, uint *args_used, Process_option_func func, void *func_ctx); diff --git a/include/mysql_com.h b/include/mysql_com.h index 5a7b99fc3d8..a49bc01da8a 100644 --- a/include/mysql_com.h +++ b/include/mysql_com.h @@ -417,6 +417,9 @@ const char *mysql_errno_to_sqlstate(unsigned int mysql_errno); /* Some other useful functions */ my_bool my_init(void); +extern uint modify_defaults_file(const char *file_location, const char *option, + const char *option_value, + const char *section_name, int remove_option); int load_defaults(const char *conf_file, const char **groups, int *argc, char ***argv); my_bool my_thread_init(void); diff --git a/libmysql/Makefile.shared b/libmysql/Makefile.shared index bcd2ec6c057..a2bfa616f6b 100644 --- a/libmysql/Makefile.shared +++ b/libmysql/Makefile.shared @@ -61,7 +61,8 @@ mysysobjects1 = my_init.lo my_static.lo my_malloc.lo my_realloc.lo \ mf_format.lo mf_path.lo mf_unixpath.lo my_fopen.lo \ my_symlink.lo my_fstream.lo \ mf_loadpath.lo my_pthread.lo my_thr_init.lo \ - thr_mutex.lo mulalloc.lo string.lo default.lo \ + thr_mutex.lo mulalloc.lo string.lo \ + default.lo default_modify.lo \ my_compress.lo array.lo my_once.lo list.lo my_net.lo \ charset.lo charset-def.lo hash.lo mf_iocache.lo \ mf_iocache2.lo my_seek.lo my_sleep.lo \ diff --git a/libmysql/libmysql.def b/libmysql/libmysql.def index 4c7a8352fb4..2da88c271ba 100644 --- a/libmysql/libmysql.def +++ b/libmysql/libmysql.def @@ -1,5 +1,5 @@ LIBRARY LIBMYSQL -DESCRIPTION 'MySQL 4.1 Client Library' +DESCRIPTION 'MySQL 5.0 Client Library' VERSION 6.0 EXPORTS _dig_vec_lower @@ -150,3 +150,4 @@ EXPORTS mysql_server_end mysql_set_character_set get_defaults_files + modify_defaults_file diff --git a/libmysqld/libmysqld.def b/libmysqld/libmysqld.def index 0612141a60d..3c8c46f5305 100644 --- a/libmysqld/libmysqld.def +++ b/libmysqld/libmysqld.def @@ -160,3 +160,4 @@ EXPORTS get_defaults_files my_charset_bin my_charset_same + modify_defaults_file diff --git a/mysys/Makefile.am b/mysys/Makefile.am index db8aa10bf1a..40743e36c29 100644 --- a/mysys/Makefile.am +++ b/mysys/Makefile.am @@ -48,7 +48,8 @@ libmysys_a_SOURCES = my_init.c my_getwd.c mf_getdate.c my_mmap.c \ my_chsize.c my_lread.c my_lwrite.c my_clock.c \ my_quick.c my_lockmem.c my_static.c \ my_sync.c my_getopt.c my_mkdir.c \ - default.c my_compress.c checksum.c raid.cc \ + default_modify.c default.c \ + my_compress.c checksum.c raid.cc \ my_net.c my_semaphore.c my_port.c my_sleep.c \ charset.c charset-def.c my_bitmap.c my_bit.c md5.c \ my_gethostbyname.c rijndael.c my_aes.c sha1.c \ diff --git a/mysys/default.c b/mysys/default.c index ee7cbeb1530..a9f3f4092ef 100644 --- a/mysys/default.c +++ b/mysys/default.c @@ -81,141 +81,6 @@ static void init_default_directories(); static char *remove_end_comment(char *ptr); -/* - Add/remove option to the option file section. - - SYNOPSYS - my_correct_defaults_file() - file_location The location of configuration file to edit - option option to look for - option_value The value of the option we would like to set - section_name the name of the section - remove_option This is true if we want to remove the option. - False otherwise. - IMPLEMENTATION - We open the option file first, then read the file line-by-line, - looking for the section we need. At the same time we put these lines - into a buffer. Then we look for the option within this section and - change/remove it. In the end we get a buffer with modified version of the - file. Then we write it to the file, truncate it if needed and close it. - - RETURN - 0 - ok - 1 - some error has occured. Probably due to the lack of resourses - -1 - cannot open the file -*/ - -int my_correct_defaults_file(const char *file_location, const char *option, - const char *option_value, - const char *section_name, int remove_option) -{ - FILE *cnf_file; - struct stat file_stat; - char linebuff[512], *ptr; - uint optlen; - uint len; - char *file_buffer; - uint position= 0; - int is_found= FALSE; - DBUG_ENTER("my_correct_defaults_file"); - - optlen= strlen(option); - - if (!(cnf_file= my_fopen(file_location, O_RDWR, MYF(0)))) - goto err_fopen; - - /* my_fstat doesn't use the flag parameter */ - if (my_fstat(fileno(cnf_file), &file_stat, MYF(0))) - goto err; - - /* - Reserve space to read the contents of the file and some more - for the option we want ot add. - */ - file_buffer= (char*) my_malloc(sizeof(char)* - (file_stat.st_size + /* current file size */ - optlen + /* option name len */ - 2 + /* reserve space for newline */ - 1 + /* reserve for '=' char */ - strlen(option_value)), /* option value len */ - MYF(MY_WME)); - - if (!file_buffer) - goto malloc_err; - while (fgets(linebuff, sizeof(linebuff), cnf_file)) - { - len= strlen(linebuff); - - /* if the section is found traverse it */ - if (is_found) - { - /* skip the old value of the option we are changing */ - if (strncmp(linebuff, option, optlen)) - { - /* copy all other lines */ - strmake(file_buffer + position, linebuff, len); - position+= len; - } - } - else - { - strmake(file_buffer + position, linebuff, len); - position+= len; - } - - - /* looking for appropriate section */ - for (ptr= linebuff ; my_isspace(&my_charset_latin1,*ptr) ; ptr++) - {} - - if (*ptr == '[') - { - /* copy the line to the buffer */ - if (!strncmp(++ptr, section_name, strlen(section_name))) - { - is_found= TRUE; - /* add option */ - if (!remove_option) - { - strmake(file_buffer + position, option, optlen); - position+= optlen; - if (*option_value) - { - *(file_buffer + position++)= '='; - strmake(file_buffer + position, option_value, - strlen(option_value)); - position+= strlen(option_value); - } - /* add a newline */ - strcat(file_buffer + position, NEWLINE); - position+= strlen(NEWLINE); - } - } - else - is_found= FALSE; /* mark that this section is of no interest to us */ - } - - } - - if (my_chsize(fileno(cnf_file), position, 0, MYF(MY_WME)) || - my_fseek(cnf_file, 0, MY_SEEK_SET, MYF(0)) || - my_fwrite(cnf_file, file_buffer, position, MYF(MY_NABP)) || - my_fclose(cnf_file, MYF(MY_WME))) - goto err; - - my_free(file_buffer, MYF(0)); - - DBUG_RETURN(0); - -err: - my_free(file_buffer, MYF(0)); -malloc_err: - my_fclose(cnf_file, MYF(0)); - DBUG_RETURN(1); /* out of resources */ -err_fopen: - DBUG_RETURN(-1); /* cannot access the option file */ -} - /* Process config files in default directories. diff --git a/mysys/default_modify.c b/mysys/default_modify.c new file mode 100644 index 00000000000..c0589f54132 --- /dev/null +++ b/mysys/default_modify.c @@ -0,0 +1,164 @@ +/* + Add/remove option to the option file section. + + SYNOPSYS + modify_defaults_file() + file_location The location of configuration file to edit + option option to look for + option value The value of the option we would like to set + section_name the name of the section + remove_option This is true if we want to remove the option. + False otherwise. + IMPLEMENTATION + We open the option file first, then read the file line-by-line, + looking for the section we need. At the same time we put these lines + into a buffer. Then we look for the option within this section and + change/remove it. In the end we get a buffer with modified version of the + file. Then we write it to the file, truncate it if needed and close it. + + RETURN + 0 - ok + 1 - some error has occured. Probably due to the lack of resourses + 2 - cannot open the file +*/ + +#include "my_global.h" +#include "mysys_priv.h" +#include "m_string.h" +#include <my_dir.h> + +#define BUFF_SIZE 1024 + +#ifdef __WIN__ +#define NEWLINE "\r\n" +#define NEWLINE_LEN 2 +#else +#define NEWLINE "\n" +#define NEWLINE_LEN 1 +#endif + +uint modify_defaults_file(const char *file_location, const char *option, + const char *option_value, + const char *section_name, int remove_option) +{ + FILE *cnf_file; + struct stat file_stat; + char linebuff[BUFF_SIZE], tmp[BUFF_SIZE], *tmp_ptr, *src_ptr, *dst_ptr, + *file_buffer; + uint optlen, optval_len, sect_len; + my_bool in_section= FALSE; + DBUG_ENTER("modify_defaults_file"); + + optlen= strlen(option); + optval_len= strlen(option_value); + sect_len= strlen(section_name); + + if (!(cnf_file= my_fopen(file_location, O_RDWR | O_BINARY, MYF(0)))) + DBUG_RETURN(2); + + /* my_fstat doesn't use the flag parameter */ + if (my_fstat(fileno(cnf_file), &file_stat, MYF(0))) + goto err; + + /* + Reserve space to read the contents of the file and some more + for the option we want to add. + */ + if (!(file_buffer= (char*) my_malloc(sizeof(char)* + (file_stat.st_size + + /* option name len */ + optlen + + /* reserve space for newline */ + NEWLINE_LEN + + /* reserve for '=' char */ + 1 + + /* option value len */ + optval_len), MYF(MY_WME)))) + goto malloc_err; + + for (dst_ptr= file_buffer, tmp_ptr= 0; + fgets(linebuff, BUFF_SIZE, cnf_file); ) + { + /* Skip over whitespaces */ + for (src_ptr= linebuff; my_isspace(&my_charset_latin1, *src_ptr); + src_ptr++) + {} + + if (in_section && !strncmp(src_ptr, option, optlen) && + (*(src_ptr + optlen) == '=' || + my_isspace(&my_charset_latin1, *(src_ptr + optlen)) || + *(src_ptr + optlen) == '\0')) + { + /* The option under modifying was found in this section. Apply new. */ + if (!remove_option) + dst_ptr= strmov(dst_ptr, tmp); + tmp_ptr= 0; /* To mark that we have already applied this */ + } + else + { + /* If going to new group and we have option to apply, do it now */ + if (tmp_ptr && *src_ptr == '[') + { + dst_ptr= strmov(dst_ptr, tmp); + tmp_ptr= 0; + } + dst_ptr= strmov(dst_ptr, linebuff); + } + /* Look for a section */ + if (*src_ptr == '[') + { + /* Copy the line to the buffer */ + if (!strncmp(++src_ptr, section_name, sect_len)) + { + src_ptr+= sect_len; + /* Skip over whitespaces. They are allowed after section name */ + for (; my_isspace(&my_charset_latin1, *src_ptr); src_ptr++) + {} + + if (*src_ptr != ']') + continue; /* Missing closing parenthesis. Assume this was no group */ + + in_section= TRUE; + /* add option */ + if (!remove_option) + { + tmp_ptr= strmov(tmp, option); + if (*option_value) + { + *tmp_ptr++= '='; + tmp_ptr= strmov(tmp_ptr, option_value); + } + /* add a newline */ + strmov(tmp_ptr, NEWLINE); + } + } + else + in_section= FALSE; /* mark that this section is of no interest to us */ + } + } + /* File ended. New option still remains to apply at the end */ + if (tmp_ptr) + { + if (*(dst_ptr - 1) != '\n') + *dst_ptr++= '\n'; + dst_ptr= strmov(dst_ptr, tmp); + } + + if (my_chsize(fileno(cnf_file), (my_off_t) (dst_ptr - file_buffer), 0, + MYF(MY_WME)) || + my_fseek(cnf_file, 0, MY_SEEK_SET, MYF(0)) || + my_fwrite(cnf_file, file_buffer, (uint) (dst_ptr - file_buffer), + MYF(MY_NABP)) || + my_fclose(cnf_file, MYF(MY_WME))) + goto err; + + my_free(file_buffer, MYF(0)); + + DBUG_RETURN(0); + +err: + my_free(file_buffer, MYF(0)); +malloc_err: + my_fclose(cnf_file, MYF(0)); + DBUG_RETURN(1); /* out of resources */ +} diff --git a/server-tools/instance-manager/commands.cc b/server-tools/instance-manager/commands.cc index fd346148df2..347f435bbc1 100644 --- a/server-tools/instance-manager/commands.cc +++ b/server-tools/instance-manager/commands.cc @@ -689,8 +689,8 @@ int Set_option::correct_file(int skip) { int error; - error= my_correct_defaults_file("/etc/my.cnf", option, - option_value, instance_name, skip); + error= modify_defaults_file("/etc/my.cnf", option, + option_value, instance_name, skip); if (error > 0) return ER_OUT_OF_RESOURCES; else if (error < 0) diff --git a/sql/mysqld.cc b/sql/mysqld.cc index e5103bb583e..f36dc2c6024 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -294,7 +294,7 @@ static I_List<THD> thread_cache; static pthread_cond_t COND_thread_cache, COND_flush_thread_cache; -#ifdef HAVE_BERKLEY_DB +#ifdef HAVE_BERKELEY_DB static my_bool opt_sync_bdb_logs; #endif |