1
/* Copyright (C) 2000-2004 DRIZZLE AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation.
7
There are special exceptions to the terms and conditions of the GPL as it
8
is applied to this software. View the full text of the exception in file
9
EXCEPTIONS-CLIENT in the directory of this software distribution.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21
#include <drizzled/global.h>
31
#include <sys/socket.h>
32
#include <netinet/in.h>
33
#include <arpa/inet.h>
38
#ifdef HAVE_SYS_SELECT_H
39
#include <sys/select.h>
49
#define INADDR_NONE -1
52
#include <sql_common.h>
53
#include "client_settings.h"
56
bool handle_local_infile(DRIZZLE *drizzle, const char *net_filename)
59
uint packet_length=MY_ALIGN(drizzle->net.max_packet-16,IO_SIZE);
60
NET *net= &drizzle->net;
62
void *li_ptr; /* pass state to local_infile functions */
63
char *buf; /* buffer to be filled by local_infile_read */
64
struct st_drizzle_options *options= &drizzle->options;
66
/* check that we've got valid callback functions */
67
if (!(options->local_infile_init &&
68
options->local_infile_read &&
69
options->local_infile_end &&
70
options->local_infile_error))
72
/* if any of the functions is invalid, set the default */
73
drizzle_set_local_infile_default(drizzle);
76
/* copy filename into local memory and allocate read buffer */
77
if (!(buf=malloc(packet_length)))
79
set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
83
/* initialize local infile (open file, usually) */
84
if ((*options->local_infile_init)(&li_ptr, net_filename,
85
options->local_infile_userdata))
87
VOID(my_net_write(net,(const uchar*) "",0)); /* Server needs one packet */
89
strcpy(net->sqlstate, unknown_sqlstate);
91
(*options->local_infile_error)(li_ptr,
93
sizeof(net->last_error)-1);
97
/* read blocks of data from local infile callback */
99
(*options->local_infile_read)(li_ptr, buf,
102
if (my_net_write(net, (uchar*) buf, readcount))
108
/* Send empty packet to mark end of file */
109
if (my_net_write(net, (const uchar*) "", 0) || net_flush(net))
111
set_drizzle_error(drizzle, CR_SERVER_LOST, unknown_sqlstate);
118
(*options->local_infile_error)(li_ptr,
120
sizeof(net->last_error)-1);
124
result=false; /* Ok */
127
/* free up memory allocated with _init, usually */
128
(*options->local_infile_end)(li_ptr);
135
/****************************************************************************
136
Default handlers for LOAD LOCAL INFILE
137
****************************************************************************/
139
typedef struct st_default_local_infile
143
const char *filename;
144
char error_msg[LOCAL_INFILE_ERROR_LEN];
145
} default_local_infile_data;
149
Open file for LOAD LOCAL INFILE
152
default_local_infile_init()
153
ptr Store pointer to internal data here
154
filename File name to open. This may be in unix format !
158
Even if this function returns an error, the load data interface
159
guarantees that default_local_infile_end() is called.
166
static int default_local_infile_init(void **ptr, const char *filename,
167
void *userdata __attribute__ ((unused)))
169
default_local_infile_data *data;
170
char tmp_name[FN_REFLEN];
172
if (!(*ptr= data= ((default_local_infile_data *)
173
malloc(sizeof(default_local_infile_data)))))
174
return 1; /* out of memory */
176
data->error_msg[0]= 0;
178
data->filename= filename;
180
if ((data->fd = open(tmp_name, O_RDONLY)) < 0)
182
data->error_num= errno;
183
snprintf(data->error_msg, sizeof(data->error_msg)-1,
184
_("File '%s' not found (Errcode: %d)"), tmp_name, data->error_num);
192
Read data for LOAD LOCAL INFILE
195
default_local_infile_read()
196
ptr Points to handle allocated by _init
198
buf_len Ammount of data to read
201
> 0 number of bytes read
206
static int default_local_infile_read(void *ptr, char *buf, uint buf_len)
209
default_local_infile_data*data = (default_local_infile_data *) ptr;
211
if ((count= (int) read(data->fd, (uchar *) buf, buf_len)) < 0)
213
data->error_num= 2; /* the errmsg for not entire file read */
214
snprintf(data->error_msg, sizeof(data->error_msg)-1,
215
_("Error reading file '%s' (Errcode: %d)"),
216
data->filename, errno);
223
Read data for LOAD LOCAL INFILE
226
default_local_infile_end()
227
ptr Points to handle allocated by _init
228
May be NULL if _init failed!
233
static void default_local_infile_end(void *ptr)
235
default_local_infile_data *data= (default_local_infile_data *) ptr;
236
if (data) /* If not error on open */
246
Return error from LOAD LOCAL INFILE
249
default_local_infile_end()
250
ptr Points to handle allocated by _init
251
May be NULL if _init failed!
252
error_msg Store error text here
253
error_msg_len Max lenght of error_msg
260
default_local_infile_error(void *ptr, char *error_msg, uint error_msg_len)
262
default_local_infile_data *data = (default_local_infile_data *) ptr;
263
if (data) /* If not error on open */
265
strncpy(error_msg, data->error_msg, error_msg_len);
266
return data->error_num;
268
/* This can only happen if we got error on malloc of handle */
269
strcpy(error_msg, ER(CR_OUT_OF_MEMORY));
270
return CR_OUT_OF_MEMORY;
275
drizzle_set_local_infile_handler(DRIZZLE *drizzle,
276
int (*local_infile_init)(void **, const char *,
278
int (*local_infile_read)(void *, char *, uint),
279
void (*local_infile_end)(void *),
280
int (*local_infile_error)(void *, char *, uint),
283
drizzle->options.local_infile_init= local_infile_init;
284
drizzle->options.local_infile_read= local_infile_read;
285
drizzle->options.local_infile_end= local_infile_end;
286
drizzle->options.local_infile_error= local_infile_error;
287
drizzle->options.local_infile_userdata = userdata;
291
void drizzle_set_local_infile_default(DRIZZLE *drizzle)
293
drizzle->options.local_infile_init= default_local_infile_init;
294
drizzle->options.local_infile_read= default_local_infile_read;
295
drizzle->options.local_infile_end= default_local_infile_end;
296
drizzle->options.local_infile_error= default_local_infile_error;