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");
103 // Mac OS-X / BSD manual says any number of 'X's, but GLibc manual
104 // says exactly six, so that's what I'm going with
106 ::snprintf(buf, 1024, "%s", p.c_str());
108 SG_LOG(SG_IO, SG_WARN, "mkdtemp failed:" << strerror(errno));
112 return Dir(SGPath(buf));
114 SGPath p(tempnam(0, templ.c_str()));
116 if (!t.create(0700)) {
117 SG_LOG(SG_IO, SG_WARN, "failed to create temporary directory at " << p.str());
124 PathList Dir::children(int types, const std::string& nameFilter) const
128 types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
132 std::string search(_path.str());
133 if (nameFilter.empty()) {
134 search += "\\*"; // everything
136 search += "\\*" + nameFilter;
139 WIN32_FIND_DATA fData;
140 HANDLE find = FindFirstFile(search.c_str(), &fData);
141 if (find == INVALID_HANDLE_VALUE) {
142 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << _path.str());
147 for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
148 if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
149 if (!(types & INCLUDE_HIDDEN)) {
154 if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
155 if (types & NO_DOT_OR_DOTDOT) {
156 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
161 if (!(types & TYPE_DIR)) {
164 } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
165 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
167 continue; // always ignore device files
168 } else if (!(types & TYPE_FILE)) {
172 result.push_back(file(fData.cFileName));
177 DIR* dp = opendir(_path.c_str());
179 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
183 int filterLen = nameFilter.size();
186 struct dirent* entry = readdir(dp);
188 break; // done iteration
191 // skip hidden files (names beginning with '.') unless requested
192 if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.') &&
193 strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
197 SGPath f = file(entry->d_name);
199 continue; // stat() failed
203 // directory handling
204 if (!(types & TYPE_DIR)) {
208 if (types & NO_DOT_OR_DOTDOT) {
209 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
213 } else if (f.isFile()) {
214 // regular file handling
215 if (!(types & TYPE_FILE)) {
219 // block device /fifo/char file, ignore
223 if (!nameFilter.empty()) {
224 int nameLen = strlen(entry->d_name);
225 if (nameLen < filterLen) {
226 continue; // name is shorter than the filter
229 char* nameSuffix = entry->d_name + (nameLen - filterLen);
230 if (strcmp(nameSuffix, nameFilter.c_str())) {
235 // passed all criteria, add to our result vector
236 result.push_back(file(entry->d_name));
244 bool Dir::exists() const
246 return _path.isDir();
249 SGPath Dir::file(const std::string& name) const
251 SGPath childPath = _path;
252 childPath.set_cached(true);
253 childPath.append(name);
258 # define sgMkDir(d,m) _mkdir(d)
260 # define sgMkDir(d,m) mkdir(d,m)
263 bool Dir::create(mode_t mode)
266 return false; // already exists
269 // recursively create parent directories
271 if (!pr.path().isNull() && !pr.exists()) {
272 bool ok = pr.create(mode);
278 // finally, create ourselves
279 int err = sgMkDir(_path.c_str(), mode);
281 SG_LOG(SG_IO, SG_WARN, "directory creation failed: (" << _path.str() << ") " << strerror(errno) );
287 bool Dir::remove(bool recursive)
290 SG_LOG(SG_IO, SG_WARN, "attempt to remove non-existant dir:" << _path.str());
296 PathList cs = children(NO_DOT_OR_DOTDOT | INCLUDE_HIDDEN | TYPE_FILE | TYPE_DIR);
297 BOOST_FOREACH(SGPath path, cs) {
300 ok = childDir.remove(true);
308 } // of child iteration
309 } // of recursive deletion
312 int err = _rmdir(_path.c_str());
314 int err = rmdir(_path.c_str());
317 SG_LOG(SG_IO, SG_WARN, "rmdir failed:" << _path.str() << ":" << strerror(errno));
322 Dir Dir::parent() const
324 return Dir(_path.dir());
327 } // of namespace simgear