changeset 642:8b7c134c2de9

No using a random temp directory on none Windows for security reason. Also deleting the created temp directory completely now. git-svn-id: svn://svn.code.sf.net/p/universalindent/code/trunk@901 59b1889a-e5ac-428c-b0c7-476e01d41282
author thomas_-_s <thomas_-_s@59b1889a-e5ac-428c-b0c7-476e01d41282>
date Wed, 12 Nov 2008 14:12:51 +0000
parents 6c3a829bc382
children d51f8724acb6
files src/SettingsPaths.cpp src/SettingsPaths.h src/UiGuiSettings.cpp
diffstat 3 files changed, 62 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/SettingsPaths.cpp	Tue Nov 11 15:52:14 2008 +0000
+++ b/src/SettingsPaths.cpp	Wed Nov 12 14:12:51 2008 +0000
@@ -18,6 +18,11 @@
  ***************************************************************************/
 
 
+#include <stdlib.h>
+#include <QDirIterator>
+#include <QStack>
+#include <QtDebug>
+
 #include "SettingsPaths.h"
 
 
@@ -123,9 +128,20 @@
         while ( tempPath.right(1) == "/" ) {
             tempPath.chop(1);
         }
-        tempPath = QDir::tempPath() + "/UniversalIndentGUI";
+        tempPath = tempPath + "/UniversalIndentGUI";
 
+#if defined(Q_OS_WIN32)
         dirCreator.mkpath( tempPath );
+#else
+        // On Unix based systems create a random temporary directory for security
+        // reasons. Otherwise an evil human being could create a symbolic link
+        // to an important existing file which gets overwritten when UiGUI writes
+        // into this normally temporary but linked file.
+        char *pathTemplate = new char[tempPath.length()+8];
+        pathTemplate = QString(tempPath + "-XXXXXX").toAscii().data();
+        pathTemplate = mkdtemp( pathTemplate );
+        tempPath = pathTemplate;
+#endif
     }
 
     alreadyInitialized = true;
@@ -196,3 +212,43 @@
     }
     return portableMode;
 }
+
+
+/*!
+    \brief Returns true if portable mode shall be used.
+ */
+void SettingsPaths::cleanAndRemoveTempDir() {
+    QDirIterator dirIterator(tempPath, QDirIterator::Subdirectories);
+    QStack<QString> directoryStack;
+    bool noErrorsOccurred = true;
+    
+    while ( dirIterator.hasNext() ) {
+        QString currentDirOrFile = dirIterator.next();
+        // If this dummy call isn't done here, calling "dirIterator.fileInfo().isDir()" later somehow fails.
+        dirIterator.fileInfo();
+
+        if ( !currentDirOrFile.isEmpty() && dirIterator.fileName() != "." && dirIterator.fileName() != ".." ) {
+            // There is a path on the stack but the current path doesn't start with that path.
+            // So we changed into another parent directory and the one on the stack can be deleted
+            // since it must be empty.
+            if ( !directoryStack.isEmpty() && !currentDirOrFile.startsWith(directoryStack.top()) ) {
+                QString dirToBeRemoved = directoryStack.pop();
+                noErrorsOccurred &= QDir(dirToBeRemoved).rmdir(dirToBeRemoved);
+                //qDebug() << "Removing Dir " << directoryStack.pop();
+            }
+            
+            // If the iterator currently points to a directory push it onto the stack.
+            if ( dirIterator.fileInfo().isDir() ) {
+                directoryStack.push( currentDirOrFile );
+                //qDebug() << "Pushing onto Stack " << currentDirOrFile;
+            }
+            // otherwise it must be a file, so delete it.
+            else {
+                noErrorsOccurred &= QFile::remove( currentDirOrFile );
+                //qDebug() << "Removing File " << currentDirOrFile;
+            }
+        }
+    }
+    noErrorsOccurred &= QDir(tempPath).rmdir(tempPath);
+    //qDebug() << "Removing tempPath " << tempPath;
+}
--- a/src/SettingsPaths.h	Tue Nov 11 15:52:14 2008 +0000
+++ b/src/SettingsPaths.h	Wed Nov 12 14:12:51 2008 +0000
@@ -35,6 +35,7 @@
     static const QString getIndenterPath();
     static const QString getTempPath();
     static bool getPortableMode();
+    static void cleanAndRemoveTempDir();
 
 private:
     SettingsPaths();
--- a/src/UiGuiSettings.cpp	Tue Nov 11 15:52:14 2008 +0000
+++ b/src/UiGuiSettings.cpp	Wed Nov 12 14:12:51 2008 +0000
@@ -58,8 +58,11 @@
     return instance;
 }
 
-
+/*!
+    \brief Deletes the existing instance of UiGuiSettings and removes the created temp dir.
+ */
 void UiGuiSettings::deleteInstance() {
+    SettingsPaths::cleanAndRemoveTempDir();
     if ( instance != NULL ) {
         delete instance;
         instance = NULL;