~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>
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
42
1089.2.4 by David Shrewsbury
Put CachedDirectory in mysys namespace; added std namespace to sql_base.cc and default.cc to replace std::
43
using namespace std;
44
2246.4.1 by Olaf van der Spek
Use BOOST_FOREACH
45
namespace drizzled {
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
46
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
47
CachedDirectory::CachedDirectory() : 
48
  error(0)
49
{
50
}
51
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
52
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
53
CachedDirectory::CachedDirectory(const string &in_path) :
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
54
  error(0),
55
  use_full_path(false)
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
56
{
57
  // TODO: Toss future exception
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
58
  (void) open(in_path);
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
59
}
60
61
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
62
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.
63
  error(0),
64
  use_full_path(false)
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
65
{
66
  // TODO: Toss future exception
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
67
  (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
68
}
69
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
70
CachedDirectory::CachedDirectory(const string& in_path, enum CachedDirectory::FILTER filter, bool use_full_path_arg) :
71
  error(0),
72
  use_full_path(use_full_path_arg)
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
73
{
74
  set<string> empty;
75
  // TODO: Toss future exception
76
  (void) open(in_path, empty, filter);
77
}
78
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
79
80
CachedDirectory::~CachedDirectory()
81
{
2246.4.1 by Olaf van der Spek
Use BOOST_FOREACH
82
	BOOST_FOREACH(Entries::reference iter, entries)
83
    delete iter;
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
84
}
85
1089.2.4 by David Shrewsbury
Put CachedDirectory in mysys namespace; added std namespace to sql_base.cc and default.cc to replace std::
86
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.
87
{
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
88
  set<string> empty;
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
89
  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
90
}
91
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
92
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
93
{
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
94
  return open(in_path, allowed_exts, CachedDirectory::NONE);
95
}
96
97
bool CachedDirectory::open(const string &in_path, set<string> &allowed_exts, enum CachedDirectory::FILTER filter)
98
{
1115.2.1 by Trond Norbye
Bug #412684: Fix memory corruption on Solaris
99
  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.
100
1115.2.1 by Trond Norbye
Bug #412684: Fix memory corruption on Solaris
101
  if (dirp == NULL)
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
102
  {
103
    error= errno;
104
    return false;
105
  }
106
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
107
  path= in_path;
108
1115.2.1 by Trond Norbye
Bug #412684: Fix memory corruption on Solaris
109
  union {
110
    dirent entry;
111
#ifdef __sun
112
    /*
113
     * The readdir_r() call on Solaris operates a bit differently from other
114
     * systems in that the dirent structure must be allocated along with enough
115
     * space to contain the filename (see man page for readdir_r on Solaris).
116
     * Instead of dynamically try to allocate this buffer, just set the max
117
     * name for a path instead.
118
     */
119
    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.
120
#endif
1115.2.1 by Trond Norbye
Bug #412684: Fix memory corruption on Solaris
121
  } buffer;
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
122
123
  int retcode;
1089.3.1 by Monty Taylor
Merged Dave from lp:~dshrews/drizzle/my_dir_removal
124
  dirent *result;
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
125
1115.2.1 by Trond Norbye
Bug #412684: Fix memory corruption on Solaris
126
  while ((retcode= readdir_r(dirp, &buffer.entry, &result)) == 0 &&
127
         result != NULL)
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
128
  {
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
129
    std::string buffered_fullpath;
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
130
    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
131
    {
132
      char *ptr= rindex(result->d_name, '.');
2192.5.1 by Olaf van der Spek
Use find_ptr
133
      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
134
      {
2192.5.1 by Olaf van der Spek
Use find_ptr
135
        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
136
      }
137
    }
138
    else
139
    {
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
140
      switch (filter)
141
      {
142
      case DIRECTORY:
143
        {
144
          struct stat entrystat;
145
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
146
          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.
147
            continue;
148
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
149
          if (use_full_path)
150
          {
151
            buffered_fullpath.append(in_path);
152
            if (buffered_fullpath[buffered_fullpath.length()] != '/')
153
              buffered_fullpath.append(1, FN_LIBCHAR);
154
          }
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
155
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
156
          buffered_fullpath.append(result->d_name);
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
157
158
          stat(buffered_fullpath.c_str(), &entrystat);
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
159
160
          if (S_ISDIR(entrystat.st_mode))
161
          {
162
            entries.push_back(new Entry(result->d_name));
163
          }
164
        }
165
        break;
166
      case FILE:
167
        {
168
          struct stat entrystat;
169
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
170
          buffered_fullpath.append(in_path);
171
          if (buffered_fullpath[buffered_fullpath.length()] != '/')
172
            buffered_fullpath.append(1, FN_LIBCHAR);
173
174
          buffered_fullpath.assign(result->d_name);
175
176
          stat(buffered_fullpath.c_str(), &entrystat);
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
177
178
          if (S_ISREG(entrystat.st_mode))
179
          {
180
            entries.push_back(new Entry(result->d_name));
181
          }
182
        }
183
        break;
184
      case NONE:
185
      case MAX:
186
        entries.push_back(new Entry(result->d_name));
187
        break;
188
      }
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
189
    }
190
  }
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
191
    
192
  closedir(dirp);
193
  error= retcode;
194
195
  return error == 0;
196
}
1241.9.44 by Monty Taylor
Made magic with cached_directory.
197
2246.4.1 by Olaf van der Spek
Use BOOST_FOREACH
198
std::ostream& operator<<(std::ostream& output, const CachedDirectory &directory)
199
{
200
  output << "CachedDirectory:(Path: " << directory.getPath() << ")\n";
201
  BOOST_FOREACH(const CachedDirectory::Entry* iter, directory.getEntries())
202
    output << "\t(" << iter->filename << ")\n";
203
  return output;
204
}
205
1241.9.44 by Monty Taylor
Made magic with cached_directory.
206
} /* namespace drizzled */