From 4aba04d2ca265611b7d2f7a26210ed1e212fee93 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 21 Sep 2023 18:14:27 -0700 Subject: [PATCH] moveToTrash/Unix: avoid QFileInfo to get an absolute file name We know what engine we're using, so don't go the long way around via QFileInfo and QFSFileEngine to get back to QFileSystemEngine in order to calculate an absolute and clean path. Since we're doing that, we may as well use QFileSystemEntry's ability to give us the file name portion of this absolute path without having to go via QFileInfo and QDir again. We just need to make sure that a dir name isn't ending in a slash: absoluteName() would remove that for us, but only if the entry isn't already absolute and clean. Change-Id: I9d43e5b91eb142d6945cfffd17871389d359e750 Reviewed-by: Volker Hilsheimer (cherry picked from commit 61d99530c83acd7197bfc9574334f0964d13459a) Reviewed-by: Ahmad Samir --- src/corelib/io/qfilesystemengine_unix.cpp | 27 ++++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/corelib/io/qfilesystemengine_unix.cpp b/src/corelib/io/qfilesystemengine_unix.cpp index 1f80a45db49..d45e85884d7 100644 --- a/src/corelib/io/qfilesystemengine_unix.cpp +++ b/src/corelib/io/qfilesystemengine_unix.cpp @@ -1295,9 +1295,15 @@ bool QFileSystemEngine::moveFileToTrash(const QFileSystemEntry &source, error = QSystemError(ENOENT, QSystemError::StandardLibraryError); return false; } - const QString sourcePath = sourceInfo.absoluteFilePath(); + const QFileSystemEntry sourcePath = [&] { + if (QString path = source.filePath(); path.size() > 1 && path.endsWith(u'/')) { + path.chop(1); + return absoluteName(QFileSystemEntry(path)); + } + return absoluteName(source); + }(); - QDir trashDir(freeDesktopTrashLocation(sourcePath)); + QDir trashDir(freeDesktopTrashLocation(sourcePath.filePath())); if (!trashDir.exists()) return false; /* @@ -1321,15 +1327,12 @@ bool QFileSystemEngine::moveFileToTrash(const QFileSystemEntry &source, file with the same name and location gets trashed many times, each subsequent trashing must not overwrite a previous copy." */ - const QString trashedName = sourceInfo.isDir() - ? QDir(sourcePath).dirName() - : sourceInfo.fileName(); - QString uniqueTrashedName = u'/' + trashedName; + QString uniqueTrashedName = u'/' + sourcePath.fileName(); QString infoFileName; int counter = 0; QFile infoFile; - auto makeUniqueTrashedName = [trashedName, &counter]() -> QString { - return QString::asprintf("/%ls-%04d", qUtf16Printable(trashedName), ++counter); + auto makeUniqueTrashedName = [sourcePath, &counter]() -> QString { + return QString::asprintf("/%ls-%04d", qUtf16Printable(sourcePath.fileName()), ++counter); }; do { while (QFile::exists(trashDir.filePath(filesDir) + uniqueTrashedName)) @@ -1353,14 +1356,12 @@ bool QFileSystemEngine::moveFileToTrash(const QFileSystemEntry &source, uniqueTrashedName = makeUniqueTrashedName(); } while (!infoFile.isOpen()); - QString pathForInfo; - const QStorageInfo storageInfo(sourcePath); + QString pathForInfo = sourcePath.filePath(); + const QStorageInfo storageInfo(pathForInfo); if (storageInfo.isValid() && storageInfo.rootPath() != rootPath() && storageInfo != QStorageInfo(QDir::home())) { - pathForInfo = sourcePath.mid(storageInfo.rootPath().length()); + pathForInfo = std::move(pathForInfo).mid(storageInfo.rootPath().length()); if (pathForInfo.front() == u'/') pathForInfo = pathForInfo.mid(1); - } else { - pathForInfo = sourcePath; } /*