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 drizzleclient_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_drizzleclient_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
drizzleclient_set_local_infile_default(drizzle);
81
/* copy filename into local memory and allocate read buffer */
82
if (!(buf=malloc(packet_length)))
84
drizzleclient_set_error(drizzle, CR_OUT_OF_MEMORY, drizzleclient_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)drizzleclient_net_write(net,(const unsigned char*) "",0); /* Server needs one packet */
93
drizzleclient_net_flush(net);
94
strcpy(net->sqlstate, drizzleclient_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 (drizzleclient_net_write(net, (unsigned char*) buf, readcount))
113
/* Send empty packet to mark end of file */
114
if (drizzleclient_net_write(net, (const unsigned char*) "", 0) || drizzleclient_net_flush(net))
116
drizzleclient_set_error(drizzle, CR_SERVER_LOST, drizzleclient_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,
175
default_local_infile_data *data;
176
char tmp_name[FN_REFLEN];
178
if (!(*ptr= data= ((default_local_infile_data *)
179
malloc(sizeof(default_local_infile_data)))))
180
return 1; /* out of memory */
182
data->error_msg[0]= 0;
184
data->filename= filename;
186
if ((data->fd = open(tmp_name, O_RDONLY)) < 0)
188
data->error_num= errno;
189
snprintf(data->error_msg, sizeof(data->error_msg)-1,
190
_("File '%s' not found (Errcode: %d)"), tmp_name, data->error_num);
198
Read data for LOAD LOCAL INFILE
201
default_local_infile_read()
202
ptr Points to handle allocated by _init
204
buf_len Ammount of data to read
207
> 0 number of bytes read
212
static int default_local_infile_read(void *ptr, char *buf, uint32_t buf_len)
215
default_local_infile_data*data = (default_local_infile_data *) ptr;
217
if ((count= (int) read(data->fd, (unsigned char *) buf, buf_len)) < 0)
219
data->error_num= 2; /* the errmsg for not entire file read */
220
snprintf(data->error_msg, sizeof(data->error_msg)-1,
221
_("Error reading file '%s' (Errcode: %d)"),
222
data->filename, errno);
229
Read data for LOAD LOCAL INFILE
232
default_local_infile_end()
233
ptr Points to handle allocated by _init
234
May be NULL if _init failed!
239
static void default_local_infile_end(void *ptr)
241
default_local_infile_data *data= (default_local_infile_data *) ptr;
242
if (data) /* If not error on open */
252
Return error from LOAD LOCAL INFILE
255
default_local_infile_end()
256
ptr Points to handle allocated by _init
257
May be NULL if _init failed!
258
error_msg Store error text here
259
error_msg_len Max lenght of error_msg
266
default_local_infile_error(void *ptr, char *error_msg, uint32_t error_msg_len)
268
default_local_infile_data *data = (default_local_infile_data *) ptr;
269
if (data) /* If not error on open */
271
strncpy(error_msg, data->error_msg, error_msg_len);
272
return data->error_num;
274
/* This can only happen if we got error on malloc of handle */
275
strcpy(error_msg, ER(CR_OUT_OF_MEMORY));
276
return CR_OUT_OF_MEMORY;
281
drizzleclient_set_local_infile_handler(DRIZZLE *drizzle,
282
int (*local_infile_init)(void **, const char *,
284
int (*local_infile_read)(void *, char *, uint32_t),
285
void (*local_infile_end)(void *),
286
int (*local_infile_error)(void *, char *, uint32_t),
289
drizzle->options.local_infile_init= local_infile_init;
290
drizzle->options.local_infile_read= local_infile_read;
291
drizzle->options.local_infile_end= local_infile_end;
292
drizzle->options.local_infile_error= local_infile_error;
293
drizzle->options.local_infile_userdata = userdata;
297
void drizzleclient_set_local_infile_default(DRIZZLE *drizzle)
299
drizzle->options.local_infile_init= default_local_infile_init;
300
drizzle->options.local_infile_read= default_local_infile_read;
301
drizzle->options.local_infile_end= default_local_infile_end;
302
drizzle->options.local_infile_error= default_local_infile_error;