]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/sg_dir.cxx
SGPath/Dir extensions to ease file handling in TerraGear. Also a unit-test, shocking.
[simgear.git] / simgear / misc / sg_dir.cxx
index 9c5aa78a8f5b9ac5885f75f286a81f5bc0b7033d..99ede3e4c6e6bd037194ac292f4171f3260281ef 100644 (file)
 #ifdef _WIN32
 #  define WIN32_LEAN_AND_MEAN
 #  include <windows.h>
+#  include <direct.h>
 #else
 #  include <sys/types.h>
 #  include <dirent.h>
+#  include <sys/stat.h>
+#  include <unistd.h>
+#  include <errno.h>
 #endif
 
-#include <sys/stat.h>
-
 #include <simgear/debug/logstream.hxx>
+#include <boost/foreach.hpp>
 
 #include <cstring>
 #include <iostream>
 
+using std::string;
+
 namespace simgear
 {
 
 Dir::Dir(const SGPath& path) :
   _path(path)
 {
+    _path.set_cached(false); // disable caching, so create/remove work
 }
 
 Dir::Dir(const Dir& rel, const SGPath& relPath) :
   _path(rel.file(relPath.str()))
 {
+    _path.set_cached(false); // disable caching, so create/remove work
+}
+
+Dir Dir::current()
+{
+#ifdef _WIN32
+    char* buf = _getcwd(NULL, 0);
+#else
+    char* buf = ::getcwd(NULL, 0);
+#endif
+    SGPath p(buf);
+    free(buf);
+    return Dir(p);
+}
+
+Dir Dir::tempDir(const std::string& templ)
+{
+    SGPath p(tempnam(0, templ.c_str()));
+    Dir t(p);
+    if (!t.create(0700)) {
+        SG_LOG(SG_IO, SG_WARN, "failed to create temporary directory at " << p.str());
+    }
+    
+    return t;
 }
 
 PathList Dir::children(int types, const std::string& nameFilter) const
@@ -108,6 +138,8 @@ PathList Dir::children(int types, const std::string& nameFilter) const
     return result;
   }
   
+  int filterLen = nameFilter.size();
+  
   while (true) {
     struct dirent* entry = readdir(dp);
     if (!entry) {
@@ -115,29 +147,18 @@ PathList Dir::children(int types, const std::string& nameFilter) const
     }
     
     // skip hidden files (names beginning with '.') unless requested
-    if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.')) {
+    if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.') &&
+         strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
       continue;
     }
     
-    int type = entry->d_type;
-    if (type == DT_LNK) {
-      // find symlink target type using stat()
-      struct stat s;
-      if (stat(file(entry->d_name).c_str(), &s)) {
-        continue; // stat() failed
-      }
-      
-      if (S_ISDIR(s.st_mode)) {
-        type = DT_DIR;
-      } else if (S_ISREG(s.st_mode)) {
-        type = DT_REG;
-      } else {
-        // symlink to block/fifo/char file, ignore
-        continue;
-      }
-    } // of symlink look-through
+    SGPath f = file(entry->d_name);
+    if (!f.exists()) {
+      continue; // stat() failed
+    }
     
-    if (type == DT_DIR) {
+    if (f.isDir()) {
+      // directory handling
       if (!(types & TYPE_DIR)) {
         continue;
       }
@@ -147,16 +168,24 @@ PathList Dir::children(int types, const std::string& nameFilter) const
           continue;
         }
       }
-    } else if (type == DT_REG) {
+    } else if (f.isFile()) {
+      // regular file handling
       if (!(types & TYPE_FILE)) {
         continue;
       }
     } else {
-      continue; // ignore char/block devices, fifos, etc
+      // block device /fifo/char file, ignore
+      continue;
     }
-    
+
     if (!nameFilter.empty()) {
-      if (strstr(entry->d_name, nameFilter.c_str()) == NULL) {
+      int nameLen = strlen(entry->d_name);
+      if (nameLen < filterLen) {
+        continue; // name is shorter than the filter
+      }
+    
+      char* nameSuffix = entry->d_name + (nameLen - filterLen);
+      if (strcmp(nameSuffix, nameFilter.c_str())) {
         continue;
       }
     }
@@ -172,30 +201,85 @@ PathList Dir::children(int types, const std::string& nameFilter) const
 
 bool Dir::exists() const
 {
-#ifdef _WIN32
-  struct _stat buf ;
-
-  if (_stat (_path.c_str(), &buf ) < 0) {
-    return false;
-  }
-  
-  return ((S_IFDIR & buf.st_mode ) !=0);
-#else
-  struct stat buf ;
-
-  if (stat(_path.c_str(), &buf ) < 0) {
-    return false ;
-  }
-  
-  return ((S_ISDIR(buf.st_mode )) != 0);
-#endif
+  return _path.isDir();
 }
 
 SGPath Dir::file(const std::string& name) const
 {
   SGPath childPath = _path;
+  childPath.set_cached(true);
   childPath.append(name);
   return childPath;  
 }
 
+#ifdef _WIN32
+#  define sgMkDir(d,m)       _mkdir(d)
+#else
+#  define sgMkDir(d,m)       mkdir(d,m)
+#endif
+
+bool Dir::create(mode_t mode)
+{
+    if (exists()) {
+        return false; // already exists
+    }
+    
+// recursively create parent directories
+    Dir pr(parent());
+    if (!pr.exists()) {
+        bool ok = pr.create(mode);
+        if (!ok) {
+            return false;
+        }
+    }
+    
+// finally, create ourselves
+    int err = sgMkDir(_path.c_str(), mode);
+    if (err) {
+        SG_LOG(SG_IO, SG_WARN,  "directory creation failed: (" << _path.str() << ") " << strerror(errno) );
+    }
+    
+    return (err == 0);
+}
+
+bool Dir::remove(bool recursive)
+{
+    if (!exists()) {
+        SG_LOG(SG_IO, SG_WARN, "attempt to remove non-existant dir:" << _path.str());
+        return false;
+    }
+    
+    if (recursive) {
+        bool ok;
+        PathList cs = children(NO_DOT_OR_DOTDOT | INCLUDE_HIDDEN | TYPE_FILE | TYPE_DIR);
+        BOOST_FOREACH(SGPath path, cs) {
+            if (path.isDir()) {
+                Dir childDir(path);
+                ok = childDir.remove(true);
+            } else {
+                ok = path.remove();
+            }
+            
+            if (!ok) {
+                return false;
+            }
+        } // of child iteration
+    } // of recursive deletion
+    
+#ifdef _WIN32
+    int err = _rmdir(_path.c_str());
+#else
+    int err = rmdir(_path.c_str());
+#endif
+    if (err) {
+        SG_LOG(SG_IO, SG_WARN, "rmdir failed:" << _path.str() << ":" << strerror(errno));
+    }
+    return (err == 0);
+}
+
+Dir Dir::parent() const
+{
+    return Dir(_path.dir());
+}
+
 } // of namespace simgear