Drizzled Public API Documentation

load_file.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *  Copyright (C) 2011 Stewart Smith
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; version 2 of the License.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 #include <drizzled/function/str/strfunc.h>
00023 #include <drizzled/function/str/load_file.h>
00024 #include <drizzled/error.h>
00025 #include <drizzled/catalog/local.h>
00026 #include <drizzled/session.h>
00027 #include <drizzled/internal/my_sys.h>
00028 #include <drizzled/sys_var.h>
00029 #include <drizzled/system_variables.h>
00030 
00031 #include <boost/filesystem.hpp>
00032 
00033 #include <fcntl.h>
00034 #include <sys/stat.h>
00035 #include <iostream>
00036 
00037 namespace fs=boost::filesystem;
00038 using namespace std;
00039 
00040 namespace drizzled
00041 {
00042 
00043 String *Item_load_file::val_str(String *str)
00044 {
00045   assert(fixed == 1);
00046   String *file_name;
00047   int file;
00048   struct stat stat_info;
00049 
00050   if (!(file_name= args[0]->val_str(str)))
00051   {
00052     null_value = 1;
00053     return 0;
00054   }
00055 
00056   fs::path target_path(fs::system_complete(catalog::local_identifier().getPath()));
00057   fs::path to_file(file_name->c_ptr());
00058   if (not to_file.has_root_directory())
00059   {
00060     target_path /= to_file;
00061   }
00062   else
00063   {
00064     target_path= to_file;
00065   }
00066 
00067   /* Read only allowed from within dir specified by secure_file_priv */
00068   if (not secure_file_priv.string().empty())
00069   {
00070     fs::path secure_file_path(fs::system_complete(secure_file_priv));
00071     if (target_path.file_string().substr(0, secure_file_path.file_string().size()) != secure_file_path.file_string())
00072     {
00073       /* Read only allowed from within dir specified by secure_file_priv */
00074       my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv"); 
00075       null_value = 1;
00076       return 0;
00077     }
00078   }
00079 
00080   if (stat(target_path.file_string().c_str(), &stat_info))
00081   {
00082     my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr());
00083     goto err;
00084   }
00085 
00086   if (!(stat_info.st_mode & S_IROTH))
00087   {
00088     my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr());
00089     goto err;
00090   }
00091 
00092   if (stat_info.st_size > (long) session.variables.max_allowed_packet)
00093   {
00094     push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
00095                         ER_WARN_ALLOWED_PACKET_OVERFLOWED,
00096                         ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
00097                         func_name(), session.variables.max_allowed_packet);
00098     goto err;
00099   }
00100 
00101   if (stat_info.st_size == 0)
00102   {
00103     goto err;
00104   }
00105 
00106   tmp_value.alloc((size_t)stat_info.st_size);
00107   if ((file = internal::my_open(target_path.file_string().c_str(), O_RDONLY, MYF(0))) < 0)
00108     goto err;
00109   if (internal::my_read(file, (unsigned char*) tmp_value.ptr(), (size_t)stat_info.st_size, MYF(MY_NABP)))
00110   {
00111     internal::my_close(file, MYF(0));
00112     goto err;
00113   }
00114   if (strlen(tmp_value.ptr()) == 0)
00115   {
00116     goto err;
00117   }
00118   tmp_value.length((size_t)stat_info.st_size);
00119   internal::my_close(file, MYF(0));
00120   null_value = 0;
00121   return(&tmp_value);
00122 
00123 err:
00124   null_value = 1;
00125   return 0;
00126 }
00127 
00128 
00129 } /* namespace drizzled */