]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_dir.cxx
Add default constructor to simgear::Dir
[simgear.git] / simgear / misc / sg_dir.cxx
1 // Written by James Turner, started July 2010.
2 //
3 // Copyright (C) 2010  Curtis L. Olson - http://www.flightgear.org/~curt
4 //
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.
9 //
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.
14 //
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.
18 //
19 // $Id$
20
21
22 #include <simgear/misc/sg_dir.hxx>
23
24 #ifdef _WIN32
25 #  define WIN32_LEAN_AND_MEAN
26 #  include <windows.h>
27 #  include <direct.h>
28 #else
29 #  include <sys/types.h>
30 #  include <dirent.h>
31 #  include <sys/stat.h>
32 #  include <unistd.h>
33 #  include <errno.h>
34 #endif
35
36 #include <simgear/debug/logstream.hxx>
37 #include <boost/foreach.hpp>
38
39 #include <cstring>
40 #include <iostream>
41
42 using std::string;
43
44 namespace simgear
45 {
46
47 Dir::Dir()
48 {
49 }
50
51 Dir::Dir(const SGPath& path) :
52   _path(path)
53 {
54     _path.set_cached(false); // disable caching, so create/remove work
55 }
56
57 Dir::Dir(const Dir& rel, const SGPath& relPath) :
58   _path(rel.file(relPath.str()))
59 {
60     _path.set_cached(false); // disable caching, so create/remove work
61 }
62
63 Dir Dir::current()
64 {
65 #ifdef _WIN32
66     char* buf = _getcwd(NULL, 0);
67 #else
68     char* buf = ::getcwd(NULL, 0);
69 #endif
70     SGPath p(buf);
71     free(buf);
72     return Dir(p);
73 }
74
75 Dir Dir::tempDir(const std::string& templ)
76 {
77     SGPath p(tempnam(0, templ.c_str()));
78     Dir t(p);
79     if (!t.create(0700)) {
80         SG_LOG(SG_IO, SG_WARN, "failed to create temporary directory at " << p.str());
81     }
82     
83     return t;
84 }
85
86 PathList Dir::children(int types, const std::string& nameFilter) const
87 {
88   PathList result;
89   if (types == 0) {
90     types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
91   }
92   
93 #ifdef _WIN32
94   std::string search(_path.str());
95   if (nameFilter.empty()) {
96     search += "\\*"; // everything
97   } else {
98     search += "\\*" + nameFilter;
99   }
100   
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());
105     return result;
106   }
107   
108   bool done = false;
109   for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
110     if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
111       if (!(types & INCLUDE_HIDDEN)) {
112         continue;
113       }
114     }
115     
116     if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
117           if (types & NO_DOT_OR_DOTDOT) {
118                 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
119                   continue;
120                 }
121           }
122
123       if (!(types & TYPE_DIR)) {
124         continue;
125       }
126         } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
127                                 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
128         {
129                 continue; // always ignore device files
130     } else if (!(types & TYPE_FILE)) {
131        continue;
132     }
133
134     result.push_back(file(fData.cFileName));
135   }
136
137   FindClose(find);
138 #else
139   DIR* dp = opendir(_path.c_str());
140   if (!dp) {
141     SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
142     return result;
143   }
144   
145   int filterLen = nameFilter.size();
146   
147   while (true) {
148     struct dirent* entry = readdir(dp);
149     if (!entry) {
150       break; // done iteration
151     }
152     
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, "..")) {
156       continue;
157     }
158     
159     SGPath f = file(entry->d_name);
160     if (!f.exists()) {
161       continue; // stat() failed
162     }
163     
164     if (f.isDir()) {
165       // directory handling
166       if (!(types & TYPE_DIR)) {
167         continue;
168       }
169       
170       if (types & NO_DOT_OR_DOTDOT) {
171         if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
172           continue;
173         }
174       }
175     } else if (f.isFile()) {
176       // regular file handling
177       if (!(types & TYPE_FILE)) {
178         continue;
179       }
180     } else {
181       // block device /fifo/char file, ignore
182       continue;
183     }
184
185     if (!nameFilter.empty()) {
186       int nameLen = strlen(entry->d_name);
187       if (nameLen < filterLen) {
188         continue; // name is shorter than the filter
189       }
190     
191       char* nameSuffix = entry->d_name + (nameLen - filterLen);
192       if (strcmp(nameSuffix, nameFilter.c_str())) {
193         continue;
194       }
195     }
196     
197   // passed all criteria, add to our result vector
198     result.push_back(file(entry->d_name));
199   }
200   
201   closedir(dp);
202 #endif
203   return result;
204 }
205
206 bool Dir::exists() const
207 {
208   return _path.isDir();
209 }
210
211 SGPath Dir::file(const std::string& name) const
212 {
213   SGPath childPath = _path;
214   childPath.set_cached(true);
215   childPath.append(name);
216   return childPath;  
217 }
218
219 #ifdef _WIN32
220 #  define sgMkDir(d,m)       _mkdir(d)
221 #else
222 #  define sgMkDir(d,m)       mkdir(d,m)
223 #endif
224
225 bool Dir::create(mode_t mode)
226 {
227     if (exists()) {
228         return false; // already exists
229     }
230     
231 // recursively create parent directories
232     Dir pr(parent());
233     if (!pr.exists()) {
234         bool ok = pr.create(mode);
235         if (!ok) {
236             return false;
237         }
238     }
239     
240 // finally, create ourselves
241     int err = sgMkDir(_path.c_str(), mode);
242     if (err) {
243         SG_LOG(SG_IO, SG_WARN,  "directory creation failed: (" << _path.str() << ") " << strerror(errno) );
244     }
245     
246     return (err == 0);
247 }
248
249 bool Dir::remove(bool recursive)
250 {
251     if (!exists()) {
252         SG_LOG(SG_IO, SG_WARN, "attempt to remove non-existant dir:" << _path.str());
253         return false;
254     }
255     
256     if (recursive) {
257         bool ok;
258         PathList cs = children(NO_DOT_OR_DOTDOT | INCLUDE_HIDDEN | TYPE_FILE | TYPE_DIR);
259         BOOST_FOREACH(SGPath path, cs) {
260             if (path.isDir()) {
261                 Dir childDir(path);
262                 ok = childDir.remove(true);
263             } else {
264                 ok = path.remove();
265             }
266             
267             if (!ok) {
268                 return false;
269             }
270         } // of child iteration
271     } // of recursive deletion
272     
273 #ifdef _WIN32
274     int err = _rmdir(_path.c_str());
275 #else
276     int err = rmdir(_path.c_str());
277 #endif
278     if (err) {
279         SG_LOG(SG_IO, SG_WARN, "rmdir failed:" << _path.str() << ":" << strerror(errno));
280     }
281     return (err == 0);
282 }
283
284 Dir Dir::parent() const
285 {
286     return Dir(_path.dir());
287 }
288
289 } // of namespace simgear