# HG changeset patch # User thomas_-_s # Date 1226499171 0 # Node ID 8b7c134c2de9984c55d0e773c114ba8640e7a6fd # Parent 6c3a829bc3822af388a98b6a1abdf375cba2c5ea 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 diff -r 6c3a829bc382 -r 8b7c134c2de9 src/SettingsPaths.cpp --- 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 +#include +#include +#include + #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 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; +} diff -r 6c3a829bc382 -r 8b7c134c2de9 src/SettingsPaths.h --- 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(); diff -r 6c3a829bc382 -r 8b7c134c2de9 src/UiGuiSettings.cpp --- 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;