~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
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) :
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
53
  error(0),
54
  use_full_path(false)
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
55
{
56
  // TODO: Toss future exception
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
57
  (void) open(in_path);
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
58
}
59
60
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
61
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.
62
  error(0),
63
  use_full_path(false)
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
64
{
65
  // TODO: Toss future exception
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
66
  (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
67
}
68
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
69
CachedDirectory::CachedDirectory(const string& in_path, enum CachedDirectory::FILTER filter, bool use_full_path_arg) :
70
  error(0),
71
  use_full_path(use_full_path_arg)
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
72
{
73
  set<string> empty;
74
  // TODO: Toss future exception
75
  (void) open(in_path, empty, filter);
76
}
77
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
78
79
CachedDirectory::~CachedDirectory()
80
{
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
81
  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.
82
  {
83
    if (*p)
84
      delete *p;
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, '.');
137
138
      if (ptr)
139
      {
140
        set<string>::iterator it;
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
141
        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
142
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
143
        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
144
        {
145
          entries.push_back(new Entry(result->d_name));
146
        }
147
      }
148
    }
149
    else
150
    {
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
151
      switch (filter)
152
      {
153
      case DIRECTORY:
154
        {
155
          struct stat entrystat;
156
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
157
          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.
158
            continue;
159
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
160
          if (use_full_path)
161
          {
162
            buffered_fullpath.append(in_path);
163
            if (buffered_fullpath[buffered_fullpath.length()] != '/')
164
              buffered_fullpath.append(1, FN_LIBCHAR);
165
          }
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
166
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
167
          buffered_fullpath.append(result->d_name);
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
168
169
          stat(buffered_fullpath.c_str(), &entrystat);
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
170
171
          if (S_ISDIR(entrystat.st_mode))
172
          {
173
            entries.push_back(new Entry(result->d_name));
174
          }
175
        }
176
        break;
177
      case FILE:
178
        {
179
          struct stat entrystat;
180
1749.3.15 by Brian Aker
Use full path for opening up directory files when checking for types.
181
          buffered_fullpath.append(in_path);
182
          if (buffered_fullpath[buffered_fullpath.length()] != '/')
183
            buffered_fullpath.append(1, FN_LIBCHAR);
184
185
          buffered_fullpath.assign(result->d_name);
186
187
          stat(buffered_fullpath.c_str(), &entrystat);
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
188
189
          if (S_ISREG(entrystat.st_mode))
190
          {
191
            entries.push_back(new Entry(result->d_name));
192
          }
193
        }
194
        break;
195
      case NONE:
196
      case MAX:
197
        entries.push_back(new Entry(result->d_name));
198
        break;
199
      }
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
200
    }
201
  }
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
202
    
203
  closedir(dirp);
204
  error= retcode;
205
206
  return error == 0;
207
}
1241.9.44 by Monty Taylor
Made magic with cached_directory.
208
209
} /* namespace drizzled */