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>
28 # define WIN32_LEAN_AND_MEAN
32 # include <sys/types.h>
34 # include <sys/stat.h>
39 #include <simgear/debug/logstream.hxx>
40 #include <boost/foreach.hpp>
51 _removeOnDestroy(false)
55 Dir::Dir(const SGPath& path) :
57 _removeOnDestroy(false)
59 _path.set_cached(false); // disable caching, so create/remove work
62 Dir::Dir(const Dir& rel, const SGPath& relPath) :
63 _path(rel.file(relPath.str())),
64 _removeOnDestroy(false)
66 _path.set_cached(false); // disable caching, so create/remove work
71 if (_removeOnDestroy) {
76 void Dir::setRemoveOnDestroy()
78 _removeOnDestroy = true;
84 char* buf = _getcwd(NULL, 0);
86 char* buf = ::getcwd(NULL, 0);
93 Dir Dir::tempDir(const std::string& templ)
97 const char* tempPath = ::getenv("TMPDIR");
101 // Mac OS-X / BSD manual says any number of 'X's, but GLibc manual
102 // says exactly six, so that's what I'm going with
103 ::snprintf(buf, 1024, "%s%s-XXXXXX", tempPath, templ.c_str());
105 SG_LOG(SG_IO, SG_WARN, "mkdtemp failed:" << strerror(errno));
109 return Dir(SGPath(buf));
111 SGPath p(tempnam(0, templ.c_str()));
113 if (!t.create(0700)) {
114 SG_LOG(SG_IO, SG_WARN, "failed to create temporary directory at " << p.str());
121 PathList Dir::children(int types, const std::string& nameFilter) const
125 types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
129 std::string search(_path.str());
130 if (nameFilter.empty()) {
131 search += "\\*"; // everything
133 search += "\\*" + nameFilter;
136 WIN32_FIND_DATA fData;
137 HANDLE find = FindFirstFile(search.c_str(), &fData);
138 if (find == INVALID_HANDLE_VALUE) {
139 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << _path.str());
144 for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
145 if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
146 if (!(types & INCLUDE_HIDDEN)) {
151 if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
152 if (types & NO_DOT_OR_DOTDOT) {
153 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
158 if (!(types & TYPE_DIR)) {
161 } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
162 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
164 continue; // always ignore device files
165 } else if (!(types & TYPE_FILE)) {
169 result.push_back(file(fData.cFileName));
174 DIR* dp = opendir(_path.c_str());
176 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
180 int filterLen = nameFilter.size();
183 struct dirent* entry = readdir(dp);
185 break; // done iteration
188 // skip hidden files (names beginning with '.') unless requested
189 if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.') &&
190 strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
194 SGPath f = file(entry->d_name);
196 continue; // stat() failed
200 // directory handling
201 if (!(types & TYPE_DIR)) {
205 if (types & NO_DOT_OR_DOTDOT) {
206 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
210 } else if (f.isFile()) {
211 // regular file handling
212 if (!(types & TYPE_FILE)) {
216 // block device /fifo/char file, ignore
220 if (!nameFilter.empty()) {
221 int nameLen = strlen(entry->d_name);
222 if (nameLen < filterLen) {
223 continue; // name is shorter than the filter
226 char* nameSuffix = entry->d_name + (nameLen - filterLen);
227 if (strcmp(nameSuffix, nameFilter.c_str())) {
232 // passed all criteria, add to our result vector
233 result.push_back(file(entry->d_name));
241 bool Dir::exists() const
243 return _path.isDir();
246 SGPath Dir::file(const std::string& name) const
248 SGPath childPath = _path;
249 childPath.set_cached(true);
250 childPath.append(name);
255 # define sgMkDir(d,m) _mkdir(d)
257 # define sgMkDir(d,m) mkdir(d,m)
260 bool Dir::create(mode_t mode)
263 return false; // already exists
266 // recursively create parent directories
269 bool ok = pr.create(mode);
275 // finally, create ourselves
276 int err = sgMkDir(_path.c_str(), mode);
278 SG_LOG(SG_IO, SG_WARN, "directory creation failed: (" << _path.str() << ") " << strerror(errno) );
284 bool Dir::remove(bool recursive)
287 SG_LOG(SG_IO, SG_WARN, "attempt to remove non-existant dir:" << _path.str());
293 PathList cs = children(NO_DOT_OR_DOTDOT | INCLUDE_HIDDEN | TYPE_FILE | TYPE_DIR);
294 BOOST_FOREACH(SGPath path, cs) {
297 ok = childDir.remove(true);
305 } // of child iteration
306 } // of recursive deletion
309 int err = _rmdir(_path.c_str());
311 int err = rmdir(_path.c_str());
314 SG_LOG(SG_IO, SG_WARN, "rmdir failed:" << _path.str() << ":" << strerror(errno));
319 Dir Dir::parent() const
321 return Dir(_path.dir());
324 } // of namespace simgear