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/misc/sg_dir.hxx>
25 # define WIN32_LEAN_AND_MEAN
29 # include <sys/types.h>
31 # include <sys/stat.h>
36 #include <simgear/debug/logstream.hxx>
37 #include <boost/foreach.hpp>
51 Dir::Dir(const SGPath& path) :
54 _path.set_cached(false); // disable caching, so create/remove work
57 Dir::Dir(const Dir& rel, const SGPath& relPath) :
58 _path(rel.file(relPath.str()))
60 _path.set_cached(false); // disable caching, so create/remove work
66 char* buf = _getcwd(NULL, 0);
68 char* buf = ::getcwd(NULL, 0);
75 Dir Dir::tempDir(const std::string& templ)
77 SGPath p(tempnam(0, templ.c_str()));
79 if (!t.create(0700)) {
80 SG_LOG(SG_IO, SG_WARN, "failed to create temporary directory at " << p.str());
86 PathList Dir::children(int types, const std::string& nameFilter) const
90 types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
94 std::string search(_path.str());
95 if (nameFilter.empty()) {
96 search += "\\*"; // everything
98 search += "\\*" + nameFilter;
101 WIN32_FIND_DATA fData;
102 HANDLE find = FindFirstFile(search.c_str(), &fData);
103 if (find == INVALID_HANDLE_VALUE) {
104 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << _path.str());
109 for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
110 if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
111 if (!(types & INCLUDE_HIDDEN)) {
116 if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
117 if (types & NO_DOT_OR_DOTDOT) {
118 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
123 if (!(types & TYPE_DIR)) {
126 } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
127 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
129 continue; // always ignore device files
130 } else if (!(types & TYPE_FILE)) {
134 result.push_back(file(fData.cFileName));
139 DIR* dp = opendir(_path.c_str());
141 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
145 int filterLen = nameFilter.size();
148 struct dirent* entry = readdir(dp);
150 break; // done iteration
153 // skip hidden files (names beginning with '.') unless requested
154 if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.') &&
155 strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
159 SGPath f = file(entry->d_name);
161 continue; // stat() failed
165 // directory handling
166 if (!(types & TYPE_DIR)) {
170 if (types & NO_DOT_OR_DOTDOT) {
171 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
175 } else if (f.isFile()) {
176 // regular file handling
177 if (!(types & TYPE_FILE)) {
181 // block device /fifo/char file, ignore
185 if (!nameFilter.empty()) {
186 int nameLen = strlen(entry->d_name);
187 if (nameLen < filterLen) {
188 continue; // name is shorter than the filter
191 char* nameSuffix = entry->d_name + (nameLen - filterLen);
192 if (strcmp(nameSuffix, nameFilter.c_str())) {
197 // passed all criteria, add to our result vector
198 result.push_back(file(entry->d_name));
206 bool Dir::exists() const
208 return _path.isDir();
211 SGPath Dir::file(const std::string& name) const
213 SGPath childPath = _path;
214 childPath.set_cached(true);
215 childPath.append(name);
220 # define sgMkDir(d,m) _mkdir(d)
222 # define sgMkDir(d,m) mkdir(d,m)
225 bool Dir::create(mode_t mode)
228 return false; // already exists
231 // recursively create parent directories
234 bool ok = pr.create(mode);
240 // finally, create ourselves
241 int err = sgMkDir(_path.c_str(), mode);
243 SG_LOG(SG_IO, SG_WARN, "directory creation failed: (" << _path.str() << ") " << strerror(errno) );
249 bool Dir::remove(bool recursive)
252 SG_LOG(SG_IO, SG_WARN, "attempt to remove non-existant dir:" << _path.str());
258 PathList cs = children(NO_DOT_OR_DOTDOT | INCLUDE_HIDDEN | TYPE_FILE | TYPE_DIR);
259 BOOST_FOREACH(SGPath path, cs) {
262 ok = childDir.remove(true);
270 } // of child iteration
271 } // of recursive deletion
274 int err = _rmdir(_path.c_str());
276 int err = rmdir(_path.c_str());
279 SG_LOG(SG_IO, SG_WARN, "rmdir failed:" << _path.str() << ":" << strerror(errno));
284 Dir Dir::parent() const
286 return Dir(_path.dir());
289 } // of namespace simgear