~drizzle-trunk/drizzle/development

1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
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.
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
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
20
/**
21
 * @file
22
 *   cached_directory.cc
23
 *
24
 * @brief
25
 *   Implementation of CachedDirectory class.
26
 */
27
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
28
#include <config.h>
2240.2.1 by Olaf van der Spek
Refactor
29
#include <dirent.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
30
#include <drizzled/definitions.h>
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
31
2246.4.1 by Olaf van der Spek
Use BOOST_FOREACH
32
#include <boost/foreach.hpp>
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
33
#include <sys/types.h>
34
#include <sys/stat.h>
35
#include <unistd.h>
36
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
37
#include <strings.h>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
38
#include <limits.h>
1241.9.44 by Monty Taylor
Made magic with cached_directory.
39
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
40
#include <drizzled/cached_directory.h>
2192.5.1 by Olaf van der Spek
Use find_ptr
41
#include <drizzled/util/find_ptr.h>
2241.5.3 by Stewart Smith
have CachedDirectory print out a warning if we can't stat() something in a directory. We should always have access to at least stat() things in directories Drizzle is running in (otherwise there is likely a problem)
42
#include <drizzled/error_t.h>
43
#include <drizzled/error.h>
44
#include <drizzled/errmsg_print.h>
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
45
1089.2.4 by David Shrewsbury
Put CachedDirectory in mysys namespace; added std namespace to sql_base.cc and default.cc to replace std::
46
using namespace std;
2241.5.3 by Stewart Smith
have CachedDirectory print out a warning if we can't stat() something in a directory. We should always have access to at least stat() things in directories Drizzle is running in (otherwise there is likely a problem)
47
using namespace drizzled;
1089.2.4 by David Shrewsbury
Put CachedDirectory in mysys namespace; added std namespace to sql_base.cc and default.cc to replace std::
48
2246.4.1 by Olaf van der Spek
Use BOOST_FOREACH
49
namespace drizzled {
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
50
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
51
CachedDirectory::CachedDirectory() : 
52
  error(0)
53
{
54
}
55
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
56
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
57
CachedDirectory::CachedDirectory(const string &in_path) :
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
58
  error(0),
59
  use_full_path(false)
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
60
{
61
  // TODO: Toss future exception
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
62
  (void) open(in_path);
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
63
}
64
65
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
66
CachedDirectory::CachedDirectory(const string& in_path, set<string>& allowed_exts) :
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
67
  error(0),
68
  use_full_path(false)
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
69
{
70
  // TODO: Toss future exception
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
71
  (void) open(in_path, allowed_exts);
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
72
}
73
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
74
CachedDirectory::CachedDirectory(const string& in_path, enum CachedDirectory::FILTER filter, bool use_full_path_arg) :
75
  error(0),
76
  use_full_path(use_full_path_arg)
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
77
{
78
  set<string> empty;
79
  // TODO: Toss future exception
80
  (void) open(in_path, empty, filter);
81
}
82
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
83
84
CachedDirectory::~CachedDirectory()
85
{
2246.4.1 by Olaf van der Spek
Use BOOST_FOREACH
86
	BOOST_FOREACH(Entries::reference iter, entries)
87
    delete iter;
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
88
}
89
1089.2.4 by David Shrewsbury
Put CachedDirectory in mysys namespace; added std namespace to sql_base.cc and default.cc to replace std::
90
bool CachedDirectory::open(const string &in_path)
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
91
{
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
92
  set<string> empty;
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
93
  return open(in_path, empty);
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
94
}
95
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
96
bool CachedDirectory::open(const string &in_path, set<string> &allowed_exts)
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
97
{
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
98
  return open(in_path, allowed_exts, CachedDirectory::NONE);
99
}
100
101
bool CachedDirectory::open(const string &in_path, set<string> &allowed_exts, enum CachedDirectory::FILTER filter)
102
{
1115.2.1 by Trond Norbye
Bug #412684: Fix memory corruption on Solaris
103
  DIR *dirp= opendir(in_path.c_str());
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
104
1115.2.1 by Trond Norbye
Bug #412684: Fix memory corruption on Solaris
105
  if (dirp == NULL)
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
106
  {
107
    error= errno;
108
    return false;
109
  }
110
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
111
  path= in_path;
112
1115.2.1 by Trond Norbye
Bug #412684: Fix memory corruption on Solaris
113
  union {
114
    dirent entry;
115
#ifdef __sun
116
    /*
117
     * The readdir_r() call on Solaris operates a bit differently from other
118
     * systems in that the dirent structure must be allocated along with enough
119
     * space to contain the filename (see man page for readdir_r on Solaris).
120
     * Instead of dynamically try to allocate this buffer, just set the max
121
     * name for a path instead.
122
     */
123
    char space[sizeof(dirent) + PATH_MAX + 1];
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
124
#endif
1115.2.1 by Trond Norbye
Bug #412684: Fix memory corruption on Solaris
125
  } buffer;
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
126
127
  int retcode;
1089.3.1 by Monty Taylor
Merged Dave from lp:~dshrews/drizzle/my_dir_removal
128
  dirent *result;
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
129
1115.2.1 by Trond Norbye
Bug #412684: Fix memory corruption on Solaris
130
  while ((retcode= readdir_r(dirp, &buffer.entry, &result)) == 0 &&
131
         result != NULL)
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
132
  {
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
133
    std::string buffered_fullpath;
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
134
    if (not allowed_exts.empty())
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
135
    {
136
      char *ptr= rindex(result->d_name, '.');
2192.5.1 by Olaf van der Spek
Use find_ptr
137
      if (ptr && allowed_exts.count(ptr))
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
138
      {
2192.5.1 by Olaf van der Spek
Use find_ptr
139
        entries.push_back(new Entry(result->d_name));
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
140
      }
141
    }
142
    else
143
    {
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
144
      switch (filter)
145
      {
146
      case DIRECTORY:
147
        {
148
          struct stat entrystat;
149
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
150
          if (result->d_name[0] == '.') // We don't pass back anything hidden at the moment.
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
151
            continue;
152
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
153
          if (use_full_path)
154
          {
155
            buffered_fullpath.append(in_path);
156
            if (buffered_fullpath[buffered_fullpath.length()] != '/')
157
              buffered_fullpath.append(1, FN_LIBCHAR);
158
          }
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
159
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
160
          buffered_fullpath.append(result->d_name);
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
161
2241.5.3 by Stewart Smith
have CachedDirectory print out a warning if we can't stat() something in a directory. We should always have access to at least stat() things in directories Drizzle is running in (otherwise there is likely a problem)
162
          int err= stat(buffered_fullpath.c_str(), &entrystat);
163
164
          if (err != 0)
165
            errmsg_printf(error::WARN, ER(ER_CANT_GET_STAT),
166
                          buffered_fullpath.c_str(),
167
                          errno);
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
168
2241.5.1 by Stewart Smith
only add a directory to CachedDirecotry if we don't get an error stat()ing it.
169
          if (err == 0 && S_ISDIR(entrystat.st_mode))
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
170
          {
171
            entries.push_back(new Entry(result->d_name));
172
          }
173
        }
174
        break;
175
      case FILE:
176
        {
177
          struct stat entrystat;
178
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
179
          buffered_fullpath.append(in_path);
2318.7.23 by Olaf van der Spek
Use operator=
180
          if (buffered_fullpath[buffered_fullpath.length() - 1] != '/')
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
181
            buffered_fullpath.append(1, FN_LIBCHAR);
182
2318.7.23 by Olaf van der Spek
Use operator=
183
          buffered_fullpath= result->d_name;
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
184
185
          stat(buffered_fullpath.c_str(), &entrystat);
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
186
187
          if (S_ISREG(entrystat.st_mode))
188
          {
189
            entries.push_back(new Entry(result->d_name));
190
          }
191
        }
192
        break;
193
      case NONE:
194
      case MAX:
195
        entries.push_back(new Entry(result->d_name));
196
        break;
197
      }
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
198
    }
199
  }
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
200
    
201
  closedir(dirp);
202
  error= retcode;
203
204
  return error == 0;
205
}
1241.9.44 by Monty Taylor
Made magic with cached_directory.
206
2246.4.1 by Olaf van der Spek
Use BOOST_FOREACH
207
std::ostream& operator<<(std::ostream& output, const CachedDirectory &directory)
208
{
209
  output << "CachedDirectory:(Path: " << directory.getPath() << ")\n";
210
  BOOST_FOREACH(const CachedDirectory::Entry* iter, directory.getEntries())
211
    output << "\t(" << iter->filename << ")\n";
212
  return output;
213
}
214
1241.9.44 by Monty Taylor
Made magic with cached_directory.
215
} /* namespace drizzled */