1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#include <drizzled/global.h>
22
#include "libdrizzle.h"
23
#include "libdrizzle_priv.h"
35
#include <sys/socket.h>
36
#include <netinet/in.h>
37
#include <arpa/inet.h>
42
#ifdef HAVE_SYS_SELECT_H
43
#include <sys/select.h>
53
#define INADDR_NONE -1
56
#include "local_infile.h"
57
#include <drizzled/gettext.h>
59
#define MY_ALIGN(A,L) (((A) + (L) - 1) & ~((L) - 1))
61
bool handle_local_infile(DRIZZLE *drizzle, const char *net_filename)
64
uint32_t packet_length=MY_ALIGN(drizzle->net.max_packet-16,IO_SIZE);
65
NET *net= &drizzle->net;
67
void *li_ptr; /* pass state to local_infile functions */
68
char *buf; /* buffer to be filled by local_infile_read */
69
struct st_drizzle_options *options= &drizzle->options;
71
/* check that we've got valid callback functions */
72
if (!(options->local_infile_init &&
73
options->local_infile_read &&
74
options->local_infile_end &&
75
options->local_infile_error))
77
/* if any of the functions is invalid, set the default */
78
drizzle_set_local_infile_default(drizzle);
81
/* copy filename into local memory and allocate read buffer */
82
if (!(buf=malloc(packet_length)))
84
drizzle_set_error(drizzle, CR_OUT_OF_MEMORY, sqlstate_get_unknown());
88
/* initialize local infile (open file, usually) */
89
if ((*options->local_infile_init)(&li_ptr, net_filename,
90
options->local_infile_userdata))
92
(void)my_net_write(net,(const unsigned char*) "",0); /* Server needs one packet */
94
strcpy(net->sqlstate, sqlstate_get_unknown());
96
(*options->local_infile_error)(li_ptr,
98
sizeof(net->last_error)-1);
102
/* read blocks of data from local infile callback */
104
(*options->local_infile_read)(li_ptr, buf,
107
if (my_net_write(net, (unsigned char*) buf, readcount))
113
/* Send empty packet to mark end of file */
114
if (my_net_write(net, (const unsigned char*) "", 0) || net_flush(net))
116
drizzle_set_error(drizzle, CR_SERVER_LOST, sqlstate_get_unknown());
123
(*options->local_infile_error)(li_ptr,
125
sizeof(net->last_error)-1);
129
result=false; /* Ok */
132
/* free up memory allocated with _init, usually */
133
(*options->local_infile_end)(li_ptr);
140
/****************************************************************************
141
Default handlers for LOAD LOCAL INFILE
142
****************************************************************************/
144
typedef struct st_default_local_infile
148
const char *filename;
149
char error_msg[LOCAL_INFILE_ERROR_LEN];
150
} default_local_infile_data;
154
Open file for LOAD LOCAL INFILE
157
default_local_infile_init()
158
ptr Store pointer to internal data here
159
filename File name to open. This may be in unix format !
163
Even if this function returns an error, the load data interface
164
guarantees that default_local_infile_end() is called.
171
static int default_local_infile_init(void **ptr, const char *filename,
172
void *userdata __attribute__ ((unused)))
174
default_local_infile_data *data;
175
char tmp_name[FN_REFLEN];
177
if (!(*ptr= data= ((default_local_infile_data *)
178
malloc(sizeof(default_local_infile_data)))))
179
return 1; /* out of memory */
181
data->error_msg[0]= 0;
183
data->filename= filename;
185
if ((data->fd = open(tmp_name, O_RDONLY)) < 0)
187
data->error_num= errno;
188
snprintf(data->error_msg, sizeof(data->error_msg)-1,
189
_("File '%s' not found (Errcode: %d)"), tmp_name, data->error_num);
197
Read data for LOAD LOCAL INFILE
200
default_local_infile_read()
201
ptr Points to handle allocated by _init
203
buf_len Ammount of data to read
206
> 0 number of bytes read
211
static int default_local_infile_read(void *ptr, char *buf, uint32_t buf_len)
214
default_local_infile_data*data = (default_local_infile_data *) ptr;
216
if ((count= (int) read(data->fd, (unsigned char *) buf, buf_len)) < 0)
218
data->error_num= 2; /* the errmsg for not entire file read */
219
snprintf(data->error_msg, sizeof(data->error_msg)-1,
220
_("Error reading file '%s' (Errcode: %d)"),
221
data->filename, errno);
228
Read data for LOAD LOCAL INFILE
231
default_local_infile_end()
232
ptr Points to handle allocated by _init
233
May be NULL if _init failed!
238
static void default_local_infile_end(void *ptr)
240
default_local_infile_data *data= (default_local_infile_data *) ptr;
241
if (data) /* If not error on open */
251
Return error from LOAD LOCAL INFILE
254
default_local_infile_end()
255
ptr Points to handle allocated by _init
256
May be NULL if _init failed!
257
error_msg Store error text here
258
error_msg_len Max lenght of error_msg
265
default_local_infile_error(void *ptr, char *error_msg, uint32_t error_msg_len)
267
default_local_infile_data *data = (default_local_infile_data *) ptr;
268
if (data) /* If not error on open */
270
strncpy(error_msg, data->error_msg, error_msg_len);
271
return data->error_num;
273
/* This can only happen if we got error on malloc of handle */
274
strcpy(error_msg, ER(CR_OUT_OF_MEMORY));
275
return CR_OUT_OF_MEMORY;
280
drizzle_set_local_infile_handler(DRIZZLE *drizzle,
281
int (*local_infile_init)(void **, const char *,
283
int (*local_infile_read)(void *, char *, uint32_t),
284
void (*local_infile_end)(void *),
285
int (*local_infile_error)(void *, char *, uint32_t),
288
drizzle->options.local_infile_init= local_infile_init;
289
drizzle->options.local_infile_read= local_infile_read;
290
drizzle->options.local_infile_end= local_infile_end;
291
drizzle->options.local_infile_error= local_infile_error;
292
drizzle->options.local_infile_userdata = userdata;
296
void drizzle_set_local_infile_default(DRIZZLE *drizzle)
298
drizzle->options.local_infile_init= default_local_infile_init;
299
drizzle->options.local_infile_read= default_local_infile_read;
300
drizzle->options.local_infile_end= default_local_infile_end;
301
drizzle->options.local_infile_error= default_local_infile_error;