~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/str/load_file.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <drizzled/server_includes.h>
21
 
#include CSTDINT_H
 
20
#include "config.h"
22
21
#include <drizzled/function/str/strfunc.h>
23
22
#include <drizzled/function/str/load_file.h>
24
23
#include <drizzled/error.h>
25
24
#include <drizzled/data_home.h>
26
25
#include <drizzled/session.h>
 
26
#include "drizzled/internal/my_sys.h"
 
27
 
 
28
#include <boost/filesystem.hpp>
 
29
 
 
30
#include <fcntl.h>
 
31
#include <sys/stat.h>
 
32
#include <iostream>
 
33
 
 
34
namespace fs=boost::filesystem;
 
35
using namespace std;
 
36
 
 
37
namespace drizzled
 
38
{
27
39
 
28
40
String *Item_load_file::val_str(String *str)
29
41
{
30
42
  assert(fixed == 1);
31
43
  String *file_name;
32
 
  File file;
 
44
  int file;
33
45
  struct stat stat_info;
34
 
  char path[FN_REFLEN];
35
46
 
36
47
  if (!(file_name= args[0]->val_str(str)))
37
 
    goto err;
 
48
  {
 
49
    null_value = 1;
 
50
    return(0);
 
51
  }
38
52
 
39
 
  (void) fn_format(path, file_name->c_ptr(), drizzle_real_data_home, "",
40
 
                   MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
 
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
  }
41
63
 
42
64
  /* Read only allowed from within dir specified by secure_file_priv */
43
 
  if (opt_secure_file_priv &&
44
 
      strncmp(opt_secure_file_priv, path, strlen(opt_secure_file_priv)))
45
 
    goto err;
 
65
  if (not secure_file_priv.string().empty())
 
66
  {
 
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())
 
69
    {
 
70
      /* Read only allowed from within dir specified by secure_file_priv */
 
71
      my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv"); 
 
72
      null_value = 1;
 
73
      return 0;
 
74
    }
 
75
  }
46
76
 
47
 
  if (stat(path, &stat_info))
 
77
  if (stat(target_path.file_string().c_str(), &stat_info))
 
78
  {
 
79
    my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr());
48
80
    goto err;
 
81
  }
49
82
 
50
83
  if (!(stat_info.st_mode & S_IROTH))
51
84
  {
52
 
    /* my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr()); */
 
85
    my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr());
53
86
    goto err;
54
87
  }
55
 
  if (stat_info.st_size > (long) current_session->variables.max_allowed_packet)
 
88
 
 
89
  if (stat_info.st_size > (long) session.variables.max_allowed_packet)
56
90
  {
57
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
91
    push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
58
92
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
59
93
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
60
 
                        func_name(), current_session->variables.max_allowed_packet);
61
 
    goto err;
62
 
  }
 
94
                        func_name(), session.variables.max_allowed_packet);
 
95
    goto err;
 
96
  }
 
97
 
 
98
  if (stat_info.st_size == 0)
 
99
  {
 
100
    goto err;
 
101
  }
 
102
 
63
103
  if (tmp_value.alloc((size_t)stat_info.st_size))
64
104
    goto err;
65
 
  if ((file = my_open(file_name->c_ptr(), O_RDONLY, MYF(0))) < 0)
66
 
    goto err;
67
 
  if (my_read(file, (unsigned char*) tmp_value.ptr(), (size_t)stat_info.st_size, MYF(MY_NABP)))
68
 
  {
69
 
    my_close(file, MYF(0));
 
105
  if ((file = internal::my_open(target_path.file_string().c_str(), O_RDONLY, MYF(0))) < 0)
 
106
    goto err;
 
107
  if (internal::my_read(file, (unsigned char*) tmp_value.ptr(), (size_t)stat_info.st_size, MYF(MY_NABP)))
 
108
  {
 
109
    internal::my_close(file, MYF(0));
 
110
    goto err;
 
111
  }
 
112
  if (strlen(tmp_value.ptr()) == 0)
 
113
  {
70
114
    goto err;
71
115
  }
72
116
  tmp_value.length((size_t)stat_info.st_size);
73
 
  my_close(file, MYF(0));
 
117
  internal::my_close(file, MYF(0));
74
118
  null_value = 0;
75
119
  return(&tmp_value);
76
120
 
80
124
}
81
125
 
82
126
 
 
127
} /* namespace drizzled */