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>
22
#include "libdrizzle.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 "local_infile.h"
57
bool handle_local_infile(DRIZZLE *drizzle, const char *net_filename)
60
uint packet_length=MY_ALIGN(drizzle->net.max_packet-16,IO_SIZE);
61
NET *net= &drizzle->net;
63
void *li_ptr; /* pass state to local_infile functions */
64
char *buf; /* buffer to be filled by local_infile_read */
65
struct st_drizzle_options *options= &drizzle->options;
67
/* check that we've got valid callback functions */
68
if (!(options->local_infile_init &&
69
options->local_infile_read &&
70
options->local_infile_end &&
71
options->local_infile_error))
73
/* if any of the functions is invalid, set the default */
74
drizzle_set_local_infile_default(drizzle);
77
/* copy filename into local memory and allocate read buffer */
78
if (!(buf=malloc(packet_length)))
80
set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
84
/* initialize local infile (open file, usually) */
85
if ((*options->local_infile_init)(&li_ptr, net_filename,
86
options->local_infile_userdata))
88
VOID(my_net_write(net,(const uchar*) "",0)); /* Server needs one packet */
90
strcpy(net->sqlstate, unknown_sqlstate);
92
(*options->local_infile_error)(li_ptr,
94
sizeof(net->last_error)-1);
98
/* read blocks of data from local infile callback */
100
(*options->local_infile_read)(li_ptr, buf,
103
if (my_net_write(net, (uchar*) buf, readcount))
109
/* Send empty packet to mark end of file */
110
if (my_net_write(net, (const uchar*) "", 0) || net_flush(net))
112
set_drizzle_error(drizzle, CR_SERVER_LOST, unknown_sqlstate);
119
(*options->local_infile_error)(li_ptr,
121
sizeof(net->last_error)-1);
125
result=false; /* Ok */
128
/* free up memory allocated with _init, usually */
129
(*options->local_infile_end)(li_ptr);
136
/****************************************************************************
137
Default handlers for LOAD LOCAL INFILE
138
****************************************************************************/
140
typedef struct st_default_local_infile
144
const char *filename;
145
char error_msg[LOCAL_INFILE_ERROR_LEN];
146
} default_local_infile_data;
150
Open file for LOAD LOCAL INFILE
153
default_local_infile_init()
154
ptr Store pointer to internal data here
155
filename File name to open. This may be in unix format !
159
Even if this function returns an error, the load data interface
160
guarantees that default_local_infile_end() is called.
167
static int default_local_infile_init(void **ptr, const char *filename,
168
void *userdata __attribute__ ((unused)))
170
default_local_infile_data *data;
171
char tmp_name[FN_REFLEN];
173
if (!(*ptr= data= ((default_local_infile_data *)
174
malloc(sizeof(default_local_infile_data)))))
175
return 1; /* out of memory */
177
data->error_msg[0]= 0;
179
data->filename= filename;
181
if ((data->fd = open(tmp_name, O_RDONLY)) < 0)
183
data->error_num= errno;
184
snprintf(data->error_msg, sizeof(data->error_msg)-1,
185
_("File '%s' not found (Errcode: %d)"), tmp_name, data->error_num);
193
Read data for LOAD LOCAL INFILE
196
default_local_infile_read()
197
ptr Points to handle allocated by _init
199
buf_len Ammount of data to read
202
> 0 number of bytes read
207
static int default_local_infile_read(void *ptr, char *buf, uint buf_len)
210
default_local_infile_data*data = (default_local_infile_data *) ptr;
212
if ((count= (int) read(data->fd, (uchar *) buf, buf_len)) < 0)
214
data->error_num= 2; /* the errmsg for not entire file read */
215
snprintf(data->error_msg, sizeof(data->error_msg)-1,
216
_("Error reading file '%s' (Errcode: %d)"),
217
data->filename, errno);
224
Read data for LOAD LOCAL INFILE
227
default_local_infile_end()
228
ptr Points to handle allocated by _init
229
May be NULL if _init failed!
234
static void default_local_infile_end(void *ptr)
236
default_local_infile_data *data= (default_local_infile_data *) ptr;
237
if (data) /* If not error on open */
247
Return error from LOAD LOCAL INFILE
250
default_local_infile_end()
251
ptr Points to handle allocated by _init
252
May be NULL if _init failed!
253
error_msg Store error text here
254
error_msg_len Max lenght of error_msg
261
default_local_infile_error(void *ptr, char *error_msg, uint error_msg_len)
263
default_local_infile_data *data = (default_local_infile_data *) ptr;
264
if (data) /* If not error on open */
266
strncpy(error_msg, data->error_msg, error_msg_len);
267
return data->error_num;
269
/* This can only happen if we got error on malloc of handle */
270
strcpy(error_msg, ER(CR_OUT_OF_MEMORY));
271
return CR_OUT_OF_MEMORY;
276
drizzle_set_local_infile_handler(DRIZZLE *drizzle,
277
int (*local_infile_init)(void **, const char *,
279
int (*local_infile_read)(void *, char *, uint),
280
void (*local_infile_end)(void *),
281
int (*local_infile_error)(void *, char *, uint),
284
drizzle->options.local_infile_init= local_infile_init;
285
drizzle->options.local_infile_read= local_infile_read;
286
drizzle->options.local_infile_end= local_infile_end;
287
drizzle->options.local_infile_error= local_infile_error;
288
drizzle->options.local_infile_userdata = userdata;
292
void drizzle_set_local_infile_default(DRIZZLE *drizzle)
294
drizzle->options.local_infile_init= default_local_infile_init;
295
drizzle->options.local_infile_read= default_local_infile_read;
296
drizzle->options.local_infile_end= default_local_infile_end;
297
drizzle->options.local_infile_error= default_local_infile_error;