drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
cached_directory.cc
Go to the documentation of this file.
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, Inc.
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
27
28#include <config.h>
29#include <dirent.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
40#include <drizzled/util/find_ptr.h>
41#include <drizzled/error_t.h>
42#include <drizzled/error.h>
43#include <drizzled/errmsg_print.h>
44
45using namespace std;
46using namespace drizzled;
47
48namespace drizzled {
49
54
55
56CachedDirectory::CachedDirectory(const string &in_path) :
57 error(0),
58 use_full_path(false)
59{
60 // TODO: Toss future exception
61 (void) open(in_path);
62}
63
64
65CachedDirectory::CachedDirectory(const string& in_path, set<string>& allowed_exts) :
66 error(0),
67 use_full_path(false)
68{
69 // TODO: Toss future exception
70 (void) open(in_path, allowed_exts);
71}
72
73CachedDirectory::CachedDirectory(const string& in_path, enum CachedDirectory::FILTER filter, bool use_full_path_arg) :
74 error(0),
75 use_full_path(use_full_path_arg)
76{
77 set<string> empty;
78 // TODO: Toss future exception
79 (void) open(in_path, empty, filter);
80}
81
82
84{
85 for (Entries::reference iter : entries)
86 delete iter;
87}
88
89bool CachedDirectory::open(const string &in_path)
90{
91 set<string> empty;
92 return open(in_path, empty);
93}
94
95bool CachedDirectory::open(const string &in_path, set<string> &allowed_exts)
96{
97 return open(in_path, allowed_exts, CachedDirectory::NONE);
98}
99
100bool CachedDirectory::open(const string &in_path, set<string> &allowed_exts, enum CachedDirectory::FILTER filter)
101{
102 DIR *dirp= opendir(in_path.c_str());
103
104 if (dirp == NULL)
105 {
106 error= errno;
107 return false;
108 }
109
110 path= in_path;
111
112 dirent *result;
113 int retcode= 0;
114
115 errno= 0;
116 while ((result= readdir(dirp)) != NULL)
117 {
118 std::string buffered_fullpath;
119 if (not allowed_exts.empty())
120 {
121 char *ptr= rindex(result->d_name, '.');
122 if (ptr && allowed_exts.count(ptr))
123 {
124 entries.push_back(new Entry(result->d_name));
125 }
126 }
127 else
128 {
129 switch (filter)
130 {
131 case DIRECTORY:
132 {
133 struct stat entrystat;
134
135 if (result->d_name[0] == '.') // We don't pass back anything hidden at the moment.
136 continue;
137
138 if (use_full_path)
139 {
140 buffered_fullpath.append(in_path);
141 if (buffered_fullpath[buffered_fullpath.length()] != '/')
142 buffered_fullpath.append(1, FN_LIBCHAR);
143 }
144
145 buffered_fullpath.append(result->d_name);
146
147 int err= stat(buffered_fullpath.c_str(), &entrystat);
148
149 if (err != 0)
150 {
151 errmsg_printf(error::WARN, ER(ER_CANT_GET_STAT),
152 buffered_fullpath.c_str(),
153 errno);
154 }
155
156 if (err == 0 && S_ISDIR(entrystat.st_mode))
157 {
158 entries.push_back(new Entry(result->d_name));
159 }
160 }
161 break;
162 case FILE:
163 {
164 struct stat entrystat;
165
166 buffered_fullpath.append(in_path);
167 if (buffered_fullpath[buffered_fullpath.length() - 1] != '/')
168 buffered_fullpath.append(1, FN_LIBCHAR);
169
170 buffered_fullpath= result->d_name;
171
172 stat(buffered_fullpath.c_str(), &entrystat);
173
174 if (S_ISREG(entrystat.st_mode))
175 {
176 entries.push_back(new Entry(result->d_name));
177 }
178 }
179 break;
180 case NONE:
181 case MAX:
182 entries.push_back(new Entry(result->d_name));
183 break;
184 }
185 }
186 }
187
188 retcode= errno;
189 closedir(dirp);
190 error= retcode;
191
192 return error == 0;
193}
194
195std::ostream& operator<<(std::ostream& output, const CachedDirectory &directory)
196{
197 output << "CachedDirectory:(Path: " << directory.getPath() << ")\n";
198 for (const CachedDirectory::Entry* iter : directory.getEntries())
199 output << "\t(" << iter->filename << ")\n";
200 return output;
201}
202
203} /* namespace drizzled */
Defines the interface to the CachedDirectory class.
const Entries & getEntries() const
std::string path
Path to the directory.
int error
Error code stored from various syscalls.
Entries entries
Entries in the directory.
const char * getPath() const
bool open(const std::string &in_path)
TODO: Rename this file - func.h is stupid.
STL namespace.