drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
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
37namespace fs=boost::filesystem;
38using namespace std;
39
40namespace drizzled
41{
42
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.string().substr(0, secure_file_path.string().size()) != secure_file_path.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.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.string().c_str(), O_RDONLY, MYF(0))) < 0)
108 {
109 goto err;
110 }
111
112 if (internal::my_read(file, (unsigned char*) tmp_value.ptr(), (size_t)stat_info.st_size, MYF(MY_NABP)))
113 {
114 internal::my_close(file, MYF(0));
115 goto err;
116 }
117 if (strlen(tmp_value.ptr()) == 0)
118 {
119 goto err;
120 }
121 tmp_value.length((size_t)stat_info.st_size);
122 internal::my_close(file, MYF(0));
123 null_value = 0;
124 return(&tmp_value);
125
126err:
127 null_value = 1;
128 return 0;
129}
130
131
132} /* namespace drizzled */
String * val_str(String *)
Definition load_file.cc:43
bool fixed
Definition item.h:122
bool null_value
Definition item.h:124
TODO: Rename this file - func.h is stupid.
STL namespace.