~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/cached_directory.cc

Merged in latest plugin-slot-reorg.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 *   Implementation of CachedDirectory class.
26
26
 */
27
27
 
28
 
#include "config.h"
29
 
 
30
 
#include "drizzled/definitions.h"
31
 
 
32
 
#include <sys/types.h>
33
 
#include <sys/stat.h>
34
 
#include <unistd.h>
35
 
 
36
 
#include <strings.h>
37
 
#include <limits.h>
38
 
 
39
 
#include "drizzled/cached_directory.h"
 
28
#include "drizzled/global.h"
 
29
#include "cached_directory.h"
40
30
 
41
31
using namespace std;
42
32
 
43
 
namespace drizzled
44
 
{
45
 
 
46
 
CachedDirectory::CachedDirectory() : 
47
 
  error(0)
48
 
{
49
 
}
50
 
 
51
 
 
52
 
CachedDirectory::CachedDirectory(const string &in_path) :
53
 
  error(0)
54
 
{
55
 
  // TODO: Toss future exception
 
33
 
 
34
CachedDirectory::CachedDirectory(const string &in_path)
 
35
  : error(0)
 
36
{
56
37
  (void) open(in_path);
57
38
}
58
39
 
59
 
 
60
 
CachedDirectory::CachedDirectory(const string& in_path, set<string>& allowed_exts) :
61
 
  error(0)
62
 
{
63
 
  // TODO: Toss future exception
64
 
  (void) open(in_path, allowed_exts);
65
 
}
66
 
 
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
 
 
75
 
 
76
40
CachedDirectory::~CachedDirectory()
77
41
{
78
 
  for (Entries::iterator p= entries.begin(); p != entries.end(); ++p)
 
42
  Entries::iterator p= entries.begin();
 
43
  while (p != entries.end())
79
44
  {
80
45
    if (*p)
81
46
      delete *p;
 
47
    ++p;
82
48
  }
83
 
  entries.clear();
84
49
}
85
50
 
86
51
bool CachedDirectory::open(const string &in_path)
87
52
{
88
 
  set<string> empty;
89
 
 
90
 
  return open(in_path, empty);
91
 
}
92
 
 
93
 
bool CachedDirectory::open(const string &in_path, set<string> &allowed_exts)
94
 
{
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
 
{
100
53
  DIR *dirp= opendir(in_path.c_str());
101
54
 
102
55
  if (dirp == NULL)
105
58
    return false;
106
59
  }
107
60
 
108
 
  path= in_path;
109
 
 
110
61
  union {
111
62
    dirent entry;
112
63
#ifdef __sun
126
77
 
127
78
  while ((retcode= readdir_r(dirp, &buffer.entry, &result)) == 0 &&
128
79
         result != NULL)
129
 
  {
130
 
    std::string buffered_fullpath;
131
 
    if (! allowed_exts.empty())
132
 
    {
133
 
      char *ptr= rindex(result->d_name, '.');
134
 
 
135
 
      if (ptr)
136
 
      {
137
 
        set<string>::iterator it;
138
 
        it= allowed_exts.find(ptr);
139
 
 
140
 
        if (it != allowed_exts.end())
141
 
        {
142
 
          entries.push_back(new Entry(result->d_name));
143
 
        }
144
 
      }
145
 
    }
146
 
    else
147
 
    {
148
 
      switch (filter)
149
 
      {
150
 
      case DIRECTORY:
151
 
        {
152
 
          struct stat entrystat;
153
 
 
154
 
          if (result->d_name[0] == '.') // We don't pass back anything hidden at the moment.
155
 
            continue;
156
 
 
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);
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
 
 
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);
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
 
      }
194
 
    }
195
 
  }
 
80
    entries.push_back(new Entry(result->d_name));
196
81
    
197
82
  closedir(dirp);
198
83
  error= retcode;
199
84
 
200
85
  return error == 0;
201
86
}
202
 
 
203
 
} /* namespace drizzled */