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>
30 # define WIN32_LEAN_AND_MEAN
34 # include <sys/types.h>
36 # include <sys/stat.h>
41 #include <simgear/debug/logstream.hxx>
42 #include <boost/foreach.hpp>
54 _removeOnDestroy(false)
58 Dir::Dir(const SGPath& path) :
60 _removeOnDestroy(false)
62 _path.set_cached(false); // disable caching, so create/remove work
65 Dir::Dir(const Dir& rel, const SGPath& relPath) :
66 _path(rel.file(relPath.str())),
67 _removeOnDestroy(false)
69 _path.set_cached(false); // disable caching, so create/remove work
74 if (_removeOnDestroy) {
79 void Dir::setRemoveOnDestroy()
81 _removeOnDestroy = true;
87 char* buf = _getcwd(NULL, 0);
89 char* buf = ::getcwd(NULL, 0);
96 Dir Dir::tempDir(const std::string& templ)
100 const char* tempPath = ::getenv("TMPDIR");
106 // Mac OS-X / BSD manual says any number of 'X's, but GLibc manual
107 // says exactly six, so that's what I'm going with
109 ::snprintf(buf, 1024, "%s", p.c_str());
111 SG_LOG(SG_IO, SG_WARN, "mkdtemp failed:" << strerror(errno));
115 return Dir(SGPath(buf));
117 SGPath p(tempnam(0, templ.c_str()));
119 if (!t.create(0700)) {
120 SG_LOG(SG_IO, SG_WARN, "failed to create temporary directory at " << p.str());
127 PathList Dir::children(int types, const std::string& nameFilter) const
131 types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
135 std::string search(_path.str());
136 if (nameFilter.empty()) {
137 search += "\\*"; // everything
139 search += "\\*" + nameFilter;
142 WIN32_FIND_DATA fData;
143 HANDLE find = FindFirstFile(search.c_str(), &fData);
144 if (find == INVALID_HANDLE_VALUE) {
145 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << _path.str());
150 for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
151 if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
152 if (!(types & INCLUDE_HIDDEN)) {
157 if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
158 if (types & NO_DOT_OR_DOTDOT) {
159 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
164 if (!(types & TYPE_DIR)) {
167 } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
168 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
170 continue; // always ignore device files
171 } else if (!(types & TYPE_FILE)) {
175 result.push_back(file(fData.cFileName));
180 DIR* dp = opendir(_path.c_str());
182 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
186 int filterLen = nameFilter.size();
189 struct dirent* entry = readdir(dp);
191 break; // done iteration
194 // skip hidden files (names beginning with '.') unless requested
195 if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.') &&
196 strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
200 SGPath f = file(entry->d_name);
202 continue; // stat() failed
206 // directory handling
207 if (!(types & TYPE_DIR)) {
211 if (types & NO_DOT_OR_DOTDOT) {
212 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
216 } else if (f.isFile()) {
217 // regular file handling
218 if (!(types & TYPE_FILE)) {
222 // block device /fifo/char file, ignore
226 if (!nameFilter.empty()) {
227 int nameLen = strlen(entry->d_name);
228 if (nameLen < filterLen) {
229 continue; // name is shorter than the filter
232 char* nameSuffix = entry->d_name + (nameLen - filterLen);
233 if (strcmp(nameSuffix, nameFilter.c_str())) {
238 // passed all criteria, add to our result vector
239 result.push_back(file(entry->d_name));
247 bool Dir::exists() const
249 return _path.isDir();
252 SGPath Dir::file(const std::string& name) const
254 SGPath childPath = _path;
255 childPath.set_cached(true);
256 childPath.append(name);
261 # define sgMkDir(d,m) _mkdir(d)
263 # define sgMkDir(d,m) mkdir(d,m)
266 bool Dir::create(mode_t mode)
269 return false; // already exists
272 // recursively create parent directories
274 if (!pr.path().isNull() && !pr.exists()) {
275 bool ok = pr.create(mode);
281 // finally, create ourselves
282 int err = sgMkDir(_path.c_str(), mode);
284 SG_LOG(SG_IO, SG_WARN, "directory creation failed: (" << _path.str() << ") " << strerror(errno) );
290 bool Dir::remove(bool recursive)
293 SG_LOG(SG_IO, SG_WARN, "attempt to remove non-existant dir:" << _path.str());
299 PathList cs = children(NO_DOT_OR_DOTDOT | INCLUDE_HIDDEN | TYPE_FILE | TYPE_DIR);
300 BOOST_FOREACH(SGPath path, cs) {
303 ok = childDir.remove(true);
311 } // of child iteration
312 } // of recursive deletion
315 int err = _rmdir(_path.c_str());
317 int err = rmdir(_path.c_str());
320 SG_LOG(SG_IO, SG_WARN, "rmdir failed:" << _path.str() << ":" << strerror(errno));
325 Dir Dir::parent() const
327 return Dir(_path.dir());
330 } // of namespace simgear