1 /* Copyright (c) 2005-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 #include "my_global.h"
17 #include "mysys_priv.h"
21 #define BUFF_SIZE 1024
22 #define RESERVE 1024 /* Extend buffer with this extent */
25 #define NEWLINE "\r\n"
32 static char *add_option(char *dst
, const char *option_value
,
33 const char *option
, int remove_option
);
37 Add/remove option to the option file section.
40 modify_defaults_file()
41 file_location The location of configuration file to edit
42 option The name of the option to look for (can be NULL)
43 option value The value of the option we would like to set (can be NULL)
44 section_name The name of the section (must be NOT NULL)
45 remove_option This defines what we want to remove:
46 - MY_REMOVE_NONE -- nothing to remove;
47 - MY_REMOVE_OPTION -- remove the specified option;
48 - MY_REMOVE_SECTION -- remove the specified section;
50 We open the option file first, then read the file line-by-line,
51 looking for the section we need. At the same time we put these lines
52 into a buffer. Then we look for the option within this section and
53 change/remove it. In the end we get a buffer with modified version of the
54 file. Then we write it to the file, truncate it if needed and close it.
55 Note that there is a small time gap, when the file is incomplete,
56 and this theoretically might introduce a problem.
60 1 - some error has occured. Probably due to the lack of resourses
61 2 - cannot open the file
64 int modify_defaults_file(const char *file_location
, const char *option
,
65 const char *option_value
,
66 const char *section_name
, int remove_option
)
70 char linebuff
[BUFF_SIZE
], *src_ptr
, *dst_ptr
, *file_buffer
;
71 size_t opt_len
= 0, optval_len
= 0, sect_len
, new_opt_len
, reserve_extended
;
72 uint nr_newlines
= 0, buffer_size
;
73 my_bool in_section
= FALSE
, opt_applied
= 0;
74 int reserve_occupied
= 0;
75 DBUG_ENTER("modify_defaults_file");
77 if (!(cnf_file
= my_fopen(file_location
, O_RDWR
| O_BINARY
, MYF(0))))
80 /* my_fstat doesn't use the flag parameter */
81 if (my_fstat(fileno(cnf_file
), &file_stat
, MYF(0)))
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
= (file_stat
.st_size
+
100 1); /* The ending zero */
103 Reserve space to read the contents of the file and some more
104 for the option we want to add.
106 if (!(file_buffer
= (char*) my_malloc(buffer_size
+ reserve_extended
,
110 sect_len
= strlen(section_name
);
112 for (dst_ptr
= file_buffer
; fgets(linebuff
, BUFF_SIZE
, cnf_file
); )
114 /* Skip over whitespaces */
115 for (src_ptr
= linebuff
; my_isspace(&my_charset_latin1
, *src_ptr
);
119 if (!*src_ptr
) /* Empty line */
125 /* correct the option (if requested) */
126 if (option
&& in_section
&& !strncmp(src_ptr
, option
, opt_len
) &&
127 (*(src_ptr
+ opt_len
) == '=' ||
128 my_isspace(&my_charset_latin1
, *(src_ptr
+ opt_len
)) ||
129 *(src_ptr
+ opt_len
) == '\0'))
131 char *old_src_ptr
= src_ptr
;
132 src_ptr
= strend(src_ptr
+ opt_len
); /* Find the end of the line */
134 /* could be negative */
135 reserve_occupied
+= (int) new_opt_len
- (int) (src_ptr
- old_src_ptr
);
136 if (reserve_occupied
>= (int) reserve_extended
)
138 reserve_extended
= (uint
) reserve_occupied
+ RESERVE
;
139 if (!(file_buffer
= (char*) my_realloc(file_buffer
, buffer_size
+
141 MYF(MY_WME
|MY_FREE_ON_ERROR
))))
145 dst_ptr
= add_option(dst_ptr
, option_value
, option
, remove_option
);
150 If we are going to the new group and have an option to apply, do
151 it now. If we are removing a single option or the whole section
152 this will only trigger opt_applied flag.
155 if (in_section
&& !opt_applied
&& *src_ptr
== '[')
157 dst_ptr
= add_option(dst_ptr
, option_value
, option
, remove_option
);
158 opt_applied
= 1; /* set the flag to do write() later */
159 reserve_occupied
= new_opt_len
+ opt_len
+ 1 + NEWLINE_LEN
;
162 for (; nr_newlines
; nr_newlines
--)
163 dst_ptr
= strmov(dst_ptr
, NEWLINE
);
165 /* Skip the section if MY_REMOVE_SECTION was given */
166 if (!in_section
|| remove_option
!= MY_REMOVE_SECTION
)
167 dst_ptr
= strmov(dst_ptr
, linebuff
);
169 /* Look for a section */
172 /* Copy the line to the buffer */
173 if (!strncmp(++src_ptr
, section_name
, sect_len
))
176 /* Skip over whitespaces. They are allowed after section name */
177 for (; my_isspace(&my_charset_latin1
, *src_ptr
); src_ptr
++)
183 continue; /* Missing closing parenthesis. Assume this was no group */
186 if (remove_option
== MY_REMOVE_SECTION
)
187 dst_ptr
= dst_ptr
- strlen(linebuff
);
192 in_section
= FALSE
; /* mark that this section is of no interest to us */
197 File ended. Apply an option or set opt_applied flag (in case of
198 MY_REMOVE_SECTION) so that the changes are saved. Do not do anything
199 if we are removing non-existent option.
202 if (!opt_applied
&& in_section
&& (remove_option
!= MY_REMOVE_OPTION
))
204 /* New option still remains to apply at the end */
205 if (!remove_option
&& *(dst_ptr
- 1) != '\n')
206 dst_ptr
= strmov(dst_ptr
, NEWLINE
);
207 dst_ptr
= add_option(dst_ptr
, option_value
, option
, remove_option
);
210 for (; nr_newlines
; nr_newlines
--)
211 dst_ptr
= strmov(dst_ptr
, NEWLINE
);
215 /* Don't write the file if there are no changes to be made */
216 if (my_chsize(fileno(cnf_file
), (my_off_t
) (dst_ptr
- file_buffer
), 0,
218 my_fseek(cnf_file
, 0, MY_SEEK_SET
, MYF(0)) ||
219 my_fwrite(cnf_file
, (uchar
*) file_buffer
, (size_t) (dst_ptr
- file_buffer
),
223 if (my_fclose(cnf_file
, MYF(MY_WME
)))
226 my_free(file_buffer
, MYF(0));
230 my_free(file_buffer
, MYF(0));
232 my_fclose(cnf_file
, MYF(0));
233 DBUG_RETURN(1); /* out of resources */
237 static char *add_option(char *dst
, const char *option_value
,
238 const char *option
, int remove_option
)
242 dst
= strmov(dst
, option
);
246 dst
= strmov(dst
, option_value
);
249 dst
= strmov(dst
, NEWLINE
);