Skip to content

Commit

Permalink
QFile::moveToTrash: work with relative file paths on Windows
Browse files Browse the repository at this point in the history
The system APIs expect an absolute "display name" of the file path,
so make it absolute.

The test was overly tolerant in accepting failure, as a QStorageInfo
initialized with a file path that doesn't exist is invalid, and thus
always different from the QStorageInfo of the home directory. Fix the
test to compare only valid QStorageInfo objects, and postpone the check
until the file we want to move has been created.

[ChangeLog][QtCore][QFile] moveToTrash supports relative file paths
on Windows

Change-Id: I94c8cd40c60fde469e38f76a98f867f20c6a0b15
Fixes: QTBUG-84015
Pick-to: 5.15
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
  • Loading branch information
vohi committed May 7, 2020
1 parent ef0287d commit a033c23
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/corelib/io/qfilesystemengine_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1548,8 +1548,8 @@ bool QFileSystemEngine::moveFileToTrash(const QFileSystemEntry &source,
QFileSystemEntry &newLocation, QSystemError &error)
{
#ifndef Q_OS_WINRT
// we need the "display name" of the file, so can't use nativeFilePath
const QString sourcePath = QDir::toNativeSeparators(source.filePath());
// we need the "display name" of the file, so can't use nativeAbsoluteFilePath
const QString sourcePath = QDir::toNativeSeparators(absoluteName(source).filePath());

/*
Windows 7 insists on showing confirmation dialogs and ignores the respective
Expand Down
20 changes: 12 additions & 8 deletions tests/auto/corelib/io/qfile/tst_qfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3714,13 +3714,6 @@ void tst_QFile::moveToTrash()
QFETCH(bool, create);
QFETCH(bool, result);

/* This test makes assumptions about the file system layout
which might be wrong - moveToTrash may fail if the file lives
on a file system that is different from the home file system, and
has no .Trash directory.
*/
const bool mayFail = QStorageInfo(source) != QStorageInfo(QDir::home());

#if defined(Q_OS_WINRT)
QSKIP("WinRT does not have a trash", SkipAll);
#endif
Expand Down Expand Up @@ -3749,9 +3742,20 @@ void tst_QFile::moveToTrash()
sourceFile.remove();
}
};

ensureFile(source, create);

/* This test makes assumptions about the file system layout
which might be wrong - moveToTrash may fail if the file lives
on a file system that is different from the home file system, and
has no .Trash directory.
*/
const QStorageInfo sourceStorage(source);
const bool mayFail = sourceStorage.isValid()
&& QStorageInfo(source) != QStorageInfo(QDir::home());

// non-static version
{
ensureFile(source, create);
QFile sourceFile(source);
const bool success = sourceFile.moveToTrash();

Expand Down

0 comments on commit a033c23

Please sign in to comment.