Utils: Add FilePath::tmpDir and createTempFile

Change-Id: I6f3143e59a87edffeee5e08708ba721293a8a369
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2023-02-07 07:57:17 +01:00
parent 02777c4179
commit 516fce6f53
5 changed files with 162 additions and 0 deletions

View File

@@ -20,6 +20,7 @@
#include <QStringView>
#include <QUrl>
#include <QtGlobal>
#include <QTemporaryFile>
#ifdef Q_OS_WIN
#ifdef QTCREATOR_PCH_H
@@ -490,6 +491,40 @@ std::optional<FilePath> FilePath::refersToExecutableFile(MatchScope matchScope)
return fileAccess()->refersToExecutableFile(*this, matchScope);
}
expected_str<FilePath> FilePath::tmpDir() const
{
if (needsDevice()) {
const Environment env = deviceEnvironment();
if (env.hasKey("TMPDIR"))
return FilePath::fromUserInput(env.value("TMPDIR")).onDevice(*this);
if (env.hasKey("TEMP"))
return FilePath::fromUserInput(env.value("TEMP")).onDevice(*this);
if (env.hasKey("TMP"))
return FilePath::fromUserInput(env.value("TMP")).onDevice(*this);
if (osType() != OsTypeWindows)
return FilePath("/tmp").onDevice(*this);
return make_unexpected(QString("Could not find temporary directory on device %1")
.arg(displayName()));
}
return FilePath::fromUserInput(QDir::tempPath());
}
expected_str<FilePath> FilePath::createTempFile() const
{
if (!needsDevice()) {
QTemporaryFile file(toFSPathString());
file.setAutoRemove(false);
if (file.open())
return FilePath::fromString(file.fileName());
return make_unexpected(QString("Could not create temporary file: %1").arg(file.errorString()));
}
return fileAccess()->createTempFile(*this);
}
bool FilePath::isReadableFile() const
{
return fileAccess()->isReadableFile(*this);