diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2023-09-21 18:14:27 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2023-10-06 18:43:30 -0700 |
commit | 61d99530c83acd7197bfc9574334f0964d13459a (patch) | |
tree | 63065df69ecdc53cc8961fdea0872c4489fbedd3 /src/corelib/io/qfilesystemengine_unix.cpp | |
parent | fa97531952dfd14573e6e57ced2dc3cd44943b59 (diff) |
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 <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/corelib/io/qfilesystemengine_unix.cpp')
-rw-r--r-- | src/corelib/io/qfilesystemengine_unix.cpp | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/src/corelib/io/qfilesystemengine_unix.cpp b/src/corelib/io/qfilesystemengine_unix.cpp index 4a5860d0b1..c67cfdb966 100644 --- a/src/corelib/io/qfilesystemengine_unix.cpp +++ b/src/corelib/io/qfilesystemengine_unix.cpp @@ -1298,9 +1298,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; /* @@ -1324,15 +1330,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)) @@ -1356,14 +1359,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; } /* |