236.3.8
by Andrey Hristov
Move local infile handling to a separate file |
1 |
/* Copyright (C) 2000-2004 DRIZZLE AB
|
2 |
||
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.
|
|
6 |
||
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.
|
|
10 |
||
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.
|
|
15 |
||
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 */
|
|
19 |
||
20 |
||
21 |
#include <drizzled/global.h> |
|
22 |
#include "drizzle.h" |
|
23 |
#include "errmsg.h" |
|
24 |
#include <sys/stat.h> |
|
25 |
#include <signal.h> |
|
26 |
#include <time.h> |
|
27 |
#ifdef HAVE_PWD_H
|
|
28 |
#include <pwd.h> |
|
29 |
#endif
|
|
30 |
||
31 |
#include <sys/socket.h> |
|
32 |
#include <netinet/in.h> |
|
33 |
#include <arpa/inet.h> |
|
34 |
#include <netdb.h> |
|
35 |
#ifdef HAVE_SELECT_H
|
|
36 |
#include <select.h> |
|
37 |
#endif
|
|
38 |
#ifdef HAVE_SYS_SELECT_H
|
|
39 |
#include <sys/select.h> |
|
40 |
#endif
|
|
41 |
||
42 |
#ifdef HAVE_POLL
|
|
43 |
#include <sys/poll.h> |
|
44 |
#endif
|
|
45 |
#ifdef HAVE_SYS_UN_H
|
|
46 |
#include <sys/un.h> |
|
47 |
#endif
|
|
48 |
#ifndef INADDR_NONE
|
|
49 |
#define INADDR_NONE -1
|
|
50 |
#endif
|
|
51 |
||
52 |
#include <sql_common.h> |
|
53 |
#include "client_settings.h" |
|
54 |
||
55 |
||
56 |
bool handle_local_infile(DRIZZLE *drizzle, const char *net_filename) |
|
57 |
{
|
|
58 |
bool result= true; |
|
59 |
uint packet_length=MY_ALIGN(drizzle->net.max_packet-16,IO_SIZE); |
|
60 |
NET *net= &drizzle->net; |
|
61 |
int readcount; |
|
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; |
|
65 |
||
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)) |
|
71 |
{
|
|
72 |
/* if any of the functions is invalid, set the default */
|
|
73 |
drizzle_set_local_infile_default(drizzle); |
|
74 |
}
|
|
75 |
||
76 |
/* copy filename into local memory and allocate read buffer */
|
|
77 |
if (!(buf=malloc(packet_length))) |
|
78 |
{
|
|
79 |
set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate); |
|
80 |
return(1); |
|
81 |
}
|
|
82 |
||
83 |
/* initialize local infile (open file, usually) */
|
|
84 |
if ((*options->local_infile_init)(&li_ptr, net_filename, |
|
85 |
options->local_infile_userdata)) |
|
86 |
{
|
|
87 |
VOID(my_net_write(net,(const uchar*) "",0)); /* Server needs one packet */ |
|
88 |
net_flush(net); |
|
240.1.5
by Toru Maesaka
Merge with head and conflicts resolved |
89 |
strcpy(net->sqlstate, unknown_sqlstate); |
236.3.8
by Andrey Hristov
Move local infile handling to a separate file |
90 |
net->last_errno= |
91 |
(*options->local_infile_error)(li_ptr, |
|
92 |
net->last_error, |
|
93 |
sizeof(net->last_error)-1); |
|
94 |
goto err; |
|
95 |
}
|
|
96 |
||
97 |
/* read blocks of data from local infile callback */
|
|
98 |
while ((readcount = |
|
99 |
(*options->local_infile_read)(li_ptr, buf, |
|
100 |
packet_length)) > 0) |
|
101 |
{
|
|
102 |
if (my_net_write(net, (uchar*) buf, readcount)) |
|
103 |
{
|
|
104 |
goto err; |
|
105 |
}
|
|
106 |
}
|
|
107 |
||
108 |
/* Send empty packet to mark end of file */
|
|
109 |
if (my_net_write(net, (const uchar*) "", 0) || net_flush(net)) |
|
110 |
{
|
|
111 |
set_drizzle_error(drizzle, CR_SERVER_LOST, unknown_sqlstate); |
|
112 |
goto err; |
|
113 |
}
|
|
114 |
||
115 |
if (readcount < 0) |
|
116 |
{
|
|
117 |
net->last_errno= |
|
118 |
(*options->local_infile_error)(li_ptr, |
|
119 |
net->last_error, |
|
120 |
sizeof(net->last_error)-1); |
|
121 |
goto err; |
|
122 |
}
|
|
123 |
||
124 |
result=false; /* Ok */ |
|
125 |
||
126 |
err: |
|
127 |
/* free up memory allocated with _init, usually */
|
|
128 |
(*options->local_infile_end)(li_ptr); |
|
129 |
if(buf) |
|
130 |
free(buf); |
|
131 |
return(result); |
|
132 |
}
|
|
133 |
||
134 |
||
135 |
/****************************************************************************
|
|
136 |
Default handlers for LOAD LOCAL INFILE
|
|
137 |
****************************************************************************/
|
|
138 |
||
139 |
typedef struct st_default_local_infile |
|
140 |
{
|
|
141 |
int fd; |
|
142 |
int error_num; |
|
143 |
const char *filename; |
|
144 |
char error_msg[LOCAL_INFILE_ERROR_LEN]; |
|
145 |
} default_local_infile_data; |
|
146 |
||
147 |
||
148 |
/*
|
|
149 |
Open file for LOAD LOCAL INFILE
|
|
150 |
||
151 |
SYNOPSIS
|
|
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 !
|
|
155 |
||
156 |
||
157 |
NOTES
|
|
158 |
Even if this function returns an error, the load data interface
|
|
159 |
guarantees that default_local_infile_end() is called.
|
|
160 |
||
161 |
RETURN
|
|
162 |
0 ok
|
|
163 |
1 error
|
|
164 |
*/
|
|
165 |
||
166 |
static int default_local_infile_init(void **ptr, const char *filename, |
|
167 |
void *userdata __attribute__ ((unused))) |
|
168 |
{
|
|
169 |
default_local_infile_data *data; |
|
170 |
char tmp_name[FN_REFLEN]; |
|
171 |
||
172 |
if (!(*ptr= data= ((default_local_infile_data *) |
|
173 |
malloc(sizeof(default_local_infile_data))))) |
|
174 |
return 1; /* out of memory */ |
|
175 |
||
176 |
data->error_msg[0]= 0; |
|
177 |
data->error_num= 0; |
|
178 |
data->filename= filename; |
|
179 |
||
333
by Monty Taylor
More mysys removes. |
180 |
if ((data->fd = open(tmp_name, O_RDONLY)) < 0) |
236.3.8
by Andrey Hristov
Move local infile handling to a separate file |
181 |
{
|
333
by Monty Taylor
More mysys removes. |
182 |
data->error_num= errno; |
236.3.8
by Andrey Hristov
Move local infile handling to a separate file |
183 |
snprintf(data->error_msg, sizeof(data->error_msg)-1, |
331
by Monty Taylor
Removed more mysys errmsg links. |
184 |
_("File '%s' not found (Errcode: %d)"), tmp_name, data->error_num); |
236.3.8
by Andrey Hristov
Move local infile handling to a separate file |
185 |
return 1; |
186 |
}
|
|
187 |
return 0; /* ok */ |
|
188 |
}
|
|
189 |
||
190 |
||
191 |
/*
|
|
192 |
Read data for LOAD LOCAL INFILE
|
|
193 |
||
194 |
SYNOPSIS
|
|
195 |
default_local_infile_read()
|
|
196 |
ptr Points to handle allocated by _init
|
|
197 |
buf Read data here
|
|
198 |
buf_len Ammount of data to read
|
|
199 |
||
200 |
RETURN
|
|
201 |
> 0 number of bytes read
|
|
202 |
== 0 End of data
|
|
203 |
< 0 Error
|
|
204 |
*/
|
|
205 |
||
206 |
static int default_local_infile_read(void *ptr, char *buf, uint buf_len) |
|
207 |
{
|
|
208 |
int count; |
|
209 |
default_local_infile_data*data = (default_local_infile_data *) ptr; |
|
210 |
||
333
by Monty Taylor
More mysys removes. |
211 |
if ((count= (int) read(data->fd, (uchar *) buf, buf_len)) < 0) |
236.3.8
by Andrey Hristov
Move local infile handling to a separate file |
212 |
{
|
331
by Monty Taylor
Removed more mysys errmsg links. |
213 |
data->error_num= 2; /* the errmsg for not entire file read */ |
236.3.8
by Andrey Hristov
Move local infile handling to a separate file |
214 |
snprintf(data->error_msg, sizeof(data->error_msg)-1, |
331
by Monty Taylor
Removed more mysys errmsg links. |
215 |
_("Error reading file '%s' (Errcode: %d)"), |
333
by Monty Taylor
More mysys removes. |
216 |
data->filename, errno); |
236.3.8
by Andrey Hristov
Move local infile handling to a separate file |
217 |
}
|
218 |
return count; |
|
219 |
}
|
|
220 |
||
221 |
||
222 |
/*
|
|
223 |
Read data for LOAD LOCAL INFILE
|
|
224 |
||
225 |
SYNOPSIS
|
|
226 |
default_local_infile_end()
|
|
227 |
ptr Points to handle allocated by _init
|
|
228 |
May be NULL if _init failed!
|
|
229 |
||
230 |
RETURN
|
|
231 |
*/
|
|
232 |
||
233 |
static void default_local_infile_end(void *ptr) |
|
234 |
{
|
|
235 |
default_local_infile_data *data= (default_local_infile_data *) ptr; |
|
236 |
if (data) /* If not error on open */ |
|
237 |
{
|
|
238 |
if (data->fd >= 0) |
|
333
by Monty Taylor
More mysys removes. |
239 |
close(data->fd); |
236.3.8
by Andrey Hristov
Move local infile handling to a separate file |
240 |
free(ptr); |
241 |
}
|
|
242 |
}
|
|
243 |
||
244 |
||
245 |
/*
|
|
246 |
Return error from LOAD LOCAL INFILE
|
|
247 |
||
248 |
SYNOPSIS
|
|
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
|
|
254 |
||
255 |
RETURN
|
|
256 |
error message number
|
|
257 |
*/
|
|
258 |
||
259 |
static int |
|
260 |
default_local_infile_error(void *ptr, char *error_msg, uint error_msg_len) |
|
261 |
{
|
|
262 |
default_local_infile_data *data = (default_local_infile_data *) ptr; |
|
263 |
if (data) /* If not error on open */ |
|
264 |
{
|
|
240.1.5
by Toru Maesaka
Merge with head and conflicts resolved |
265 |
strncpy(error_msg, data->error_msg, error_msg_len); |
236.3.8
by Andrey Hristov
Move local infile handling to a separate file |
266 |
return data->error_num; |
267 |
}
|
|
268 |
/* This can only happen if we got error on malloc of handle */
|
|
240.1.5
by Toru Maesaka
Merge with head and conflicts resolved |
269 |
strcpy(error_msg, ER(CR_OUT_OF_MEMORY)); |
236.3.8
by Andrey Hristov
Move local infile handling to a separate file |
270 |
return CR_OUT_OF_MEMORY; |
271 |
}
|
|
272 |
||
273 |
||
274 |
void
|
|
275 |
drizzle_set_local_infile_handler(DRIZZLE *drizzle, |
|
276 |
int (*local_infile_init)(void **, const char *, |
|
277 |
void *), |
|
278 |
int (*local_infile_read)(void *, char *, uint), |
|
279 |
void (*local_infile_end)(void *), |
|
280 |
int (*local_infile_error)(void *, char *, uint), |
|
281 |
void *userdata) |
|
282 |
{
|
|
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; |
|
288 |
}
|
|
289 |
||
290 |
||
291 |
void drizzle_set_local_infile_default(DRIZZLE *drizzle) |
|
292 |
{
|
|
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; |
|
297 |
}
|