QTemporaryFile(Name): don't make the path absolute on generation

I need to use QTemporaryFileName in a context where absolute paths are
not allowed because they change the behavior of the system call (the
-at() POSIX system calls); see next commit. This required a fix to a
seemingly unrelated test, which depended on the absolute path, because
QPluginLoader and QLibrary assume a file name with no path components
imply "search the standard places".

[ChangeLog][Important Behavior Changes][QTemporaryFile] This class will
now return relative file paths in fileName() if the file template was
also a relative path (it used to always return an absolute path). The
temporary files are still created in the same directory; this change
only affects the length of the path the function returns.

Change-Id: I79e700614d034281bf55fffd178f65f2b3d602d8
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Thiago Macieira 2023-10-18 20:54:37 -07:00
parent 84e70976f3
commit 935562a77b
2 changed files with 5 additions and 4 deletions

View File

@ -70,9 +70,8 @@ QTemporaryFileName::QTemporaryFileName(const QString &templateName)
qfilename.append(".XXXXXX"_L1);
// "Nativify" :-)
QFileSystemEntry::NativePath filename = QFileSystemEngine::absoluteName(
QFileSystemEntry(qfilename, QFileSystemEntry::FromInternalPath()))
.nativeFilePath();
QFileSystemEntry::NativePath filename =
QFileSystemEntry(QDir::cleanPath(qfilename)).nativeFilePath();
// Find mask in native path
phPos = filename.size();

View File

@ -16,6 +16,8 @@
# include <QtCore/private/qmachparser_p.h>
#endif
using namespace Qt::StringLiterals;
// Helper macros to let us know if some suffixes are valid
#define bundle_VALID false
#define dylib_VALID false
@ -128,7 +130,7 @@ static std::unique_ptr<QTemporaryFile> patchElf(const QString &source, ElfPatche
const char *basename = QTest::currentDataTag();
if (!basename)
basename = QTest::currentTestFunction();
tmplib.reset(new QTemporaryFile(basename + QString(".XXXXXX" SUFFIX)));
tmplib.reset(new QTemporaryFile(QDir::currentPath() + u'/' + basename + u".XXXXXX" SUFFIX ""_s));
QVERIFY2(tmplib->open(), qPrintable(tmplib->errorString()));
// sanity-check