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>
47 Dir::Dir(const SGPath& path) :
50 _path.set_cached(false); // disable caching, so create/remove work
53 Dir::Dir(const Dir& rel, const SGPath& relPath) :
54 _path(rel.file(relPath.str()))
56 _path.set_cached(false); // disable caching, so create/remove work
62 char* buf = _getcwd(NULL, 0);
64 char* buf = ::getcwd(NULL, 0);
71 Dir Dir::tempDir(const std::string& templ)
73 SGPath p(tempnam(0, templ.c_str()));
75 if (!t.create(0700)) {
76 SG_LOG(SG_IO, SG_WARN, "failed to create temporary directory at " << p.str());
82 PathList Dir::children(int types, const std::string& nameFilter) const
86 types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
90 std::string search(_path.str());
91 if (nameFilter.empty()) {
92 search += "\\*"; // everything
94 search += "\\*" + nameFilter;
97 WIN32_FIND_DATA fData;
98 HANDLE find = FindFirstFile(search.c_str(), &fData);
99 if (find == INVALID_HANDLE_VALUE) {
100 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << _path.str());
105 for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
106 if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
107 if (!(types & INCLUDE_HIDDEN)) {
112 if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
113 if (types & NO_DOT_OR_DOTDOT) {
114 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
119 if (!(types & TYPE_DIR)) {
122 } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
123 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
125 continue; // always ignore device files
126 } else if (!(types & TYPE_FILE)) {
130 result.push_back(file(fData.cFileName));
135 DIR* dp = opendir(_path.c_str());
137 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
141 int filterLen = nameFilter.size();
144 struct dirent* entry = readdir(dp);
146 break; // done iteration
149 // skip hidden files (names beginning with '.') unless requested
150 if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.') &&
151 strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
155 SGPath f = file(entry->d_name);
157 continue; // stat() failed
161 // directory handling
162 if (!(types & TYPE_DIR)) {
166 if (types & NO_DOT_OR_DOTDOT) {
167 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
171 } else if (f.isFile()) {
172 // regular file handling
173 if (!(types & TYPE_FILE)) {
177 // block device /fifo/char file, ignore
181 if (!nameFilter.empty()) {
182 int nameLen = strlen(entry->d_name);
183 if (nameLen < filterLen) {
184 continue; // name is shorter than the filter
187 char* nameSuffix = entry->d_name + (nameLen - filterLen);
188 if (strcmp(nameSuffix, nameFilter.c_str())) {
193 // passed all criteria, add to our result vector
194 result.push_back(file(entry->d_name));
202 bool Dir::exists() const
204 return _path.isDir();
207 SGPath Dir::file(const std::string& name) const
209 SGPath childPath = _path;
210 childPath.set_cached(true);
211 childPath.append(name);
216 # define sgMkDir(d,m) _mkdir(d)
218 # define sgMkDir(d,m) mkdir(d,m)
221 bool Dir::create(mode_t mode)
224 return false; // already exists
227 // recursively create parent directories
230 bool ok = pr.create(mode);
236 // finally, create ourselves
237 int err = sgMkDir(_path.c_str(), mode);
239 SG_LOG(SG_IO, SG_WARN, "directory creation failed: (" << _path.str() << ") " << strerror(errno) );
245 bool Dir::remove(bool recursive)
248 SG_LOG(SG_IO, SG_WARN, "attempt to remove non-existant dir:" << _path.str());
254 PathList cs = children(NO_DOT_OR_DOTDOT | INCLUDE_HIDDEN | TYPE_FILE | TYPE_DIR);
255 BOOST_FOREACH(SGPath path, cs) {
258 ok = childDir.remove(true);
266 } // of child iteration
267 } // of recursive deletion
270 int err = _rmdir(_path.c_str());
272 int err = rmdir(_path.c_str());
275 SG_LOG(SG_IO, SG_WARN, "rmdir failed:" << _path.str() << ":" << strerror(errno));
280 Dir Dir::parent() const
282 return Dir(_path.dir());
285 } // of namespace simgear