Commit c6f90e57 authored by Tobias Hunger's avatar Tobias Hunger Committed by Tim Jenssen

Utils: Introduce a TemporaryDirectory and TemporaryFile class

Both wrap the corresponding Qt class, but make sure all temporary files
or directories are created inside a "master temporary directory".

Change-Id: I55461be507c828c965224c02863ea5ed9bbf9498
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent e6017c40
......@@ -31,8 +31,10 @@
#include <extensionsystem/pluginmanager.h>
#include <extensionsystem/pluginspec.h>
#include <qtsingleapplication.h>
#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
#include <utils/temporarydirectory.h>
#include <QDebug>
#include <QDir>
......@@ -278,6 +280,8 @@ void loadFonts()
int main(int argc, char **argv)
{
Utils::TemporaryDirectory::setMasterTemporaryDirectory(QDir::tempPath() + "/QtCreator-XXXXXX");
const char *highDpiEnvironmentVariable = setHighDpiEnvironmentVariable();
QLoggingCategory::setFilterRules(QLatin1String("qtc.*.debug=false\nqtc.*.info=false"));
......@@ -345,11 +349,9 @@ int main(int argc, char **argv)
testOptionProvided = true;
}
}
QScopedPointer<QTemporaryDir> temporaryCleanSettingsDir;
QScopedPointer<Utils::TemporaryDirectory> temporaryCleanSettingsDir;
if (settingsPath.isEmpty() && testOptionProvided) {
const QString settingsPathTemplate = QDir::cleanPath(QDir::tempPath()
+ QString::fromLatin1("/qtc-test-settings-XXXXXX"));
temporaryCleanSettingsDir.reset(new QTemporaryDir(settingsPathTemplate));
temporaryCleanSettingsDir.reset(new Utils::TemporaryDirectory("qtc-test-settings"));
if (!temporaryCleanSettingsDir->isValid())
return 1;
settingsPath = temporaryCleanSettingsDir->path();
......
......@@ -26,7 +26,6 @@
#include "clangcodemodelconnectionclient.h"
#include <QCoreApplication>
#include <QTemporaryDir>
namespace ClangBackEnd {
......
......@@ -37,7 +37,6 @@
QT_BEGIN_NAMESPACE
class QProcess;
class QTemporaryDir;
QT_END_NAMESPACE
class Utf8String;
......
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "temporarydirectory.h"
#include "qtcassert.h"
#include <memory>
namespace Utils {
static std::unique_ptr<QTemporaryDir> m_masterTemporaryDir;
TemporaryDirectory::TemporaryDirectory(const QString &pattern) :
QTemporaryDir(m_masterTemporaryDir->path() + '/' + pattern)
{
QTC_CHECK(!QFileInfo(pattern).isAbsolute());
}
QTemporaryDir *TemporaryDirectory::masterTemporaryDirectory()
{
return m_masterTemporaryDir.get();
}
void TemporaryDirectory::setMasterTemporaryDirectory(const QString &pattern)
{
m_masterTemporaryDir = std::make_unique<QTemporaryDir>(pattern);
}
QString TemporaryDirectory::masterDirectoryPath()
{
return m_masterTemporaryDir->path();
}
} // namespace Utils
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "utils_global.h"
#include <QTemporaryDir>
namespace Utils {
class QTCREATOR_UTILS_EXPORT TemporaryDirectory : public QTemporaryDir
{
public:
explicit TemporaryDirectory(const QString &pattern);
static QTemporaryDir *masterTemporaryDirectory();
static void setMasterTemporaryDirectory(const QString &pattern);
static QString masterDirectoryPath();
};
} // namespace Utils
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "temporaryfile.h"
#include "temporarydirectory.h"
#include "qtcassert.h"
namespace Utils {
TemporaryFile::TemporaryFile(const QString &pattern) :
QTemporaryFile(TemporaryDirectory::masterTemporaryDirectory()->path() + '/' + pattern)
{
QTC_CHECK(!QFileInfo(pattern).isAbsolute());
}
} // namespace Utils
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "utils_global.h"
#include <QTemporaryFile>
namespace Utils {
class QTCREATOR_UTILS_EXPORT TemporaryFile : public QTemporaryFile
{
public:
explicit TemporaryFile(const QString &pattern);
};
} // namespace Utils
......@@ -31,6 +31,8 @@ SOURCES += $$PWD/environment.cpp \
$$PWD/settingsselector.cpp \
$$PWD/stringutils.cpp \
$$PWD/templateengine.cpp \
$$PWD/temporarydirectory.cpp \
$$PWD/temporaryfile.cpp \
$$PWD/textfieldcheckbox.cpp \
$$PWD/textfieldcombobox.cpp \
$$PWD/filesearch.cpp \
......@@ -128,6 +130,8 @@ HEADERS += \
$$PWD/shellcommandpage.h \
$$PWD/stringutils.h \
$$PWD/templateengine.h \
$$PWD/temporarydirectory.h \
$$PWD/temporaryfile.h \
$$PWD/textfieldcheckbox.h \
$$PWD/textfieldcombobox.h \
$$PWD/filesearch.h \
......
......@@ -214,6 +214,10 @@ Project {
"synchronousprocess.h",
"templateengine.cpp",
"templateengine.h",
"temporarydirectory.cpp",
"temporarydirectory.h",
"temporaryfile.cpp",
"temporaryfile.h",
"textfieldcheckbox.cpp",
"textfieldcheckbox.h",
"textfieldcombobox.cpp",
......
......@@ -41,13 +41,13 @@
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <utils/synchronousprocess.h>
#include <utils/temporaryfile.h>
#include <chrono>
#include <memory>
#include <QApplication>
#include <QDir>
#include <QTime>
#include <QTemporaryFile>
#include <QTcpServer>
#include <QTcpSocket>
......@@ -495,7 +495,7 @@ void AndroidRunnerWorker::asyncStart(const QString &intentName,
} else {
// Handling ping.
for (int i = 0; ; ++i) {
QTemporaryFile tmp(QDir::tempPath() + "/pingpong");
Utils::TemporaryFile tmp("pingpong");
tmp.open();
tmp.close();
......@@ -563,7 +563,7 @@ void AndroidRunnerWorker::handleRemoteDebuggerRunning()
m_socket->waitForBytesWritten();
m_socket->close();
} else {
QTemporaryFile tmp(QDir::tempPath() + "/pingpong");
Utils::TemporaryFile tmp("pingpong");
tmp.open();
runAdb(selector() << "push" << tmp.fileName() << m_pongFile);
......
......@@ -26,7 +26,6 @@
#pragma once
#include <QObject>
#include <QTemporaryDir>
namespace CppTools { namespace Tests { class TemporaryCopiedDir; } }
......
......@@ -56,6 +56,7 @@
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <utils/synchronousprocess.h>
#include <utils/temporarydirectory.h>
#include <QDir>
#include <QFileInfo>
......@@ -84,7 +85,8 @@ FormatTask format(FormatTask task)
case Command::FileProcessing: {
// Save text to temporary file
const QFileInfo fi(task.filePath);
Utils::TempFileSaver sourceFile(QDir::tempPath() + "/qtc_beautifier_XXXXXXXX."
Utils::TempFileSaver sourceFile(Utils::TemporaryDirectory::masterDirectoryPath()
+ "/qtc_beautifier_XXXXXXXX."
+ fi.suffix());
sourceFile.setAutoRemove(true);
sourceFile.write(task.sourceData.toUtf8());
......
......@@ -33,8 +33,7 @@
namespace ClangCodeModel {
namespace Internal {
UiHeaderOnDiskManager::UiHeaderOnDiskManager()
: m_temporaryDir(QDir::tempPath() + QStringLiteral("/qtc-clang-uiheader-XXXXXX"))
UiHeaderOnDiskManager::UiHeaderOnDiskManager() : m_temporaryDir("/qtc-clang-uiheader-XXXXXX")
{
QTC_CHECK(m_temporaryDir.isValid());
}
......
......@@ -26,7 +26,6 @@
#include "refactoringconnectionclient.h"
#include <QCoreApplication>
#include <QTemporaryDir>
namespace ClangBackEnd {
......
......@@ -55,9 +55,9 @@
#include <utils/algorithm.h>
#include <utils/hostosinfo.h>
#include <utils/temporarydirectory.h>
#include <QLoggingCategory>
#include <QTemporaryDir>
using namespace CppTools;
using namespace ProjectExplorer;
......@@ -513,7 +513,7 @@ void ClangStaticAnalyzerRunControl::start()
m_clangExecutable = executable;
// Create log dir
QTemporaryDir temporaryDir(QDir::tempPath() + QLatin1String("/qtc-clangstaticanalyzer-XXXXXX"));
Utils::TemporaryDirectory temporaryDir("qtc-clangstaticanalyzer-XXXXXX");
temporaryDir.setAutoRemove(false);
if (!temporaryDir.isValid()) {
const QString errorMessage
......
......@@ -29,12 +29,12 @@
#include <utils/qtcprocess.h>
#include <utils/synchronousprocess.h>
#include <utils/temporaryfile.h>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QLoggingCategory>
#include <QTemporaryFile>
static Q_LOGGING_CATEGORY(LOG, "qtc.clangstaticanalyzer.runner")
......@@ -165,7 +165,7 @@ QString ClangStaticAnalyzerRunner::createLogFile(const QString &filePath) const
const QString fileTemplate = m_clangLogFileDir
+ QLatin1String("/report-") + fileName + QLatin1String("-XXXXXX.plist");
QTemporaryFile temporaryFile;
Utils::TemporaryFile temporaryFile("clangstaticanalyzer");
temporaryFile.setAutoRemove(false);
temporaryFile.setFileTemplate(fileTemplate);
if (temporaryFile.open()) {
......
......@@ -39,7 +39,6 @@
#include <QEventLoop>
#include <QSignalSpy>
#include <QTemporaryDir>
#include <QTimer>
#include <QtTest>
......
......@@ -26,7 +26,6 @@
#pragma once
#include <QObject>
#include <QTemporaryDir>
namespace CppTools { namespace Tests { class TemporaryCopiedDir; } }
......
......@@ -56,6 +56,7 @@
#include <utils/algorithm.h>
#include <utils/mimetypes/mimedatabase.h>
#include <utils/synchronousprocess.h>
#include <utils/temporarydirectory.h>
#include <utils/parameteraction.h>
#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
......@@ -85,7 +86,6 @@
#include <QProcess>
#include <QRegExp>
#include <QSharedPointer>
#include <QTemporaryFile>
#include <QTextCodec>
#include <QtPlugin>
#include <QUrl>
......@@ -1119,8 +1119,8 @@ void ClearCasePlugin::diffActivity()
diffGraphical(pair.first, pair.second);
return;
}
rmdir(QDir::tempPath() + QLatin1String("/ccdiff/") + activity);
QDir(QDir::tempPath()).rmpath(QLatin1String("ccdiff/") + activity);
rmdir(Utils::TemporaryDirectory::masterDirectoryPath() + QLatin1String("/ccdiff/") + activity);
QDir(Utils::TemporaryDirectory::masterDirectoryPath()).rmpath(QLatin1String("ccdiff/") + activity);
m_diffPrefix = activity;
const FileVerIt fend = filever.end();
for (FileVerIt it = filever.begin(); it != fend; ++it) {
......
......@@ -55,6 +55,7 @@
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>
#include <utils/synchronousprocess.h>
#include <utils/temporarydirectory.h>
#include <QDateTime>
#include <QFile>
......@@ -62,7 +63,6 @@
#include <QMessageBox>
#include <QRegularExpression>
#include <QSet>
#include <QTemporaryDir>
using namespace ProjectExplorer;
......@@ -91,10 +91,9 @@ const Utils::FileName BuildDirManager::workDirectory() const
if (bdir.exists())
return bdir;
if (!m_tempDir) {
const QString path = QDir::tempPath() + QLatin1String("/qtc-cmake-XXXXXX");
m_tempDir.reset(new QTemporaryDir(path));
m_tempDir.reset(new Utils::TemporaryDirectory("qtc-cmake-XXXXXXXX"));
if (!m_tempDir->isValid())
emit errorOccured(tr("Failed to create temporary directory using template \"%1\".").arg(path));
emit errorOccured(tr("Failed to create temporary directory \"%1\".").arg(m_tempDir->path()));
}
return Utils::FileName::fromString(m_tempDir->path());
}
......
......@@ -29,9 +29,9 @@
#include "cmakeconfigitem.h"
#include <utils/fileutils.h>
#include <utils/temporarydirectory.h>
#include <QObject>
#include <QTemporaryDir>
#include <QTimer>
#include <functional>
......@@ -104,7 +104,7 @@ private:
void becameDirty();
CMakeBuildConfiguration *m_buildConfiguration = nullptr;
mutable std::unique_ptr<QTemporaryDir> m_tempDir = nullptr;
mutable std::unique_ptr<Utils::TemporaryDirectory> m_tempDir = nullptr;
mutable CMakeConfig m_cmakeCache;
QTimer m_reparseTimer;
......
......@@ -33,6 +33,7 @@
#include <QByteArray>
#include <QCryptographicHash>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLocalSocket>
......
......@@ -28,7 +28,6 @@
#include <utils/qtcprocess.h>
#include <QLoggingCategory>
#include <QTemporaryDir>
#include <QTimer>
#include <QVariantMap>
......
......@@ -31,7 +31,6 @@
#include <utils/qtcprocess.h>
#include <QSet>
#include <QTemporaryDir>
#include <QTimer>
#include <memory>
......
......@@ -46,12 +46,12 @@
#include <utils/fileutils.h>
#include <utils/mimetypes/mimedatabase.h>
#include <utils/qtcassert.h>
#include <utils/temporarydirectory.h>
#include <texteditor/texteditor.h>
#include <texteditor/textdocument.h>
#include <QtPlugin>
#include <QDebug>
#include <QDir>
#include <QAction>
#include <QApplication>
#include <QClipboard>
......@@ -335,7 +335,7 @@ static inline QString filePrefixFromTitle(const QString &title)
static inline QString tempFilePattern(const QString &prefix, const QString &extension)
{
// Get directory
QString pattern = QDir::tempPath();
QString pattern = Utils::TemporaryDirectory::masterDirectoryPath();
const QChar slash = QLatin1Char('/');
if (!pattern.endsWith(slash))
pattern.append(slash);
......
......@@ -28,7 +28,8 @@
#include <coreplugin/icore.h>
#include <QDir>
#include <utils/temporarydirectory.h>
#include <QSettings>
#include <QCoreApplication>
......@@ -39,7 +40,7 @@ static const char displayCountKeyC[] = "DisplayCount";
namespace CodePaster {
FileShareProtocolSettings::FileShareProtocolSettings() :
path(QDir::tempPath()), displayCount(10)
path(Utils::TemporaryDirectory::masterDirectoryPath()), displayCount(10)
{
}
......
......@@ -42,9 +42,9 @@
#include <cplusplus/LookupContext.h>
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <utils/temporarydirectory.h>
#include <QCoreApplication>
#include <QDir>
#include <QElapsedTimer>
#include <QRegularExpression>
......@@ -72,10 +72,9 @@ public:
WriteTaskFileForDiagnostics()
: m_processedDiagnostics(0)
{
const QString fileName = QDir::tempPath()
+ QLatin1String("/qtc_findErrorsIndexing.diagnostics.")
+ QDateTime::currentDateTime().toString(QLatin1String("yyMMdd_HHmm"))
+ QLatin1String(".tasks");
const QString fileName = Utils::TemporaryDirectory::masterDirectoryPath()
+ "/qtc_findErrorsIndexing.diagnostics."
+ QDateTime::currentDateTime().toString("yyMMdd_HHmm") + ".tasks";
m_file.setFileName(fileName);
Q_ASSERT(m_file.open(QIODevice::WriteOnly | QIODevice::Text));
......
......@@ -29,10 +29,10 @@
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <utils/temporarydirectory.h>
#include <QtTest>
#include <QDebug>
#include <QDir>
/*!
Tests for various parts of the code generation. Well, okay, currently it only
......@@ -527,7 +527,7 @@ void CppToolsPlugin::test_codegen_definition_middle_member()
"\n"
"}\n"
"\n"
"int y;\n").arg(QDir::tempPath()).toLatin1();
"int y;\n").arg(Utils::TemporaryDirectory::masterDirectoryPath()).toLatin1();
Document::Ptr sourceDocument = createDocumentAndFile(&temporaryDir, "file.cpp", sourceText, 4U);
QVERIFY(sourceDocument);
......
......@@ -33,6 +33,7 @@
#include <cpptools/cppprojectfile.h>
#include <projectexplorer/project.h>
#include <utils/algorithm.h>
#include <utils/temporarydirectory.h>
#include <cplusplus/CppDocument.h>
#include <cplusplus/Token.h>
......@@ -445,9 +446,10 @@ Dumper::Dumper(const CPlusPlus::Snapshot &globalSnapshot, const QString &logFile
QString logFileId_ = logFileId;
if (!logFileId_.isEmpty())
logFileId_.prepend(QLatin1Char('_'));
const QString logFileName = QDir::tempPath() + QString::fromLatin1("/qtc-codemodelinspection")
const QString logFileName = ::Utils::TemporaryDirectory::masterDirectoryPath()
+ "/qtc-codemodelinspection"
+ ideRevision_
+ QDateTime::currentDateTime().toString(QLatin1String("_yyMMdd_hhmmss"))
+ QDateTime::currentDateTime().toString("_yyMMdd_hhmmss")
+ logFileId_
+ QLatin1String(".txt");
......
......@@ -29,6 +29,7 @@
#include "cppfilesettingspage.h"
#include <utils/fileutils.h>
#include <utils/temporarydirectory.h>
#include <QDir>
#include <QtTest>
......@@ -45,7 +46,7 @@ static void createTempFile(const QString &fileName)