1 // Written by James Turner, started July 2010.
3 // Copyright (C) 2010 Curtis L. Olson - http://www.flightgear.org/~curt
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library 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 GNU
13 // Library General Public License for more details.
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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 # include <simgear_config.h>
25 #include <simgear/misc/sg_dir.hxx>
31 # define WIN32_LEAN_AND_MEAN
35 # include <sys/types.h>
37 # include <sys/stat.h>
42 #include <simgear/debug/logstream.hxx>
43 #include <boost/foreach.hpp>
48 #include <algorithm> // for std::sort
56 _removeOnDestroy(false)
60 Dir::Dir(const SGPath& path) :
62 _removeOnDestroy(false)
64 _path.set_cached(false); // disable caching, so create/remove work
67 Dir::Dir(const Dir& rel, const SGPath& relPath) :
68 _path(rel.file(relPath.str())),
69 _removeOnDestroy(false)
71 _path.set_cached(false); // disable caching, so create/remove work
76 if (_removeOnDestroy) {
81 void Dir::setRemoveOnDestroy()
83 _removeOnDestroy = true;
89 char* buf = _getcwd(NULL, 0);
91 char* buf = ::getcwd(NULL, 0);
98 Dir Dir::tempDir(const std::string& templ)
102 const char* tempPath = ::getenv("TMPDIR");
108 // Mac OS-X / BSD manual says any number of 'X's, but GLibc manual
109 // says exactly six, so that's what I'm going with
111 ::snprintf(buf, 1024, "%s", p.c_str());
113 SG_LOG(SG_IO, SG_WARN, "mkdtemp failed:" << strerror(errno));
117 return Dir(SGPath(buf));
119 SGPath p(tempnam(0, templ.c_str()));
121 if (!t.create(0700)) {
122 SG_LOG(SG_IO, SG_WARN, "failed to create temporary directory at " << p.str());
129 static bool pathSortPredicate(const SGPath& p1, const SGPath& p2)
131 return p1.file() < p2.file();
134 PathList Dir::children(int types, const std::string& nameFilter) const
138 types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
142 std::string search(_path.str());
143 if (nameFilter.empty()) {
144 search += "\\*"; // everything
146 search += "\\*" + nameFilter;
149 WIN32_FIND_DATA fData;
150 HANDLE find = FindFirstFile(search.c_str(), &fData);
151 if (find == INVALID_HANDLE_VALUE) {
152 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << _path.str());
157 for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
158 if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
159 if (!(types & INCLUDE_HIDDEN)) {
164 if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
165 if (types & NO_DOT_OR_DOTDOT) {
166 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
171 if (!(types & TYPE_DIR)) {
174 } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
175 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
177 continue; // always ignore device files
178 } else if (!(types & TYPE_FILE)) {
182 result.push_back(file(fData.cFileName));
187 DIR* dp = opendir(_path.c_str());
189 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
193 int filterLen = nameFilter.size();
196 struct dirent* entry = readdir(dp);
198 break; // done iteration
201 // skip hidden files (names beginning with '.') unless requested
202 if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.') &&
203 strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
207 SGPath f = file(entry->d_name);
209 continue; // stat() failed
213 // directory handling
214 if (!(types & TYPE_DIR)) {
218 if (types & NO_DOT_OR_DOTDOT) {
219 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
223 } else if (f.isFile()) {
224 // regular file handling
225 if (!(types & TYPE_FILE)) {
229 // block device /fifo/char file, ignore
233 if (!nameFilter.empty()) {
234 int nameLen = strlen(entry->d_name);
235 if (nameLen < filterLen) {
236 continue; // name is shorter than the filter
239 char* nameSuffix = entry->d_name + (nameLen - filterLen);
240 if (strcmp(nameSuffix, nameFilter.c_str())) {
245 // passed all criteria, add to our result vector
246 result.push_back(file(entry->d_name));
252 // File system order is random. Make things deterministic,
253 // so it's the same for every user.
254 std::sort(result.begin(), result.end(), pathSortPredicate);
259 bool Dir::isEmpty() const
263 WIN32_FIND_DATA fData;
264 HANDLE find = FindFirstFile("\\*", &fData);
265 if (find == INVALID_HANDLE_VALUE) {
269 // since an empty dir will still have . and .. children, we need
270 // watch for those - anything else means the dir is really non-empty
272 for (; !done; done = (FindNextFile(find, &fData) == 0)) {
273 if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
274 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
285 DIR* dp = opendir(_path.c_str());
291 struct dirent* entry = readdir(dp);
293 break; // done iteration
296 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
308 bool Dir::exists() const
310 return _path.isDir();
313 SGPath Dir::file(const std::string& name) const
315 SGPath childPath = _path;
316 childPath.set_cached(true);
317 childPath.append(name);
322 # define sgMkDir(d,m) _mkdir(d)
324 # define sgMkDir(d,m) mkdir(d,m)
327 bool Dir::create(mode_t mode)
330 return false; // already exists
333 // recursively create parent directories
335 if (!pr.path().isNull() && !pr.exists()) {
336 bool ok = pr.create(mode);
342 // finally, create ourselves
343 int err = sgMkDir(_path.c_str(), mode);
345 SG_LOG(SG_IO, SG_WARN, "directory creation failed: (" << _path.str() << ") " << strerror(errno) );
351 bool Dir::removeChildren() const
354 PathList cs = children(NO_DOT_OR_DOTDOT | INCLUDE_HIDDEN | TYPE_FILE | TYPE_DIR);
355 BOOST_FOREACH(SGPath path, cs) {
358 ok = childDir.remove(true);
364 SG_LOG(SG_IO, SG_WARN, "failed to remove:" << path);
367 } // of child iteration
372 bool Dir::remove(bool recursive)
375 SG_LOG(SG_IO, SG_WARN, "attempt to remove non-existant dir:" << _path);
380 if (!removeChildren()) {
383 } // of recursive deletion
386 int err = _rmdir(_path.c_str());
388 int err = rmdir(_path.c_str());
391 SG_LOG(SG_IO, SG_WARN, "rmdir failed:" << _path.str() << ":" << strerror(errno));
396 Dir Dir::parent() const
398 return Dir(_path.dir());
401 } // of namespace simgear