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