#include "my_global.h"#include "mysys_priv.h"#include "m_string.h"#include <my_dir.h>Include dependency graph for default_modify.c:

Go to the source code of this file.
Defines | |
| #define | BUFF_SIZE 1024 |
| #define | RESERVE 1024 |
| #define | NEWLINE "\n" |
| #define | NEWLINE_LEN 1 |
Functions | |
| static char * | add_option (char *dst, const char *option_value, const char *option, int remove_option) |
| int | modify_defaults_file (const char *file_location, const char *option, const char *option_value, const char *section_name, int remove_option) |
| #define BUFF_SIZE 1024 |
Definition at line 22 of file default_modify.c.
| #define NEWLINE "\n" |
Definition at line 29 of file default_modify.c.
| #define NEWLINE_LEN 1 |
Definition at line 30 of file default_modify.c.
| #define RESERVE 1024 |
Definition at line 23 of file default_modify.c.
| static char * add_option | ( | char * | dst, | |
| const char * | option_value, | |||
| const char * | option, | |||
| int | remove_option | |||
| ) | [static] |
Definition at line 241 of file default_modify.c.
References NEWLINE, and strmov().
00243 { 00244 if (!remove_option) 00245 { 00246 dst= strmov(dst, option); 00247 if (*option_value) 00248 { 00249 *dst++= '='; 00250 dst= strmov(dst, option_value); 00251 } 00252 /* add a newline */ 00253 dst= strmov(dst, NEWLINE); 00254 } 00255 return dst; 00256 }
Here is the call graph for this function:

| int modify_defaults_file | ( | const char * | file_location, | |
| const char * | option, | |||
| const char * | option_value, | |||
| const char * | section_name, | |||
| int | remove_option | |||
| ) |
Definition at line 65 of file default_modify.c.
References add_option(), BUFF_SIZE, DBUG_ENTER, DBUG_RETURN, err, FALSE, file_buffer, int(), my_charset_latin1, my_chsize(), my_fclose(), my_fopen(), my_free, MY_FREE_ON_ERROR, my_fseek(), my_fstat(), my_fwrite(), my_isspace, my_malloc(), MY_NABP, my_realloc(), MY_REMOVE_OPTION, MY_REMOVE_SECTION, MY_SEEK_SET, MY_STAT, MY_WME, MYF, NEWLINE, NEWLINE_LEN, O_BINARY, RESERVE, strend(), strlen(), strmov(), and TRUE.
Referenced by Abstract_option_cmd::correct_file(), and Drop_instance::execute_impl().
00068 { 00069 FILE *cnf_file; 00070 MY_STAT file_stat; 00071 char linebuff[BUFF_SIZE], *src_ptr, *dst_ptr, *file_buffer; 00072 uint opt_len= 0; 00073 uint optval_len= 0; 00074 uint sect_len, nr_newlines= 0, buffer_size; 00075 my_bool in_section= FALSE, opt_applied= 0; 00076 uint reserve_extended; 00077 uint new_opt_len; 00078 int reserve_occupied= 0; 00079 DBUG_ENTER("modify_defaults_file"); 00080 00081 if (!(cnf_file= my_fopen(file_location, O_RDWR | O_BINARY, MYF(0)))) 00082 DBUG_RETURN(2); 00083 00084 /* my_fstat doesn't use the flag parameter */ 00085 if (my_fstat(fileno(cnf_file), &file_stat, MYF(0))) 00086 goto malloc_err; 00087 00088 if (option && option_value) 00089 { 00090 opt_len= (uint) strlen(option); 00091 optval_len= (uint) strlen(option_value); 00092 } 00093 00094 new_opt_len= opt_len + 1 + optval_len + NEWLINE_LEN; 00095 00096 /* calculate the size of the buffer we need */ 00097 reserve_extended= (opt_len + 00098 1 + /* For '=' char */ 00099 optval_len + /* Option value len */ 00100 NEWLINE_LEN + /* Space for newline */ 00101 RESERVE); /* Some additional space */ 00102 00103 buffer_size= (file_stat.st_size + 00104 1); /* The ending zero */ 00105 00106 /* 00107 Reserve space to read the contents of the file and some more 00108 for the option we want to add. 00109 */ 00110 if (!(file_buffer= (char*) my_malloc(buffer_size + reserve_extended, 00111 MYF(MY_WME)))) 00112 goto malloc_err; 00113 00114 sect_len= (uint) strlen(section_name); 00115 00116 for (dst_ptr= file_buffer; fgets(linebuff, BUFF_SIZE, cnf_file); ) 00117 { 00118 /* Skip over whitespaces */ 00119 for (src_ptr= linebuff; my_isspace(&my_charset_latin1, *src_ptr); 00120 src_ptr++) 00121 {} 00122 00123 if (!*src_ptr) /* Empty line */ 00124 { 00125 nr_newlines++; 00126 continue; 00127 } 00128 00129 /* correct the option (if requested) */ 00130 if (option && in_section && !strncmp(src_ptr, option, opt_len) && 00131 (*(src_ptr + opt_len) == '=' || 00132 my_isspace(&my_charset_latin1, *(src_ptr + opt_len)) || 00133 *(src_ptr + opt_len) == '\0')) 00134 { 00135 char *old_src_ptr= src_ptr; 00136 src_ptr= strend(src_ptr+ opt_len); /* Find the end of the line */ 00137 00138 /* could be negative */ 00139 reserve_occupied+= (int) new_opt_len - (int) (src_ptr - old_src_ptr); 00140 if (reserve_occupied >= (int) reserve_extended) 00141 { 00142 reserve_extended= (uint) reserve_occupied + RESERVE; 00143 if (!(file_buffer= (char*) my_realloc(file_buffer, buffer_size + 00144 reserve_extended, 00145 MYF(MY_WME|MY_FREE_ON_ERROR)))) 00146 goto malloc_err; 00147 } 00148 opt_applied= 1; 00149 dst_ptr= add_option(dst_ptr, option_value, option, remove_option); 00150 } 00151 else 00152 { 00153 /* 00154 If we are going to the new group and have an option to apply, do 00155 it now. If we are removing a single option or the whole section 00156 this will only trigger opt_applied flag. 00157 */ 00158 00159 if (in_section && !opt_applied && *src_ptr == '[') 00160 { 00161 dst_ptr= add_option(dst_ptr, option_value, option, remove_option); 00162 opt_applied= 1; /* set the flag to do write() later */ 00163 reserve_occupied= new_opt_len+ opt_len + 1 + NEWLINE_LEN; 00164 } 00165 00166 for (; nr_newlines; nr_newlines--) 00167 dst_ptr= strmov(dst_ptr, NEWLINE); 00168 00169 /* Skip the section if MY_REMOVE_SECTION was given */ 00170 if (!in_section || remove_option != MY_REMOVE_SECTION) 00171 dst_ptr= strmov(dst_ptr, linebuff); 00172 } 00173 /* Look for a section */ 00174 if (*src_ptr == '[') 00175 { 00176 /* Copy the line to the buffer */ 00177 if (!strncmp(++src_ptr, section_name, sect_len)) 00178 { 00179 src_ptr+= sect_len; 00180 /* Skip over whitespaces. They are allowed after section name */ 00181 for (; my_isspace(&my_charset_latin1, *src_ptr); src_ptr++) 00182 {} 00183 00184 if (*src_ptr != ']') 00185 { 00186 in_section= FALSE; 00187 continue; /* Missing closing parenthesis. Assume this was no group */ 00188 } 00189 00190 if (remove_option == MY_REMOVE_SECTION) 00191 dst_ptr= dst_ptr - strlen(linebuff); 00192 00193 in_section= TRUE; 00194 } 00195 else 00196 in_section= FALSE; /* mark that this section is of no interest to us */ 00197 } 00198 } 00199 00200 /* 00201 File ended. Apply an option or set opt_applied flag (in case of 00202 MY_REMOVE_SECTION) so that the changes are saved. Do not do anything 00203 if we are removing non-existent option. 00204 */ 00205 00206 if (!opt_applied && in_section && (remove_option != MY_REMOVE_OPTION)) 00207 { 00208 /* New option still remains to apply at the end */ 00209 if (!remove_option && *(dst_ptr - 1) != '\n') 00210 dst_ptr= strmov(dst_ptr, NEWLINE); 00211 dst_ptr= add_option(dst_ptr, option_value, option, remove_option); 00212 opt_applied= 1; 00213 } 00214 for (; nr_newlines; nr_newlines--) 00215 dst_ptr= strmov(dst_ptr, NEWLINE); 00216 00217 if (opt_applied) 00218 { 00219 /* Don't write the file if there are no changes to be made */ 00220 if (my_chsize(fileno(cnf_file), (my_off_t) (dst_ptr - file_buffer), 0, 00221 MYF(MY_WME)) || 00222 my_fseek(cnf_file, 0, MY_SEEK_SET, MYF(0)) || 00223 my_fwrite(cnf_file, file_buffer, (uint) (dst_ptr - file_buffer), 00224 MYF(MY_NABP))) 00225 goto err; 00226 } 00227 if (my_fclose(cnf_file, MYF(MY_WME))) 00228 DBUG_RETURN(1); 00229 00230 my_free(file_buffer, MYF(0)); 00231 DBUG_RETURN(0); 00232 00233 err: 00234 my_free(file_buffer, MYF(0)); 00235 malloc_err: 00236 my_fclose(cnf_file, MYF(0)); 00237 DBUG_RETURN(1); /* out of resources */ 00238 }
Here is the call graph for this function:

Here is the caller graph for this function:

1.4.7

