1
by brian
clean slate |
1 |
/* Copyright (C) 2005 MySQL AB
|
2 |
||
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.
|
|
6 |
||
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.
|
|
11 |
||
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 */
|
|
15 |
||
994.2.4
by Monty Taylor
Blast. Fixed some make distcheck issues. |
16 |
#include "mysys/mysys_priv.h" |
212.5.18
by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype. |
17 |
#include <mystrings/m_string.h> |
612.2.6
by Monty Taylor
OpenSolaris builds. |
18 |
#include <mysys/my_dir.h> |
19 |
||
20 |
#include <stdio.h> |
|
1
by brian
clean slate |
21 |
|
22 |
#define BUFF_SIZE 1024
|
|
23 |
#define RESERVE 1024 /* Extend buffer with this extent */ |
|
24 |
||
25 |
#define NEWLINE "\n"
|
|
26 |
#define NEWLINE_LEN 1
|
|
27 |
||
28 |
static char *add_option(char *dst, const char *option_value, |
|
29 |
const char *option, int remove_option); |
|
30 |
||
31 |
||
32 |
/*
|
|
33 |
Add/remove option to the option file section.
|
|
34 |
||
35 |
SYNOPSYS
|
|
36 |
modify_defaults_file()
|
|
37 |
file_location The location of configuration file to edit
|
|
38 |
option The name of the option to look for (can be NULL)
|
|
39 |
option value The value of the option we would like to set (can be NULL)
|
|
40 |
section_name The name of the section (must be NOT NULL)
|
|
41 |
remove_option This defines what we want to remove:
|
|
42 |
- MY_REMOVE_NONE -- nothing to remove;
|
|
43 |
- MY_REMOVE_OPTION -- remove the specified option;
|
|
44 |
- MY_REMOVE_SECTION -- remove the specified section;
|
|
45 |
IMPLEMENTATION
|
|
46 |
We open the option file first, then read the file line-by-line,
|
|
47 |
looking for the section we need. At the same time we put these lines
|
|
48 |
into a buffer. Then we look for the option within this section and
|
|
49 |
change/remove it. In the end we get a buffer with modified version of the
|
|
50 |
file. Then we write it to the file, truncate it if needed and close it.
|
|
51 |
Note that there is a small time gap, when the file is incomplete,
|
|
52 |
and this theoretically might introduce a problem.
|
|
53 |
||
54 |
RETURN
|
|
55 |
0 - ok
|
|
56 |
1 - some error has occured. Probably due to the lack of resourses
|
|
57 |
2 - cannot open the file
|
|
58 |
*/
|
|
59 |
||
60 |
int modify_defaults_file(const char *file_location, const char *option, |
|
61 |
const char *option_value, |
|
62 |
const char *section_name, int remove_option) |
|
63 |
{
|
|
64 |
FILE *cnf_file; |
|
15
by brian
Fix for stat, NETWARE removal |
65 |
struct stat file_stat; |
1
by brian
clean slate |
66 |
char linebuff[BUFF_SIZE], *src_ptr, *dst_ptr, *file_buffer; |
67 |
size_t opt_len= 0, optval_len= 0, sect_len; |
|
632.1.11
by Monty Taylor
Fixed Sun Studio warnings in mysys. |
68 |
uint32_t nr_newlines= 0; |
69 |
size_t buffer_size; |
|
163
by Brian Aker
Merge Monty's code. |
70 |
bool in_section= false, opt_applied= 0; |
632.1.11
by Monty Taylor
Fixed Sun Studio warnings in mysys. |
71 |
size_t reserve_extended; |
482
by Brian Aker
Remove uint. |
72 |
uint32_t new_opt_len; |
1
by brian
clean slate |
73 |
int reserve_occupied= 0; |
74 |
||
910.1.3
by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov) |
75 |
if (!(cnf_file= fopen(file_location, "r+"))) |
51.3.22
by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile |
76 |
return(2); |
1
by brian
clean slate |
77 |
|
15
by brian
Fix for stat, NETWARE removal |
78 |
if (fstat(fileno(cnf_file), &file_stat)) |
1
by brian
clean slate |
79 |
goto malloc_err; |
80 |
||
81 |
if (option && option_value) |
|
82 |
{
|
|
83 |
opt_len= strlen(option); |
|
84 |
optval_len= strlen(option_value); |
|
85 |
}
|
|
86 |
||
87 |
new_opt_len= opt_len + 1 + optval_len + NEWLINE_LEN; |
|
88 |
||
89 |
/* calculate the size of the buffer we need */
|
|
90 |
reserve_extended= (opt_len + |
|
91 |
1 + /* For '=' char */ |
|
92 |
optval_len + /* Option value len */ |
|
93 |
NEWLINE_LEN + /* Space for newline */ |
|
94 |
RESERVE); /* Some additional space */ |
|
95 |
||
632.1.16
by Monty Taylor
Added some shitty casts. |
96 |
buffer_size= (size_t)cmax((uint64_t)file_stat.st_size + 1, (uint64_t)SIZE_MAX); |
1
by brian
clean slate |
97 |
|
98 |
/*
|
|
99 |
Reserve space to read the contents of the file and some more
|
|
100 |
for the option we want to add.
|
|
101 |
*/
|
|
632.1.11
by Monty Taylor
Fixed Sun Studio warnings in mysys. |
102 |
if (!(file_buffer= (char*) malloc(cmax(buffer_size + reserve_extended, |
103 |
SIZE_MAX)))) |
|
1
by brian
clean slate |
104 |
goto malloc_err; |
105 |
||
106 |
sect_len= strlen(section_name); |
|
107 |
||
108 |
for (dst_ptr= file_buffer; fgets(linebuff, BUFF_SIZE, cnf_file); ) |
|
109 |
{
|
|
110 |
/* Skip over whitespaces */
|
|
383.1.11
by Brian Aker
Cleanup default character set. |
111 |
for (src_ptr= linebuff; my_isspace(&my_charset_utf8_general_ci, *src_ptr); |
1
by brian
clean slate |
112 |
src_ptr++) |
113 |
{}
|
|
114 |
||
115 |
if (!*src_ptr) /* Empty line */ |
|
116 |
{
|
|
117 |
nr_newlines++; |
|
118 |
continue; |
|
119 |
}
|
|
120 |
||
121 |
/* correct the option (if requested) */
|
|
122 |
if (option && in_section && !strncmp(src_ptr, option, opt_len) && |
|
123 |
(*(src_ptr + opt_len) == '=' || |
|
383.1.11
by Brian Aker
Cleanup default character set. |
124 |
my_isspace(&my_charset_utf8_general_ci, *(src_ptr + opt_len)) || |
1
by brian
clean slate |
125 |
*(src_ptr + opt_len) == '\0')) |
126 |
{
|
|
127 |
char *old_src_ptr= src_ptr; |
|
376
by Brian Aker
strend remove |
128 |
src_ptr= strchr(src_ptr+ opt_len, '\0'); /* Find the end of the line */ |
1
by brian
clean slate |
129 |
|
130 |
/* could be negative */
|
|
131 |
reserve_occupied+= (int) new_opt_len - (int) (src_ptr - old_src_ptr); |
|
132 |
if (reserve_occupied >= (int) reserve_extended) |
|
133 |
{
|
|
895
by Brian Aker
Completion (?) of uint conversion. |
134 |
reserve_extended= (uint32_t) reserve_occupied + RESERVE; |
656.1.26
by Monty Taylor
Finally removed all of the my_malloc stuff. |
135 |
if (!(file_buffer= (char*) realloc(file_buffer, buffer_size + |
136 |
reserve_extended))) |
|
1
by brian
clean slate |
137 |
goto malloc_err; |
138 |
}
|
|
139 |
opt_applied= 1; |
|
140 |
dst_ptr= add_option(dst_ptr, option_value, option, remove_option); |
|
141 |
}
|
|
142 |
else
|
|
143 |
{
|
|
144 |
/*
|
|
145 |
If we are going to the new group and have an option to apply, do
|
|
146 |
it now. If we are removing a single option or the whole section
|
|
147 |
this will only trigger opt_applied flag.
|
|
148 |
*/
|
|
149 |
||
150 |
if (in_section && !opt_applied && *src_ptr == '[') |
|
151 |
{
|
|
152 |
dst_ptr= add_option(dst_ptr, option_value, option, remove_option); |
|
153 |
opt_applied= 1; /* set the flag to do write() later */ |
|
154 |
reserve_occupied= new_opt_len+ opt_len + 1 + NEWLINE_LEN; |
|
155 |
}
|
|
156 |
||
157 |
for (; nr_newlines; nr_newlines--) |
|
641.4.3
by Toru Maesaka
Final pass of replacing MySQL's my_stpcpy() with appropriate libc calls |
158 |
dst_ptr= strcpy(dst_ptr, NEWLINE)+NEWLINE_LEN; |
1
by brian
clean slate |
159 |
|
160 |
/* Skip the section if MY_REMOVE_SECTION was given */
|
|
161 |
if (!in_section || remove_option != MY_REMOVE_SECTION) |
|
641.4.3
by Toru Maesaka
Final pass of replacing MySQL's my_stpcpy() with appropriate libc calls |
162 |
dst_ptr= strcpy(dst_ptr, linebuff); |
163 |
dst_ptr+= strlen(linebuff); |
|
1
by brian
clean slate |
164 |
}
|
165 |
/* Look for a section */
|
|
166 |
if (*src_ptr == '[') |
|
167 |
{
|
|
168 |
/* Copy the line to the buffer */
|
|
169 |
if (!strncmp(++src_ptr, section_name, sect_len)) |
|
170 |
{
|
|
171 |
src_ptr+= sect_len; |
|
172 |
/* Skip over whitespaces. They are allowed after section name */
|
|
383.1.11
by Brian Aker
Cleanup default character set. |
173 |
for (; my_isspace(&my_charset_utf8_general_ci, *src_ptr); src_ptr++) |
1
by brian
clean slate |
174 |
{}
|
175 |
||
176 |
if (*src_ptr != ']') |
|
177 |
{
|
|
163
by Brian Aker
Merge Monty's code. |
178 |
in_section= false; |
1
by brian
clean slate |
179 |
continue; /* Missing closing parenthesis. Assume this was no group */ |
180 |
}
|
|
181 |
||
182 |
if (remove_option == MY_REMOVE_SECTION) |
|
183 |
dst_ptr= dst_ptr - strlen(linebuff); |
|
184 |
||
163
by Brian Aker
Merge Monty's code. |
185 |
in_section= true; |
1
by brian
clean slate |
186 |
}
|
187 |
else
|
|
163
by Brian Aker
Merge Monty's code. |
188 |
in_section= false; /* mark that this section is of no interest to us */ |
1
by brian
clean slate |
189 |
}
|
190 |
}
|
|
191 |
||
192 |
/*
|
|
193 |
File ended. Apply an option or set opt_applied flag (in case of
|
|
194 |
MY_REMOVE_SECTION) so that the changes are saved. Do not do anything
|
|
195 |
if we are removing non-existent option.
|
|
196 |
*/
|
|
197 |
||
198 |
if (!opt_applied && in_section && (remove_option != MY_REMOVE_OPTION)) |
|
199 |
{
|
|
200 |
/* New option still remains to apply at the end */
|
|
201 |
if (!remove_option && *(dst_ptr - 1) != '\n') |
|
641.4.3
by Toru Maesaka
Final pass of replacing MySQL's my_stpcpy() with appropriate libc calls |
202 |
dst_ptr= strcpy(dst_ptr, NEWLINE)+NEWLINE_LEN; |
1
by brian
clean slate |
203 |
dst_ptr= add_option(dst_ptr, option_value, option, remove_option); |
204 |
opt_applied= 1; |
|
205 |
}
|
|
206 |
for (; nr_newlines; nr_newlines--) |
|
641.4.3
by Toru Maesaka
Final pass of replacing MySQL's my_stpcpy() with appropriate libc calls |
207 |
dst_ptr= strcpy(dst_ptr, NEWLINE)+NEWLINE_LEN; |
1
by brian
clean slate |
208 |
|
209 |
if (opt_applied) |
|
210 |
{
|
|
211 |
/* Don't write the file if there are no changes to be made */
|
|
656.1.39
by Monty Taylor
Removed my_seek, my_tell, my_fwrite, my_fseek. |
212 |
if (ftruncate(fileno(cnf_file), (size_t) (dst_ptr - file_buffer)) || |
213 |
fseeko(cnf_file, 0, SEEK_SET) || |
|
214 |
fwrite(file_buffer, 1, (size_t) (dst_ptr - file_buffer), cnf_file)) |
|
1
by brian
clean slate |
215 |
goto err; |
216 |
}
|
|
910.1.3
by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov) |
217 |
if (fclose(cnf_file)) |
51.3.22
by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile |
218 |
return(1); |
1
by brian
clean slate |
219 |
|
477
by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that. |
220 |
free(file_buffer); |
51.3.22
by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile |
221 |
return(0); |
1
by brian
clean slate |
222 |
|
223 |
err: |
|
477
by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that. |
224 |
free(file_buffer); |
1
by brian
clean slate |
225 |
malloc_err: |
910.1.3
by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov) |
226 |
fclose(cnf_file); |
227 |
||
228 |
return 1; /* out of resources */ |
|
1
by brian
clean slate |
229 |
}
|
230 |
||
231 |
||
232 |
static char *add_option(char *dst, const char *option_value, |
|
233 |
const char *option, int remove_option) |
|
234 |
{
|
|
235 |
if (!remove_option) |
|
236 |
{
|
|
641.4.3
by Toru Maesaka
Final pass of replacing MySQL's my_stpcpy() with appropriate libc calls |
237 |
dst= strcpy(dst, option); |
238 |
dst+= strlen(option); |
|
1
by brian
clean slate |
239 |
if (*option_value) |
240 |
{
|
|
241 |
*dst++= '='; |
|
641.4.3
by Toru Maesaka
Final pass of replacing MySQL's my_stpcpy() with appropriate libc calls |
242 |
dst= strcpy(dst, option_value); |
243 |
dst+= strlen(option_value); |
|
1
by brian
clean slate |
244 |
}
|
245 |
/* add a newline */
|
|
641.4.3
by Toru Maesaka
Final pass of replacing MySQL's my_stpcpy() with appropriate libc calls |
246 |
dst= strcpy(dst, NEWLINE)+NEWLINE_LEN; |
1
by brian
clean slate |
247 |
}
|
248 |
return dst; |
|
249 |
}
|