Debian Codegraph @

src/lib/object_store/Directory.cpp

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 Directory.cpp
29
30 Helper functions for accessing directories.
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "Directory.h"
35 #include "OSPathSep.h"
36 #include "log.h"
37 #include <string>
38 #include <vector>
39 #ifndef _WIN32
40 #include <dirent.h>
41 #include <unistd.h>
42 #else
43 #include <direct.h>
44 #include <io.h>
45 #endif
46 #include <errno.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51
52 // Constructor
53 Directory::Directory(std::string inPath)
54 {
55 path = inPath;
56 dirMutex = MutexFactory::i()->getMutex();
57
58 valid = (dirMutex != NULL) && refresh();
59 }
60
61 // Destructor
62 Directory::~Directory()
63 {
64 MutexFactory::i()->recycleMutex(dirMutex);
65 }
66
67 // Check if the directory is valid
68 bool Directory::isValid()
69 {
70 return valid;
71 }
72
73 // Return a list of all files in a directory
74 std::vector<std::string> Directory::getFiles()
75 {
76 // Make sure that no other thread is in the process of changing
77 // the file list when we return it
78 MutexLocker lock(dirMutex);
79
80 return files;
81 }
82
83 // Return a list of all subdirectories in a directory
84 std::vector<std::string> Directory::getSubDirs()
85 {
86 // Make sure that no other thread is in the process of changing
87 // the subdirectory list when we return it
88 MutexLocker lock(dirMutex);
89
90 return subDirs;
91 }
92
93 // Refresh the directory listing
94 bool Directory::refresh()
95 {
96 // Prevent concurrent call until valid is reset
97 MutexLocker lock(dirMutex);
98
99 // Reset the state
100 valid = false;
101
102 subDirs.clear();
103 files.clear();
104
105 #ifndef _WIN32
106 // Enumerate the directory
107 DIR* dir = opendir(path.c_str());
108
109 if (dir == NULL)
110 {
111 DEBUG_MSG("Failed to open directory %s", path.c_str());
112
113 return false;
114 }
115
116 // Enumerate the directory
117 struct dirent* entry = NULL;
118
119 while ((entry = readdir(dir)) != NULL)
120 {
121 bool pushed = false;
122
123 // Check if this is the . or .. entry
124 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
125 {
126 continue;
127 }
128
129 // Convert the name of the entry to a C++ string
130 std::string name(entry->d_name);
131
132 #if defined(_DIRENT_HAVE_D_TYPE) && defined(_BSD_SOURCE)
133 // Determine the type of the entry
134 switch(entry->d_type)
135 {
136 case DT_DIR:
137 // This is a directory
138 subDirs.push_back(name);
139 pushed = true;
140 break;
141 case DT_REG:
142 // This is a regular file
143 files.push_back(name);
144 pushed = true;
145 break;
146 default:
147 break;
148 }
149 #endif
150 if (!pushed) {
151 // The entry type has to be determined using lstat
152 struct stat entryStatus;
153
154 std::string fullPath = path + OS_PATHSEP + name;
155
156 if (!lstat(fullPath.c_str(), &entryStatus))
157 {
158 if (S_ISDIR(entryStatus.st_mode))
159 {
160 subDirs.push_back(name);
161 }
162 else if (S_ISREG(entryStatus.st_mode))
163 {
164 files.push_back(name);
165 }
166 else
167 {
168 DEBUG_MSG("File not used %s", name.c_str());
169 }
170 }
171 }
172 }
173
174 // Close the directory
175 closedir(dir);
176
177 #else
178 // Enumerate the directory
179 std::string pattern;
180 intptr_t h;
181 struct _finddata_t fi;
182
183 if ((path.back() == '/') || (path.back() == '\\'))
184 pattern = path + "*";
185 else
186 pattern = path + "/*";
187 memset(&fi, 0, sizeof(fi));
188 h = _findfirst(pattern.c_str(), &fi);
189 if (h == -1)
190 {
191 // empty directory
192 if (errno == ENOENT)
193 goto finished;
194
195 DEBUG_MSG("Failed to open directory %s", path.c_str());
196
197 return false;
198 }
199
200 // scan files & subdirs
201 do {
202 // Check if this is the . or .. entry
203 if (!strcmp(fi.name, ".") || !strcmp(fi.name, ".."))
204 continue;
205
206 if ((fi.attrib & _A_SUBDIR) == 0)
207 files.push_back(fi.name);
208 else
209 subDirs.push_back(fi.name);
210
211 memset(&fi, 0, sizeof(fi));
212 } while (_findnext(h, &fi) == 0);
213
214 (void) _findclose(h);
215
216 finished:
217 #endif
218
219 valid = true;
220
221 return true;
222 }
223
224 // Create a new subdirectory
225 bool Directory::mkdir(std::string name)
226 {
227 std::string fullPath = path + OS_PATHSEP + name;
228
229 #ifndef _WIN32
230 int rv = ::mkdir(fullPath.c_str(), S_IFDIR | S_IRWXU);
231 #else
232 int rv = _mkdir(fullPath.c_str());
233 #endif
234
235 if (rv != 0)
236 {
237 ERROR_MSG("Failed to create the directory (%s): %s", strerror(errno), fullPath.c_str());
238
239 return false;
240 }
241
242 return refresh();
243 }
244
245 // Delete a subdirectory in the directory
246 bool Directory::rmdir(std::string name, bool doRefresh /* = false */)
247 {
248 std::string fullPath;
249
250 if (name.empty())
251 fullPath = path;
252 else
253 fullPath = path + OS_PATHSEP + name;
254
255 #ifndef _WIN32
256 if (::rmdir(fullPath.c_str()) != 0)
257 return false;
258 #else
259 if (_rmdir(fullPath.c_str()) != 0)
260 return false;
261 #endif
262 if (doRefresh)
263 return refresh();
264 return true;
265 }
266
267 // Delete a file in the directory
268 bool Directory::remove(std::string name)
269 {
270 std::string fullPath = path + OS_PATHSEP + name;
271
272 #ifndef _WIN32
273 return (!::remove(fullPath.c_str()) && refresh());
274 #else
275 return (!_unlink(fullPath.c_str()) && refresh());
276 #endif
277 }
278