Drizzled Public API Documentation

load_file.cc
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  * Copyright (C) 2011 Stewart Smith
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 #include <drizzled/function/str/strfunc.h>
23 #include <drizzled/function/str/load_file.h>
24 #include <drizzled/error.h>
25 #include <drizzled/catalog/local.h>
26 #include <drizzled/session.h>
27 #include <drizzled/internal/my_sys.h>
28 #include <drizzled/sys_var.h>
29 #include <drizzled/system_variables.h>
30 
31 #include <boost/filesystem.hpp>
32 
33 #include <fcntl.h>
34 #include <sys/stat.h>
35 #include <iostream>
36 
37 namespace fs=boost::filesystem;
38 using namespace std;
39 
40 namespace drizzled
41 {
42 
43 String *Item_load_file::val_str(String *str)
44 {
45  assert(fixed == 1);
46  String *file_name;
47  int file;
48  struct stat stat_info;
49 
50  if (!(file_name= args[0]->val_str(str)))
51  {
52  null_value = 1;
53  return 0;
54  }
55 
56  fs::path target_path(fs::system_complete(catalog::local_identifier().getPath()));
57  fs::path to_file(file_name->c_ptr());
58  if (not to_file.has_root_directory())
59  {
60  target_path /= to_file;
61  }
62  else
63  {
64  target_path= to_file;
65  }
66 
67  /* Read only allowed from within dir specified by secure_file_priv */
68  if (not secure_file_priv.string().empty())
69  {
70  fs::path secure_file_path(fs::system_complete(secure_file_priv));
71  if (target_path.file_string().substr(0, secure_file_path.file_string().size()) != secure_file_path.file_string())
72  {
73  /* Read only allowed from within dir specified by secure_file_priv */
74  my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");
75  null_value = 1;
76  return 0;
77  }
78  }
79 
80  if (stat(target_path.file_string().c_str(), &stat_info))
81  {
82  my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr());
83  goto err;
84  }
85 
86  if (!(stat_info.st_mode & S_IROTH))
87  {
88  my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr());
89  goto err;
90  }
91 
92  if (stat_info.st_size > (long) session.variables.max_allowed_packet)
93  {
94  push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
95  ER_WARN_ALLOWED_PACKET_OVERFLOWED,
96  ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
97  func_name(), session.variables.max_allowed_packet);
98  goto err;
99  }
100 
101  if (stat_info.st_size == 0)
102  {
103  goto err;
104  }
105 
106  tmp_value.alloc((size_t)stat_info.st_size);
107  if ((file = internal::my_open(target_path.file_string().c_str(), O_RDONLY, MYF(0))) < 0)
108  goto err;
109  if (internal::my_read(file, (unsigned char*) tmp_value.ptr(), (size_t)stat_info.st_size, MYF(MY_NABP)))
110  {
111  internal::my_close(file, MYF(0));
112  goto err;
113  }
114  if (strlen(tmp_value.ptr()) == 0)
115  {
116  goto err;
117  }
118  tmp_value.length((size_t)stat_info.st_size);
119  internal::my_close(file, MYF(0));
120  null_value = 0;
121  return(&tmp_value);
122 
123 err:
124  null_value = 1;
125  return 0;
126 }
127 
128 
129 } /* namespace drizzled */