Merge jamppa@bk-internal.mysql.com:/home/bk/mysql-5.0

into a193-229-222-105.elisa-laajakaista.fi:/home/my/bk/mysql-5.0
parents 57192cb6 9f3daa23
...@@ -37,22 +37,20 @@ ...@@ -37,22 +37,20 @@
#define NEWLINE_LEN 1 #define NEWLINE_LEN 1
#endif #endif
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, int modify_defaults_file(const char *file_location, const char *option,
const char *option_value, const char *option_value,
const char *section_name, int remove_option) const char *section_name, int remove_option)
{ {
FILE *cnf_file; FILE *cnf_file;
MY_STAT file_stat; MY_STAT file_stat;
char linebuff[BUFF_SIZE], tmp[BUFF_SIZE], *tmp_ptr, *src_ptr, *dst_ptr, char linebuff[BUFF_SIZE], *src_ptr, *dst_ptr, *file_buffer;
*file_buffer; uint optlen, optval_len, sect_len, nr_newlines= 0;
uint optlen, optval_len, sect_len; my_bool in_section= FALSE, opt_applied= 0;
my_bool in_section= FALSE;
DBUG_ENTER("modify_defaults_file"); 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)))) if (!(cnf_file= my_fopen(file_location, O_RDWR | O_BINARY, MYF(0))))
DBUG_RETURN(2); DBUG_RETURN(2);
...@@ -60,11 +58,14 @@ int modify_defaults_file(const char *file_location, const char *option, ...@@ -60,11 +58,14 @@ int modify_defaults_file(const char *file_location, const char *option,
if (my_fstat(fileno(cnf_file), &file_stat, MYF(0))) if (my_fstat(fileno(cnf_file), &file_stat, MYF(0)))
goto err; goto err;
optlen= strlen(option);
optval_len= strlen(option_value);
/* /*
Reserve space to read the contents of the file and some more Reserve space to read the contents of the file and some more
for the option we want to add. for the option we want to add.
*/ */
if (!(file_buffer= (char*) my_malloc(sizeof(char)* if (!(file_buffer= (char*) my_malloc(sizeof(char) *
(file_stat.st_size + (file_stat.st_size +
/* option name len */ /* option name len */
optlen + optlen +
...@@ -73,35 +74,44 @@ int modify_defaults_file(const char *file_location, const char *option, ...@@ -73,35 +74,44 @@ int modify_defaults_file(const char *file_location, const char *option,
/* reserve for '=' char */ /* reserve for '=' char */
1 + 1 +
/* option value len */ /* option value len */
optval_len), MYF(MY_WME)))) optval_len +
/* The ending zero plus some safety */
FN_REFLEN), MYF(MY_WME))))
goto malloc_err; goto malloc_err;
for (dst_ptr= file_buffer, tmp_ptr= 0; sect_len= strlen(section_name);
fgets(linebuff, BUFF_SIZE, cnf_file); )
for (dst_ptr= file_buffer; fgets(linebuff, BUFF_SIZE, cnf_file); )
{ {
/* Skip over whitespaces */ /* Skip over whitespaces */
for (src_ptr= linebuff; my_isspace(&my_charset_latin1, *src_ptr); for (src_ptr= linebuff; my_isspace(&my_charset_latin1, *src_ptr);
src_ptr++) src_ptr++)
{} {}
if (in_section && !strncmp(src_ptr, option, optlen) && if (!*src_ptr) /* Empty line */
{
nr_newlines++;
continue;
}
if (!opt_applied && in_section && !strncmp(src_ptr, option, optlen) &&
(*(src_ptr + optlen) == '=' || (*(src_ptr + optlen) == '=' ||
my_isspace(&my_charset_latin1, *(src_ptr + optlen)) || my_isspace(&my_charset_latin1, *(src_ptr + optlen)) ||
*(src_ptr + optlen) == '\0')) *(src_ptr + optlen) == '\0'))
{ {
/* The option under modifying was found in this section. Apply new. */ dst_ptr= add_option(dst_ptr, option_value, option, remove_option);
if (!remove_option) opt_applied= 1;
dst_ptr= strmov(dst_ptr, tmp);
tmp_ptr= 0; /* To mark that we have already applied this */
} }
else else
{ {
/* If going to new group and we have option to apply, do it now */ /* If going to new group and we have option to apply, do it now */
if (tmp_ptr && *src_ptr == '[') if (in_section && !opt_applied && *src_ptr == '[')
{ {
dst_ptr= strmov(dst_ptr, tmp); dst_ptr= add_option(dst_ptr, option_value, option, remove_option);
tmp_ptr= 0;
} }
for (; nr_newlines; nr_newlines--)
dst_ptr= strmov(dst_ptr, NEWLINE);
dst_ptr= strmov(dst_ptr, linebuff); dst_ptr= strmov(dst_ptr, linebuff);
} }
/* Look for a section */ /* Look for a section */
...@@ -117,43 +127,38 @@ int modify_defaults_file(const char *file_location, const char *option, ...@@ -117,43 +127,38 @@ int modify_defaults_file(const char *file_location, const char *option,
if (*src_ptr != ']') if (*src_ptr != ']')
continue; /* Missing closing parenthesis. Assume this was no group */ continue; /* Missing closing parenthesis. Assume this was no group */
in_section= TRUE; 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 else
in_section= FALSE; /* mark that this section is of no interest to us */ in_section= FALSE; /* mark that this section is of no interest to us */
} }
} }
/* File ended. New option still remains to apply at the end */ /* File ended. */
if (tmp_ptr) if (!opt_applied && !remove_option && in_section)
{ {
/* New option still remains to apply at the end */
if (*(dst_ptr - 1) != '\n') if (*(dst_ptr - 1) != '\n')
*dst_ptr++= '\n'; dst_ptr= strmov(dst_ptr, NEWLINE);
dst_ptr= strmov(dst_ptr, tmp); dst_ptr= add_option(dst_ptr, option_value, option, remove_option);
opt_applied= 1;
} }
for (; nr_newlines; nr_newlines--)
dst_ptr= strmov(dst_ptr, NEWLINE);
if (opt_applied)
{
/* Don't write the file if there are no changes to be made */
if (my_chsize(fileno(cnf_file), (my_off_t) (dst_ptr - file_buffer), 0, if (my_chsize(fileno(cnf_file), (my_off_t) (dst_ptr - file_buffer), 0,
MYF(MY_WME)) || MYF(MY_WME)) ||
my_fseek(cnf_file, 0, MY_SEEK_SET, MYF(0)) || my_fseek(cnf_file, 0, MY_SEEK_SET, MYF(0)) ||
my_fwrite(cnf_file, file_buffer, (uint) (dst_ptr - file_buffer), my_fwrite(cnf_file, file_buffer, (uint) (dst_ptr - file_buffer),
MYF(MY_NABP)) || MYF(MY_NABP)))
my_fclose(cnf_file, MYF(MY_WME))) goto err;
}
if (my_fclose(cnf_file, MYF(MY_WME)))
goto err; goto err;
my_free(file_buffer, MYF(0)); my_free(file_buffer, MYF(0));
DBUG_RETURN(0); DBUG_RETURN(0);
err: err:
...@@ -162,3 +167,21 @@ int modify_defaults_file(const char *file_location, const char *option, ...@@ -162,3 +167,21 @@ int modify_defaults_file(const char *file_location, const char *option,
my_fclose(cnf_file, MYF(0)); my_fclose(cnf_file, MYF(0));
DBUG_RETURN(1); /* out of resources */ DBUG_RETURN(1); /* out of resources */
} }
static char *add_option(char *dst, const char *option_value,
const char *option, int remove_option)
{
if (!remove_option)
{
dst= strmov(dst, option);
if (*option_value)
{
*dst++= '=';
dst= strmov(dst, option_value);
}
/* add a newline */
dst= strmov(dst, NEWLINE);
}
return dst;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment