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>
25
#include <vio/violite.h>
33
#include <sys/socket.h>
34
#include <netinet/in.h>
35
#include <arpa/inet.h>
40
#ifdef HAVE_SYS_SELECT_H
41
#include <sys/select.h>
51
#define INADDR_NONE -1
54
#include <sql_common.h>
55
#include "client_settings.h"
58
bool handle_local_infile(DRIZZLE *drizzle, const char *net_filename)
61
uint packet_length=MY_ALIGN(drizzle->net.max_packet-16,IO_SIZE);
62
NET *net= &drizzle->net;
64
void *li_ptr; /* pass state to local_infile functions */
65
char *buf; /* buffer to be filled by local_infile_read */
66
struct st_drizzle_options *options= &drizzle->options;
68
/* check that we've got valid callback functions */
69
if (!(options->local_infile_init &&
70
options->local_infile_read &&
71
options->local_infile_end &&
72
options->local_infile_error))
74
/* if any of the functions is invalid, set the default */
75
drizzle_set_local_infile_default(drizzle);
78
/* copy filename into local memory and allocate read buffer */
79
if (!(buf=malloc(packet_length)))
81
set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
85
/* initialize local infile (open file, usually) */
86
if ((*options->local_infile_init)(&li_ptr, net_filename,
87
options->local_infile_userdata))
89
VOID(my_net_write(net,(const uchar*) "",0)); /* Server needs one packet */
91
strcpy(net->sqlstate, unknown_sqlstate);
93
(*options->local_infile_error)(li_ptr,
95
sizeof(net->last_error)-1);
99
/* read blocks of data from local infile callback */
101
(*options->local_infile_read)(li_ptr, buf,
104
if (my_net_write(net, (uchar*) buf, readcount))
110
/* Send empty packet to mark end of file */
111
if (my_net_write(net, (const uchar*) "", 0) || net_flush(net))
113
set_drizzle_error(drizzle, CR_SERVER_LOST, unknown_sqlstate);
120
(*options->local_infile_error)(li_ptr,
122
sizeof(net->last_error)-1);
126
result=false; /* Ok */
129
/* free up memory allocated with _init, usually */
130
(*options->local_infile_end)(li_ptr);
137
/****************************************************************************
138
Default handlers for LOAD LOCAL INFILE
139
****************************************************************************/
141
typedef struct st_default_local_infile
145
const char *filename;
146
char error_msg[LOCAL_INFILE_ERROR_LEN];
147
} default_local_infile_data;
151
Open file for LOAD LOCAL INFILE
154
default_local_infile_init()
155
ptr Store pointer to internal data here
156
filename File name to open. This may be in unix format !
160
Even if this function returns an error, the load data interface
161
guarantees that default_local_infile_end() is called.
168
static int default_local_infile_init(void **ptr, const char *filename,
169
void *userdata __attribute__ ((unused)))
171
default_local_infile_data *data;
172
char tmp_name[FN_REFLEN];
174
if (!(*ptr= data= ((default_local_infile_data *)
175
malloc(sizeof(default_local_infile_data)))))
176
return 1; /* out of memory */
178
data->error_msg[0]= 0;
180
data->filename= filename;
182
if ((data->fd = open(tmp_name, O_RDONLY)) < 0)
184
data->error_num= errno;
185
snprintf(data->error_msg, sizeof(data->error_msg)-1,
186
_("File '%s' not found (Errcode: %d)"), tmp_name, data->error_num);
194
Read data for LOAD LOCAL INFILE
197
default_local_infile_read()
198
ptr Points to handle allocated by _init
200
buf_len Ammount of data to read
203
> 0 number of bytes read
208
static int default_local_infile_read(void *ptr, char *buf, uint buf_len)
211
default_local_infile_data*data = (default_local_infile_data *) ptr;
213
if ((count= (int) read(data->fd, (uchar *) buf, buf_len)) < 0)
215
data->error_num= 2; /* the errmsg for not entire file read */
216
snprintf(data->error_msg, sizeof(data->error_msg)-1,
217
_("Error reading file '%s' (Errcode: %d)"),
218
data->filename, errno);
225
Read data for LOAD LOCAL INFILE
228
default_local_infile_end()
229
ptr Points to handle allocated by _init
230
May be NULL if _init failed!
235
static void default_local_infile_end(void *ptr)
237
default_local_infile_data *data= (default_local_infile_data *) ptr;
238
if (data) /* If not error on open */
248
Return error from LOAD LOCAL INFILE
251
default_local_infile_end()
252
ptr Points to handle allocated by _init
253
May be NULL if _init failed!
254
error_msg Store error text here
255
error_msg_len Max lenght of error_msg
262
default_local_infile_error(void *ptr, char *error_msg, uint error_msg_len)
264
default_local_infile_data *data = (default_local_infile_data *) ptr;
265
if (data) /* If not error on open */
267
strncpy(error_msg, data->error_msg, error_msg_len);
268
return data->error_num;
270
/* This can only happen if we got error on malloc of handle */
271
strcpy(error_msg, ER(CR_OUT_OF_MEMORY));
272
return CR_OUT_OF_MEMORY;
277
drizzle_set_local_infile_handler(DRIZZLE *drizzle,
278
int (*local_infile_init)(void **, const char *,
280
int (*local_infile_read)(void *, char *, uint),
281
void (*local_infile_end)(void *),
282
int (*local_infile_error)(void *, char *, uint),
285
drizzle->options.local_infile_init= local_infile_init;
286
drizzle->options.local_infile_read= local_infile_read;
287
drizzle->options.local_infile_end= local_infile_end;
288
drizzle->options.local_infile_error= local_infile_error;
289
drizzle->options.local_infile_userdata = userdata;
293
void drizzle_set_local_infile_default(DRIZZLE *drizzle)
295
drizzle->options.local_infile_init= default_local_infile_init;
296
drizzle->options.local_infile_read= default_local_infile_read;
297
drizzle->options.local_infile_end= default_local_infile_end;
298
drizzle->options.local_infile_error= default_local_infile_error;