~drizzle-trunk/drizzle/development

629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
21
#include <drizzled/function/str/strfunc.h>
22
#include <drizzled/function/str/load_file.h>
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
23
#include <drizzled/error.h>
24
#include <drizzled/data_home.h>
25
#include <drizzled/session.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
26
#include <drizzled/internal/my_sys.h>
2239.1.10 by Olaf van der Spek
Refactor includes
27
#include <drizzled/sys_var.h>
2241.3.2 by Olaf van der Spek
Refactor Session::variables
28
#include <drizzled/system_variables.h>
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
29
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
30
#include <boost/filesystem.hpp>
31
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
32
#include <fcntl.h>
33
#include <sys/stat.h>
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
34
#include <iostream>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
35
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
36
namespace fs=boost::filesystem;
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
37
using namespace std;
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
38
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
39
namespace drizzled
40
{
41
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
42
String *Item_load_file::val_str(String *str)
43
{
44
  assert(fixed == 1);
45
  String *file_name;
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
46
  int file;
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
47
  struct stat stat_info;
48
49
  if (!(file_name= args[0]->val_str(str)))
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
50
  {
51
    null_value = 1;
2318.6.58 by Olaf van der Spek
Refactor
52
    return 0;
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
53
  }
54
55
  fs::path target_path(fs::system_complete(getDataHomeCatalog()));
56
  fs::path to_file(file_name->c_ptr());
57
  if (not to_file.has_root_directory())
58
  {
59
    target_path /= to_file;
60
  }
61
  else
62
  {
63
    target_path= to_file;
64
  }
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
65
66
  /* Read only allowed from within dir specified by secure_file_priv */
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
67
  if (not secure_file_priv.string().empty())
68
  {
1813.2.13 by Monty Taylor
Made load_file() work sensibly with fs::path. Now it throws errors. Good grief.
69
    fs::path secure_file_path(fs::system_complete(secure_file_priv));
70
    if (target_path.file_string().substr(0, secure_file_path.file_string().size()) != secure_file_path.file_string())
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
71
    {
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
72
      /* Read only allowed from within dir specified by secure_file_priv */
73
      my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv"); 
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
74
      null_value = 1;
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
75
      return 0;
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
76
    }
77
  }
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
78
1813.2.13 by Monty Taylor
Made load_file() work sensibly with fs::path. Now it throws errors. Good grief.
79
  if (stat(target_path.file_string().c_str(), &stat_info))
1813.2.12 by Monty Taylor
Made load_file work properly with throwing errors and opening files.
80
  {
1813.2.13 by Monty Taylor
Made load_file() work sensibly with fs::path. Now it throws errors. Good grief.
81
    my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr());
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
82
    goto err;
1813.2.12 by Monty Taylor
Made load_file work properly with throwing errors and opening files.
83
  }
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
84
85
  if (!(stat_info.st_mode & S_IROTH))
86
  {
1813.2.13 by Monty Taylor
Made load_file() work sensibly with fs::path. Now it throws errors. Good grief.
87
    my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr());
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
88
    goto err;
89
  }
1813.2.12 by Monty Taylor
Made load_file work properly with throwing errors and opening files.
90
1633.4.6 by Brian Aker
More current_session issues.
91
  if (stat_info.st_size > (long) session.variables.max_allowed_packet)
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
92
  {
1633.4.6 by Brian Aker
More current_session issues.
93
    push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
94
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
95
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1633.4.6 by Brian Aker
More current_session issues.
96
                        func_name(), session.variables.max_allowed_packet);
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
97
    goto err;
98
  }
1813.2.12 by Monty Taylor
Made load_file work properly with throwing errors and opening files.
99
100
  if (stat_info.st_size == 0)
101
  {
102
    goto err;
103
  }
104
2275.2.12 by Olaf van der Spek
Return void
105
  tmp_value.alloc((size_t)stat_info.st_size);
1813.2.13 by Monty Taylor
Made load_file() work sensibly with fs::path. Now it throws errors. Good grief.
106
  if ((file = internal::my_open(target_path.file_string().c_str(), O_RDONLY, MYF(0))) < 0)
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
107
    goto err;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
108
  if (internal::my_read(file, (unsigned char*) tmp_value.ptr(), (size_t)stat_info.st_size, MYF(MY_NABP)))
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
109
  {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
110
    internal::my_close(file, MYF(0));
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
111
    goto err;
112
  }
1813.2.12 by Monty Taylor
Made load_file work properly with throwing errors and opening files.
113
  if (strlen(tmp_value.ptr()) == 0)
114
  {
115
    goto err;
116
  }
892.2.2 by Monty Taylor
More solaris warnings.
117
  tmp_value.length((size_t)stat_info.st_size);
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
118
  internal::my_close(file, MYF(0));
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
119
  null_value = 0;
120
  return(&tmp_value);
121
122
err:
123
  null_value = 1;
2318.6.58 by Olaf van der Spek
Refactor
124
  return 0;
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
125
}
126
127
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
128
} /* namespace drizzled */