~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/local_infile.c

Merged from jay.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
 
 *
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.
9
 
 *
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.
14
 
 *
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
18
 
 */
19
 
 
20
 
#include <config.h>
21
 
 
22
 
#include "libdrizzle.h"
23
 
#include "libdrizzle_priv.h"
 
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 <mysys/my_sys.h>
 
23
#include "my_time.h"
 
24
#include <mysys/mysys_err.h>
 
25
#include <mystrings/m_string.h>
 
26
#include <mystrings/m_ctype.h>
 
27
#include "drizzle.h"
24
28
#include "errmsg.h"
 
29
#include <vio/violite.h>
25
30
#include <sys/stat.h>
26
31
#include <signal.h>
27
32
#include <time.h>
28
 
#include <fcntl.h>
29
 
#include <stdio.h>
30
 
 
31
33
#ifdef   HAVE_PWD_H
32
34
#include <pwd.h>
33
35
#endif
53
55
#define INADDR_NONE  -1
54
56
#endif
55
57
 
56
 
#include "local_infile.h"
57
 
#include <drizzled/gettext.h>
 
58
#include <sql_common.h>
 
59
#include "client_settings.h"
58
60
 
59
 
#define MY_ALIGN(A,L)   (((A) + (L) - 1) & ~((L) - 1))
60
61
 
61
62
bool handle_local_infile(DRIZZLE *drizzle, const char *net_filename)
62
63
{
63
64
  bool result= true;
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;
66
67
  int readcount;
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)))
83
84
  {
84
 
    drizzle_set_error(drizzle, CR_OUT_OF_MEMORY, sqlstate_get_unknown());
 
85
    set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
85
86
    return(1);
86
87
  }
87
88
 
89
90
  if ((*options->local_infile_init)(&li_ptr, net_filename,
90
91
    options->local_infile_userdata))
91
92
  {
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 */
93
94
    net_flush(net);
94
 
    strcpy(net->sqlstate, sqlstate_get_unknown());
 
95
    strcpy(net->sqlstate, unknown_sqlstate);
95
96
    net->last_errno=
96
97
      (*options->local_infile_error)(li_ptr,
97
98
                                     net->last_error,
104
105
    (*options->local_infile_read)(li_ptr, buf,
105
106
          packet_length)) > 0)
106
107
  {
107
 
    if (my_net_write(net, (unsigned char*) buf, readcount))
 
108
    if (my_net_write(net, (uchar*) buf, readcount))
108
109
    {
109
110
      goto err;
110
111
    }
111
112
  }
112
113
 
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))
115
116
  {
116
 
    drizzle_set_error(drizzle, CR_SERVER_LOST, sqlstate_get_unknown());
 
117
    set_drizzle_error(drizzle, CR_SERVER_LOST, unknown_sqlstate);
117
118
    goto err;
118
119
  }
119
120
 
182
183
  data->error_num=    0;
183
184
  data->filename= filename;
184
185
 
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)
186
188
  {
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);
190
192
    return 1;
191
193
  }
192
194
  return 0; /* ok */
208
210
    < 0    Error
209
211
*/
210
212
 
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)
212
214
{
213
215
  int count;
214
216
  default_local_infile_data*data = (default_local_infile_data *) ptr;
215
217
 
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)
217
219
  {
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);
 
222
             EE(EE_READ),
 
223
             data->filename, my_errno);
222
224
  }
223
225
  return count;
224
226
}
241
243
  if (data)          /* If not error on open */
242
244
  {
243
245
    if (data->fd >= 0)
244
 
      close(data->fd);
 
246
      my_close(data->fd, MYF(MY_WME));
245
247
    free(ptr);
246
248
  }
247
249
}
262
264
*/
263
265
 
264
266
static int
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)
266
268
{
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 *,
282
284
                               void *),
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),
286
288
                               void *userdata)
287
289
{
288
290
  drizzle->options.local_infile_init=  local_infile_init;