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