~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/default_modify.cc

  • Committer: Monty Taylor
  • Date: 2009-01-30 21:02:37 UTC
  • mto: (779.7.3 devel)
  • mto: This revision was merged to the branch mainline in revision 823.
  • Revision ID: mordred@inaugust.com-20090130210237-3n6ld8a9jc084jko
Commented out a test in subselect_sj - I think it might be a regression. Jay?

Show diffs side-by-side

added added

removed removed

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