~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/libdrizzle.c

  • Committer: Andrey Hristov
  • Date: 2008-07-31 16:19:47 UTC
  • mto: (236.1.30 codestyle)
  • mto: This revision was merged to the branch mainline in revision 244.
  • Revision ID: ahristov@mysql.com-20080731161947-df0un65r7v51bkhu
Move local infile handling to a separate file

Show diffs side-by-side

added added

removed removed

Lines of Context:
228
228
}
229
229
 
230
230
 
231
 
bool handle_local_infile(DRIZZLE *drizzle, const char *net_filename)
232
 
{
233
 
  bool result= true;
234
 
  uint packet_length=MY_ALIGN(drizzle->net.max_packet-16,IO_SIZE);
235
 
  NET *net= &drizzle->net;
236
 
  int readcount;
237
 
  void *li_ptr;          /* pass state to local_infile functions */
238
 
  char *buf;    /* buffer to be filled by local_infile_read */
239
 
  struct st_drizzle_options *options= &drizzle->options;
240
 
 
241
 
  /* check that we've got valid callback functions */
242
 
  if (!(options->local_infile_init &&
243
 
  options->local_infile_read &&
244
 
  options->local_infile_end &&
245
 
  options->local_infile_error))
246
 
  {
247
 
    /* if any of the functions is invalid, set the default */
248
 
    drizzle_set_local_infile_default(drizzle);
249
 
  }
250
 
 
251
 
  /* copy filename into local memory and allocate read buffer */
252
 
  if (!(buf=malloc(packet_length)))
253
 
  {
254
 
    set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
255
 
    return(1);
256
 
  }
257
 
 
258
 
  /* initialize local infile (open file, usually) */
259
 
  if ((*options->local_infile_init)(&li_ptr, net_filename,
260
 
    options->local_infile_userdata))
261
 
  {
262
 
    VOID(my_net_write(net,(const uchar*) "",0)); /* Server needs one packet */
263
 
    net_flush(net);
264
 
    strmov(net->sqlstate, unknown_sqlstate);
265
 
    net->last_errno=
266
 
      (*options->local_infile_error)(li_ptr,
267
 
                                     net->last_error,
268
 
                                     sizeof(net->last_error)-1);
269
 
    goto err;
270
 
  }
271
 
 
272
 
  /* read blocks of data from local infile callback */
273
 
  while ((readcount =
274
 
    (*options->local_infile_read)(li_ptr, buf,
275
 
          packet_length)) > 0)
276
 
  {
277
 
    if (my_net_write(net, (uchar*) buf, readcount))
278
 
    {
279
 
      goto err;
280
 
    }
281
 
  }
282
 
 
283
 
  /* Send empty packet to mark end of file */
284
 
  if (my_net_write(net, (const uchar*) "", 0) || net_flush(net))
285
 
  {
286
 
    set_drizzle_error(drizzle, CR_SERVER_LOST, unknown_sqlstate);
287
 
    goto err;
288
 
  }
289
 
 
290
 
  if (readcount < 0)
291
 
  {
292
 
    net->last_errno=
293
 
      (*options->local_infile_error)(li_ptr,
294
 
                                     net->last_error,
295
 
                                     sizeof(net->last_error)-1);
296
 
    goto err;
297
 
  }
298
 
 
299
 
  result=false;                                 /* Ok */
300
 
 
301
 
err:
302
 
  /* free up memory allocated with _init, usually */
303
 
  (*options->local_infile_end)(li_ptr);
304
 
  if(buf)
305
 
    free(buf);
306
 
  return(result);
307
 
}
308
 
 
309
 
 
310
 
/****************************************************************************
311
 
  Default handlers for LOAD LOCAL INFILE
312
 
****************************************************************************/
313
 
 
314
 
typedef struct st_default_local_infile
315
 
{
316
 
  int fd;
317
 
  int error_num;
318
 
  const char *filename;
319
 
  char error_msg[LOCAL_INFILE_ERROR_LEN];
320
 
} default_local_infile_data;
321
 
 
322
 
 
323
 
/*
324
 
  Open file for LOAD LOCAL INFILE
325
 
 
326
 
  SYNOPSIS
327
 
    default_local_infile_init()
328
 
    ptr      Store pointer to internal data here
329
 
    filename    File name to open. This may be in unix format !
330
 
 
331
 
 
332
 
  NOTES
333
 
    Even if this function returns an error, the load data interface
334
 
    guarantees that default_local_infile_end() is called.
335
 
 
336
 
  RETURN
337
 
    0  ok
338
 
    1  error
339
 
*/
340
 
 
341
 
static int default_local_infile_init(void **ptr, const char *filename,
342
 
             void *userdata __attribute__ ((unused)))
343
 
{
344
 
  default_local_infile_data *data;
345
 
  char tmp_name[FN_REFLEN];
346
 
 
347
 
  if (!(*ptr= data= ((default_local_infile_data *)
348
 
                     malloc(sizeof(default_local_infile_data)))))
349
 
    return 1; /* out of memory */
350
 
 
351
 
  data->error_msg[0]= 0;
352
 
  data->error_num=    0;
353
 
  data->filename= filename;
354
 
 
355
 
  fn_format(tmp_name, filename, "", "", MY_UNPACK_FILENAME);
356
 
  if ((data->fd = my_open(tmp_name, O_RDONLY, MYF(0))) < 0)
357
 
  {
358
 
    data->error_num= my_errno;
359
 
    snprintf(data->error_msg, sizeof(data->error_msg)-1,
360
 
             EE(EE_FILENOTFOUND), tmp_name, data->error_num);
361
 
    return 1;
362
 
  }
363
 
  return 0; /* ok */
364
 
}
365
 
 
366
 
 
367
 
/*
368
 
  Read data for LOAD LOCAL INFILE
369
 
 
370
 
  SYNOPSIS
371
 
    default_local_infile_read()
372
 
    ptr      Points to handle allocated by _init
373
 
    buf      Read data here
374
 
    buf_len    Ammount of data to read
375
 
 
376
 
  RETURN
377
 
    > 0    number of bytes read
378
 
    == 0  End of data
379
 
    < 0    Error
380
 
*/
381
 
 
382
 
static int default_local_infile_read(void *ptr, char *buf, uint buf_len)
383
 
{
384
 
  int count;
385
 
  default_local_infile_data*data = (default_local_infile_data *) ptr;
386
 
 
387
 
  if ((count= (int) my_read(data->fd, (uchar *) buf, buf_len, MYF(0))) < 0)
388
 
  {
389
 
    data->error_num= EE_READ; /* the errmsg for not entire file read */
390
 
    snprintf(data->error_msg, sizeof(data->error_msg)-1,
391
 
             EE(EE_READ),
392
 
             data->filename, my_errno);
393
 
  }
394
 
  return count;
395
 
}
396
 
 
397
 
 
398
 
/*
399
 
  Read data for LOAD LOCAL INFILE
400
 
 
401
 
  SYNOPSIS
402
 
    default_local_infile_end()
403
 
    ptr      Points to handle allocated by _init
404
 
      May be NULL if _init failed!
405
 
 
406
 
  RETURN
407
 
*/
408
 
 
409
 
static void default_local_infile_end(void *ptr)
410
 
{
411
 
  default_local_infile_data *data= (default_local_infile_data *) ptr;
412
 
  if (data)          /* If not error on open */
413
 
  {
414
 
    if (data->fd >= 0)
415
 
      my_close(data->fd, MYF(MY_WME));
416
 
    free(ptr);
417
 
  }
418
 
}
419
 
 
420
 
 
421
 
/*
422
 
  Return error from LOAD LOCAL INFILE
423
 
 
424
 
  SYNOPSIS
425
 
    default_local_infile_end()
426
 
    ptr      Points to handle allocated by _init
427
 
      May be NULL if _init failed!
428
 
    error_msg    Store error text here
429
 
    error_msg_len  Max lenght of error_msg
430
 
 
431
 
  RETURN
432
 
    error message number
433
 
*/
434
 
 
435
 
static int
436
 
default_local_infile_error(void *ptr, char *error_msg, uint error_msg_len)
437
 
{
438
 
  default_local_infile_data *data = (default_local_infile_data *) ptr;
439
 
  if (data)          /* If not error on open */
440
 
  {
441
 
    strmake(error_msg, data->error_msg, error_msg_len);
442
 
    return data->error_num;
443
 
  }
444
 
  /* This can only happen if we got error on malloc of handle */
445
 
  strmov(error_msg, ER(CR_OUT_OF_MEMORY));
446
 
  return CR_OUT_OF_MEMORY;
447
 
}
448
 
 
449
 
 
450
 
void
451
 
drizzle_set_local_infile_handler(DRIZZLE *drizzle,
452
 
                               int (*local_infile_init)(void **, const char *,
453
 
                               void *),
454
 
                               int (*local_infile_read)(void *, char *, uint),
455
 
                               void (*local_infile_end)(void *),
456
 
                               int (*local_infile_error)(void *, char *, uint),
457
 
                               void *userdata)
458
 
{
459
 
  drizzle->options.local_infile_init=  local_infile_init;
460
 
  drizzle->options.local_infile_read=  local_infile_read;
461
 
  drizzle->options.local_infile_end=   local_infile_end;
462
 
  drizzle->options.local_infile_error= local_infile_error;
463
 
  drizzle->options.local_infile_userdata = userdata;
464
 
}
465
 
 
466
 
 
467
 
void drizzle_set_local_infile_default(DRIZZLE *drizzle)
468
 
{
469
 
  drizzle->options.local_infile_init=  default_local_infile_init;
470
 
  drizzle->options.local_infile_read=  default_local_infile_read;
471
 
  drizzle->options.local_infile_end=   default_local_infile_end;
472
 
  drizzle->options.local_infile_error= default_local_infile_error;
473
 
}
474
 
 
475
 
 
476
231
/**************************************************************************
477
232
  Do a query. If query returned rows, free old rows.
478
233
  Read data by drizzle_store_result or by repeat call of drizzle_fetch_row