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>
This commit is contained in:
Thiago Macieira 2023-09-21 18:14:27 -07:00
parent fa97531952
commit 61d99530c8

View File

@ -1298,9 +1298,15 @@ bool QFileSystemEngine::moveFileToTrash(const QFileSystemEntry &source,
error = QSystemError(ENOENT, QSystemError::StandardLibraryError); error = QSystemError(ENOENT, QSystemError::StandardLibraryError);
return false; 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()) if (!trashDir.exists())
return false; 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 file with the same name and location gets trashed many times, each subsequent
trashing must not overwrite a previous copy." trashing must not overwrite a previous copy."
*/ */
const QString trashedName = sourceInfo.isDir() QString uniqueTrashedName = u'/' + sourcePath.fileName();
? QDir(sourcePath).dirName()
: sourceInfo.fileName();
QString uniqueTrashedName = u'/' + trashedName;
QString infoFileName; QString infoFileName;
int counter = 0; int counter = 0;
QFile infoFile; QFile infoFile;
auto makeUniqueTrashedName = [trashedName, &counter]() -> QString { auto makeUniqueTrashedName = [sourcePath, &counter]() -> QString {
return QString::asprintf("/%ls-%04d", qUtf16Printable(trashedName), ++counter); return QString::asprintf("/%ls-%04d", qUtf16Printable(sourcePath.fileName()), ++counter);
}; };
do { do {
while (QFile::exists(trashDir.filePath(filesDir) + uniqueTrashedName)) while (QFile::exists(trashDir.filePath(filesDir) + uniqueTrashedName))
@ -1356,14 +1359,12 @@ bool QFileSystemEngine::moveFileToTrash(const QFileSystemEntry &source,
uniqueTrashedName = makeUniqueTrashedName(); uniqueTrashedName = makeUniqueTrashedName();
} while (!infoFile.isOpen()); } while (!infoFile.isOpen());
QString pathForInfo; QString pathForInfo = sourcePath.filePath();
const QStorageInfo storageInfo(sourcePath); const QStorageInfo storageInfo(pathForInfo);
if (storageInfo.isValid() && storageInfo.rootPath() != rootPath() && storageInfo != QStorageInfo(QDir::home())) { 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'/') if (pathForInfo.front() == u'/')
pathForInfo = pathForInfo.mid(1); pathForInfo = pathForInfo.mid(1);
} else {
pathForInfo = sourcePath;
} }
/* /*