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