~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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
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>
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
26
#include "drizzled/internal/my_sys.h"
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
27
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
28
#include <boost/filesystem.hpp>
29
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
30
#include <fcntl.h>
31
#include <sys/stat.h>
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
32
#include <iostream>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
33
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
34
namespace fs=boost::filesystem;
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
35
using namespace std;
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
36
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
37
namespace drizzled
38
{
39
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
40
String *Item_load_file::val_str(String *str)
41
{
42
  assert(fixed == 1);
43
  String *file_name;
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
44
  int file;
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
45
  struct stat stat_info;
46
47
  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.
48
  {
49
    null_value = 1;
50
    return(0);
51
  }
52
53
  fs::path target_path(fs::system_complete(getDataHomeCatalog()));
54
  fs::path to_file(file_name->c_ptr());
55
  if (not to_file.has_root_directory())
56
  {
57
    target_path /= to_file;
58
  }
59
  else
60
  {
61
    target_path= to_file;
62
  }
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
63
64
  /* 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.
65
  if (not secure_file_priv.string().empty())
66
  {
1813.2.13 by Monty Taylor
Made load_file() work sensibly with fs::path. Now it throws errors. Good grief.
67
    fs::path secure_file_path(fs::system_complete(secure_file_priv));
68
    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.
69
    {
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
70
      /* Read only allowed from within dir specified by secure_file_priv */
71
      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.
72
      null_value = 1;
1813.2.11 by Monty Taylor
Throw an error on load_file() from outside the secure_file_priv locaiton.
73
      return 0;
1813.2.6 by Monty Taylor
Made secure_file_priv be an fs::path from the beginning.
74
    }
75
  }
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
76
1813.2.13 by Monty Taylor
Made load_file() work sensibly with fs::path. Now it throws errors. Good grief.
77
  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.
78
  {
1813.2.13 by Monty Taylor
Made load_file() work sensibly with fs::path. Now it throws errors. Good grief.
79
    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
80
    goto err;
1813.2.12 by Monty Taylor
Made load_file work properly with throwing errors and opening files.
81
  }
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
82
83
  if (!(stat_info.st_mode & S_IROTH))
84
  {
1813.2.13 by Monty Taylor
Made load_file() work sensibly with fs::path. Now it throws errors. Good grief.
85
    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
86
    goto err;
87
  }
1813.2.12 by Monty Taylor
Made load_file work properly with throwing errors and opening files.
88
1633.4.6 by Brian Aker
More current_session issues.
89
  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
90
  {
1633.4.6 by Brian Aker
More current_session issues.
91
    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
92
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
93
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1633.4.6 by Brian Aker
More current_session issues.
94
                        func_name(), session.variables.max_allowed_packet);
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
95
    goto err;
96
  }
1813.2.12 by Monty Taylor
Made load_file work properly with throwing errors and opening files.
97
98
  if (stat_info.st_size == 0)
99
  {
100
    goto err;
101
  }
102
892.2.2 by Monty Taylor
More solaris warnings.
103
  if (tmp_value.alloc((size_t)stat_info.st_size))
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
104
    goto err;
1813.2.13 by Monty Taylor
Made load_file() work sensibly with fs::path. Now it throws errors. Good grief.
105
  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
106
    goto err;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
107
  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
108
  {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
109
    internal::my_close(file, MYF(0));
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
110
    goto err;
111
  }
1813.2.12 by Monty Taylor
Made load_file work properly with throwing errors and opening files.
112
  if (strlen(tmp_value.ptr()) == 0)
113
  {
114
    goto err;
115
  }
892.2.2 by Monty Taylor
More solaris warnings.
116
  tmp_value.length((size_t)stat_info.st_size);
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
117
  internal::my_close(file, MYF(0));
629.6.5 by Lee
clean up remaining functions in strfunc, move to functions/str directory
118
  null_value = 0;
119
  return(&tmp_value);
120
121
err:
122
  null_value = 1;
123
  return(0);
124
}
125
126
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
127
} /* namespace drizzled */