]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/sg_dir.cxx
Update doxgen config and some comments.
[simgear.git] / simgear / misc / sg_dir.cxx
index 99ede3e4c6e6bd037194ac292f4171f3260281ef..5ffc10d67c29aa500e8f28d2552ee8550259bac9 100644 (file)
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#  include <simgear_config.h>
+#endif
 
 #include <simgear/misc/sg_dir.hxx>
+#include <math.h>
+#include <stdlib.h>
+#include <cstdio>
 
 #ifdef _WIN32
 #  define WIN32_LEAN_AND_MEAN
 #include <boost/foreach.hpp>
 
 #include <cstring>
+#include <cstdlib>
 #include <iostream>
+#include <algorithm> // for std::sort
 
 using std::string;
 
 namespace simgear
 {
 
+Dir::Dir() :
+    _removeOnDestroy(false)
+{
+}
+
 Dir::Dir(const SGPath& path) :
-  _path(path)
+  _path(path),
+  _removeOnDestroy(false)
 {
     _path.set_cached(false); // disable caching, so create/remove work
 }
 
 Dir::Dir(const Dir& rel, const SGPath& relPath) :
-  _path(rel.file(relPath.str()))
+  _path(rel.file(relPath.str())),
+  _removeOnDestroy(false)
 {
     _path.set_cached(false); // disable caching, so create/remove work
 }
 
+Dir::~Dir()
+{
+    if (_removeOnDestroy) {
+        remove(true);
+    }
+}
+
+void Dir::setRemoveOnDestroy()
+{
+    _removeOnDestroy = true;
+}
+
 Dir Dir::current()
 {
 #ifdef _WIN32
@@ -70,6 +97,25 @@ Dir Dir::current()
 
 Dir Dir::tempDir(const std::string& templ)
 {
+#ifdef HAVE_MKDTEMP
+    char buf[1024];
+    const char* tempPath = ::getenv("TMPDIR");
+    if (!tempPath) {
+        tempPath = "/tmp";
+    }
+    SGPath p(tempPath);
+    p.append(templ);
+    // Mac OS-X / BSD manual says any number of 'X's, but GLibc manual
+    // says exactly six, so that's what I'm going with
+    p.concat("-XXXXXX");
+    ::snprintf(buf, 1024, "%s", p.c_str());
+    if (!mkdtemp(buf)) {
+        SG_LOG(SG_IO, SG_WARN, "mkdtemp failed:" << strerror(errno));
+        return Dir();
+    }
+    
+    return Dir(SGPath(buf));
+#else
     SGPath p(tempnam(0, templ.c_str()));
     Dir t(p);
     if (!t.create(0700)) {
@@ -77,6 +123,12 @@ Dir Dir::tempDir(const std::string& templ)
     }
     
     return t;
+#endif
+}
+
+static bool pathSortPredicate(const SGPath& p1, const SGPath& p2)
+{
+  return p1.file() < p2.file();
 }
 
 PathList Dir::children(int types, const std::string& nameFilter) const
@@ -97,7 +149,11 @@ PathList Dir::children(int types, const std::string& nameFilter) const
   WIN32_FIND_DATA fData;
   HANDLE find = FindFirstFile(search.c_str(), &fData);
   if (find == INVALID_HANDLE_VALUE) {
-    SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << _path.str());
+         int err = GetLastError();
+         if (err != ERROR_FILE_NOT_FOUND) {
+               SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << 
+                       _path.str() << " with error:" << err);
+         }
     return result;
   }
   
@@ -196,9 +252,63 @@ PathList Dir::children(int types, const std::string& nameFilter) const
   
   closedir(dp);
 #endif
+
+  // File system order is random. Make things deterministic,
+  // so it's the same for every user.
+  std::sort(result.begin(), result.end(), pathSortPredicate);
+
   return result;
 }
 
+bool Dir::isEmpty() const
+{
+  bool empty= true;
+#ifdef _WIN32 
+  WIN32_FIND_DATA fData;
+  HANDLE find = FindFirstFile("\\*", &fData);
+  if (find == INVALID_HANDLE_VALUE) {
+    return true;
+  }
+  
+// since an empty dir will still have . and .. children, we need
+// watch for those - anything else means the dir is really non-empty
+  bool done = false;
+  for (; !done; done = (FindNextFile(find, &fData) == 0)) {
+    if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
+                 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
+                   continue;
+                 }
+         }
+    
+    empty = false;
+    break;
+  }
+    
+  FindClose(find);
+#else
+  DIR* dp = opendir(_path.c_str());
+  if (!dp) {
+    return true;
+  }
+    
+  while (true) {
+    struct dirent* entry = readdir(dp);
+    if (!entry) {
+      break; // done iteration
+    }
+    
+    if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
+      continue;
+    }
+      
+    empty = false;
+    break;
+  }
+  closedir(dp);
+#endif
+  return empty;
+}
+
 bool Dir::exists() const
 {
   return _path.isDir();
@@ -226,7 +336,7 @@ bool Dir::create(mode_t mode)
     
 // recursively create parent directories
     Dir pr(parent());
-    if (!pr.exists()) {
+    if (!pr.path().isNull() && !pr.exists()) {
         bool ok = pr.create(mode);
         if (!ok) {
             return false;
@@ -242,28 +352,38 @@ bool Dir::create(mode_t mode)
     return (err == 0);
 }
 
+bool Dir::removeChildren() const
+{
+    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) {
+            SG_LOG(SG_IO, SG_WARN, "failed to remove:" << path);
+            return false;
+        }
+    } // of child iteration
+    
+    return true;
+}
+
 bool Dir::remove(bool recursive)
 {
     if (!exists()) {
-        SG_LOG(SG_IO, SG_WARN, "attempt to remove non-existant dir:" << _path.str());
+        SG_LOG(SG_IO, SG_WARN, "attempt to remove non-existant dir:" << _path);
         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
+        if (!removeChildren()) {
+            return false;
+        }
     } // of recursive deletion
     
 #ifdef _WIN32