~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/default_modify.cc

  • Committer: Brian Aker
  • Date: 2009-07-11 08:51:36 UTC
  • mfrom: (1089.3.11 merge)
  • Revision ID: brian@gaz-20090711085136-qj01nwm3qynghwtc
Merge Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
#include "mysys_priv.h"
 
16
#include "mysys/mysys_priv.h"
17
17
#include <mystrings/m_string.h>
18
 
#include <my_dir.h>
 
18
#include <mysys/my_dir.h>
 
19
 
 
20
#include <stdio.h>
 
21
#include <algorithm>
 
22
 
 
23
using namespace std;
19
24
 
20
25
#define BUFF_SIZE 1024
21
26
#define RESERVE 1024                   /* Extend buffer with this extent */
63
68
  struct stat file_stat;
64
69
  char linebuff[BUFF_SIZE], *src_ptr, *dst_ptr, *file_buffer;
65
70
  size_t opt_len= 0, optval_len= 0, sect_len;
66
 
  uint nr_newlines= 0, buffer_size;
 
71
  uint32_t nr_newlines= 0;
 
72
  size_t buffer_size;
67
73
  bool in_section= false, opt_applied= 0;
68
 
  uint reserve_extended;
69
 
  uint new_opt_len;
 
74
  size_t reserve_extended;
 
75
  uint32_t new_opt_len;
70
76
  int reserve_occupied= 0;
71
77
 
72
 
  if (!(cnf_file= my_fopen(file_location, O_RDWR | O_BINARY, MYF(0))))
 
78
  if (!(cnf_file= fopen(file_location, "r+")))
73
79
    return(2);
74
80
 
75
81
  if (fstat(fileno(cnf_file), &file_stat))
90
96
                     NEWLINE_LEN +              /* Space for newline */
91
97
                     RESERVE);                  /* Some additional space */
92
98
 
93
 
  buffer_size= (file_stat.st_size +
94
 
                1);                             /* The ending zero */
 
99
  buffer_size= (size_t)max((uint64_t)file_stat.st_size + 1, (uint64_t)SIZE_MAX);
95
100
 
96
101
  /*
97
102
    Reserve space to read the contents of the file and some more
98
103
    for the option we want to add.
99
104
  */
100
 
  if (!(file_buffer= (char*) my_malloc(buffer_size + reserve_extended,
101
 
                                       MYF(MY_WME))))
 
105
  if (!(file_buffer= (char*) malloc(max(buffer_size + reserve_extended,
 
106
                                        (size_t)SIZE_MAX))))
102
107
    goto malloc_err;
103
108
 
104
109
  sect_len= strlen(section_name);
106
111
  for (dst_ptr= file_buffer; fgets(linebuff, BUFF_SIZE, cnf_file); )
107
112
  {
108
113
    /* Skip over whitespaces */
109
 
    for (src_ptr= linebuff; my_isspace(&my_charset_latin1, *src_ptr);
 
114
    for (src_ptr= linebuff; my_isspace(&my_charset_utf8_general_ci, *src_ptr);
110
115
         src_ptr++)
111
116
    {}
112
117
 
119
124
    /* correct the option (if requested) */
120
125
    if (option && in_section && !strncmp(src_ptr, option, opt_len) &&
121
126
        (*(src_ptr + opt_len) == '=' ||
122
 
         my_isspace(&my_charset_latin1, *(src_ptr + opt_len)) ||
 
127
         my_isspace(&my_charset_utf8_general_ci, *(src_ptr + opt_len)) ||
123
128
         *(src_ptr + opt_len) == '\0'))
124
129
    {
125
130
      char *old_src_ptr= src_ptr;
126
 
      src_ptr= strend(src_ptr+ opt_len);        /* Find the end of the line */
 
131
      src_ptr= strchr(src_ptr+ opt_len, '\0');        /* Find the end of the line */
127
132
 
128
133
      /* could be negative */
129
134
      reserve_occupied+= (int) new_opt_len - (int) (src_ptr - old_src_ptr);
130
135
      if (reserve_occupied >= (int) reserve_extended)
131
136
      {
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))))
 
137
        reserve_extended= (uint32_t) reserve_occupied + RESERVE;
 
138
        if (!(file_buffer= (char*) realloc(file_buffer, buffer_size +
 
139
                                           reserve_extended)))
136
140
          goto malloc_err;
137
141
      }
138
142
      opt_applied= 1;
154
158
      }
155
159
 
156
160
      for (; nr_newlines; nr_newlines--)
157
 
        dst_ptr= stpcpy(dst_ptr, NEWLINE);
 
161
        dst_ptr= strcpy(dst_ptr, NEWLINE)+NEWLINE_LEN;
158
162
 
159
163
      /* Skip the section if MY_REMOVE_SECTION was given */
160
164
      if (!in_section || remove_option != MY_REMOVE_SECTION)
161
 
        dst_ptr= stpcpy(dst_ptr, linebuff);
 
165
        dst_ptr= strcpy(dst_ptr, linebuff);
 
166
        dst_ptr+= strlen(linebuff);
162
167
    }
163
168
    /* Look for a section */
164
169
    if (*src_ptr == '[')
168
173
      {
169
174
        src_ptr+= sect_len;
170
175
        /* Skip over whitespaces. They are allowed after section name */
171
 
        for (; my_isspace(&my_charset_latin1, *src_ptr); src_ptr++)
 
176
        for (; my_isspace(&my_charset_utf8_general_ci, *src_ptr); src_ptr++)
172
177
        {}
173
178
 
174
179
        if (*src_ptr != ']')
197
202
  {
198
203
    /* New option still remains to apply at the end */
199
204
    if (!remove_option && *(dst_ptr - 1) != '\n')
200
 
      dst_ptr= stpcpy(dst_ptr, NEWLINE);
 
205
      dst_ptr= strcpy(dst_ptr, NEWLINE)+NEWLINE_LEN;
201
206
    dst_ptr= add_option(dst_ptr, option_value, option, remove_option);
202
207
    opt_applied= 1;
203
208
  }
204
209
  for (; nr_newlines; nr_newlines--)
205
 
    dst_ptr= stpcpy(dst_ptr, NEWLINE);
 
210
    dst_ptr= strcpy(dst_ptr, NEWLINE)+NEWLINE_LEN;
206
211
 
207
212
  if (opt_applied)
208
213
  {
209
214
    /* Don't write the file if there are no changes to be made */
210
 
    if (ftruncate(fileno(cnf_file), (my_off_t) (dst_ptr - file_buffer)) ||
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)))
 
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))
214
218
      goto err;
215
219
  }
216
 
  if (my_fclose(cnf_file, MYF(MY_WME)))
 
220
  if (fclose(cnf_file))
217
221
    return(1);
218
222
 
219
 
  my_free(file_buffer, MYF(0));
 
223
  free(file_buffer);
220
224
  return(0);
221
225
 
222
226
err:
223
 
  my_free(file_buffer, MYF(0));
 
227
  free(file_buffer);
224
228
malloc_err:
225
 
  my_fclose(cnf_file, MYF(0));
226
 
  return(1); /* out of resources */
 
229
  fclose(cnf_file);
 
230
 
 
231
  return 1; /* out of resources */
227
232
}
228
233
 
229
234
 
232
237
{
233
238
  if (!remove_option)
234
239
  {
235
 
    dst= stpcpy(dst, option);
 
240
    dst= strcpy(dst, option);
 
241
    dst+= strlen(option);
236
242
    if (*option_value)
237
243
    {
238
244
      *dst++= '=';
239
 
      dst= stpcpy(dst, option_value);
 
245
      dst= strcpy(dst, option_value);
 
246
      dst+= strlen(option_value);
240
247
    }
241
248
    /* add a newline */
242
 
    dst= stpcpy(dst, NEWLINE);
 
249
    dst= strcpy(dst, NEWLINE)+NEWLINE_LEN;
243
250
  }
244
251
  return dst;
245
252
}