]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/sg_dir.cxx
Make tsync part of libSimGearCore when building shared libraries
[simgear.git] / simgear / misc / sg_dir.cxx
index e1a2610f3c42e12e3c33d04a604380aa1252285a..1514c9501a38497cd7a1080e078fc5ea2110753b 100644 (file)
@@ -18,6 +18,9 @@
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#  include <simgear_config.h>
+#endif
 
 #include <simgear/misc/sg_dir.hxx>
 
@@ -44,22 +47,37 @@ using std::string;
 namespace simgear
 {
 
-Dir::Dir()
+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
@@ -74,6 +92,22 @@ 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/";
+    }
+    // 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
+    ::snprintf(buf, 1024, "%s%s-XXXXXX", tempPath, templ.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)) {
@@ -81,6 +115,7 @@ Dir Dir::tempDir(const std::string& templ)
     }
     
     return t;
+#endif
 }
 
 PathList Dir::children(int types, const std::string& nameFilter) const
@@ -230,7 +265,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;