1
/* Copyright (C) 2005 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "mysys/mysys_priv.h"
17
#include <mystrings/m_string.h>
18
#include <mysys/my_dir.h>
25
#define BUFF_SIZE 1024
26
#define RESERVE 1024 /* Extend buffer with this extent */
31
static char *add_option(char *dst, const char *option_value,
32
const char *option, int remove_option);
36
Add/remove option to the option file section.
39
modify_defaults_file()
40
file_location The location of configuration file to edit
41
option The name of the option to look for (can be NULL)
42
option value The value of the option we would like to set (can be NULL)
43
section_name The name of the section (must be NOT NULL)
44
remove_option This defines what we want to remove:
45
- MY_REMOVE_NONE -- nothing to remove;
46
- MY_REMOVE_OPTION -- remove the specified option;
47
- MY_REMOVE_SECTION -- remove the specified section;
49
We open the option file first, then read the file line-by-line,
50
looking for the section we need. At the same time we put these lines
51
into a buffer. Then we look for the option within this section and
52
change/remove it. In the end we get a buffer with modified version of the
53
file. Then we write it to the file, truncate it if needed and close it.
54
Note that there is a small time gap, when the file is incomplete,
55
and this theoretically might introduce a problem.
59
1 - some error has occured. Probably due to the lack of resourses
60
2 - cannot open the file
63
int modify_defaults_file(const char *file_location, const char *option,
64
const char *option_value,
65
const char *section_name, int remove_option)
68
struct stat file_stat;
69
char linebuff[BUFF_SIZE], *src_ptr, *dst_ptr, *file_buffer;
70
size_t opt_len= 0, optval_len= 0, sect_len;
71
uint32_t nr_newlines= 0;
73
bool in_section= false, opt_applied= 0;
74
size_t reserve_extended;
76
int reserve_occupied= 0;
78
if (!(cnf_file= fopen(file_location, "r+")))
81
if (fstat(fileno(cnf_file), &file_stat))
84
if (option && option_value)
86
opt_len= strlen(option);
87
optval_len= strlen(option_value);
90
new_opt_len= opt_len + 1 + optval_len + NEWLINE_LEN;
92
/* calculate the size of the buffer we need */
93
reserve_extended= (opt_len +
94
1 + /* For '=' char */
95
optval_len + /* Option value len */
96
NEWLINE_LEN + /* Space for newline */
97
RESERVE); /* Some additional space */
99
buffer_size= (size_t)max((uint64_t)file_stat.st_size + 1, (uint64_t)SIZE_MAX);
102
Reserve space to read the contents of the file and some more
103
for the option we want to add.
105
if (!(file_buffer= (char*) malloc(max(buffer_size + reserve_extended,
109
sect_len= strlen(section_name);
111
for (dst_ptr= file_buffer; fgets(linebuff, BUFF_SIZE, cnf_file); )
113
/* Skip over whitespaces */
114
for (src_ptr= linebuff; my_isspace(&my_charset_utf8_general_ci, *src_ptr);
118
if (!*src_ptr) /* Empty line */
124
/* correct the option (if requested) */
125
if (option && in_section && !strncmp(src_ptr, option, opt_len) &&
126
(*(src_ptr + opt_len) == '=' ||
127
my_isspace(&my_charset_utf8_general_ci, *(src_ptr + opt_len)) ||
128
*(src_ptr + opt_len) == '\0'))
130
char *old_src_ptr= src_ptr;
131
src_ptr= strchr(src_ptr+ opt_len, '\0'); /* Find the end of the line */
133
/* could be negative */
134
reserve_occupied+= (int) new_opt_len - (int) (src_ptr - old_src_ptr);
135
if (reserve_occupied >= (int) reserve_extended)
137
reserve_extended= (uint32_t) reserve_occupied + RESERVE;
138
if (!(file_buffer= (char*) realloc(file_buffer, buffer_size +
143
dst_ptr= add_option(dst_ptr, option_value, option, remove_option);
148
If we are going to the new group and have an option to apply, do
149
it now. If we are removing a single option or the whole section
150
this will only trigger opt_applied flag.
153
if (in_section && !opt_applied && *src_ptr == '[')
155
dst_ptr= add_option(dst_ptr, option_value, option, remove_option);
156
opt_applied= 1; /* set the flag to do write() later */
157
reserve_occupied= new_opt_len+ opt_len + 1 + NEWLINE_LEN;
160
for (; nr_newlines; nr_newlines--)
161
dst_ptr= strcpy(dst_ptr, NEWLINE)+NEWLINE_LEN;
163
/* Skip the section if MY_REMOVE_SECTION was given */
164
if (!in_section || remove_option != MY_REMOVE_SECTION)
165
dst_ptr= strcpy(dst_ptr, linebuff);
166
dst_ptr+= strlen(linebuff);
168
/* Look for a section */
171
/* Copy the line to the buffer */
172
if (!strncmp(++src_ptr, section_name, sect_len))
175
/* Skip over whitespaces. They are allowed after section name */
176
for (; my_isspace(&my_charset_utf8_general_ci, *src_ptr); src_ptr++)
182
continue; /* Missing closing parenthesis. Assume this was no group */
185
if (remove_option == MY_REMOVE_SECTION)
186
dst_ptr= dst_ptr - strlen(linebuff);
191
in_section= false; /* mark that this section is of no interest to us */
196
File ended. Apply an option or set opt_applied flag (in case of
197
MY_REMOVE_SECTION) so that the changes are saved. Do not do anything
198
if we are removing non-existent option.
201
if (!opt_applied && in_section && (remove_option != MY_REMOVE_OPTION))
203
/* New option still remains to apply at the end */
204
if (!remove_option && *(dst_ptr - 1) != '\n')
205
dst_ptr= strcpy(dst_ptr, NEWLINE)+NEWLINE_LEN;
206
dst_ptr= add_option(dst_ptr, option_value, option, remove_option);
209
for (; nr_newlines; nr_newlines--)
210
dst_ptr= strcpy(dst_ptr, NEWLINE)+NEWLINE_LEN;
214
/* Don't write the file if there are no changes to be made */
215
if (ftruncate(fileno(cnf_file), (size_t) (dst_ptr - file_buffer)) ||
216
fseeko(cnf_file, 0, SEEK_SET) ||
217
fwrite(file_buffer, 1, (size_t) (dst_ptr - file_buffer), cnf_file))
220
if (fclose(cnf_file))
231
return 1; /* out of resources */
235
static char *add_option(char *dst, const char *option_value,
236
const char *option, int remove_option)
240
dst= strcpy(dst, option);
241
dst+= strlen(option);
245
dst= strcpy(dst, option_value);
246
dst+= strlen(option_value);
249
dst= strcpy(dst, NEWLINE)+NEWLINE_LEN;