kdecore: do not prepend the temporary directory and main component name if template is absolute path from KTemporaryFile::filePath()

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-10-23 18:05:15 +03:00
parent 8224768f2a
commit b287467137
6 changed files with 54 additions and 38 deletions

View file

@ -100,8 +100,10 @@ QString KTemporaryFile::filePath(const QString &pathtemplate)
result.replace(xindex, 1, QChar::fromLatin1(tmpnamechars[KRandom::randomMax(52)]));
xindex = result.indexOf(xchar, xindex + 1);
}
if (!QDir::isAbsolutePath(result)) {
result.prepend(underscorechar);
result.prepend(KGlobal::mainComponent().componentName());
result.prepend(KGlobal::dirs()->saveLocation("tmp"));
}
return result;
}

View file

@ -138,8 +138,8 @@ public:
* If @p pathtemplate is empty the result will have 10 characters, the
* standard temporary directory prepended along with the main component
* name. Otherwise any 'X' in @p pathtemplate is replaced with random
* character, the standard temporary directory prepended along with the
* main component name.
* character. The standard temporary directory prepended along with the
* main component name unless @p pathtemplate is absolute path.
*
* @param pathtemplate The template to use when generating filepath.
*/

View file

@ -28,6 +28,7 @@ private Q_SLOTS:
void testNoDelete();
void testAutoDelete();
void testCreateSubDir();
void testCreateSubDir();
};
#endif
#endif // KTEMPDIRTEST_H

View file

@ -98,3 +98,15 @@ void KTemporaryFileTest::testKTemporaryFile()
//directories we have write access to?
}
void KTemporaryFileTest::testFilePath()
{
const QString tmpdir = KGlobal::dirs()->saveLocation("tmp");
QString tmpfilepath = KTemporaryFile::filePath();
QVERIFY(tmpfilepath.startsWith(tmpdir));
QVERIFY(!tmpfilepath.contains('X'));
tmpfilepath = KTemporaryFile::filePath("/foo/bar/XXXXX.tmp");
QVERIFY(tmpfilepath.startsWith(QLatin1String("/foo/bar/")));
QVERIFY(!tmpfilepath.contains('X'));
}

View file

@ -17,10 +17,11 @@ private slots:
void initTestCase();
void cleanupTestCase();
void testKTemporaryFile();
void testFilePath();
private:
QString kdeTempDir;
QString componentName;
};
#endif
#endif // KTEMPORARYFILETEST_H