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>
54 Dir::Dir(const SGPath& path) :
57 _path.set_cached(false); // disable caching, so create/remove work
60 Dir::Dir(const Dir& rel, const SGPath& relPath) :
61 _path(rel.file(relPath.str()))
63 _path.set_cached(false); // disable caching, so create/remove work
69 char* buf = _getcwd(NULL, 0);
71 char* buf = ::getcwd(NULL, 0);
78 Dir Dir::tempDir(const std::string& templ)
82 char* tempPath = ::getenv("TMPDIR");
86 // Mac OS-X / BSD manual says any number of 'X's, but GLibc manual
87 // says exactly six, so that's what I'm going with
88 ::snprintf(buf, 1024, "%s%s-XXXXXX", tempPath, templ.c_str());
90 SG_LOG(SG_IO, SG_WARN, "mkdtemp failed:" << strerror(errno));
94 return Dir(SGPath(buf));
96 SGPath p(tempnam(0, templ.c_str()));
98 if (!t.create(0700)) {
99 SG_LOG(SG_IO, SG_WARN, "failed to create temporary directory at " << p.str());
106 PathList Dir::children(int types, const std::string& nameFilter) const
110 types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
114 std::string search(_path.str());
115 if (nameFilter.empty()) {
116 search += "\\*"; // everything
118 search += "\\*" + nameFilter;
121 WIN32_FIND_DATA fData;
122 HANDLE find = FindFirstFile(search.c_str(), &fData);
123 if (find == INVALID_HANDLE_VALUE) {
124 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << _path.str());
129 for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
130 if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
131 if (!(types & INCLUDE_HIDDEN)) {
136 if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
137 if (types & NO_DOT_OR_DOTDOT) {
138 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
143 if (!(types & TYPE_DIR)) {
146 } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
147 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
149 continue; // always ignore device files
150 } else if (!(types & TYPE_FILE)) {
154 result.push_back(file(fData.cFileName));
159 DIR* dp = opendir(_path.c_str());
161 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
165 int filterLen = nameFilter.size();
168 struct dirent* entry = readdir(dp);
170 break; // done iteration
173 // skip hidden files (names beginning with '.') unless requested
174 if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.') &&
175 strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
179 SGPath f = file(entry->d_name);
181 continue; // stat() failed
185 // directory handling
186 if (!(types & TYPE_DIR)) {
190 if (types & NO_DOT_OR_DOTDOT) {
191 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
195 } else if (f.isFile()) {
196 // regular file handling
197 if (!(types & TYPE_FILE)) {
201 // block device /fifo/char file, ignore
205 if (!nameFilter.empty()) {
206 int nameLen = strlen(entry->d_name);
207 if (nameLen < filterLen) {
208 continue; // name is shorter than the filter
211 char* nameSuffix = entry->d_name + (nameLen - filterLen);
212 if (strcmp(nameSuffix, nameFilter.c_str())) {
217 // passed all criteria, add to our result vector
218 result.push_back(file(entry->d_name));
226 bool Dir::exists() const
228 return _path.isDir();
231 SGPath Dir::file(const std::string& name) const
233 SGPath childPath = _path;
234 childPath.set_cached(true);
235 childPath.append(name);
240 # define sgMkDir(d,m) _mkdir(d)
242 # define sgMkDir(d,m) mkdir(d,m)
245 bool Dir::create(mode_t mode)
248 return false; // already exists
251 // recursively create parent directories
254 bool ok = pr.create(mode);
260 // finally, create ourselves
261 int err = sgMkDir(_path.c_str(), mode);
263 SG_LOG(SG_IO, SG_WARN, "directory creation failed: (" << _path.str() << ") " << strerror(errno) );
269 bool Dir::remove(bool recursive)
272 SG_LOG(SG_IO, SG_WARN, "attempt to remove non-existant dir:" << _path.str());
278 PathList cs = children(NO_DOT_OR_DOTDOT | INCLUDE_HIDDEN | TYPE_FILE | TYPE_DIR);
279 BOOST_FOREACH(SGPath path, cs) {
282 ok = childDir.remove(true);
290 } // of child iteration
291 } // of recursive deletion
294 int err = _rmdir(_path.c_str());
296 int err = rmdir(_path.c_str());
299 SG_LOG(SG_IO, SG_WARN, "rmdir failed:" << _path.str() << ":" << strerror(errno));
304 Dir Dir::parent() const
306 return Dir(_path.dir());
309 } // of namespace simgear