~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/local_infile.c

  • Committer: Monty Taylor
  • Date: 2008-09-16 00:00:48 UTC
  • mto: This revision was merged to the branch mainline in revision 391.
  • Revision ID: monty@inaugust.com-20080916000048-3rvrv3gv9l0ad3gs
Fixed copyright headers in drizzled/

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "libdrizzle.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 "local_infile.h"
 
54
 
 
55
 
 
56
 
 
57
bool handle_local_infile(DRIZZLE *drizzle, const char *net_filename)
 
58
{
 
59
  bool result= true;
 
60
  uint packet_length=MY_ALIGN(drizzle->net.max_packet-16,IO_SIZE);
 
61
  NET *net= &drizzle->net;
 
62
  int readcount;
 
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;
 
66
 
 
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))
 
72
  {
 
73
    /* if any of the functions is invalid, set the default */
 
74
    drizzle_set_local_infile_default(drizzle);
 
75
  }
 
76
 
 
77
  /* copy filename into local memory and allocate read buffer */
 
78
  if (!(buf=malloc(packet_length)))
 
79
  {
 
80
    set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
 
81
    return(1);
 
82
  }
 
83
 
 
84
  /* initialize local infile (open file, usually) */
 
85
  if ((*options->local_infile_init)(&li_ptr, net_filename,
 
86
    options->local_infile_userdata))
 
87
  {
 
88
    VOID(my_net_write(net,(const uchar*) "",0)); /* Server needs one packet */
 
89
    net_flush(net);
 
90
    strcpy(net->sqlstate, unknown_sqlstate);
 
91
    net->last_errno=
 
92
      (*options->local_infile_error)(li_ptr,
 
93
                                     net->last_error,
 
94
                                     sizeof(net->last_error)-1);
 
95
    goto err;
 
96
  }
 
97
 
 
98
  /* read blocks of data from local infile callback */
 
99
  while ((readcount =
 
100
    (*options->local_infile_read)(li_ptr, buf,
 
101
          packet_length)) > 0)
 
102
  {
 
103
    if (my_net_write(net, (uchar*) buf, readcount))
 
104
    {
 
105
      goto err;
 
106
    }
 
107
  }
 
108
 
 
109
  /* Send empty packet to mark end of file */
 
110
  if (my_net_write(net, (const uchar*) "", 0) || net_flush(net))
 
111
  {
 
112
    set_drizzle_error(drizzle, CR_SERVER_LOST, unknown_sqlstate);
 
113
    goto err;
 
114
  }
 
115
 
 
116
  if (readcount < 0)
 
117
  {
 
118
    net->last_errno=
 
119
      (*options->local_infile_error)(li_ptr,
 
120
                                     net->last_error,
 
121
                                     sizeof(net->last_error)-1);
 
122
    goto err;
 
123
  }
 
124
 
 
125
  result=false;                                 /* Ok */
 
126
 
 
127
err:
 
128
  /* free up memory allocated with _init, usually */
 
129
  (*options->local_infile_end)(li_ptr);
 
130
  if(buf)
 
131
    free(buf);
 
132
  return(result);
 
133
}
 
134
 
 
135
 
 
136
/****************************************************************************
 
137
  Default handlers for LOAD LOCAL INFILE
 
138
****************************************************************************/
 
139
 
 
140
typedef struct st_default_local_infile
 
141
{
 
142
  int fd;
 
143
  int error_num;
 
144
  const char *filename;
 
145
  char error_msg[LOCAL_INFILE_ERROR_LEN];
 
146
} default_local_infile_data;
 
147
 
 
148
 
 
149
/*
 
150
  Open file for LOAD LOCAL INFILE
 
151
 
 
152
  SYNOPSIS
 
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 !
 
156
 
 
157
 
 
158
  NOTES
 
159
    Even if this function returns an error, the load data interface
 
160
    guarantees that default_local_infile_end() is called.
 
161
 
 
162
  RETURN
 
163
    0  ok
 
164
    1  error
 
165
*/
 
166
 
 
167
static int default_local_infile_init(void **ptr, const char *filename,
 
168
             void *userdata __attribute__ ((unused)))
 
169
{
 
170
  default_local_infile_data *data;
 
171
  char tmp_name[FN_REFLEN];
 
172
 
 
173
  if (!(*ptr= data= ((default_local_infile_data *)
 
174
                     malloc(sizeof(default_local_infile_data)))))
 
175
    return 1; /* out of memory */
 
176
 
 
177
  data->error_msg[0]= 0;
 
178
  data->error_num=    0;
 
179
  data->filename= filename;
 
180
 
 
181
  if ((data->fd = open(tmp_name, O_RDONLY)) < 0)
 
182
  {
 
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);
 
186
    return 1;
 
187
  }
 
188
  return 0; /* ok */
 
189
}
 
190
 
 
191
 
 
192
/*
 
193
  Read data for LOAD LOCAL INFILE
 
194
 
 
195
  SYNOPSIS
 
196
    default_local_infile_read()
 
197
    ptr      Points to handle allocated by _init
 
198
    buf      Read data here
 
199
    buf_len    Ammount of data to read
 
200
 
 
201
  RETURN
 
202
    > 0    number of bytes read
 
203
    == 0  End of data
 
204
    < 0    Error
 
205
*/
 
206
 
 
207
static int default_local_infile_read(void *ptr, char *buf, uint buf_len)
 
208
{
 
209
  int count;
 
210
  default_local_infile_data*data = (default_local_infile_data *) ptr;
 
211
 
 
212
  if ((count= (int) read(data->fd, (uchar *) buf, buf_len)) < 0)
 
213
  {
 
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);
 
218
  }
 
219
  return count;
 
220
}
 
221
 
 
222
 
 
223
/*
 
224
  Read data for LOAD LOCAL INFILE
 
225
 
 
226
  SYNOPSIS
 
227
    default_local_infile_end()
 
228
    ptr      Points to handle allocated by _init
 
229
      May be NULL if _init failed!
 
230
 
 
231
  RETURN
 
232
*/
 
233
 
 
234
static void default_local_infile_end(void *ptr)
 
235
{
 
236
  default_local_infile_data *data= (default_local_infile_data *) ptr;
 
237
  if (data)          /* If not error on open */
 
238
  {
 
239
    if (data->fd >= 0)
 
240
      close(data->fd);
 
241
    free(ptr);
 
242
  }
 
243
}
 
244
 
 
245
 
 
246
/*
 
247
  Return error from LOAD LOCAL INFILE
 
248
 
 
249
  SYNOPSIS
 
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
 
255
 
 
256
  RETURN
 
257
    error message number
 
258
*/
 
259
 
 
260
static int
 
261
default_local_infile_error(void *ptr, char *error_msg, uint error_msg_len)
 
262
{
 
263
  default_local_infile_data *data = (default_local_infile_data *) ptr;
 
264
  if (data)          /* If not error on open */
 
265
  {
 
266
    strncpy(error_msg, data->error_msg, error_msg_len);
 
267
    return data->error_num;
 
268
  }
 
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;
 
272
}
 
273
 
 
274
 
 
275
void
 
276
drizzle_set_local_infile_handler(DRIZZLE *drizzle,
 
277
                               int (*local_infile_init)(void **, const char *,
 
278
                               void *),
 
279
                               int (*local_infile_read)(void *, char *, uint),
 
280
                               void (*local_infile_end)(void *),
 
281
                               int (*local_infile_error)(void *, char *, uint),
 
282
                               void *userdata)
 
283
{
 
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;
 
289
}
 
290
 
 
291
 
 
292
void drizzle_set_local_infile_default(DRIZZLE *drizzle)
 
293
{
 
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;
 
298
}