~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/str/load_file.cc

  • Committer: Monty Taylor
  • Date: 2009-10-06 19:37:52 UTC
  • mto: This revision was merged to the branch mainline in revision 1184.
  • Revision ID: mordred@inaugust.com-20091006193752-4dx2c8u35j4em79g
Removed more server_includes.h from headers.

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
 
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 <drizzled/server_includes.h>
 
21
#include CSTDINT_H
 
22
#include <drizzled/function/str/strfunc.h>
 
23
#include <drizzled/function/str/load_file.h>
 
24
#include <drizzled/error.h>
 
25
#include <drizzled/data_home.h>
 
26
#include <drizzled/session.h>
 
27
 
 
28
String *Item_load_file::val_str(String *str)
 
29
{
 
30
  assert(fixed == 1);
 
31
  String *file_name;
 
32
  File file;
 
33
  struct stat stat_info;
 
34
  char path[FN_REFLEN];
 
35
 
 
36
  if (!(file_name= args[0]->val_str(str)))
 
37
    goto err;
 
38
 
 
39
  (void) fn_format(path, file_name->c_ptr(), drizzle_real_data_home, "",
 
40
                   MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
 
41
 
 
42
  /* Read only allowed from within dir specified by secure_file_priv */
 
43
  if (opt_secure_file_priv &&
 
44
      strncmp(opt_secure_file_priv, path, strlen(opt_secure_file_priv)))
 
45
    goto err;
 
46
 
 
47
  if (stat(path, &stat_info))
 
48
    goto err;
 
49
 
 
50
  if (!(stat_info.st_mode & S_IROTH))
 
51
  {
 
52
    /* my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr()); */
 
53
    goto err;
 
54
  }
 
55
  if (stat_info.st_size > (long) current_session->variables.max_allowed_packet)
 
56
  {
 
57
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
58
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
 
59
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
 
60
                        func_name(), current_session->variables.max_allowed_packet);
 
61
    goto err;
 
62
  }
 
63
  if (tmp_value.alloc((size_t)stat_info.st_size))
 
64
    goto err;
 
65
  if ((file = my_open(file_name->c_ptr(), O_RDONLY, MYF(0))) < 0)
 
66
    goto err;
 
67
  if (my_read(file, (unsigned char*) tmp_value.ptr(), (size_t)stat_info.st_size, MYF(MY_NABP)))
 
68
  {
 
69
    my_close(file, MYF(0));
 
70
    goto err;
 
71
  }
 
72
  tmp_value.length((size_t)stat_info.st_size);
 
73
  my_close(file, MYF(0));
 
74
  null_value = 0;
 
75
  return(&tmp_value);
 
76
 
 
77
err:
 
78
  null_value = 1;
 
79
  return(0);
 
80
}
 
81
 
 
82