diff --git a/src/corelib/io/io.pri b/src/corelib/io/io.pri index c4c6f41387..a33ffe75f2 100644 --- a/src/corelib/io/io.pri +++ b/src/corelib/io/io.pri @@ -183,7 +183,9 @@ win32 { SOURCES += io/qstorageinfo_mac.cpp qtConfig(processenvironment): \ OBJECTIVE_SOURCES += io/qprocess_darwin.mm - OBJECTIVE_SOURCES += io/qstandardpaths_mac.mm + OBJECTIVE_SOURCES += \ + io/qstandardpaths_mac.mm \ + io/qfilesystemengine_mac.mm osx { LIBS += -framework DiskArbitration -framework IOKit } else { diff --git a/src/corelib/io/qfilesystemengine_mac.mm b/src/corelib/io/qfilesystemengine_mac.mm new file mode 100644 index 0000000000..4bbce9e5f6 --- /dev/null +++ b/src/corelib/io/qfilesystemengine_mac.mm @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qplatformdefs.h" +#include "qfilesystemengine_p.h" +#include "qfile.h" +#include "qurl.h" + +#include +#include + +QT_BEGIN_NAMESPACE + +/* + This implementation does not enable the "put back" option in Finder + for the trashed object. The only way to get this is to use Finder automation, + which would query the user for permission to access Finder using a modal, + blocking dialog - which we definitely can't have in a console application. + + Using Finder would also play the trash sound, which we don't want either in + such a core API; applications that want that can play the sound themselves. +*/ +//static +bool QFileSystemEngine::moveFileToTrash(const QFileSystemEntry &source, + QFileSystemEntry &newLocation, QSystemError &error) +{ + QFileInfo info(source.filePath()); + @autoreleasepool { + NSString *filepath = info.filePath().toNSString(); + NSURL *fileurl = [NSURL fileURLWithPath:filepath isDirectory:info.isDir()]; + NSURL *resultingUrl = nil; + NSError *nserror = nil; + NSFileManager *fm = [NSFileManager defaultManager]; + if ([fm trashItemAtURL:fileurl resultingItemURL:&resultingUrl error:&nserror] != YES) { + error = QSystemError(nserror.code, QSystemError::NativeError); + return false; + } + newLocation = QFileSystemEntry(QUrl::fromNSURL(resultingUrl).path()); + } + return true; +} + +QT_END_NAMESPACE diff --git a/src/corelib/io/qfilesystemengine_p.h b/src/corelib/io/qfilesystemengine_p.h index ecfdc03743..555483a972 100644 --- a/src/corelib/io/qfilesystemengine_p.h +++ b/src/corelib/io/qfilesystemengine_p.h @@ -88,7 +88,7 @@ inline bool qIsFilenameBroken(const QFileSystemEntry &entry) Q_RETURN_ON_INVALID_FILENAME("Broken filename passed to function", (result)); \ } while (false) -class QFileSystemEngine +class Q_AUTOTEST_EXPORT QFileSystemEngine { public: static bool isCaseSensitive() @@ -155,6 +155,7 @@ public: static bool createLink(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error); static bool copyFile(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error); + static bool moveFileToTrash(const QFileSystemEntry &source, QFileSystemEntry &newLocation, QSystemError &error); static bool renameFile(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error); static bool renameOverwriteFile(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error); static bool removeFile(const QFileSystemEntry &entry, QSystemError &error); diff --git a/src/corelib/io/qfilesystemengine_unix.cpp b/src/corelib/io/qfilesystemengine_unix.cpp index 3bbebc7fe9..16901be187 100644 --- a/src/corelib/io/qfilesystemengine_unix.cpp +++ b/src/corelib/io/qfilesystemengine_unix.cpp @@ -42,6 +42,8 @@ #include "qplatformdefs.h" #include "qfilesystemengine_p.h" #include "qfile.h" +#include "qstorageinfo.h" +#include "qtextstream.h" #include #include @@ -1197,6 +1199,197 @@ bool QFileSystemEngine::createLink(const QFileSystemEntry &source, const QFileSy return false; } +#ifndef Q_OS_DARWIN +/* + Implementing as per https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html +*/ +static QString freeDesktopTrashLocation(const QString &sourcePath) +{ + auto makeTrashDir = [](const QDir &topDir, const QString &trashDir) -> QString { + auto ownerPerms = QFileDevice::ReadOwner + | QFileDevice::WriteOwner + | QFileDevice::ExeOwner; + QString targetDir = topDir.filePath(trashDir); + if (topDir.mkdir(trashDir)) + QFile::setPermissions(targetDir, ownerPerms); + if (QFileInfo(targetDir).isDir()) + return targetDir; + return QString(); + }; + auto isSticky = [](const QFileInfo &fileInfo) -> bool { + struct stat st; + if (stat(QFile::encodeName(fileInfo.absoluteFilePath()).constData(), &st) == 0) + return st.st_mode & S_ISVTX; + + return false; + }; + + QString trash; + const QLatin1String dotTrash(".Trash"); + const QStorageInfo sourceStorage(sourcePath); + const QStorageInfo homeStorage(QDir::home()); + // We support trashing of files outside the users home partition + if (sourceStorage != homeStorage) { + QDir topDir(sourceStorage.rootPath()); + /* + Method 1: + "An administrator can create an $topdir/.Trash directory. The permissions on this + directories should permit all users who can trash files at all to write in it; + and the “sticky bit” in the permissions must be set, if the file system supports + it. + When trashing a file from a non-home partition/device, an implementation + (if it supports trashing in top directories) MUST check for the presence + of $topdir/.Trash." + */ + const QString userID = QString::number(::getuid()); + if (topDir.cd(dotTrash)) { + const QFileInfo trashInfo(topDir.path()); + + // we MUST check that the sticky bit is set, and that it is not a symlink + if (trashInfo.isSymLink()) { + // we SHOULD report the failed check to the administrator + qCritical("Warning: '%s' is a symlink to '%s'", + trashInfo.absoluteFilePath().toLocal8Bit().constData(), + trashInfo.symLinkTarget().toLatin1().constData()); + } else if (!isSticky(trashInfo)) { + // we SHOULD report the failed check to the administrator + qCritical("Warning: '%s' doesn't have sticky bit set!", + trashInfo.absoluteFilePath().toLocal8Bit().constData()); + } else if (trashInfo.isDir()) { + /* + "If the directory exists and passes the checks, a subdirectory of the + $topdir/.Trash directory is to be used as the user's trash directory + for this partition/device. The name of this subdirectory is the numeric + identifier of the current user ($topdir/.Trash/$uid). + When trashing a file, if this directory does not exist for the current user, + the implementation MUST immediately create it, without any warnings or + delays for the user." + */ + trash = makeTrashDir(topDir, userID); + } + } + /* + Method 2: + "If an $topdir/.Trash directory is absent, an $topdir/.Trash-$uid directory is to be + used as the user's trash directory for this device/partition. [...] When trashing a + file, if an $topdir/.Trash-$uid directory does not exist, the implementation MUST + immediately create it, without any warnings or delays for the user." + */ + if (trash.isEmpty()) { + topDir = QDir(sourceStorage.rootPath()); + const QString userTrashDir = dotTrash + QLatin1Char('-') + userID; + trash = makeTrashDir(topDir, userTrashDir); + } + } + /* + "If both (1) and (2) fail [...], the implementation MUST either trash the + file into the user's “home trash” or refuse to trash it." + + We trash the file into the user's home trash. + */ + if (trash.isEmpty()) { + QDir topDir = QDir::home(); + trash = makeTrashDir(topDir, dotTrash); + if (!QFileInfo(trash).isDir()) { + qWarning("Unable to establish trash directory %s in %s", + dotTrash.latin1(), topDir.path().toLocal8Bit().constData()); + } + } + + return trash; +} + +//static +bool QFileSystemEngine::moveFileToTrash(const QFileSystemEntry &source, + QFileSystemEntry &newLocation, QSystemError &error) +{ + const QFileInfo sourceInfo(source.filePath()); + if (!sourceInfo.exists()) { + error = QSystemError(ENOENT, QSystemError::StandardLibraryError); + return false; + } + const QString sourcePath = sourceInfo.absoluteFilePath(); + + QDir trashDir(freeDesktopTrashLocation(sourcePath)); + if (!trashDir.exists()) + return false; + /* + "A trash directory contains two subdirectories, named info and files." + */ + const QLatin1String filesDir("files"); + const QLatin1String infoDir("info"); + trashDir.mkdir(filesDir); + int savedErrno = errno; + trashDir.mkdir(infoDir); + if (!savedErrno) + savedErrno = errno; + if (!trashDir.exists(filesDir) || !trashDir.exists(infoDir)) { + error = QSystemError(savedErrno, QSystemError::StandardLibraryError); + return false; + } + /* + "The $trash/files directory contains the files and directories that were trashed. + The names of files in this directory are to be determined by the implementation; + the only limitation is that they must be unique within the directory. Even if a + 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 = QLatin1Char('/') + trashedName; + QString infoFileName; + int counter = 0; + QFile infoFile; + do { + while (QFile::exists(trashDir.filePath(filesDir) + uniqueTrashedName)) { + ++counter; + uniqueTrashedName = QString(QLatin1String("/%1-%2")) + .arg(trashedName) + .arg(counter, 4, 10, QLatin1Char('0')); + } + /* + "The $trash/info directory contains an "information file" for every file and directory + in $trash/files. This file MUST have exactly the same name as the file or directory in + $trash/files, plus the extension ".trashinfo" + [...] + When trashing a file or directory, the implementation MUST create the corresponding + file in $trash/info first. Moreover, it MUST try to do this in an atomic fashion, + so that if two processes try to trash files with the same filename this will result + in two different trash files. On Unix-like systems this is done by generating a + filename, and then opening with O_EXCL. If that succeeds the creation was atomic + (at least on the same machine), if it fails you need to pick another filename." + */ + infoFileName = trashDir.filePath(infoDir) + + uniqueTrashedName + QLatin1String(".trashinfo"); + infoFile.setFileName(infoFileName); + } while (!infoFile.open(QIODevice::NewOnly | QIODevice::WriteOnly | QIODevice::Text)); + + const QString targetPath = trashDir.filePath(filesDir) + uniqueTrashedName; + const QFileSystemEntry target(targetPath); + + if (!renameFile(source, target, error)) { + infoFile.close(); + infoFile.remove(); + error = QSystemError(errno, QSystemError::StandardLibraryError); + return false; + } + + QTextStream out(&infoFile); +#if QT_CONFIG(textcodec) + out.setCodec("UTF-8"); +#endif + out << "[Trash Info]" << Qt::endl; + out << "Path=" << sourcePath << Qt::endl; + out << "DeletionDate=" + << QDateTime::currentDateTime().toString(QLatin1String("yyyy-MM-ddThh:mm:ss")) << Qt::endl; + infoFile.close(); + + newLocation = QFileSystemEntry(targetPath); + return true; +} +#endif + //static bool QFileSystemEngine::copyFile(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error) { diff --git a/src/corelib/io/qfilesystemengine_win.cpp b/src/corelib/io/qfilesystemengine_win.cpp index 0579872f8d..71a0e36693 100644 --- a/src/corelib/io/qfilesystemengine_win.cpp +++ b/src/corelib/io/qfilesystemengine_win.cpp @@ -41,6 +41,7 @@ #include "qoperatingsystemversion.h" #include "qplatformdefs.h" #include "qsysinfo.h" +#include "qscopeguard.h" #include "private/qabstractfileengine_p.h" #include "private/qfsfileengine_p.h" #include @@ -59,6 +60,8 @@ #include #ifndef Q_OS_WINRT # include +# include +# include # include # include #endif @@ -422,6 +425,104 @@ static inline bool getFindData(QString path, WIN32_FIND_DATA &findData) return false; } +#if defined(__IFileOperation_INTERFACE_DEFINED__) +class FileOperationProgressSink : public IFileOperationProgressSink +{ +public: + FileOperationProgressSink() + : ref(1) + {} + virtual ~FileOperationProgressSink() {} + + ULONG STDMETHODCALLTYPE AddRef() + { + return ++ref; + } + ULONG STDMETHODCALLTYPE Release() + { + if (--ref == 0) { + delete this; + return 0; + } + return ref; + } + HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject) + { + if (!ppvObject) + return E_POINTER; + + *ppvObject = nullptr; + + if (iid == __uuidof(IUnknown)) { + *ppvObject = static_cast(this); + } else if (iid == __uuidof(IFileOperationProgressSink)) { + *ppvObject = static_cast(this); + } + + if (*ppvObject) { + AddRef(); + return S_OK; + } + + return E_NOINTERFACE; + } + + HRESULT STDMETHODCALLTYPE StartOperations() + { return S_OK; } + HRESULT STDMETHODCALLTYPE FinishOperations(HRESULT) + { return S_OK; } + HRESULT STDMETHODCALLTYPE PreRenameItem(DWORD, IShellItem *, LPCWSTR) + { return S_OK; } + HRESULT STDMETHODCALLTYPE PostRenameItem(DWORD, IShellItem *, LPCWSTR, HRESULT, IShellItem *) + { return S_OK; } + HRESULT STDMETHODCALLTYPE PreMoveItem(DWORD, IShellItem *, IShellItem *, LPCWSTR) + { return S_OK; } + HRESULT STDMETHODCALLTYPE PostMoveItem(DWORD, IShellItem *, IShellItem *, LPCWSTR, HRESULT, + IShellItem *) + { return S_OK; } + HRESULT STDMETHODCALLTYPE PreCopyItem(DWORD, IShellItem *, IShellItem *, LPCWSTR ) + { return S_OK; } + HRESULT STDMETHODCALLTYPE PostCopyItem(DWORD, IShellItem *, IShellItem *, LPCWSTR, HRESULT, + IShellItem *) + { return S_OK; } + HRESULT STDMETHODCALLTYPE PreDeleteItem(DWORD dwFlags, IShellItem *) + { + // stop the operation if the file will be deleted rather than trashed + return (dwFlags & TSF_DELETE_RECYCLE_IF_POSSIBLE) ? S_OK : E_FAIL; + } + HRESULT STDMETHODCALLTYPE PostDeleteItem(DWORD /* dwFlags */, IShellItem * /* psiItem */, + HRESULT /* hrDelete */, IShellItem *psiNewlyCreated) + { + if (psiNewlyCreated) { + wchar_t *pszName = nullptr; + psiNewlyCreated->GetDisplayName(SIGDN_FILESYSPATH, &pszName); + if (pszName) { + targetPath = QString::fromWCharArray(pszName); + CoTaskMemFree(pszName); + } + } + return S_OK; + } + HRESULT STDMETHODCALLTYPE PreNewItem(DWORD, IShellItem *, LPCWSTR) + { return S_OK; } + HRESULT STDMETHODCALLTYPE PostNewItem(DWORD, IShellItem *, LPCWSTR, LPCWSTR, DWORD, HRESULT, + IShellItem *) + { return S_OK; } + HRESULT STDMETHODCALLTYPE UpdateProgress(UINT,UINT) + { return S_OK; } + HRESULT STDMETHODCALLTYPE ResetTimer() + { return S_OK; } + HRESULT STDMETHODCALLTYPE PauseTimer() + { return S_OK; } + HRESULT STDMETHODCALLTYPE ResumeTimer() + { return S_OK; } + + QString targetPath; +private: + ULONG ref; +}; +#endif + bool QFileSystemEngine::uncListSharesOnServer(const QString &server, QStringList *list) { DWORD res = ERROR_NOT_SUPPORTED; @@ -1431,6 +1532,97 @@ bool QFileSystemEngine::removeFile(const QFileSystemEntry &entry, QSystemError & return ret; } +/* + If possible, we use the IFileOperation implementation, which allows us to determine + the location of the object in the trash. + If not (likely on mingw), we fall back to the old API, which won't allow us to know + that. +*/ +//static +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()); + +# if defined(__IFileOperation_INTERFACE_DEFINED__) + CoInitialize(NULL); + IFileOperation *pfo = nullptr; + IShellItem *deleteItem = nullptr; + FileOperationProgressSink *sink = nullptr; + HRESULT hres = E_FAIL; + + auto coUninitialize = qScopeGuard([&](){ + if (sink) + sink->Release(); + if (deleteItem) + deleteItem->Release(); + if (pfo) + pfo->Release(); + CoUninitialize(); + if (!SUCCEEDED(hres)) + error = QSystemError(hres, QSystemError::NativeError); + }); + + hres = CoCreateInstance(CLSID_FileOperation, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&pfo)); + if (!pfo) + return false; + pfo->SetOperationFlags(FOF_ALLOWUNDO | FOFX_RECYCLEONDELETE | FOF_NOCONFIRMATION + | FOF_SILENT | FOF_NOERRORUI); + hres = SHCreateItemFromParsingName(reinterpret_cast(sourcePath.utf16()), + nullptr, IID_PPV_ARGS(&deleteItem)); + if (!deleteItem) + return false; + sink = new FileOperationProgressSink; + hres = pfo->DeleteItem(deleteItem, static_cast(sink)); + if (!SUCCEEDED(hres)) + return false; + hres = pfo->PerformOperations(); + if (!SUCCEEDED(hres)) + return false; + newLocation = QFileSystemEntry(sink->targetPath); + +# else // no IFileOperation in SDK (mingw, likely) - fall back to SHFileOperation + + // double null termination needed, so can't use QString::utf16 + QVarLengthArray winFile(sourcePath.length() + 2); + sourcePath.toWCharArray(winFile.data()); + winFile[sourcePath.length()] = wchar_t{}; + winFile[sourcePath.length() + 1] = wchar_t{}; + + SHFILEOPSTRUCTW operation; + operation.hwnd = nullptr; + operation.wFunc = FO_DELETE; + operation.pFrom = winFile.constData(); + operation.pTo = nullptr; + operation.fFlags = FOF_ALLOWUNDO | FOF_NO_UI; + operation.fAnyOperationsAborted = FALSE; + operation.hNameMappings = nullptr; + operation.lpszProgressTitle = nullptr; + + int result = SHFileOperation(&operation); + if (result != 0) { + error = QSystemError(result, QSystemError::NativeError); + return false; + } + /* + This implementation doesn't let us know where the file ended up, even if + we would specify FOF_WANTMAPPINGHANDLE | FOF_RENAMEONCOLLISION, as + FOF_RENAMEONCOLLISION has no effect unless files are moved, copied, or renamed. + */ + Q_UNUSED(newLocation); +# endif // IFileOperation fallback + return true; + +#else // Q_OS_WINRT + Q_UNUSED(source); + Q_UNUSED(newLocation); + Q_UNUSED(error); + return false; +#endif +} + //static bool QFileSystemEngine::setPermissions(const QFileSystemEntry &entry, QFile::Permissions permissions, QSystemError &error, QFileSystemMetaData *data) diff --git a/tests/auto/corelib/io/qfilesystemengine/qfilesystemengine.pro b/tests/auto/corelib/io/qfilesystemengine/qfilesystemengine.pro new file mode 100644 index 0000000000..ed59b48d5e --- /dev/null +++ b/tests/auto/corelib/io/qfilesystemengine/qfilesystemengine.pro @@ -0,0 +1,6 @@ +CONFIG += testcase +TARGET = tst_qfilesystemengine +QT = core-private testlib +SOURCES = tst_qfilesystemengine.cpp \ + $$QT_SOURCE_TREE/src/corelib/io/qfilesystementry.cpp +HEADERS = $$QT_SOURCE_TREE/src/corelib/io/qfilesystementry_p.h diff --git a/tests/auto/corelib/io/qfilesystemengine/tst_qfilesystemengine.cpp b/tests/auto/corelib/io/qfilesystemengine/tst_qfilesystemengine.cpp new file mode 100644 index 0000000000..b64852057a --- /dev/null +++ b/tests/auto/corelib/io/qfilesystemengine/tst_qfilesystemengine.cpp @@ -0,0 +1,168 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include + +#include +#include + +class tst_QFileSystemEngine : public QObject +{ + Q_OBJECT + +private slots: + void cleanupTestCase(); + void moveToTrash_data(); + void moveToTrash(); + +private: + QStringList createdEntries; +}; + +void tst_QFileSystemEngine::cleanupTestCase() +{ + for (QString entry : createdEntries) { + QFileInfo entryInfo(entry); + if (!entryInfo.exists()) + continue; + QDir entryDir(entry); + if (entryInfo.isDir()) { + if (!entryDir.removeRecursively()) + qWarning("Failed to remove trashed dir '%s'", entry.toLocal8Bit().constData()); + } else if (!QFile::remove(entry)) { + qWarning("Failed to remove trashed file '%s'", entry.toLocal8Bit().constData()); + } + } +} + +void tst_QFileSystemEngine::moveToTrash_data() +{ + QTest::addColumn("filePath"); + QTest::addColumn("create"); + QTest::addColumn("success"); + + { + QTemporaryFile tempFile; + tempFile.open(); + QTest::newRow("temporary file") + << tempFile.fileName() + << true << true; + } + { + QTemporaryDir tempDir; + tempDir.setAutoRemove(false); + QTest::newRow("temporary dir") + << tempDir.path() + QLatin1Char('/') + << true << true; + } + { + QTemporaryDir homeDir(QFileSystemEngine::homePath() + QLatin1String("/XXXXXX")); + homeDir.setAutoRemove(false); + QTemporaryFile homeFile(homeDir.path() + + QLatin1String("/tst_qfilesystemengine-XXXXXX")); + homeFile.open(); + QTest::newRow("home file") + << homeFile.fileName() + << true << true; + + QTest::newRow("home dir") + << homeDir.path() + QLatin1Char('/') + << true << true; + } + + QTest::newRow("unmovable") + << QFileSystemEngine::rootPath() + << false << false; + QTest::newRow("no such file") + << QString::fromLatin1("no/such/file") + << false << false; +} + +void tst_QFileSystemEngine::moveToTrash() +{ + QFETCH(QString, filePath); + QFETCH(bool, create); + QFETCH(bool, success); + +#if defined(Q_OS_WINRT) + QSKIP("WinRT does not have a trash", SkipAll); +#endif + + if (create && !QFileInfo::exists(filePath)) { + createdEntries << filePath; + if (filePath.endsWith(QLatin1Char('/'))) { + QDir temp(QFileSystemEngine::rootPath()); + temp.mkdir(filePath); + QFile file(filePath + QLatin1String("test")); + if (!file.open(QIODevice::WriteOnly)) + QSKIP("Couldn't create directory with file"); + } else { + QFile file(filePath); + if (!file.open(QIODevice::WriteOnly)) + QSKIP("Couldn't open file for writing"); + } + QVERIFY(QFileInfo::exists(filePath)); + } + + QFileSystemEntry entry(filePath); + QFileSystemEntry newLocation; + QSystemError error; + + bool existed = QFileInfo::exists(filePath); + bool result = QFileSystemEngine::moveFileToTrash(entry, newLocation, error); + QCOMPARE(result, success); + if (result) { + QCOMPARE(error.error(), 0); + QVERIFY(existed != QFileInfo::exists(filePath)); + const QString newPath = newLocation.filePath(); +#if defined(Q_OS_WIN) + // one of the Windows code paths doesn't provide the location of the object in the trash + if (newPath.isEmpty()) + QEXPECT_FAIL("", "Qt built without IFileOperations support on Windows!", Continue); +#endif + QVERIFY(!newPath.isEmpty()); + if (!newPath.isEmpty()) { + createdEntries << newPath; + QFileInfo trashInfo(newPath); + QVERIFY(trashInfo.exists()); +#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN) + QString infoFile = trashInfo.absolutePath() + QLatin1String("/../info/") + + trashInfo.fileName() + QLatin1String(".trashinfo"); + createdEntries << infoFile; +#endif + } + } else { + QVERIFY(error.error() != 0); + QCOMPARE(existed, QFileInfo::exists(filePath)); + } +} + +QTEST_MAIN(tst_QFileSystemEngine) +#include