~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/default_modify.cc

  • Committer: Stewart Smith
  • Date: 2009-06-16 03:02:59 UTC
  • mto: This revision was merged to the branch mainline in revision 1065.
  • Revision ID: stewart@flamingspork.com-20090616030259-tn2thqrajk6cappd
ER_NISAMCHK is unused, mark it as so. Thanks to Paul DuBois for researching this for MySQL.

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>
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
 
  uint32_t 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
 
  uint32_t reserve_extended;
 
71
  size_t reserve_extended;
69
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= fopen(file_location, "r+")))
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);
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
 
        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))))
 
134
        reserve_extended= (uint32_t) reserve_occupied + RESERVE;
 
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= my_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= my_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 == '[')
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= my_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= my_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, (unsigned char*) 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
 
  if (my_fclose(cnf_file, MYF(MY_WME)))
 
217
  if (fclose(cnf_file))
217
218
    return(1);
218
219
 
219
220
  free(file_buffer);
222
223
err:
223
224
  free(file_buffer);
224
225
malloc_err:
225
 
  my_fclose(cnf_file, MYF(0));
226
 
  return(1); /* out of resources */
 
226
  fclose(cnf_file);
 
227
 
 
228
  return 1; /* out of resources */
227
229
}
228
230
 
229
231
 
232
234
{
233
235
  if (!remove_option)
234
236
  {
235
 
    dst= my_stpcpy(dst, option);
 
237
    dst= strcpy(dst, option);
 
238
    dst+= strlen(option);
236
239
    if (*option_value)
237
240
    {
238
241
      *dst++= '=';
239
 
      dst= my_stpcpy(dst, option_value);
 
242
      dst= strcpy(dst, option_value);
 
243
      dst+= strlen(option_value);
240
244
    }
241
245
    /* add a newline */
242
 
    dst= my_stpcpy(dst, NEWLINE);
 
246
    dst= strcpy(dst, NEWLINE)+NEWLINE_LEN;
243
247
  }
244
248
  return dst;
245
249
}