~drizzle-trunk/libdrizzle/jenkins-Libdrizzle-77

« back to all changes in this revision

Viewing changes to libdrizzle/binlog.cc

  • Committer: Continuous Integration
  • Date: 2013-01-27 05:23:57 UTC
  • mfrom: (98.1.2 5.1-binlog-callback)
  • Revision ID: ci@drizzle.org-20130127052357-jb87upa8hd75wfyu
Merge lp:~linuxjedi/libdrizzle/5.1-binlog-callback Build: jenkins-Libdrizzle-57

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
#include <zlib.h>
42
42
 
43
 
drizzle_result_st *drizzle_start_binlog(drizzle_st *con,
44
 
                                            uint32_t server_id,
45
 
                                            const char *file,
46
 
                                            uint32_t start_position,
47
 
                                            bool verify_checksums,
48
 
                                            drizzle_return_t *ret_ptr)
 
43
drizzle_binlog_st *drizzle_binlog_init(drizzle_st *con,
 
44
                                       drizzle_binlog_fn *binlog_fn,
 
45
                                       drizzle_binlog_error_fn *error_fn,
 
46
                                       void *context,
 
47
                                       bool verify_checksums)
 
48
{
 
49
  if (con == NULL)
 
50
  {
 
51
    return NULL;
 
52
  }
 
53
 
 
54
  drizzle_binlog_st *binlog= new (std::nothrow) drizzle_binlog_st;
 
55
  if (binlog == NULL)
 
56
  {
 
57
    drizzle_set_error(con, __func__, "error allocating binlog struct");
 
58
    return NULL;
 
59
  }
 
60
  binlog->con= con;
 
61
  binlog->binlog_fn= binlog_fn;
 
62
  binlog->error_fn= error_fn;
 
63
  binlog->binlog_context= context;
 
64
  binlog->verify_checksums= verify_checksums;
 
65
 
 
66
  return binlog;
 
67
}
 
68
 
 
69
void drizzle_binlog_free(drizzle_binlog_st *binlog)
 
70
{
 
71
  delete binlog;
 
72
}
 
73
 
 
74
drizzle_return_t drizzle_binlog_start(drizzle_binlog_st *binlog,
 
75
                                          uint32_t server_id,
 
76
                                          const char *file,
 
77
                                          uint32_t start_position)
49
78
50
79
  unsigned char data[128];
51
80
  unsigned char *ptr;
52
81
  uint8_t len= 0, fn_len= 0;
53
82
  drizzle_result_st *result;
 
83
  drizzle_st *con;
 
84
  drizzle_return_t ret;
 
85
 
 
86
  if (binlog == NULL)
 
87
  {
 
88
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
89
  }
 
90
 
 
91
  con= binlog->con;
54
92
 
55
93
  // Hack in 5.6 to say that client support checksums
56
 
  result= drizzle_query(con, "SET @master_binlog_checksum='NONE'", 0, ret_ptr);
 
94
  result= drizzle_query(con, "SET @master_binlog_checksum='NONE'", 0, &ret);
57
95
  drizzle_result_free(result);
58
 
  if (*ret_ptr != DRIZZLE_RETURN_OK)
 
96
  if (ret != DRIZZLE_RETURN_OK)
59
97
  {
60
 
    return NULL;
 
98
    return ret;
61
99
  }
62
100
 
63
101
  ptr= data;
96
134
  }
97
135
 
98
136
  result= drizzle_command_write(con, NULL, DRIZZLE_COMMAND_BINLOG_DUMP,
99
 
                                   data, len, len, ret_ptr);
100
 
  result->binlog_event= new (std::nothrow) drizzle_binlog_st;
101
 
  result->binlog_event->verify_checksums= verify_checksums;
102
 
  return result;
103
 
}
 
137
                                   data, len, len, &ret);
104
138
 
105
 
drizzle_return_t drizzle_binlog_get_next_event(drizzle_result_st *result)
106
 
{
107
 
  if (result == NULL)
 
139
  con->binlog= binlog;
 
140
  if (ret != DRIZZLE_RETURN_OK)
108
141
  {
109
 
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
142
    return ret;
110
143
  }
111
 
 
112
144
  result->push_state(drizzle_state_binlog_read);
113
145
  result->push_state(drizzle_state_packet_read);
114
 
 
115
 
  return drizzle_state_loop(result->con);
 
146
  return drizzle_state_loop(con);
116
147
}
117
148
 
118
 
uint32_t drizzle_binlog_event_timestamp(drizzle_result_st *result)
 
149
uint32_t drizzle_binlog_event_timestamp(drizzle_binlog_event_st *event)
119
150
{
120
 
  if ((result == NULL) || (result->binlog_event == NULL))
 
151
  if (event == NULL)
121
152
  {
122
153
    return 0;
123
154
  }
124
155
 
125
 
  return result->binlog_event->timestamp;
 
156
  return event->timestamp;
126
157
}
127
158
 
128
 
drizzle_binlog_event_types_t drizzle_binlog_event_type(drizzle_result_st *result)
 
159
drizzle_binlog_event_types_t drizzle_binlog_event_type(drizzle_binlog_event_st *event)
129
160
{
130
 
  if ((result == NULL) || (result->binlog_event == NULL))
 
161
  if (event == NULL)
131
162
  {
132
163
    return drizzle_binlog_event_types_t();
133
164
  }
134
165
 
135
 
  return result->binlog_event->type;
136
 
}
137
 
 
138
 
uint32_t drizzle_binlog_event_server_id(drizzle_result_st *result)
139
 
{
140
 
  if ((result == NULL) || (result->binlog_event == NULL))
141
 
  {
142
 
    return 0;
143
 
  }
144
 
 
145
 
  return result->binlog_event->server_id;
146
 
}
147
 
 
148
 
uint32_t drizzle_binlog_event_length(drizzle_result_st *result)
149
 
{
150
 
  if ((result == NULL) || (result->binlog_event == NULL))
151
 
  {
152
 
    return 0;
153
 
  }
154
 
 
155
 
  return result->binlog_event->length;
156
 
}
157
 
 
158
 
uint32_t drizzle_binlog_event_next_pos(drizzle_result_st *result)
159
 
{
160
 
  if ((result == NULL) || (result->binlog_event == NULL))
161
 
  {
162
 
    return 0;
163
 
  }
164
 
 
165
 
  return result->binlog_event->next_pos;
166
 
}
167
 
 
168
 
uint16_t drizzle_binlog_event_flags(drizzle_result_st *result)
169
 
{
170
 
  if ((result == NULL) || (result->binlog_event == NULL))
171
 
  {
172
 
    return 0;
173
 
  }
174
 
 
175
 
  return result->binlog_event->flags;
176
 
}
177
 
 
178
 
uint16_t drizzle_binlog_event_extra_flags(drizzle_result_st *result)
179
 
{
180
 
  if ((result == NULL) || (result->binlog_event == NULL))
181
 
  {
182
 
    return 0;
183
 
  }
184
 
 
185
 
  return result->binlog_event->extra_flags;
186
 
}
187
 
 
188
 
const unsigned char *drizzle_binlog_event_data(drizzle_result_st *result)
189
 
{
190
 
  if ((result == NULL) || (result->binlog_event == NULL))
191
 
  {
192
 
    return NULL;
193
 
  }
194
 
 
195
 
  return result->binlog_event->data;
196
 
}
197
 
 
198
 
const unsigned char *drizzle_binlog_event_raw_data(drizzle_result_st *result)
199
 
{
200
 
  if ((result == NULL) || (result->binlog_event == NULL))
201
 
  {
202
 
    return NULL;
203
 
  }
204
 
 
205
 
  return result->binlog_event->raw_data;
206
 
}
207
 
 
208
 
uint32_t drizzle_binlog_event_raw_length(drizzle_result_st *result)
209
 
{
210
 
  if ((result == NULL) || (result->binlog_event == NULL))
211
 
  {
212
 
    return 0;
213
 
  }
214
 
 
215
 
  return result->binlog_event->raw_length;
 
166
  return event->type;
 
167
}
 
168
 
 
169
uint32_t drizzle_binlog_event_server_id(drizzle_binlog_event_st *event)
 
170
{
 
171
  if (event == NULL)
 
172
  {
 
173
    return 0;
 
174
  }
 
175
 
 
176
  return event->server_id;
 
177
}
 
178
 
 
179
uint32_t drizzle_binlog_event_length(drizzle_binlog_event_st *event)
 
180
{
 
181
  if (event == NULL)
 
182
  {
 
183
    return 0;
 
184
  }
 
185
 
 
186
  return event->length;
 
187
}
 
188
 
 
189
uint32_t drizzle_binlog_event_next_pos(drizzle_binlog_event_st *event)
 
190
{
 
191
  if (event == NULL)
 
192
  {
 
193
    return 0;
 
194
  }
 
195
 
 
196
  return event->next_pos;
 
197
}
 
198
 
 
199
uint16_t drizzle_binlog_event_flags(drizzle_binlog_event_st *event)
 
200
{
 
201
  if (event == NULL)
 
202
  {
 
203
    return 0;
 
204
  }
 
205
 
 
206
  return event->flags;
 
207
}
 
208
 
 
209
uint16_t drizzle_binlog_event_extra_flags(drizzle_binlog_event_st *event)
 
210
{
 
211
  if (event == NULL)
 
212
  {
 
213
    return 0;
 
214
  }
 
215
 
 
216
  return event->extra_flags;
 
217
}
 
218
 
 
219
const unsigned char *drizzle_binlog_event_data(drizzle_binlog_event_st *event)
 
220
{
 
221
  if (event == NULL)
 
222
  {
 
223
    return NULL;
 
224
  }
 
225
 
 
226
  return event->data;
 
227
}
 
228
 
 
229
const unsigned char *drizzle_binlog_event_raw_data(drizzle_binlog_event_st *event)
 
230
{
 
231
  if (event == NULL)
 
232
  {
 
233
    return NULL;
 
234
  }
 
235
 
 
236
  return event->raw_data;
 
237
}
 
238
 
 
239
uint32_t drizzle_binlog_event_raw_length(drizzle_binlog_event_st *event)
 
240
{
 
241
  if (event == NULL)
 
242
  {
 
243
    return 0;
 
244
  }
 
245
 
 
246
  return event->raw_length;
216
247
}
217
248
 
218
249
drizzle_return_t drizzle_state_binlog_read(drizzle_st *con)
219
250
{
220
 
  drizzle_binlog_st *binlog_event;
 
251
  drizzle_binlog_event_st *binlog_event;
221
252
 
222
253
  if (con == NULL)
223
254
  {
224
255
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
225
256
  }
226
257
 
227
 
  binlog_event= con->result->binlog_event;
 
258
  binlog_event= &con->binlog->event;
228
259
 
229
260
  if (con->packet_size != 0 && con->buffer_size < con->packet_size)
230
261
  {
240
271
    con->buffer_ptr+= 5;
241
272
    con->buffer_size-= 5;
242
273
    con->pop_state();
 
274
    con->binlog->error_fn(DRIZZLE_RETURN_EOF, con->binlog->binlog_context);
243
275
    return DRIZZLE_RETURN_EOF;
244
276
  }
245
277
  else if (con->buffer_ptr[0] == 255)
267
299
    con->packet_size= 0;
268
300
 
269
301
    con->pop_state();
 
302
    con->binlog->error_fn(DRIZZLE_RETURN_ERROR_CODE, con->binlog->binlog_context);
270
303
    return DRIZZLE_RETURN_ERROR_CODE;
271
304
  }
272
305
  else
283
316
    {
284
317
        drizzle_set_error(con, "drizzle_state_binlog_read",
285
318
                          "packet size error:%zu:%zu", con->packet_size, binlog_event->length);
 
319
        con->binlog->error_fn(DRIZZLE_RETURN_UNEXPECTED_DATA, con->binlog->binlog_context);
286
320
        return DRIZZLE_RETURN_UNEXPECTED_DATA;
287
321
    }
288
322
    if (binlog_event->length <= 27)
311
345
      {
312
346
        if (strncmp((const char*)con->buffer_ptr + 2, DRIZZLE_BINLOG_CHECKSUM_VERSION, strlen(DRIZZLE_BINLOG_CHECKSUM_VERSION)) <= 0)
313
347
        {
314
 
          con->result->binlog_checksums= true;
 
348
          con->binlog->has_checksums= true;
315
349
        }
316
350
      }
317
351
      /* A checksum is basically a CRC32 at the end of the event data (4 bytes) */
320
354
      con->buffer_size-= binlog_event->length;
321
355
      con->packet_size-= binlog_event->length;
322
356
      /* Remove the CRC32 from the event length */
323
 
      if (con->result->binlog_checksums)
 
357
      if (con->binlog->has_checksums)
324
358
      {
325
359
        binlog_event->length-= DRIZZLE_BINLOG_CRC32_LEN;
326
360
      }
330
364
     * each event is checksummed individually, the checksum is the last 4 bytes
331
365
     * of the binary log event
332
366
     * */
333
 
    if (con->result->binlog_checksums)
 
367
    if (con->binlog->has_checksums)
334
368
    {
335
369
      uint32_t event_crc;
336
370
      memcpy(&binlog_event->checksum, binlog_event->raw_data + (binlog_event->raw_length - DRIZZLE_BINLOG_CRC32_LEN), DRIZZLE_BINLOG_CRC32_LEN);
337
 
      if (binlog_event->verify_checksums)
 
371
      if (con->binlog->verify_checksums)
338
372
      {
339
373
        event_crc= crc32(0, binlog_event->raw_data, (binlog_event->raw_length - DRIZZLE_BINLOG_CRC32_LEN));
340
374
        if (event_crc != binlog_event->checksum)
341
375
        {
342
376
          drizzle_set_error(con, __func__, "CRC doesn't match: 0x%lX, 0x%lX", event_crc, binlog_event->checksum);
 
377
          con->binlog->error_fn(DRIZZLE_RETURN_BINLOG_CRC, con->binlog->binlog_context);
343
378
          return DRIZZLE_RETURN_BINLOG_CRC;
344
379
        }
345
380
      }
349
384
    {
350
385
      drizzle_set_error(con, "drizzle_state_binlog_read",
351
386
                        "unexpected data after packet:%zu", con->buffer_size);
 
387
      con->binlog->error_fn(DRIZZLE_RETURN_UNEXPECTED_DATA, con->binlog->binlog_context);
352
388
      return DRIZZLE_RETURN_UNEXPECTED_DATA;
353
389
    }
354
390
    con->pop_state();
355
391
  }
 
392
 
 
393
  con->binlog->binlog_fn(&con->binlog->event, con->binlog->binlog_context);
 
394
  con->push_state(drizzle_state_binlog_read);
 
395
  con->push_state(drizzle_state_packet_read);
 
396
 
356
397
  return DRIZZLE_RETURN_OK;
357
398
}
358
399