~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>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
29
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
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
39
#include <drizzled/cached_directory.h>
2192.5.1 by Olaf van der Spek
Use find_ptr
40
#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.
41
1089.2.4 by David Shrewsbury
Put CachedDirectory in mysys namespace; added std namespace to sql_base.cc and default.cc to replace std::
42
using namespace std;
43
1241.9.44 by Monty Taylor
Made magic with cached_directory.
44
namespace drizzled
45
{
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
{
2172.3.22 by Brian Aker
This patch adds safe_delete(). All of these locations in the code should be
82
  for (Entries::iterator iter= entries.begin(); iter != entries.end(); ++iter)
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
83
  {
2172.3.22 by Brian Aker
This patch adds safe_delete(). All of these locations in the code should be
84
    delete *iter;
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
85
  }
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
86
  entries.clear();
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
87
}
88
1089.2.4 by David Shrewsbury
Put CachedDirectory in mysys namespace; added std namespace to sql_base.cc and default.cc to replace std::
89
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.
90
{
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
91
  set<string> empty;
92
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
162
          stat(buffered_fullpath.c_str(), &entrystat);
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
163
164
          if (S_ISDIR(entrystat.st_mode))
165
          {
166
            entries.push_back(new Entry(result->d_name));
167
          }
168
        }
169
        break;
170
      case FILE:
171
        {
172
          struct stat entrystat;
173
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
174
          buffered_fullpath.append(in_path);
175
          if (buffered_fullpath[buffered_fullpath.length()] != '/')
176
            buffered_fullpath.append(1, FN_LIBCHAR);
177
178
          buffered_fullpath.assign(result->d_name);
179
180
          stat(buffered_fullpath.c_str(), &entrystat);
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
181
182
          if (S_ISREG(entrystat.st_mode))
183
          {
184
            entries.push_back(new Entry(result->d_name));
185
          }
186
        }
187
        break;
188
      case NONE:
189
      case MAX:
190
        entries.push_back(new Entry(result->d_name));
191
        break;
192
      }
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
193
    }
194
  }
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
195
    
196
  closedir(dirp);
197
  error= retcode;
198
199
  return error == 0;
200
}
1241.9.44 by Monty Taylor
Made magic with cached_directory.
201
202
} /* namespace drizzled */