~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/cached_directory.cc

Renamed more stuff to drizzle.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
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"
40
 
 
41
 
using namespace std;
42
 
 
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
56
 
  (void) open(in_path);
57
 
}
58
 
 
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
 
CachedDirectory::~CachedDirectory()
77
 
{
78
 
  for (Entries::iterator p= entries.begin(); p != entries.end(); ++p)
79
 
  {
80
 
    if (*p)
81
 
      delete *p;
82
 
  }
83
 
  entries.clear();
84
 
}
85
 
 
86
 
bool CachedDirectory::open(const string &in_path)
87
 
{
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
 
  DIR *dirp= opendir(in_path.c_str());
101
 
 
102
 
  if (dirp == NULL)
103
 
  {
104
 
    error= errno;
105
 
    return false;
106
 
  }
107
 
 
108
 
  path= in_path;
109
 
 
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];
121
 
#endif
122
 
  } buffer;
123
 
 
124
 
  int retcode;
125
 
  dirent *result;
126
 
 
127
 
  while ((retcode= readdir_r(dirp, &buffer.entry, &result)) == 0 &&
128
 
         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
 
  }
196
 
    
197
 
  closedir(dirp);
198
 
  error= retcode;
199
 
 
200
 
  return error == 0;
201
 
}
202
 
 
203
 
} /* namespace drizzled */