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
22
#include "libdrizzle.h"
23
#include "libdrizzle_priv.h"
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 <mysys/my_sys.h>
24
#include <mysys/mysys_err.h>
25
#include <mystrings/m_string.h>
26
#include <mystrings/m_ctype.h>
24
28
#include "errmsg.h"
29
#include <vio/violite.h>
25
30
#include <sys/stat.h>
26
31
#include <signal.h>
53
55
#define INADDR_NONE -1
56
#include "local_infile.h"
57
#include <drizzled/gettext.h>
58
#include <sql_common.h>
59
#include "client_settings.h"
59
#define MY_ALIGN(A,L) (((A) + (L) - 1) & ~((L) - 1))
61
62
bool handle_local_infile(DRIZZLE *drizzle, const char *net_filename)
64
uint32_t packet_length=MY_ALIGN(drizzle->net.max_packet-16,IO_SIZE);
65
uint packet_length=MY_ALIGN(drizzle->net.max_packet-16,IO_SIZE);
65
66
NET *net= &drizzle->net;
67
68
void *li_ptr; /* pass state to local_infile functions */
81
82
/* copy filename into local memory and allocate read buffer */
82
83
if (!(buf=malloc(packet_length)))
84
drizzle_set_error(drizzle, CR_OUT_OF_MEMORY, sqlstate_get_unknown());
85
set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
89
90
if ((*options->local_infile_init)(&li_ptr, net_filename,
90
91
options->local_infile_userdata))
92
(void)my_net_write(net,(const unsigned char*) "",0); /* Server needs one packet */
93
VOID(my_net_write(net,(const uchar*) "",0)); /* Server needs one packet */
94
strcpy(net->sqlstate, sqlstate_get_unknown());
95
strcpy(net->sqlstate, unknown_sqlstate);
96
97
(*options->local_infile_error)(li_ptr,
104
105
(*options->local_infile_read)(li_ptr, buf,
105
106
packet_length)) > 0)
107
if (my_net_write(net, (unsigned char*) buf, readcount))
108
if (my_net_write(net, (uchar*) buf, readcount))
113
114
/* Send empty packet to mark end of file */
114
if (my_net_write(net, (const unsigned char*) "", 0) || net_flush(net))
115
if (my_net_write(net, (const uchar*) "", 0) || net_flush(net))
116
drizzle_set_error(drizzle, CR_SERVER_LOST, sqlstate_get_unknown());
117
set_drizzle_error(drizzle, CR_SERVER_LOST, unknown_sqlstate);
182
183
data->error_num= 0;
183
184
data->filename= filename;
185
if ((data->fd = open(tmp_name, O_RDONLY)) < 0)
186
fn_format(tmp_name, filename, "", "", MY_UNPACK_FILENAME);
187
if ((data->fd = my_open(tmp_name, O_RDONLY, MYF(0))) < 0)
187
data->error_num= errno;
189
data->error_num= my_errno;
188
190
snprintf(data->error_msg, sizeof(data->error_msg)-1,
189
_("File '%s' not found (Errcode: %d)"), tmp_name, data->error_num);
191
EE(EE_FILENOTFOUND), tmp_name, data->error_num);
192
194
return 0; /* ok */
211
static int default_local_infile_read(void *ptr, char *buf, uint32_t buf_len)
213
static int default_local_infile_read(void *ptr, char *buf, uint buf_len)
214
216
default_local_infile_data*data = (default_local_infile_data *) ptr;
216
if ((count= (int) read(data->fd, (unsigned char *) buf, buf_len)) < 0)
218
if ((count= (int) my_read(data->fd, (uchar *) buf, buf_len, MYF(0))) < 0)
218
data->error_num= 2; /* the errmsg for not entire file read */
220
data->error_num= EE_READ; /* the errmsg for not entire file read */
219
221
snprintf(data->error_msg, sizeof(data->error_msg)-1,
220
_("Error reading file '%s' (Errcode: %d)"),
221
data->filename, errno);
223
data->filename, my_errno);
265
default_local_infile_error(void *ptr, char *error_msg, uint32_t error_msg_len)
267
default_local_infile_error(void *ptr, char *error_msg, uint error_msg_len)
267
269
default_local_infile_data *data = (default_local_infile_data *) ptr;
268
270
if (data) /* If not error on open */
280
282
drizzle_set_local_infile_handler(DRIZZLE *drizzle,
281
283
int (*local_infile_init)(void **, const char *,
283
int (*local_infile_read)(void *, char *, uint32_t),
285
int (*local_infile_read)(void *, char *, uint),
284
286
void (*local_infile_end)(void *),
285
int (*local_infile_error)(void *, char *, uint32_t),
287
int (*local_infile_error)(void *, char *, uint),
288
290
drizzle->options.local_infile_init= local_infile_init;