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>
47 #include <algorithm> // for std::sort
55 _removeOnDestroy(false)
59 Dir::Dir(const SGPath& path) :
61 _removeOnDestroy(false)
63 _path.set_cached(false); // disable caching, so create/remove work
66 Dir::Dir(const Dir& rel, const SGPath& relPath) :
67 _path(rel.file(relPath.str())),
68 _removeOnDestroy(false)
70 _path.set_cached(false); // disable caching, so create/remove work
75 if (_removeOnDestroy) {
80 void Dir::setRemoveOnDestroy()
82 _removeOnDestroy = true;
88 char* buf = _getcwd(NULL, 0);
90 char* buf = ::getcwd(NULL, 0);
97 Dir Dir::tempDir(const std::string& templ)
101 const char* tempPath = ::getenv("TMPDIR");
107 // Mac OS-X / BSD manual says any number of 'X's, but GLibc manual
108 // says exactly six, so that's what I'm going with
110 ::snprintf(buf, 1024, "%s", p.c_str());
112 SG_LOG(SG_IO, SG_WARN, "mkdtemp failed:" << strerror(errno));
116 return Dir(SGPath(buf));
118 SGPath p(tempnam(0, templ.c_str()));
120 if (!t.create(0700)) {
121 SG_LOG(SG_IO, SG_WARN, "failed to create temporary directory at " << p.str());
128 static bool pathSortPredicate(const SGPath& p1, const SGPath& p2)
130 return p1.file() < p2.file();
133 PathList Dir::children(int types, const std::string& nameFilter) const
137 types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
141 std::string search(_path.str());
142 if (nameFilter.empty()) {
143 search += "\\*"; // everything
145 search += "\\*" + nameFilter;
148 WIN32_FIND_DATA fData;
149 HANDLE find = FindFirstFile(search.c_str(), &fData);
150 if (find == INVALID_HANDLE_VALUE) {
151 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << _path.str());
156 for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
157 if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
158 if (!(types & INCLUDE_HIDDEN)) {
163 if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
164 if (types & NO_DOT_OR_DOTDOT) {
165 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
170 if (!(types & TYPE_DIR)) {
173 } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
174 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
176 continue; // always ignore device files
177 } else if (!(types & TYPE_FILE)) {
181 result.push_back(file(fData.cFileName));
186 DIR* dp = opendir(_path.c_str());
188 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
192 int filterLen = nameFilter.size();
195 struct dirent* entry = readdir(dp);
197 break; // done iteration
200 // skip hidden files (names beginning with '.') unless requested
201 if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.') &&
202 strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
206 SGPath f = file(entry->d_name);
208 continue; // stat() failed
212 // directory handling
213 if (!(types & TYPE_DIR)) {
217 if (types & NO_DOT_OR_DOTDOT) {
218 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
222 } else if (f.isFile()) {
223 // regular file handling
224 if (!(types & TYPE_FILE)) {
228 // block device /fifo/char file, ignore
232 if (!nameFilter.empty()) {
233 int nameLen = strlen(entry->d_name);
234 if (nameLen < filterLen) {
235 continue; // name is shorter than the filter
238 char* nameSuffix = entry->d_name + (nameLen - filterLen);
239 if (strcmp(nameSuffix, nameFilter.c_str())) {
244 // passed all criteria, add to our result vector
245 result.push_back(file(entry->d_name));
251 // File system order is random. Make things deterministic,
252 // so it's the same for every user.
253 std::sort(result.begin(), result.end(), pathSortPredicate);
258 bool Dir::exists() const
260 return _path.isDir();
263 SGPath Dir::file(const std::string& name) const
265 SGPath childPath = _path;
266 childPath.set_cached(true);
267 childPath.append(name);
272 # define sgMkDir(d,m) _mkdir(d)
274 # define sgMkDir(d,m) mkdir(d,m)
277 bool Dir::create(mode_t mode)
280 return false; // already exists
283 // recursively create parent directories
285 if (!pr.path().isNull() && !pr.exists()) {
286 bool ok = pr.create(mode);
292 // finally, create ourselves
293 int err = sgMkDir(_path.c_str(), mode);
295 SG_LOG(SG_IO, SG_WARN, "directory creation failed: (" << _path.str() << ") " << strerror(errno) );
301 bool Dir::remove(bool recursive)
304 SG_LOG(SG_IO, SG_WARN, "attempt to remove non-existant dir:" << _path.str());
310 PathList cs = children(NO_DOT_OR_DOTDOT | INCLUDE_HIDDEN | TYPE_FILE | TYPE_DIR);
311 BOOST_FOREACH(SGPath path, cs) {
314 ok = childDir.remove(true);
322 } // of child iteration
323 } // of recursive deletion
326 int err = _rmdir(_path.c_str());
328 int err = rmdir(_path.c_str());
331 SG_LOG(SG_IO, SG_WARN, "rmdir failed:" << _path.str() << ":" << strerror(errno));
336 Dir Dir::parent() const
338 return Dir(_path.dir());
341 } // of namespace simgear