00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021 #include <drizzled/function/str/strfunc.h>
00022 #include <drizzled/function/str/load_file.h>
00023 #include <drizzled/error.h>
00024 #include <drizzled/data_home.h>
00025 #include <drizzled/session.h>
00026 #include <drizzled/internal/my_sys.h>
00027
00028 #include <boost/filesystem.hpp>
00029
00030 #include <fcntl.h>
00031 #include <sys/stat.h>
00032 #include <iostream>
00033
00034 namespace fs=boost::filesystem;
00035 using namespace std;
00036
00037 namespace drizzled
00038 {
00039
00040 String *Item_load_file::val_str(String *str)
00041 {
00042 assert(fixed == 1);
00043 String *file_name;
00044 int file;
00045 struct stat stat_info;
00046
00047 if (!(file_name= args[0]->val_str(str)))
00048 {
00049 null_value = 1;
00050 return(0);
00051 }
00052
00053 fs::path target_path(fs::system_complete(getDataHomeCatalog()));
00054 fs::path to_file(file_name->c_ptr());
00055 if (not to_file.has_root_directory())
00056 {
00057 target_path /= to_file;
00058 }
00059 else
00060 {
00061 target_path= to_file;
00062 }
00063
00064
00065 if (not secure_file_priv.string().empty())
00066 {
00067 fs::path secure_file_path(fs::system_complete(secure_file_priv));
00068 if (target_path.file_string().substr(0, secure_file_path.file_string().size()) != secure_file_path.file_string())
00069 {
00070
00071 my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");
00072 null_value = 1;
00073 return 0;
00074 }
00075 }
00076
00077 if (stat(target_path.file_string().c_str(), &stat_info))
00078 {
00079 my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr());
00080 goto err;
00081 }
00082
00083 if (!(stat_info.st_mode & S_IROTH))
00084 {
00085 my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr());
00086 goto err;
00087 }
00088
00089 if (stat_info.st_size > (long) session.variables.max_allowed_packet)
00090 {
00091 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
00092 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
00093 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
00094 func_name(), session.variables.max_allowed_packet);
00095 goto err;
00096 }
00097
00098 if (stat_info.st_size == 0)
00099 {
00100 goto err;
00101 }
00102
00103 if (tmp_value.alloc((size_t)stat_info.st_size))
00104 goto err;
00105 if ((file = internal::my_open(target_path.file_string().c_str(), O_RDONLY, MYF(0))) < 0)
00106 goto err;
00107 if (internal::my_read(file, (unsigned char*) tmp_value.ptr(), (size_t)stat_info.st_size, MYF(MY_NABP)))
00108 {
00109 internal::my_close(file, MYF(0));
00110 goto err;
00111 }
00112 if (strlen(tmp_value.ptr()) == 0)
00113 {
00114 goto err;
00115 }
00116 tmp_value.length((size_t)stat_info.st_size);
00117 internal::my_close(file, MYF(0));
00118 null_value = 0;
00119 return(&tmp_value);
00120
00121 err:
00122 null_value = 1;
00123 return(0);
00124 }
00125
00126
00127 }