~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.
2478.3.1 by Stewart Smith
Partially move towards using LOCAL catalog in all path generation by removing getDataHomeCatalog() and instead using catalog::local_identifier() around the place. We do a bit of prep work in the schema engine too. Next step is to remove chdir to 'local' and generate local as part of path to all objects.
5
 *  Copyright (C) 2011 Stewart Smith
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
22
#include <drizzled/function/str/strfunc.h>
23
#include <drizzled/function/str/load_file.h>
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
24
#include <drizzled/error.h>
2478.3.1 by Stewart Smith
Partially move towards using LOCAL catalog in all path generation by removing getDataHomeCatalog() and instead using catalog::local_identifier() around the place. We do a bit of prep work in the schema engine too. Next step is to remove chdir to 'local' and generate local as part of path to all objects.
25
#include <drizzled/catalog/local.h>
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
26
#include <drizzled/session.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
27
#include <drizzled/internal/my_sys.h>
2239.1.10 by Olaf van der Spek
Refactor includes
28
#include <drizzled/sys_var.h>
2241.3.2 by Olaf van der Spek
Refactor Session::variables
29
#include <drizzled/system_variables.h>
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
30
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
31
#include <boost/filesystem.hpp>
32
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
33
#include <fcntl.h>
34
#include <sys/stat.h>
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
35
#include <iostream>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
36
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
37
namespace fs=boost::filesystem;
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
38
using namespace std;
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
39
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
40
namespace drizzled
41
{
42
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
43
String *Item_load_file::val_str(String *str)
44
{
45
  assert(fixed == 1);
46
  String *file_name;
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
47
  int file;
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
48
  struct stat stat_info;
49
50
  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.
51
  {
52
    null_value = 1;
2318.6.58 by Olaf van der Spek
Refactor
53
    return 0;
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
54
  }
55
2478.3.1 by Stewart Smith
Partially move towards using LOCAL catalog in all path generation by removing getDataHomeCatalog() and instead using catalog::local_identifier() around the place. We do a bit of prep work in the schema engine too. Next step is to remove chdir to 'local' and generate local as part of path to all objects.
56
  fs::path target_path(fs::system_complete(catalog::local_identifier().getPath()));
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
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
  }
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
66
67
  /* 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.
68
  if (not secure_file_priv.string().empty())
69
  {
1813.2.13 by Monty Taylor
Made load_file() work sensibly with fs::path. Now it throws errors. Good grief.
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())
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
72
    {
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
73
      /* Read only allowed from within dir specified by secure_file_priv */
74
      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.
75
      null_value = 1;
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
76
      return 0;
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
77
    }
78
  }
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
79
1813.2.13 by Monty Taylor
Made load_file() work sensibly with fs::path. Now it throws errors. Good grief.
80
  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.
81
  {
1813.2.13 by Monty Taylor
Made load_file() work sensibly with fs::path. Now it throws errors. Good grief.
82
    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
83
    goto err;
1813.2.12 by Monty Taylor
Made load_file work properly with throwing errors and opening files.
84
  }
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
85
86
  if (!(stat_info.st_mode & S_IROTH))
87
  {
1813.2.13 by Monty Taylor
Made load_file() work sensibly with fs::path. Now it throws errors. Good grief.
88
    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
89
    goto err;
90
  }
1813.2.12 by Monty Taylor
Made load_file work properly with throwing errors and opening files.
91
1633.4.6 by Brian Aker
More current_session issues.
92
  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
93
  {
1633.4.6 by Brian Aker
More current_session issues.
94
    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
95
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
96
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1633.4.6 by Brian Aker
More current_session issues.
97
                        func_name(), session.variables.max_allowed_packet);
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
98
    goto err;
99
  }
1813.2.12 by Monty Taylor
Made load_file work properly with throwing errors and opening files.
100
101
  if (stat_info.st_size == 0)
102
  {
103
    goto err;
104
  }
105
2275.2.12 by Olaf van der Spek
Return void
106
  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.
107
  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
108
    goto err;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
109
  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
110
  {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
111
    internal::my_close(file, MYF(0));
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
112
    goto err;
113
  }
1813.2.12 by Monty Taylor
Made load_file work properly with throwing errors and opening files.
114
  if (strlen(tmp_value.ptr()) == 0)
115
  {
116
    goto err;
117
  }
892.2.2 by Monty Taylor
More solaris warnings.
118
  tmp_value.length((size_t)stat_info.st_size);
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
119
  internal::my_close(file, MYF(0));
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
120
  null_value = 0;
121
  return(&tmp_value);
122
123
err:
124
  null_value = 1;
2318.6.58 by Olaf van der Spek
Refactor
125
  return 0;
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
126
}
127
128
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
129
} /* namespace drizzled */