2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2020-07-28 09:20:24 +00:00
|
|
|
** Copyright (C) 2020 The Qt Company Ltd.
|
2016-01-15 07:08:27 +00:00
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the QtCore module of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2012-09-19 12:28:29 +00:00
|
|
|
** 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
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 07:08:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2016-01-15 07:08:27 +00:00
|
|
|
** 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.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** 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.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "qfilesystemengine_p.h"
|
2016-09-09 18:21:29 +00:00
|
|
|
#include "qoperatingsystemversion.h"
|
2011-04-27 10:05:43 +00:00
|
|
|
#include "qplatformdefs.h"
|
2013-01-09 08:11:17 +00:00
|
|
|
#include "qsysinfo.h"
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
#include "qscopeguard.h"
|
2012-02-17 19:09:17 +00:00
|
|
|
#include "private/qabstractfileengine_p.h"
|
2011-04-27 10:05:43 +00:00
|
|
|
#include "private/qfsfileengine_p.h"
|
|
|
|
#include <private/qsystemlibrary_p.h>
|
|
|
|
#include <qdebug.h>
|
|
|
|
|
|
|
|
#include "qdir.h"
|
|
|
|
#include "qdatetime.h"
|
2020-06-26 06:21:15 +00:00
|
|
|
#include "qfile.h"
|
|
|
|
#include "qvarlengtharray.h"
|
2011-04-27 10:05:43 +00:00
|
|
|
#include "qt_windows.h"
|
2020-03-18 15:25:26 +00:00
|
|
|
#if QT_CONFIG(regularexpression)
|
|
|
|
#include "qregularexpression.h"
|
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-03-23 09:25:46 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <direct.h>
|
|
|
|
#include <winioctl.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <objbase.h>
|
2020-06-05 07:24:37 +00:00
|
|
|
#include <shlobj.h>
|
|
|
|
#include <shobjidl.h>
|
|
|
|
#include <shellapi.h>
|
|
|
|
#include <lm.h>
|
|
|
|
#include <accctrl.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <initguid.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <limits.h>
|
2020-06-05 07:24:37 +00:00
|
|
|
#define SECURITY_WIN32
|
|
|
|
#include <security.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#ifndef SPI_GETPLATFORMTYPE
|
|
|
|
#define SPI_GETPLATFORMTYPE 257
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX FILENAME_MAX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _INTPTR_T_DEFINED
|
|
|
|
#ifdef _WIN64
|
|
|
|
typedef __int64 intptr_t;
|
|
|
|
#else
|
|
|
|
#ifdef _W64
|
|
|
|
typedef _W64 int intptr_t;
|
|
|
|
#else
|
|
|
|
typedef INT_PTR intptr_t;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#define _INTPTR_T_DEFINED
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef INVALID_FILE_ATTRIBUTES
|
|
|
|
# define INVALID_FILE_ATTRIBUTES (DWORD (-1))
|
|
|
|
#endif
|
|
|
|
|
2016-03-23 09:25:46 +00:00
|
|
|
#if !defined(REPARSE_DATA_BUFFER_HEADER_SIZE)
|
2011-04-27 10:05:43 +00:00
|
|
|
typedef struct _REPARSE_DATA_BUFFER {
|
|
|
|
ULONG ReparseTag;
|
|
|
|
USHORT ReparseDataLength;
|
|
|
|
USHORT Reserved;
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
USHORT SubstituteNameOffset;
|
|
|
|
USHORT SubstituteNameLength;
|
|
|
|
USHORT PrintNameOffset;
|
|
|
|
USHORT PrintNameLength;
|
|
|
|
ULONG Flags;
|
|
|
|
WCHAR PathBuffer[1];
|
|
|
|
} SymbolicLinkReparseBuffer;
|
|
|
|
struct {
|
|
|
|
USHORT SubstituteNameOffset;
|
|
|
|
USHORT SubstituteNameLength;
|
|
|
|
USHORT PrintNameOffset;
|
|
|
|
USHORT PrintNameLength;
|
|
|
|
WCHAR PathBuffer[1];
|
|
|
|
} MountPointReparseBuffer;
|
|
|
|
struct {
|
|
|
|
UCHAR DataBuffer[1];
|
|
|
|
} GenericReparseBuffer;
|
|
|
|
};
|
|
|
|
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
|
2016-03-23 09:25:46 +00:00
|
|
|
# define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER, GenericReparseBuffer)
|
|
|
|
#endif // !defined(REPARSE_DATA_BUFFER_HEADER_SIZE)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-03-23 09:25:46 +00:00
|
|
|
#ifndef MAXIMUM_REPARSE_DATA_BUFFER_SIZE
|
|
|
|
# define MAXIMUM_REPARSE_DATA_BUFFER_SIZE 16384
|
|
|
|
#endif
|
|
|
|
#ifndef IO_REPARSE_TAG_SYMLINK
|
|
|
|
# define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
|
|
|
|
#endif
|
|
|
|
#ifndef FSCTL_GET_REPARSE_POINT
|
|
|
|
# define FSCTL_GET_REPARSE_POINT CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 42, METHOD_BUFFERED, FILE_ANY_ACCESS)
|
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-06-05 07:24:37 +00:00
|
|
|
#if defined(QT_BOOTSTRAPPED)
|
2016-12-19 09:34:32 +00:00
|
|
|
# define QT_FEATURE_fslibs -1
|
|
|
|
#else
|
2017-07-09 20:51:33 +00:00
|
|
|
# define QT_FEATURE_fslibs 1
|
2020-06-05 07:24:37 +00:00
|
|
|
#endif // QT_BOOTSTRAPPED
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-12-19 09:34:32 +00:00
|
|
|
#if QT_CONFIG(fslibs)
|
2017-07-09 20:51:33 +00:00
|
|
|
#include <aclapi.h>
|
|
|
|
#include <userenv.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
static TRUSTEE_W currentUserTrusteeW;
|
|
|
|
static TRUSTEE_W worldTrusteeW;
|
2012-01-12 13:43:07 +00:00
|
|
|
static PSID currentUserSID = 0;
|
|
|
|
static PSID worldSID = 0;
|
2014-09-15 21:09:22 +00:00
|
|
|
static HANDLE currentUserImpersonatedToken = nullptr;
|
2012-01-12 13:43:07 +00:00
|
|
|
|
2017-07-09 20:51:33 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
2016-04-29 01:22:50 +00:00
|
|
|
namespace {
|
2017-07-09 20:51:33 +00:00
|
|
|
struct GlobalSid
|
2012-01-12 13:43:07 +00:00
|
|
|
{
|
2017-07-09 20:51:33 +00:00
|
|
|
GlobalSid();
|
|
|
|
~GlobalSid();
|
2012-01-12 13:43:07 +00:00
|
|
|
};
|
|
|
|
|
2017-07-09 20:51:33 +00:00
|
|
|
GlobalSid::~GlobalSid()
|
2012-01-12 13:43:07 +00:00
|
|
|
{
|
2012-01-21 14:09:51 +00:00
|
|
|
free(currentUserSID);
|
2012-01-12 13:43:07 +00:00
|
|
|
currentUserSID = 0;
|
|
|
|
|
|
|
|
// worldSID was allocated with AllocateAndInitializeSid so it needs to be freed with FreeSid
|
|
|
|
if (worldSID) {
|
|
|
|
::FreeSid(worldSID);
|
|
|
|
worldSID = 0;
|
|
|
|
}
|
2014-09-15 21:09:22 +00:00
|
|
|
|
|
|
|
if (currentUserImpersonatedToken) {
|
|
|
|
::CloseHandle(currentUserImpersonatedToken);
|
|
|
|
currentUserImpersonatedToken = nullptr;
|
|
|
|
}
|
2012-01-12 13:43:07 +00:00
|
|
|
}
|
|
|
|
|
2017-07-09 20:51:33 +00:00
|
|
|
GlobalSid::GlobalSid()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2016-04-29 01:22:50 +00:00
|
|
|
{
|
2017-07-09 20:51:33 +00:00
|
|
|
{
|
2011-04-27 10:05:43 +00:00
|
|
|
// Create TRUSTEE for current user
|
|
|
|
HANDLE hnd = ::GetCurrentProcess();
|
|
|
|
HANDLE token = 0;
|
|
|
|
if (::OpenProcessToken(hnd, TOKEN_QUERY, &token)) {
|
2012-01-12 13:43:07 +00:00
|
|
|
DWORD retsize = 0;
|
|
|
|
// GetTokenInformation requires a buffer big enough for the TOKEN_USER struct and
|
|
|
|
// the SID struct. Since the SID struct can have variable number of subauthorities
|
|
|
|
// tacked at the end, its size is variable. Obtain the required size by first
|
|
|
|
// doing a dummy GetTokenInformation call.
|
|
|
|
::GetTokenInformation(token, TokenUser, 0, 0, &retsize);
|
|
|
|
if (retsize) {
|
2012-01-21 14:09:51 +00:00
|
|
|
void *tokenBuffer = malloc(retsize);
|
2018-10-20 15:34:54 +00:00
|
|
|
Q_CHECK_PTR(tokenBuffer);
|
2012-01-12 13:43:07 +00:00
|
|
|
if (::GetTokenInformation(token, TokenUser, tokenBuffer, retsize, &retsize)) {
|
|
|
|
PSID tokenSid = reinterpret_cast<PTOKEN_USER>(tokenBuffer)->User.Sid;
|
|
|
|
DWORD sidLen = ::GetLengthSid(tokenSid);
|
2012-01-21 14:09:51 +00:00
|
|
|
currentUserSID = reinterpret_cast<PSID>(malloc(sidLen));
|
2018-10-20 15:34:54 +00:00
|
|
|
Q_CHECK_PTR(currentUserSID);
|
2012-01-12 13:43:07 +00:00
|
|
|
if (::CopySid(sidLen, currentUserSID, tokenSid))
|
2017-07-09 20:51:33 +00:00
|
|
|
BuildTrusteeWithSid(¤tUserTrusteeW, currentUserSID);
|
2012-01-12 13:43:07 +00:00
|
|
|
}
|
2012-01-21 14:09:51 +00:00
|
|
|
free(tokenBuffer);
|
2012-01-12 13:43:07 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
::CloseHandle(token);
|
|
|
|
}
|
|
|
|
|
2014-09-15 21:09:22 +00:00
|
|
|
token = nullptr;
|
|
|
|
if (::OpenProcessToken(hnd, TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE | STANDARD_RIGHTS_READ, &token)) {
|
|
|
|
::DuplicateToken(token, SecurityImpersonation, ¤tUserImpersonatedToken);
|
|
|
|
::CloseHandle(token);
|
|
|
|
}
|
|
|
|
|
2017-07-09 20:51:33 +00:00
|
|
|
{
|
2011-04-27 10:05:43 +00:00
|
|
|
// Create TRUSTEE for Everyone (World)
|
|
|
|
SID_IDENTIFIER_AUTHORITY worldAuth = { SECURITY_WORLD_SID_AUTHORITY };
|
2017-07-09 20:51:33 +00:00
|
|
|
if (AllocateAndInitializeSid(&worldAuth, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &worldSID))
|
|
|
|
BuildTrusteeWithSid(&worldTrusteeW, worldSID);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-09 20:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Q_GLOBAL_STATIC(GlobalSid, initGlobalSid)
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|
2016-04-29 01:22:50 +00:00
|
|
|
|
|
|
|
} // anonymous namespace
|
2016-12-19 09:34:32 +00:00
|
|
|
#endif // QT_CONFIG(fslibs)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-07-09 20:51:33 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
Q_CORE_EXPORT int qt_ntfs_permission_lookup = 0;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-07-02 17:28:39 +00:00
|
|
|
static inline bool toFileTime(const QDateTime &date, FILETIME *fileTime)
|
|
|
|
{
|
2017-07-10 22:16:47 +00:00
|
|
|
SYSTEMTIME sTime;
|
|
|
|
if (date.timeSpec() == Qt::LocalTime) {
|
|
|
|
SYSTEMTIME lTime;
|
|
|
|
const QDate d = date.date();
|
|
|
|
const QTime t = date.time();
|
2017-07-02 17:28:39 +00:00
|
|
|
|
2017-07-10 22:16:47 +00:00
|
|
|
lTime.wYear = d.year();
|
|
|
|
lTime.wMonth = d.month();
|
|
|
|
lTime.wDay = d.day();
|
|
|
|
lTime.wHour = t.hour();
|
|
|
|
lTime.wMinute = t.minute();
|
|
|
|
lTime.wSecond = t.second();
|
|
|
|
lTime.wMilliseconds = t.msec();
|
|
|
|
lTime.wDayOfWeek = d.dayOfWeek() % 7;
|
2017-07-02 17:28:39 +00:00
|
|
|
|
2017-07-10 22:16:47 +00:00
|
|
|
if (!::TzSpecificLocalTimeToSystemTime(0, &lTime, &sTime))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
QDateTime utcDate = date.toUTC();
|
|
|
|
const QDate d = utcDate.date();
|
|
|
|
const QTime t = utcDate.time();
|
|
|
|
|
|
|
|
sTime.wYear = d.year();
|
|
|
|
sTime.wMonth = d.month();
|
|
|
|
sTime.wDay = d.day();
|
|
|
|
sTime.wHour = t.hour();
|
|
|
|
sTime.wMinute = t.minute();
|
|
|
|
sTime.wSecond = t.second();
|
|
|
|
sTime.wMilliseconds = t.msec();
|
|
|
|
sTime.wDayOfWeek = d.dayOfWeek() % 7;
|
|
|
|
}
|
2017-07-02 17:28:39 +00:00
|
|
|
|
|
|
|
return ::SystemTimeToFileTime(&sTime, fileTime);
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
static QString readSymLink(const QFileSystemEntry &link)
|
|
|
|
{
|
|
|
|
QString result;
|
|
|
|
HANDLE handle = CreateFile((wchar_t*)link.nativeFilePath().utf16(),
|
|
|
|
FILE_READ_EA,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
|
|
|
0,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
|
|
|
|
0);
|
|
|
|
if (handle != INVALID_HANDLE_VALUE) {
|
|
|
|
DWORD bufsize = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
|
2011-12-20 20:39:12 +00:00
|
|
|
REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER*)malloc(bufsize);
|
2018-10-20 15:34:54 +00:00
|
|
|
Q_CHECK_PTR(rdb);
|
2011-04-27 10:05:43 +00:00
|
|
|
DWORD retsize = 0;
|
|
|
|
if (::DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, 0, 0, rdb, bufsize, &retsize, 0)) {
|
|
|
|
if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
|
|
|
|
int length = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
|
|
|
|
int offset = rdb->MountPointReparseBuffer.SubstituteNameOffset / sizeof(wchar_t);
|
|
|
|
const wchar_t* PathBuffer = &rdb->MountPointReparseBuffer.PathBuffer[offset];
|
|
|
|
result = QString::fromWCharArray(PathBuffer, length);
|
|
|
|
} else if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
|
|
|
|
int length = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
|
|
|
|
int offset = rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset / sizeof(wchar_t);
|
|
|
|
const wchar_t* PathBuffer = &rdb->SymbolicLinkReparseBuffer.PathBuffer[offset];
|
|
|
|
result = QString::fromWCharArray(PathBuffer, length);
|
|
|
|
}
|
2021-05-11 12:09:11 +00:00
|
|
|
// remove "\\?\", "\??\" or "\\?\UNC\"
|
|
|
|
result = QFileSystemEntry::removeUncOrLongPathPrefix(result);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2011-12-20 20:39:12 +00:00
|
|
|
free(rdb);
|
2011-04-27 10:05:43 +00:00
|
|
|
CloseHandle(handle);
|
|
|
|
|
2020-03-18 15:25:26 +00:00
|
|
|
#if QT_CONFIG(fslibs) && QT_CONFIG(regularexpression)
|
2017-07-09 20:51:33 +00:00
|
|
|
initGlobalSid();
|
2020-03-18 15:25:26 +00:00
|
|
|
QRegularExpression matchVolumeRe(QLatin1String("^Volume\\{([a-z]|[0-9]|-)+\\}\\\\"), QRegularExpression::CaseInsensitiveOption);
|
|
|
|
auto matchVolume = matchVolumeRe.match(result);
|
|
|
|
if (matchVolume.hasMatch()) {
|
|
|
|
Q_ASSERT(matchVolume.capturedStart() == 0);
|
2016-03-16 11:59:56 +00:00
|
|
|
DWORD len;
|
|
|
|
wchar_t buffer[MAX_PATH];
|
2020-03-18 15:25:26 +00:00
|
|
|
const QString volumeName = QLatin1String("\\\\?\\") + matchVolume.captured();
|
2016-03-16 11:59:56 +00:00
|
|
|
if (GetVolumePathNamesForVolumeName(reinterpret_cast<LPCWSTR>(volumeName.utf16()), buffer, MAX_PATH, &len) != 0)
|
2020-03-18 15:25:26 +00:00
|
|
|
result.replace(0, matchVolume.capturedLength(), QString::fromWCharArray(buffer));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2016-12-19 09:34:32 +00:00
|
|
|
#endif // QT_CONFIG(fslibs)
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString readLink(const QFileSystemEntry &link)
|
|
|
|
{
|
2016-12-19 09:34:32 +00:00
|
|
|
#if QT_CONFIG(fslibs)
|
2011-04-27 10:05:43 +00:00
|
|
|
QString ret;
|
|
|
|
|
|
|
|
bool neededCoInit = false;
|
|
|
|
IShellLink *psl; // pointer to IShellLink i/f
|
|
|
|
WIN32_FIND_DATA wfd;
|
|
|
|
wchar_t szGotPath[MAX_PATH];
|
|
|
|
|
|
|
|
// Get pointer to the IShellLink interface.
|
|
|
|
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *)&psl);
|
|
|
|
|
|
|
|
if (hres == CO_E_NOTINITIALIZED) { // COM was not initialized
|
|
|
|
neededCoInit = true;
|
|
|
|
CoInitialize(NULL);
|
|
|
|
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
|
|
|
|
IID_IShellLink, (LPVOID *)&psl);
|
|
|
|
}
|
|
|
|
if (SUCCEEDED(hres)) { // Get pointer to the IPersistFile interface.
|
|
|
|
IPersistFile *ppf;
|
|
|
|
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf);
|
|
|
|
if (SUCCEEDED(hres)) {
|
|
|
|
hres = ppf->Load((LPOLESTR)link.nativeFilePath().utf16(), STGM_READ);
|
|
|
|
//The original path of the link is retrieved. If the file/folder
|
|
|
|
//was moved, the return value still have the old path.
|
|
|
|
if (SUCCEEDED(hres)) {
|
|
|
|
if (psl->GetPath(szGotPath, MAX_PATH, &wfd, SLGP_UNCPRIORITY) == NOERROR)
|
|
|
|
ret = QString::fromWCharArray(szGotPath);
|
|
|
|
}
|
|
|
|
ppf->Release();
|
|
|
|
}
|
|
|
|
psl->Release();
|
|
|
|
}
|
|
|
|
if (neededCoInit)
|
|
|
|
CoUninitialize();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
Q_UNUSED(link);
|
|
|
|
return QString();
|
2016-12-19 09:34:32 +00:00
|
|
|
#endif // QT_CONFIG(fslibs)
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool uncShareExists(const QString &server)
|
|
|
|
{
|
|
|
|
// This code assumes the UNC path is always like \\?\UNC\server...
|
2020-05-22 14:49:01 +00:00
|
|
|
const auto parts = QStringView{server}.split(QLatin1Char('\\'), Qt::SkipEmptyParts);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (parts.count() >= 3) {
|
|
|
|
QStringList shares;
|
|
|
|
if (QFileSystemEngine::uncListSharesOnServer(QLatin1String("\\\\") + parts.at(2), &shares))
|
2016-03-31 09:01:59 +00:00
|
|
|
return parts.count() < 4 || shares.contains(parts.at(3).toString(), Qt::CaseInsensitive);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool getFindData(QString path, WIN32_FIND_DATA &findData)
|
|
|
|
{
|
|
|
|
// path should not end with a trailing slash
|
|
|
|
while (path.endsWith(QLatin1Char('\\')))
|
|
|
|
path.chop(1);
|
|
|
|
|
|
|
|
// can't handle drives
|
|
|
|
if (!path.endsWith(QLatin1Char(':'))) {
|
|
|
|
HANDLE hFind = ::FindFirstFile((wchar_t*)path.utf16(), &findData);
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE) {
|
|
|
|
::FindClose(hFind);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
#if defined(__IFileOperation_INTERFACE_DEFINED__)
|
|
|
|
class FileOperationProgressSink : public IFileOperationProgressSink
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FileOperationProgressSink()
|
|
|
|
: ref(1)
|
|
|
|
{}
|
|
|
|
virtual ~FileOperationProgressSink() {}
|
|
|
|
|
2020-07-23 06:19:45 +00:00
|
|
|
ULONG STDMETHODCALLTYPE AddRef() override { return ++ref; }
|
|
|
|
ULONG STDMETHODCALLTYPE Release() override
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
{
|
|
|
|
if (--ref == 0) {
|
|
|
|
delete this;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ref;
|
|
|
|
}
|
2020-07-23 06:19:45 +00:00
|
|
|
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject) override
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
{
|
|
|
|
if (!ppvObject)
|
|
|
|
return E_POINTER;
|
|
|
|
|
|
|
|
*ppvObject = nullptr;
|
|
|
|
|
|
|
|
if (iid == __uuidof(IUnknown)) {
|
|
|
|
*ppvObject = static_cast<IUnknown*>(this);
|
|
|
|
} else if (iid == __uuidof(IFileOperationProgressSink)) {
|
|
|
|
*ppvObject = static_cast<IFileOperationProgressSink*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*ppvObject) {
|
|
|
|
AddRef();
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
2020-07-23 06:19:45 +00:00
|
|
|
HRESULT STDMETHODCALLTYPE StartOperations() override { return S_OK; }
|
|
|
|
HRESULT STDMETHODCALLTYPE FinishOperations(HRESULT) override { return S_OK; }
|
|
|
|
HRESULT STDMETHODCALLTYPE PreRenameItem(DWORD, IShellItem *, LPCWSTR) override { return S_OK; }
|
|
|
|
HRESULT STDMETHODCALLTYPE PostRenameItem(DWORD, IShellItem *, LPCWSTR, HRESULT,
|
|
|
|
IShellItem *) override
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
{ return S_OK; }
|
2020-07-23 06:19:45 +00:00
|
|
|
HRESULT STDMETHODCALLTYPE PreMoveItem(DWORD, IShellItem *, IShellItem *, LPCWSTR) override
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
{ return S_OK; }
|
|
|
|
HRESULT STDMETHODCALLTYPE PostMoveItem(DWORD, IShellItem *, IShellItem *, LPCWSTR, HRESULT,
|
2020-07-23 06:19:45 +00:00
|
|
|
IShellItem *) override
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
{ return S_OK; }
|
2020-07-23 06:19:45 +00:00
|
|
|
HRESULT STDMETHODCALLTYPE PreCopyItem(DWORD, IShellItem *, IShellItem *, LPCWSTR) override
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
{ return S_OK; }
|
|
|
|
HRESULT STDMETHODCALLTYPE PostCopyItem(DWORD, IShellItem *, IShellItem *, LPCWSTR, HRESULT,
|
2020-07-23 06:19:45 +00:00
|
|
|
IShellItem *) override
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
{ return S_OK; }
|
2020-07-23 06:19:45 +00:00
|
|
|
HRESULT STDMETHODCALLTYPE PreDeleteItem(DWORD dwFlags, IShellItem *) override
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
{
|
|
|
|
// 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 */,
|
2020-07-23 06:19:45 +00:00
|
|
|
HRESULT /* hrDelete */,
|
|
|
|
IShellItem *psiNewlyCreated) override
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
{
|
|
|
|
if (psiNewlyCreated) {
|
|
|
|
wchar_t *pszName = nullptr;
|
|
|
|
psiNewlyCreated->GetDisplayName(SIGDN_FILESYSPATH, &pszName);
|
|
|
|
if (pszName) {
|
|
|
|
targetPath = QString::fromWCharArray(pszName);
|
|
|
|
CoTaskMemFree(pszName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
2020-07-23 06:19:45 +00:00
|
|
|
HRESULT STDMETHODCALLTYPE PreNewItem(DWORD, IShellItem *, LPCWSTR) override { return S_OK; }
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
HRESULT STDMETHODCALLTYPE PostNewItem(DWORD, IShellItem *, LPCWSTR, LPCWSTR, DWORD, HRESULT,
|
2020-07-23 06:19:45 +00:00
|
|
|
IShellItem *) override
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
{ return S_OK; }
|
2020-07-23 06:19:45 +00:00
|
|
|
HRESULT STDMETHODCALLTYPE UpdateProgress(UINT, UINT) override { return S_OK; }
|
|
|
|
HRESULT STDMETHODCALLTYPE ResetTimer() override { return S_OK; }
|
|
|
|
HRESULT STDMETHODCALLTYPE PauseTimer() override { return S_OK; }
|
|
|
|
HRESULT STDMETHODCALLTYPE ResumeTimer() override { return S_OK; }
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
|
|
|
|
QString targetPath;
|
|
|
|
private:
|
|
|
|
ULONG ref;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
bool QFileSystemEngine::uncListSharesOnServer(const QString &server, QStringList *list)
|
|
|
|
{
|
2017-07-09 20:58:26 +00:00
|
|
|
DWORD res = ERROR_NOT_SUPPORTED;
|
|
|
|
SHARE_INFO_1 *BufPtr, *p;
|
|
|
|
DWORD er = 0, tr = 0, resume = 0, i;
|
|
|
|
do {
|
|
|
|
res = NetShareEnum((wchar_t*)server.utf16(), 1, (LPBYTE *)&BufPtr, DWORD(-1), &er, &tr, &resume);
|
|
|
|
if (res == ERROR_SUCCESS || res == ERROR_MORE_DATA) {
|
|
|
|
p = BufPtr;
|
|
|
|
for (i = 1; i <= er; ++i) {
|
|
|
|
if (list && p->shi1_type == 0)
|
|
|
|
list->append(QString::fromWCharArray(p->shi1_netname));
|
|
|
|
p++;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2017-07-09 20:58:26 +00:00
|
|
|
}
|
|
|
|
NetApiBufferFree(BufPtr);
|
|
|
|
} while (res == ERROR_MORE_DATA);
|
|
|
|
return res == ERROR_SUCCESS;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QFileSystemEngine::clearWinStatData(QFileSystemMetaData &data)
|
|
|
|
{
|
|
|
|
data.size_ = 0;
|
|
|
|
data.fileAttribute_ = 0;
|
2016-07-23 18:14:57 +00:00
|
|
|
data.birthTime_ = FILETIME();
|
|
|
|
data.changeTime_ = FILETIME();
|
2011-04-27 10:05:43 +00:00
|
|
|
data.lastAccessTime_ = FILETIME();
|
|
|
|
data.lastWriteTime_ = FILETIME();
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
QFileSystemEntry QFileSystemEngine::getLinkTarget(const QFileSystemEntry &link,
|
|
|
|
QFileSystemMetaData &data)
|
|
|
|
{
|
2019-09-04 13:26:10 +00:00
|
|
|
Q_CHECK_FILE_NAME(link, link);
|
|
|
|
|
|
|
|
if (data.missingFlags(QFileSystemMetaData::LinkType))
|
2011-04-27 10:05:43 +00:00
|
|
|
QFileSystemEngine::fillMetaData(link, data, QFileSystemMetaData::LinkType);
|
|
|
|
|
2017-08-26 19:54:33 +00:00
|
|
|
QString target;
|
2011-04-27 10:05:43 +00:00
|
|
|
if (data.isLnkFile())
|
2017-08-26 19:54:33 +00:00
|
|
|
target = readLink(link);
|
2011-04-27 10:05:43 +00:00
|
|
|
else if (data.isLink())
|
2017-08-26 19:54:33 +00:00
|
|
|
target = readSymLink(link);
|
|
|
|
QFileSystemEntry ret(target);
|
|
|
|
if (!target.isEmpty() && ret.isRelative()) {
|
|
|
|
target.prepend(absoluteName(link).path() + QLatin1Char('/'));
|
|
|
|
ret = QFileSystemEntry(QDir::cleanPath(target));
|
|
|
|
}
|
|
|
|
return ret;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 14:25:21 +00:00
|
|
|
//static
|
|
|
|
QFileSystemEntry QFileSystemEngine::junctionTarget(const QFileSystemEntry &link,
|
|
|
|
QFileSystemMetaData &data)
|
|
|
|
{
|
|
|
|
Q_CHECK_FILE_NAME(link, link);
|
|
|
|
|
|
|
|
if (data.missingFlags(QFileSystemMetaData::JunctionType))
|
|
|
|
QFileSystemEngine::fillMetaData(link, data, QFileSystemMetaData::LinkType);
|
|
|
|
|
|
|
|
QString target;
|
|
|
|
if (data.isJunction())
|
|
|
|
target = readSymLink(link);
|
|
|
|
QFileSystemEntry ret(target);
|
|
|
|
if (!target.isEmpty() && ret.isRelative()) {
|
|
|
|
target.prepend(absoluteName(link).path() + QLatin1Char('/'));
|
|
|
|
ret = QFileSystemEntry(QDir::cleanPath(target));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
//static
|
|
|
|
QFileSystemEntry QFileSystemEngine::canonicalName(const QFileSystemEntry &entry, QFileSystemMetaData &data)
|
|
|
|
{
|
2019-09-04 13:26:10 +00:00
|
|
|
Q_CHECK_FILE_NAME(entry, entry);
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
if (data.missingFlags(QFileSystemMetaData::ExistsAttribute))
|
|
|
|
QFileSystemEngine::fillMetaData(entry, data, QFileSystemMetaData::ExistsAttribute);
|
|
|
|
|
|
|
|
if (data.exists())
|
|
|
|
return QFileSystemEntry(slowCanonicalized(absoluteName(entry).filePath()));
|
|
|
|
else
|
|
|
|
return QFileSystemEntry();
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
QString QFileSystemEngine::nativeAbsoluteFilePath(const QString &path)
|
|
|
|
{
|
2019-09-04 13:26:10 +00:00
|
|
|
Q_CHECK_FILE_NAME(path, QString());
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
// can be //server or //server/share
|
|
|
|
QString absPath;
|
|
|
|
QVarLengthArray<wchar_t, MAX_PATH> buf(qMax(MAX_PATH, path.size() + 1));
|
2020-10-07 11:05:48 +00:00
|
|
|
wchar_t *fileName = nullptr;
|
2011-04-27 10:05:43 +00:00
|
|
|
DWORD retLen = GetFullPathName((wchar_t*)path.utf16(), buf.size(), buf.data(), &fileName);
|
|
|
|
if (retLen > (DWORD)buf.size()) {
|
|
|
|
buf.resize(retLen);
|
|
|
|
retLen = GetFullPathName((wchar_t*)path.utf16(), buf.size(), buf.data(), &fileName);
|
|
|
|
}
|
|
|
|
if (retLen != 0)
|
|
|
|
absPath = QString::fromWCharArray(buf.data(), retLen);
|
2016-11-22 13:54:02 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
// This is really ugly, but GetFullPathName strips off whitespace at the end.
|
|
|
|
// If you for instance write ". " in the lineedit of QFileDialog,
|
|
|
|
// (which is an invalid filename) this function will strip the space off and viola,
|
|
|
|
// the file is later reported as existing. Therefore, we re-add the whitespace that
|
|
|
|
// was at the end of path in order to keep the filename invalid.
|
|
|
|
if (!path.isEmpty() && path.at(path.size() - 1) == QLatin1Char(' '))
|
|
|
|
absPath.append(QLatin1Char(' '));
|
|
|
|
return absPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
QFileSystemEntry QFileSystemEngine::absoluteName(const QFileSystemEntry &entry)
|
|
|
|
{
|
2019-09-04 13:26:10 +00:00
|
|
|
Q_CHECK_FILE_NAME(entry, entry);
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QString ret;
|
|
|
|
|
|
|
|
if (!entry.isRelative()) {
|
2016-03-23 09:25:46 +00:00
|
|
|
if (entry.isAbsolute() && entry.isClean())
|
2011-04-27 10:05:43 +00:00
|
|
|
ret = entry.filePath();
|
2016-03-23 09:25:46 +00:00
|
|
|
else
|
2011-04-27 10:05:43 +00:00
|
|
|
ret = QDir::fromNativeSeparators(nativeAbsoluteFilePath(entry.filePath()));
|
|
|
|
} else {
|
|
|
|
ret = QDir::cleanPath(QDir::currentPath() + QLatin1Char('/') + entry.filePath());
|
|
|
|
}
|
|
|
|
|
|
|
|
// The path should be absolute at this point.
|
|
|
|
// From the docs :
|
|
|
|
// Absolute paths begin with the directory separator "/"
|
|
|
|
// (optionally preceded by a drive specification under Windows).
|
|
|
|
if (ret.at(0) != QLatin1Char('/')) {
|
|
|
|
Q_ASSERT(ret.length() >= 2);
|
|
|
|
Q_ASSERT(ret.at(0).isLetter());
|
|
|
|
Q_ASSERT(ret.at(1) == QLatin1Char(':'));
|
|
|
|
|
|
|
|
// Force uppercase drive letters.
|
|
|
|
ret[0] = ret.at(0).toUpper();
|
|
|
|
}
|
2011-08-26 09:01:48 +00:00
|
|
|
return QFileSystemEntry(ret, QFileSystemEntry::FromInternalPath());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 20:01:39 +00:00
|
|
|
#if defined(Q_CC_MINGW) && WINVER < 0x0602 && _WIN32_WINNT < _WIN32_WINNT_WIN8 // Windows 8 onwards
|
2013-01-09 08:11:17 +00:00
|
|
|
|
|
|
|
typedef struct _FILE_ID_INFO {
|
|
|
|
ULONGLONG VolumeSerialNumber;
|
|
|
|
FILE_ID_128 FileId;
|
|
|
|
} FILE_ID_INFO, *PFILE_ID_INFO;
|
2016-03-16 11:59:56 +00:00
|
|
|
|
2021-08-16 20:01:39 +00:00
|
|
|
#endif // if defined(Q_CC_MINGW) && WINVER < 0x0602 && _WIN32_WINNT < _WIN32_WINNT_WIN8
|
2013-01-09 08:11:17 +00:00
|
|
|
|
2019-04-01 15:40:29 +00:00
|
|
|
// File ID for Windows up to version 7 and FAT32 drives
|
2013-01-09 08:11:17 +00:00
|
|
|
static inline QByteArray fileId(HANDLE handle)
|
|
|
|
{
|
|
|
|
BY_HANDLE_FILE_INFORMATION info;
|
|
|
|
if (GetFileInformationByHandle(handle, &info)) {
|
2017-07-09 19:29:26 +00:00
|
|
|
char buffer[sizeof "01234567:0123456701234567"];
|
|
|
|
qsnprintf(buffer, sizeof(buffer), "%lx:%08lx%08lx",
|
|
|
|
info.dwVolumeSerialNumber,
|
|
|
|
info.nFileIndexHigh,
|
|
|
|
info.nFileIndexLow);
|
|
|
|
return buffer;
|
2013-01-09 08:11:17 +00:00
|
|
|
}
|
2017-07-09 19:29:26 +00:00
|
|
|
return QByteArray();
|
2013-01-09 08:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// File ID for Windows starting from version 8.
|
|
|
|
QByteArray fileIdWin8(HANDLE handle)
|
|
|
|
{
|
2021-01-28 15:06:26 +00:00
|
|
|
#if !defined(QT_BOOTSTRAPPED)
|
2013-09-02 10:57:51 +00:00
|
|
|
QByteArray result;
|
|
|
|
FILE_ID_INFO infoEx;
|
2016-03-16 11:59:56 +00:00
|
|
|
if (GetFileInformationByHandleEx(handle,
|
|
|
|
static_cast<FILE_INFO_BY_HANDLE_CLASS>(18), // FileIdInfo in Windows 8
|
2013-09-02 10:57:51 +00:00
|
|
|
&infoEx, sizeof(FILE_ID_INFO))) {
|
|
|
|
result = QByteArray::number(infoEx.VolumeSerialNumber, 16);
|
|
|
|
result += ':';
|
2016-03-16 11:59:56 +00:00
|
|
|
// Note: MinGW-64's definition of FILE_ID_128 differs from the MSVC one.
|
|
|
|
result += QByteArray(reinterpret_cast<const char *>(&infoEx.FileId), int(sizeof(infoEx.FileId))).toHex();
|
2019-04-01 15:40:29 +00:00
|
|
|
} else {
|
|
|
|
result = fileId(handle); // GetFileInformationByHandleEx() is observed to fail for FAT32, QTBUG-74759
|
2013-09-02 10:57:51 +00:00
|
|
|
}
|
2013-01-09 08:11:17 +00:00
|
|
|
return result;
|
2021-01-28 15:06:26 +00:00
|
|
|
#else // !QT_BOOTSTRAPPED
|
2016-03-16 11:59:56 +00:00
|
|
|
return fileId(handle);
|
|
|
|
#endif
|
2013-01-09 08:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
QByteArray QFileSystemEngine::id(const QFileSystemEntry &entry)
|
|
|
|
{
|
2019-09-04 13:26:10 +00:00
|
|
|
Q_CHECK_FILE_NAME(entry, QByteArray());
|
|
|
|
|
2013-01-09 08:11:17 +00:00
|
|
|
QByteArray result;
|
2017-07-09 19:41:32 +00:00
|
|
|
|
|
|
|
const HANDLE handle =
|
2017-07-05 21:41:11 +00:00
|
|
|
CreateFile((wchar_t*)entry.nativeFilePath().utf16(), 0,
|
2017-07-09 19:41:32 +00:00
|
|
|
FILE_SHARE_READ, NULL, OPEN_EXISTING,
|
|
|
|
FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
2017-07-05 21:41:11 +00:00
|
|
|
if (handle != INVALID_HANDLE_VALUE) {
|
2017-06-29 19:55:54 +00:00
|
|
|
result = id(handle);
|
2013-01-09 08:11:17 +00:00
|
|
|
CloseHandle(handle);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-06-29 19:55:54 +00:00
|
|
|
//static
|
|
|
|
QByteArray QFileSystemEngine::id(HANDLE fHandle)
|
|
|
|
{
|
2021-07-15 15:19:43 +00:00
|
|
|
return fileIdWin8(HANDLE(fHandle));
|
2017-06-29 19:55:54 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
//static
|
2017-07-02 17:28:39 +00:00
|
|
|
bool QFileSystemEngine::setFileTime(HANDLE fHandle, const QDateTime &newDate,
|
|
|
|
QAbstractFileEngine::FileTime time, QSystemError &error)
|
|
|
|
{
|
|
|
|
FILETIME fTime;
|
|
|
|
FILETIME *pLastWrite = NULL;
|
|
|
|
FILETIME *pLastAccess = NULL;
|
|
|
|
FILETIME *pCreationTime = NULL;
|
|
|
|
|
|
|
|
switch (time) {
|
|
|
|
case QAbstractFileEngine::ModificationTime:
|
|
|
|
pLastWrite = &fTime;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QAbstractFileEngine::AccessTime:
|
|
|
|
pLastAccess = &fTime;
|
|
|
|
break;
|
|
|
|
|
2016-07-23 18:14:57 +00:00
|
|
|
case QAbstractFileEngine::BirthTime:
|
2017-07-02 17:28:39 +00:00
|
|
|
pCreationTime = &fTime;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = QSystemError(ERROR_INVALID_PARAMETER, QSystemError::NativeError);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!toFileTime(newDate, &fTime))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!::SetFileTime(fHandle, pCreationTime, pLastAccess, pLastWrite)) {
|
|
|
|
error = QSystemError(::GetLastError(), QSystemError::NativeError);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QString QFileSystemEngine::owner(const QFileSystemEntry &entry, QAbstractFileEngine::FileOwner own)
|
|
|
|
{
|
|
|
|
QString name;
|
2016-12-19 09:34:32 +00:00
|
|
|
#if QT_CONFIG(fslibs)
|
2011-04-27 10:05:43 +00:00
|
|
|
extern int qt_ntfs_permission_lookup;
|
2017-07-09 20:51:33 +00:00
|
|
|
if (qt_ntfs_permission_lookup > 0) {
|
|
|
|
initGlobalSid();
|
|
|
|
{
|
2011-04-27 10:05:43 +00:00
|
|
|
PSID pOwner = 0;
|
|
|
|
PSECURITY_DESCRIPTOR pSD;
|
2017-07-09 20:51:33 +00:00
|
|
|
if (GetNamedSecurityInfo(reinterpret_cast<const wchar_t*>(entry.nativeFilePath().utf16()), SE_FILE_OBJECT,
|
|
|
|
own == QAbstractFileEngine::OwnerGroup ? GROUP_SECURITY_INFORMATION : OWNER_SECURITY_INFORMATION,
|
|
|
|
own == QAbstractFileEngine::OwnerUser ? &pOwner : 0, own == QAbstractFileEngine::OwnerGroup ? &pOwner : 0,
|
|
|
|
0, 0, &pSD) == ERROR_SUCCESS) {
|
2011-04-27 10:05:43 +00:00
|
|
|
DWORD lowner = 64;
|
|
|
|
DWORD ldomain = 64;
|
|
|
|
QVarLengthArray<wchar_t, 64> owner(lowner);
|
|
|
|
QVarLengthArray<wchar_t, 64> domain(ldomain);
|
|
|
|
SID_NAME_USE use = SidTypeUnknown;
|
|
|
|
// First call, to determine size of the strings (with '\0').
|
2017-07-09 20:51:33 +00:00
|
|
|
if (!LookupAccountSid(NULL, pOwner, (LPWSTR)owner.data(), &lowner,
|
|
|
|
domain.data(), &ldomain, &use)) {
|
2011-04-27 10:05:43 +00:00
|
|
|
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
if (lowner > (DWORD)owner.size())
|
|
|
|
owner.resize(lowner);
|
|
|
|
if (ldomain > (DWORD)domain.size())
|
|
|
|
domain.resize(ldomain);
|
|
|
|
// Second call, try on resized buf-s
|
2017-07-09 20:51:33 +00:00
|
|
|
if (!LookupAccountSid(NULL, pOwner, owner.data(), &lowner,
|
|
|
|
domain.data(), &ldomain, &use)) {
|
2011-04-27 10:05:43 +00:00
|
|
|
lowner = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lowner = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lowner != 0)
|
|
|
|
name = QString::fromWCharArray(owner.data());
|
|
|
|
LocalFree(pSD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2011-04-26 10:03:17 +00:00
|
|
|
Q_UNUSED(entry);
|
2011-04-27 10:05:43 +00:00
|
|
|
Q_UNUSED(own);
|
|
|
|
#endif
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
bool QFileSystemEngine::fillPermissions(const QFileSystemEntry &entry, QFileSystemMetaData &data,
|
|
|
|
QFileSystemMetaData::MetaDataFlags what)
|
|
|
|
{
|
2016-12-19 09:34:32 +00:00
|
|
|
#if QT_CONFIG(fslibs)
|
2017-07-09 20:51:33 +00:00
|
|
|
if (qt_ntfs_permission_lookup > 0) {
|
|
|
|
initGlobalSid();
|
|
|
|
{
|
2011-04-27 10:05:43 +00:00
|
|
|
enum { ReadMask = 0x00000001, WriteMask = 0x00000002, ExecMask = 0x00000020 };
|
|
|
|
|
2012-05-02 15:46:52 +00:00
|
|
|
QString fname = entry.nativeFilePath();
|
2011-04-27 10:05:43 +00:00
|
|
|
PSID pOwner = 0;
|
|
|
|
PSID pGroup = 0;
|
|
|
|
PACL pDacl;
|
|
|
|
PSECURITY_DESCRIPTOR pSD;
|
2017-07-09 20:51:33 +00:00
|
|
|
DWORD res = GetNamedSecurityInfo(reinterpret_cast<const wchar_t*>(fname.utf16()), SE_FILE_OBJECT,
|
|
|
|
OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
|
|
|
|
&pOwner, &pGroup, &pDacl, 0, &pSD);
|
2020-11-16 03:19:17 +00:00
|
|
|
if (res == ERROR_SUCCESS) {
|
2011-04-27 10:05:43 +00:00
|
|
|
ACCESS_MASK access_mask;
|
|
|
|
TRUSTEE_W trustee;
|
|
|
|
if (what & QFileSystemMetaData::UserPermissions) { // user
|
2014-09-15 21:09:22 +00:00
|
|
|
// Using AccessCheck because GetEffectiveRightsFromAcl doesn't account for elevation
|
|
|
|
if (currentUserImpersonatedToken) {
|
|
|
|
GENERIC_MAPPING mapping = {FILE_GENERIC_READ, FILE_GENERIC_WRITE, FILE_GENERIC_EXECUTE, FILE_ALL_ACCESS};
|
|
|
|
PRIVILEGE_SET privileges;
|
|
|
|
DWORD grantedAccess;
|
|
|
|
BOOL result;
|
|
|
|
|
|
|
|
data.knownFlagsMask |= QFileSystemMetaData::UserPermissions;
|
|
|
|
DWORD genericAccessRights = GENERIC_READ;
|
|
|
|
::MapGenericMask(&genericAccessRights, &mapping);
|
|
|
|
|
|
|
|
DWORD privilegesLength = sizeof(privileges);
|
|
|
|
if (::AccessCheck(pSD, currentUserImpersonatedToken, genericAccessRights,
|
|
|
|
&mapping, &privileges, &privilegesLength, &grantedAccess, &result) && result) {
|
|
|
|
data.entryFlags |= QFileSystemMetaData::UserReadPermission;
|
|
|
|
}
|
|
|
|
|
|
|
|
privilegesLength = sizeof(privileges);
|
|
|
|
genericAccessRights = GENERIC_WRITE;
|
|
|
|
::MapGenericMask(&genericAccessRights, &mapping);
|
|
|
|
if (::AccessCheck(pSD, currentUserImpersonatedToken, genericAccessRights,
|
|
|
|
&mapping, &privileges, &privilegesLength, &grantedAccess, &result) && result) {
|
|
|
|
data.entryFlags |= QFileSystemMetaData::UserWritePermission;
|
|
|
|
}
|
|
|
|
|
|
|
|
privilegesLength = sizeof(privileges);
|
|
|
|
genericAccessRights = GENERIC_EXECUTE;
|
|
|
|
::MapGenericMask(&genericAccessRights, &mapping);
|
|
|
|
if (::AccessCheck(pSD, currentUserImpersonatedToken, genericAccessRights,
|
|
|
|
&mapping, &privileges, &privilegesLength, &grantedAccess, &result) && result) {
|
|
|
|
data.entryFlags |= QFileSystemMetaData::UserExecutePermission;
|
|
|
|
}
|
|
|
|
} else { // fallback to GetEffectiveRightsFromAcl
|
|
|
|
data.knownFlagsMask |= QFileSystemMetaData::UserPermissions;
|
2017-12-29 14:56:33 +00:00
|
|
|
if (GetEffectiveRightsFromAclW(pDacl, ¤tUserTrusteeW, &access_mask) != ERROR_SUCCESS)
|
2014-09-15 21:09:22 +00:00
|
|
|
access_mask = ACCESS_MASK(-1);
|
|
|
|
if (access_mask & ReadMask)
|
|
|
|
data.entryFlags |= QFileSystemMetaData::UserReadPermission;
|
|
|
|
if (access_mask & WriteMask)
|
|
|
|
data.entryFlags|= QFileSystemMetaData::UserWritePermission;
|
|
|
|
if (access_mask & ExecMask)
|
|
|
|
data.entryFlags|= QFileSystemMetaData::UserExecutePermission;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
if (what & QFileSystemMetaData::OwnerPermissions) { // owner
|
|
|
|
data.knownFlagsMask |= QFileSystemMetaData::OwnerPermissions;
|
2017-07-09 20:51:33 +00:00
|
|
|
BuildTrusteeWithSid(&trustee, pOwner);
|
|
|
|
if (GetEffectiveRightsFromAcl(pDacl, &trustee, &access_mask) != ERROR_SUCCESS)
|
2011-04-27 10:05:43 +00:00
|
|
|
access_mask = (ACCESS_MASK)-1;
|
2020-11-16 03:19:17 +00:00
|
|
|
if (access_mask & ReadMask)
|
2011-04-27 10:05:43 +00:00
|
|
|
data.entryFlags |= QFileSystemMetaData::OwnerReadPermission;
|
2020-11-16 03:19:17 +00:00
|
|
|
if (access_mask & WriteMask)
|
2011-04-27 10:05:43 +00:00
|
|
|
data.entryFlags |= QFileSystemMetaData::OwnerWritePermission;
|
2020-11-16 03:19:17 +00:00
|
|
|
if (access_mask & ExecMask)
|
2011-04-27 10:05:43 +00:00
|
|
|
data.entryFlags |= QFileSystemMetaData::OwnerExecutePermission;
|
|
|
|
}
|
|
|
|
if (what & QFileSystemMetaData::GroupPermissions) { // group
|
|
|
|
data.knownFlagsMask |= QFileSystemMetaData::GroupPermissions;
|
2017-07-09 20:51:33 +00:00
|
|
|
BuildTrusteeWithSid(&trustee, pGroup);
|
|
|
|
if (GetEffectiveRightsFromAcl(pDacl, &trustee, &access_mask) != ERROR_SUCCESS)
|
2011-04-27 10:05:43 +00:00
|
|
|
access_mask = (ACCESS_MASK)-1;
|
2020-11-16 03:19:17 +00:00
|
|
|
if (access_mask & ReadMask)
|
2011-04-27 10:05:43 +00:00
|
|
|
data.entryFlags |= QFileSystemMetaData::GroupReadPermission;
|
2020-11-16 03:19:17 +00:00
|
|
|
if (access_mask & WriteMask)
|
2011-04-27 10:05:43 +00:00
|
|
|
data.entryFlags |= QFileSystemMetaData::GroupWritePermission;
|
2020-11-16 03:19:17 +00:00
|
|
|
if (access_mask & ExecMask)
|
2011-04-27 10:05:43 +00:00
|
|
|
data.entryFlags |= QFileSystemMetaData::GroupExecutePermission;
|
|
|
|
}
|
|
|
|
if (what & QFileSystemMetaData::OtherPermissions) { // other (world)
|
|
|
|
data.knownFlagsMask |= QFileSystemMetaData::OtherPermissions;
|
2017-07-09 20:51:33 +00:00
|
|
|
if (GetEffectiveRightsFromAcl(pDacl, &worldTrusteeW, &access_mask) != ERROR_SUCCESS)
|
2011-04-27 10:05:43 +00:00
|
|
|
access_mask = (ACCESS_MASK)-1; // ###
|
2020-11-16 03:19:17 +00:00
|
|
|
if (access_mask & ReadMask)
|
2011-04-27 10:05:43 +00:00
|
|
|
data.entryFlags |= QFileSystemMetaData::OtherReadPermission;
|
2020-11-16 03:19:17 +00:00
|
|
|
if (access_mask & WriteMask)
|
2011-04-27 10:05:43 +00:00
|
|
|
data.entryFlags |= QFileSystemMetaData::OtherWritePermission;
|
2020-11-16 03:19:17 +00:00
|
|
|
if (access_mask & ExecMask)
|
2011-04-27 10:05:43 +00:00
|
|
|
data.entryFlags |= QFileSystemMetaData::OwnerExecutePermission;
|
|
|
|
}
|
|
|
|
LocalFree(pSD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
//### what to do with permissions if we don't use NTFS
|
|
|
|
// for now just add all permissions and what about exe missions ??
|
|
|
|
// also qt_ntfs_permission_lookup is now not set by default ... should it ?
|
|
|
|
data.entryFlags |= QFileSystemMetaData::OwnerReadPermission
|
|
|
|
| QFileSystemMetaData::GroupReadPermission
|
|
|
|
| QFileSystemMetaData::OtherReadPermission;
|
|
|
|
|
|
|
|
if (!(data.fileAttribute_ & FILE_ATTRIBUTE_READONLY)) {
|
|
|
|
data.entryFlags |= QFileSystemMetaData::OwnerWritePermission
|
|
|
|
| QFileSystemMetaData::GroupWritePermission
|
|
|
|
| QFileSystemMetaData::OtherWritePermission;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString fname = entry.filePath();
|
|
|
|
QString ext = fname.right(4).toLower();
|
|
|
|
if (data.isDirectory() ||
|
|
|
|
ext == QLatin1String(".exe") || ext == QLatin1String(".com") || ext == QLatin1String(".bat") ||
|
|
|
|
ext == QLatin1String(".pif") || ext == QLatin1String(".cmd")) {
|
|
|
|
data.entryFlags |= QFileSystemMetaData::OwnerExecutePermission | QFileSystemMetaData::GroupExecutePermission
|
|
|
|
| QFileSystemMetaData::OtherExecutePermission | QFileSystemMetaData::UserExecutePermission;
|
|
|
|
}
|
|
|
|
data.knownFlagsMask |= QFileSystemMetaData::OwnerPermissions | QFileSystemMetaData::GroupPermissions
|
|
|
|
| QFileSystemMetaData::OtherPermissions | QFileSystemMetaData::UserExecutePermission;
|
|
|
|
// calculate user permissions
|
|
|
|
if (what & QFileSystemMetaData::UserReadPermission) {
|
|
|
|
if (::_waccess((wchar_t*)entry.nativeFilePath().utf16(), R_OK) == 0)
|
|
|
|
data.entryFlags |= QFileSystemMetaData::UserReadPermission;
|
|
|
|
data.knownFlagsMask |= QFileSystemMetaData::UserReadPermission;
|
|
|
|
}
|
|
|
|
if (what & QFileSystemMetaData::UserWritePermission) {
|
|
|
|
if (::_waccess((wchar_t*)entry.nativeFilePath().utf16(), W_OK) == 0)
|
|
|
|
data.entryFlags |= QFileSystemMetaData::UserWritePermission;
|
2011-10-06 12:46:30 +00:00
|
|
|
data.knownFlagsMask |= QFileSystemMetaData::UserWritePermission;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.hasFlags(what);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool tryDriveUNCFallback(const QFileSystemEntry &fname, QFileSystemMetaData &data)
|
|
|
|
{
|
|
|
|
bool entryExists = false;
|
|
|
|
DWORD fileAttrib = 0;
|
|
|
|
if (fname.isDriveRoot()) {
|
|
|
|
// a valid drive ??
|
2016-05-12 06:46:13 +00:00
|
|
|
const UINT oldErrorMode = ::SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
|
2011-04-27 10:05:43 +00:00
|
|
|
DWORD drivesBitmask = ::GetLogicalDrives();
|
2016-05-12 06:46:13 +00:00
|
|
|
::SetErrorMode(oldErrorMode);
|
2011-04-27 10:05:43 +00:00
|
|
|
int drivebit = 1 << (fname.filePath().at(0).toUpper().unicode() - QLatin1Char('A').unicode());
|
|
|
|
if (drivesBitmask & drivebit) {
|
|
|
|
fileAttrib = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM;
|
|
|
|
entryExists = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const QString &path = fname.nativeFilePath();
|
|
|
|
bool is_dir = false;
|
|
|
|
if (path.startsWith(QLatin1String("\\\\?\\UNC"))) {
|
|
|
|
// UNC - stat doesn't work for all cases (Windows bug)
|
|
|
|
int s = path.indexOf(path.at(0),7);
|
|
|
|
if (s > 0) {
|
|
|
|
// "\\?\UNC\server\..."
|
|
|
|
s = path.indexOf(path.at(0),s+1);
|
|
|
|
if (s > 0) {
|
|
|
|
// "\\?\UNC\server\share\..."
|
|
|
|
if (s == path.size() - 1) {
|
|
|
|
// "\\?\UNC\server\share\"
|
|
|
|
is_dir = true;
|
|
|
|
} else {
|
|
|
|
// "\\?\UNC\server\share\notfound"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// "\\?\UNC\server\share"
|
|
|
|
is_dir = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// "\\?\UNC\server"
|
|
|
|
is_dir = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_dir && uncShareExists(path)) {
|
|
|
|
// looks like a UNC dir, is a dir.
|
|
|
|
fileAttrib = FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
entryExists = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (entryExists)
|
|
|
|
data.fillFromFileAttribute(fileAttrib);
|
|
|
|
return entryExists;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool tryFindFallback(const QFileSystemEntry &fname, QFileSystemMetaData &data)
|
|
|
|
{
|
|
|
|
bool filledData = false;
|
|
|
|
// This assumes the last call to a Windows API failed.
|
|
|
|
int errorCode = GetLastError();
|
|
|
|
if (errorCode == ERROR_ACCESS_DENIED || errorCode == ERROR_SHARING_VIOLATION) {
|
|
|
|
WIN32_FIND_DATA findData;
|
2012-01-18 13:44:42 +00:00
|
|
|
if (getFindData(fname.nativeFilePath(), findData)
|
2011-04-27 10:05:43 +00:00
|
|
|
&& findData.dwFileAttributes != INVALID_FILE_ATTRIBUTES) {
|
2012-01-18 13:44:42 +00:00
|
|
|
data.fillFromFindData(findData, true, fname.isDriveRoot());
|
2011-04-27 10:05:43 +00:00
|
|
|
filledData = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filledData;
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
bool QFileSystemEngine::fillMetaData(int fd, QFileSystemMetaData &data,
|
|
|
|
QFileSystemMetaData::MetaDataFlags what)
|
|
|
|
{
|
2018-08-29 06:31:08 +00:00
|
|
|
auto fHandle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
|
2011-04-27 10:05:43 +00:00
|
|
|
if (fHandle != INVALID_HANDLE_VALUE) {
|
|
|
|
return fillMetaData(fHandle, data, what);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
bool QFileSystemEngine::fillMetaData(HANDLE fHandle, QFileSystemMetaData &data,
|
|
|
|
QFileSystemMetaData::MetaDataFlags what)
|
|
|
|
{
|
|
|
|
data.entryFlags &= ~what;
|
|
|
|
clearWinStatData(data);
|
|
|
|
BY_HANDLE_FILE_INFORMATION fileInfo;
|
|
|
|
UINT oldmode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
|
|
|
|
if (GetFileInformationByHandle(fHandle , &fileInfo)) {
|
|
|
|
data.fillFromFindInfo(fileInfo);
|
|
|
|
}
|
|
|
|
SetErrorMode(oldmode);
|
|
|
|
return data.hasFlags(what);
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
bool QFileSystemEngine::fillMetaData(const QFileSystemEntry &entry, QFileSystemMetaData &data,
|
|
|
|
QFileSystemMetaData::MetaDataFlags what)
|
|
|
|
{
|
2019-09-04 13:26:10 +00:00
|
|
|
Q_CHECK_FILE_NAME(entry, false);
|
2011-04-27 10:05:43 +00:00
|
|
|
what |= QFileSystemMetaData::WinLnkType | QFileSystemMetaData::WinStatFlags;
|
|
|
|
data.entryFlags &= ~what;
|
|
|
|
|
|
|
|
QFileSystemEntry fname;
|
|
|
|
data.knownFlagsMask |= QFileSystemMetaData::WinLnkType;
|
2012-11-23 13:54:18 +00:00
|
|
|
// Check for ".lnk": Directories named ".lnk" should be skipped, corrupted
|
|
|
|
// link files should still be detected as links.
|
|
|
|
const QString origFilePath = entry.filePath();
|
2021-09-27 21:03:58 +00:00
|
|
|
if (origFilePath.endsWith(QLatin1String(".lnk")) && !isDirPath(origFilePath, nullptr)) {
|
2011-04-27 10:05:43 +00:00
|
|
|
data.entryFlags |= QFileSystemMetaData::WinLnkType;
|
|
|
|
fname = QFileSystemEntry(readLink(entry));
|
|
|
|
} else {
|
|
|
|
fname = entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fname.isEmpty()) {
|
|
|
|
data.knownFlagsMask |= what;
|
|
|
|
clearWinStatData(data);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (what & QFileSystemMetaData::WinStatFlags) {
|
|
|
|
UINT oldmode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
|
|
|
|
clearWinStatData(data);
|
|
|
|
WIN32_FIND_DATA findData;
|
|
|
|
// The memory structure for WIN32_FIND_DATA is same as WIN32_FILE_ATTRIBUTE_DATA
|
|
|
|
// for all members used by fillFindData().
|
2018-08-29 06:31:08 +00:00
|
|
|
bool ok = ::GetFileAttributesEx(reinterpret_cast<const wchar_t*>(fname.nativeFilePath().utf16()),
|
|
|
|
GetFileExInfoStandard,
|
2011-04-27 10:05:43 +00:00
|
|
|
reinterpret_cast<WIN32_FILE_ATTRIBUTE_DATA *>(&findData));
|
|
|
|
if (ok) {
|
|
|
|
data.fillFromFindData(findData, false, fname.isDriveRoot());
|
|
|
|
} else {
|
2020-07-28 09:20:24 +00:00
|
|
|
const DWORD lastError = GetLastError();
|
|
|
|
if (lastError == ERROR_LOGON_FAILURE || lastError == ERROR_BAD_NETPATH // disconnected drive
|
|
|
|
|| (!tryFindFallback(fname, data) && !tryDriveUNCFallback(fname, data))) {
|
|
|
|
data.clearFlags();
|
|
|
|
SetErrorMode(oldmode);
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
SetErrorMode(oldmode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (what & QFileSystemMetaData::Permissions)
|
|
|
|
fillPermissions(fname, data, what);
|
2018-12-20 16:38:03 +00:00
|
|
|
if (what & QFileSystemMetaData::LinkType) {
|
2011-04-27 10:05:43 +00:00
|
|
|
data.knownFlagsMask |= QFileSystemMetaData::LinkType;
|
|
|
|
if (data.fileAttribute_ & FILE_ATTRIBUTE_REPARSE_POINT) {
|
|
|
|
WIN32_FIND_DATA findData;
|
2012-01-18 13:44:42 +00:00
|
|
|
if (getFindData(fname.nativeFilePath(), findData))
|
|
|
|
data.fillFromFindData(findData, true);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
data.knownFlagsMask |= what;
|
|
|
|
return data.hasFlags(what);
|
|
|
|
}
|
|
|
|
|
2013-10-17 09:13:01 +00:00
|
|
|
static inline bool mkDir(const QString &path, DWORD *lastError = 0)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-10-17 09:13:01 +00:00
|
|
|
if (lastError)
|
|
|
|
*lastError = 0;
|
|
|
|
const QString longPath = QFSFileEnginePrivate::longFileName(path);
|
|
|
|
const bool result = ::CreateDirectory((wchar_t*)longPath.utf16(), 0);
|
|
|
|
if (lastError) // Capture lastError before any QString is freed since custom allocators might change it.
|
|
|
|
*lastError = GetLastError();
|
|
|
|
return result;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool rmDir(const QString &path)
|
|
|
|
{
|
|
|
|
return ::RemoveDirectory((wchar_t*)QFSFileEnginePrivate::longFileName(path).utf16());
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:56:16 +00:00
|
|
|
//static
|
|
|
|
bool QFileSystemEngine::isDirPath(const QString &dirPath, bool *existed)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QString path = dirPath;
|
|
|
|
if (path.length() == 2 && path.at(1) == QLatin1Char(':'))
|
|
|
|
path += QLatin1Char('\\');
|
|
|
|
|
2018-08-29 06:31:08 +00:00
|
|
|
const QString longPath = QFSFileEnginePrivate::longFileName(path);
|
|
|
|
DWORD fileAttrib = ::GetFileAttributes(reinterpret_cast<const wchar_t*>(longPath.utf16()));
|
2011-04-27 10:05:43 +00:00
|
|
|
if (fileAttrib == INVALID_FILE_ATTRIBUTES) {
|
|
|
|
int errorCode = GetLastError();
|
|
|
|
if (errorCode == ERROR_ACCESS_DENIED || errorCode == ERROR_SHARING_VIOLATION) {
|
|
|
|
WIN32_FIND_DATA findData;
|
2018-08-29 06:31:08 +00:00
|
|
|
if (getFindData(longPath, findData))
|
2011-04-27 10:05:43 +00:00
|
|
|
fileAttrib = findData.dwFileAttributes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existed)
|
|
|
|
*existed = fileAttrib != INVALID_FILE_ATTRIBUTES;
|
|
|
|
|
|
|
|
if (fileAttrib == INVALID_FILE_ATTRIBUTES)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return fileAttrib & FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
}
|
|
|
|
|
2019-09-25 10:52:07 +00:00
|
|
|
// NOTE: if \a shouldMkdirFirst is false, we assume the caller did try to mkdir
|
|
|
|
// before calling this function.
|
|
|
|
static bool createDirectoryWithParents(const QString &nativeName, bool shouldMkdirFirst = true)
|
|
|
|
{
|
|
|
|
const auto isUNCRoot = [](const QString &nativeName) {
|
|
|
|
return nativeName.startsWith(QLatin1String("\\\\")) && nativeName.count(QDir::separator()) <= 3;
|
|
|
|
};
|
|
|
|
const auto isDriveName = [](const QString &nativeName) {
|
|
|
|
return nativeName.size() == 2 && nativeName.at(1) == QLatin1Char(':');
|
|
|
|
};
|
|
|
|
const auto isDir = [](const QString &nativeName) {
|
|
|
|
bool exists = false;
|
2021-09-28 11:56:16 +00:00
|
|
|
return QFileSystemEngine::isDirPath(nativeName, &exists) && exists;
|
2019-09-25 10:52:07 +00:00
|
|
|
};
|
|
|
|
// Do not try to mkdir a UNC root path or a drive letter.
|
|
|
|
if (isUNCRoot(nativeName) || isDriveName(nativeName))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (shouldMkdirFirst) {
|
|
|
|
if (mkDir(nativeName))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int backSlash = nativeName.lastIndexOf(QDir::separator());
|
|
|
|
if (backSlash < 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const QString parentNativeName = nativeName.left(backSlash);
|
|
|
|
if (!createDirectoryWithParents(parentNativeName))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// try again
|
|
|
|
if (mkDir(nativeName))
|
|
|
|
return true;
|
|
|
|
return isDir(nativeName);
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
//static
|
|
|
|
bool QFileSystemEngine::createDirectory(const QFileSystemEntry &entry, bool createParents)
|
|
|
|
{
|
|
|
|
QString dirName = entry.filePath();
|
2019-09-04 13:26:10 +00:00
|
|
|
Q_CHECK_FILE_NAME(dirName, false);
|
|
|
|
|
2019-09-25 10:52:07 +00:00
|
|
|
dirName = QDir::toNativeSeparators(QDir::cleanPath(dirName));
|
|
|
|
|
|
|
|
// try to mkdir this directory
|
|
|
|
DWORD lastError;
|
|
|
|
if (mkDir(dirName, &lastError))
|
2011-04-27 10:05:43 +00:00
|
|
|
return true;
|
2019-09-25 10:52:07 +00:00
|
|
|
// mkpath should return true, if the directory already exists, mkdir false.
|
|
|
|
if (!createParents)
|
|
|
|
return false;
|
|
|
|
if (lastError == ERROR_ALREADY_EXISTS)
|
|
|
|
return isDirPath(dirName, nullptr);
|
|
|
|
|
|
|
|
return createDirectoryWithParents(dirName, false);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
bool QFileSystemEngine::removeDirectory(const QFileSystemEntry &entry, bool removeEmptyParents)
|
|
|
|
{
|
|
|
|
QString dirName = entry.filePath();
|
2019-09-04 13:26:10 +00:00
|
|
|
Q_CHECK_FILE_NAME(dirName, false);
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
if (removeEmptyParents) {
|
|
|
|
dirName = QDir::toNativeSeparators(QDir::cleanPath(dirName));
|
|
|
|
for (int oldslash = 0, slash=dirName.length(); slash > 0; oldslash = slash) {
|
2020-05-22 14:49:01 +00:00
|
|
|
const auto chunkRef = QStringView{dirName}.left(slash);
|
2016-03-31 09:01:59 +00:00
|
|
|
if (chunkRef.length() == 2 && chunkRef.at(0).isLetter() && chunkRef.at(1) == QLatin1Char(':'))
|
2011-04-27 10:05:43 +00:00
|
|
|
break;
|
2016-03-31 09:01:59 +00:00
|
|
|
const QString chunk = chunkRef.toString();
|
2021-09-27 21:03:58 +00:00
|
|
|
if (!isDirPath(chunk, nullptr))
|
2011-04-27 10:05:43 +00:00
|
|
|
return false;
|
|
|
|
if (!rmDir(chunk))
|
|
|
|
return oldslash != 0;
|
|
|
|
slash = dirName.lastIndexOf(QDir::separator(), oldslash-1);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return rmDir(entry.filePath());
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
QString QFileSystemEngine::rootPath()
|
|
|
|
{
|
2015-04-16 17:54:52 +00:00
|
|
|
QString ret = QString::fromLatin1(qgetenv("SystemDrive"));
|
2011-04-27 10:05:43 +00:00
|
|
|
if (ret.isEmpty())
|
|
|
|
ret = QLatin1String("c:");
|
|
|
|
ret.append(QLatin1Char('/'));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
QString QFileSystemEngine::homePath()
|
|
|
|
{
|
|
|
|
QString ret;
|
2016-12-19 09:34:32 +00:00
|
|
|
#if QT_CONFIG(fslibs)
|
2017-07-09 20:51:33 +00:00
|
|
|
initGlobalSid();
|
|
|
|
{
|
2011-04-27 10:05:43 +00:00
|
|
|
HANDLE hnd = ::GetCurrentProcess();
|
|
|
|
HANDLE token = 0;
|
|
|
|
BOOL ok = ::OpenProcessToken(hnd, TOKEN_QUERY, &token);
|
|
|
|
if (ok) {
|
|
|
|
DWORD dwBufferSize = 0;
|
|
|
|
// First call, to determine size of the strings (with '\0').
|
2017-07-09 20:51:33 +00:00
|
|
|
ok = GetUserProfileDirectory(token, NULL, &dwBufferSize);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!ok && dwBufferSize != 0) { // We got the required buffer size
|
|
|
|
wchar_t *userDirectory = new wchar_t[dwBufferSize];
|
|
|
|
// Second call, now we can fill the allocated buffer.
|
2017-07-09 20:51:33 +00:00
|
|
|
ok = GetUserProfileDirectory(token, userDirectory, &dwBufferSize);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (ok)
|
|
|
|
ret = QString::fromWCharArray(userDirectory);
|
|
|
|
delete [] userDirectory;
|
|
|
|
}
|
|
|
|
::CloseHandle(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (ret.isEmpty() || !QFile::exists(ret)) {
|
2015-04-16 17:54:52 +00:00
|
|
|
ret = QString::fromLocal8Bit(qgetenv("USERPROFILE"));
|
2011-04-27 10:05:43 +00:00
|
|
|
if (ret.isEmpty() || !QFile::exists(ret)) {
|
2015-04-16 17:54:52 +00:00
|
|
|
ret = QString::fromLocal8Bit(qgetenv("HOMEDRIVE"))
|
|
|
|
+ QString::fromLocal8Bit(qgetenv("HOMEPATH"));
|
2011-04-27 10:05:43 +00:00
|
|
|
if (ret.isEmpty() || !QFile::exists(ret)) {
|
2015-04-16 17:54:52 +00:00
|
|
|
ret = QString::fromLocal8Bit(qgetenv("HOME"));
|
2016-03-23 09:25:46 +00:00
|
|
|
if (ret.isEmpty() || !QFile::exists(ret))
|
|
|
|
ret = rootPath();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QDir::fromNativeSeparators(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString QFileSystemEngine::tempPath()
|
|
|
|
{
|
|
|
|
QString ret;
|
|
|
|
wchar_t tempPath[MAX_PATH];
|
2012-07-16 13:35:58 +00:00
|
|
|
const DWORD len = GetTempPath(MAX_PATH, tempPath);
|
|
|
|
if (len) { // GetTempPath() can return short names, expand.
|
|
|
|
wchar_t longTempPath[MAX_PATH];
|
|
|
|
const DWORD longLen = GetLongPathName(tempPath, longTempPath, MAX_PATH);
|
|
|
|
ret = longLen && longLen < MAX_PATH ?
|
|
|
|
QString::fromWCharArray(longTempPath, longLen) :
|
|
|
|
QString::fromWCharArray(tempPath, len);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!ret.isEmpty()) {
|
|
|
|
while (ret.endsWith(QLatin1Char('\\')))
|
|
|
|
ret.chop(1);
|
|
|
|
ret = QDir::fromNativeSeparators(ret);
|
|
|
|
}
|
|
|
|
if (ret.isEmpty()) {
|
2011-08-26 09:03:55 +00:00
|
|
|
ret = QLatin1String("C:/tmp");
|
|
|
|
} else if (ret.length() >= 2 && ret[1] == QLatin1Char(':'))
|
|
|
|
ret[0] = ret.at(0).toUpper(); // Force uppercase drive letters.
|
2011-04-27 10:05:43 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QFileSystemEngine::setCurrentPath(const QFileSystemEntry &entry)
|
|
|
|
{
|
|
|
|
QFileSystemMetaData meta;
|
|
|
|
fillMetaData(entry, meta, QFileSystemMetaData::ExistsAttribute | QFileSystemMetaData::DirectoryType);
|
2020-11-16 03:19:17 +00:00
|
|
|
if (!(meta.exists() && meta.isDirectory()))
|
2011-04-27 10:05:43 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
//TODO: this should really be using nativeFilePath(), but that returns a path in long format \\?\c:\foo
|
|
|
|
//which causes many problems later on when it's returned through currentPath()
|
|
|
|
return ::SetCurrentDirectory(reinterpret_cast<const wchar_t*>(QDir::toNativeSeparators(entry.filePath()).utf16())) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFileSystemEntry QFileSystemEngine::currentPath()
|
|
|
|
{
|
|
|
|
QString ret;
|
|
|
|
DWORD size = 0;
|
|
|
|
wchar_t currentName[PATH_MAX];
|
|
|
|
size = ::GetCurrentDirectory(PATH_MAX, currentName);
|
|
|
|
if (size != 0) {
|
|
|
|
if (size > PATH_MAX) {
|
|
|
|
wchar_t *newCurrentName = new wchar_t[size];
|
|
|
|
if (::GetCurrentDirectory(PATH_MAX, newCurrentName) != 0)
|
|
|
|
ret = QString::fromWCharArray(newCurrentName, size);
|
|
|
|
delete [] newCurrentName;
|
|
|
|
} else {
|
|
|
|
ret = QString::fromWCharArray(currentName, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret.length() >= 2 && ret[1] == QLatin1Char(':'))
|
|
|
|
ret[0] = ret.at(0).toUpper(); // Force uppercase drive letters.
|
|
|
|
return QFileSystemEntry(ret, QFileSystemEntry::FromNativePath());
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
bool QFileSystemEngine::createLink(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error)
|
|
|
|
{
|
2021-04-12 12:56:18 +00:00
|
|
|
bool ret = false;
|
|
|
|
IShellLink *psl = nullptr;
|
|
|
|
HRESULT hres = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLink,
|
|
|
|
reinterpret_cast<void **>(&psl));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2021-04-12 12:56:18 +00:00
|
|
|
bool neededCoInit = false;
|
|
|
|
if (hres == CO_E_NOTINITIALIZED) { // COM was not initialized
|
|
|
|
neededCoInit = true;
|
|
|
|
CoInitialize(nullptr);
|
|
|
|
hres = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLink,
|
|
|
|
reinterpret_cast<void **>(&psl));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SUCCEEDED(hres)) {
|
|
|
|
const auto name = QDir::toNativeSeparators(source.filePath());
|
|
|
|
const auto pathName = QDir::toNativeSeparators(source.path());
|
|
|
|
if (SUCCEEDED(psl->SetPath(reinterpret_cast<const wchar_t *>(name.utf16())))
|
|
|
|
&& SUCCEEDED(psl->SetWorkingDirectory(reinterpret_cast<const wchar_t *>(pathName.utf16())))) {
|
|
|
|
IPersistFile *ppf = nullptr;
|
|
|
|
if (SUCCEEDED(psl->QueryInterface(IID_IPersistFile, reinterpret_cast<void **>(&ppf)))) {
|
|
|
|
ret = SUCCEEDED(ppf->Save(reinterpret_cast<const wchar_t *>(target.filePath().utf16()), TRUE));
|
|
|
|
ppf->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
psl->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
error = QSystemError(::GetLastError(), QSystemError::NativeError);
|
|
|
|
|
|
|
|
if (neededCoInit)
|
|
|
|
CoUninitialize();
|
|
|
|
|
|
|
|
return ret;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
bool QFileSystemEngine::copyFile(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error)
|
|
|
|
{
|
|
|
|
bool ret = ::CopyFile((wchar_t*)source.nativeFilePath().utf16(),
|
|
|
|
(wchar_t*)target.nativeFilePath().utf16(), true) != 0;
|
2020-11-16 03:19:17 +00:00
|
|
|
if (!ret)
|
2011-04-27 10:05:43 +00:00
|
|
|
error = QSystemError(::GetLastError(), QSystemError::NativeError);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
bool QFileSystemEngine::renameFile(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error)
|
|
|
|
{
|
2019-09-04 13:26:10 +00:00
|
|
|
Q_CHECK_FILE_NAME(source, false);
|
|
|
|
Q_CHECK_FILE_NAME(target, false);
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
bool ret = ::MoveFile((wchar_t*)source.nativeFilePath().utf16(),
|
|
|
|
(wchar_t*)target.nativeFilePath().utf16()) != 0;
|
2020-11-16 03:19:17 +00:00
|
|
|
if (!ret)
|
2011-04-27 10:05:43 +00:00
|
|
|
error = QSystemError(::GetLastError(), QSystemError::NativeError);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-29 21:03:26 +00:00
|
|
|
//static
|
|
|
|
bool QFileSystemEngine::renameOverwriteFile(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error)
|
|
|
|
{
|
2019-09-04 13:26:10 +00:00
|
|
|
Q_CHECK_FILE_NAME(source, false);
|
|
|
|
Q_CHECK_FILE_NAME(target, false);
|
|
|
|
|
2017-06-29 21:03:26 +00:00
|
|
|
bool ret = ::MoveFileEx(reinterpret_cast<const wchar_t *>(source.nativeFilePath().utf16()),
|
|
|
|
reinterpret_cast<const wchar_t *>(target.nativeFilePath().utf16()),
|
|
|
|
MOVEFILE_REPLACE_EXISTING) != 0;
|
|
|
|
if (!ret)
|
|
|
|
error = QSystemError(::GetLastError(), QSystemError::NativeError);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
//static
|
|
|
|
bool QFileSystemEngine::removeFile(const QFileSystemEntry &entry, QSystemError &error)
|
|
|
|
{
|
2019-09-04 13:26:10 +00:00
|
|
|
Q_CHECK_FILE_NAME(entry, false);
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
bool ret = ::DeleteFile((wchar_t*)entry.nativeFilePath().utf16()) != 0;
|
2020-11-16 03:19:17 +00:00
|
|
|
if (!ret)
|
2011-04-27 10:05:43 +00:00
|
|
|
error = QSystemError(::GetLastError(), QSystemError::NativeError);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
/*
|
|
|
|
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)
|
|
|
|
{
|
2020-05-06 14:36:37 +00:00
|
|
|
// we need the "display name" of the file, so can't use nativeAbsoluteFilePath
|
|
|
|
const QString sourcePath = QDir::toNativeSeparators(absoluteName(source).filePath());
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
|
|
|
|
# if defined(__IFileOperation_INTERFACE_DEFINED__)
|
2021-07-15 15:19:43 +00:00
|
|
|
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();
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
if (!SUCCEEDED(hres))
|
2021-07-15 15:19:43 +00:00
|
|
|
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<const wchar_t*>(sourcePath.utf16()),
|
|
|
|
nullptr, IID_PPV_ARGS(&deleteItem));
|
|
|
|
if (!deleteItem)
|
|
|
|
return false;
|
|
|
|
sink = new FileOperationProgressSink;
|
|
|
|
hres = pfo->DeleteItem(deleteItem, static_cast<IFileOperationProgressSink*>(sink));
|
|
|
|
if (!SUCCEEDED(hres))
|
|
|
|
return false;
|
|
|
|
hres = pfo->PerformOperations();
|
|
|
|
if (!SUCCEEDED(hres))
|
|
|
|
return false;
|
|
|
|
newLocation = QFileSystemEntry(sink->targetPath);
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
|
2020-02-06 09:17:52 +00:00
|
|
|
# endif // no IFileOperation in SDK (mingw, likely) - fall back to SHFileOperation
|
Implement moving of a single file system entry to the trash
This implements the operation for Windows, macOS, and Unix, for now
only as a private API (since QFileSystemEngine is private). This adds
the capability as a testable function; public API to be agreed on and
added in a separate commit.
The Unix implementation follows the freedesktop.org specification [1]
version 1.0.
[1] https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
On macOS and Windows, native APIs are used, with each having some
limitations:
* on macOS, the file in the trash won't have a "put back" option,
as we don't use Finder automation, for the reasons provided in the
comments
* on Windows, we might not be able to use the modern IFileOperation
API, e.g. if Qt is built with mingw which doesn't seem to provide
the interface definition; the fallback doesn't provide access to
the file name in the trash
The test case creates files and directories, and moves them to the
trash. As part of the cleanup routine, it deletes all file system
entries created. If run on Windows without IFileOperations support,
this will add a file in the trash for each test run, filling up
hard drive space.
Task-number: QTBUG-47703
Change-Id: I5f5f4e578be2f45d7da84f70a03acbe1a12a1231
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2019-10-31 16:20:13 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
//static
|
|
|
|
bool QFileSystemEngine::setPermissions(const QFileSystemEntry &entry, QFile::Permissions permissions, QSystemError &error,
|
|
|
|
QFileSystemMetaData *data)
|
|
|
|
{
|
2019-09-04 13:26:10 +00:00
|
|
|
Q_CHECK_FILE_NAME(entry, false);
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
Q_UNUSED(data);
|
|
|
|
int mode = 0;
|
|
|
|
|
2015-04-07 12:46:40 +00:00
|
|
|
if (permissions & (QFile::ReadOwner | QFile::ReadUser | QFile::ReadGroup | QFile::ReadOther))
|
2011-04-27 10:05:43 +00:00
|
|
|
mode |= _S_IREAD;
|
2015-04-07 12:46:40 +00:00
|
|
|
if (permissions & (QFile::WriteOwner | QFile::WriteUser | QFile::WriteGroup | QFile::WriteOther))
|
2011-04-27 10:05:43 +00:00
|
|
|
mode |= _S_IWRITE;
|
|
|
|
|
|
|
|
if (mode == 0) // not supported
|
|
|
|
return false;
|
|
|
|
|
2018-08-29 06:31:08 +00:00
|
|
|
bool ret = ::_wchmod(reinterpret_cast<const wchar_t*>(entry.nativeFilePath().utf16()), mode) == 0;
|
2020-11-16 03:19:17 +00:00
|
|
|
if (!ret)
|
2011-04-27 10:05:43 +00:00
|
|
|
error = QSystemError(errno, QSystemError::StandardLibraryError);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline QDateTime fileTimeToQDateTime(const FILETIME *time)
|
|
|
|
{
|
2016-07-23 18:14:57 +00:00
|
|
|
if (time->dwHighDateTime == 0 && time->dwLowDateTime == 0)
|
|
|
|
return QDateTime();
|
|
|
|
|
2016-12-14 10:18:50 +00:00
|
|
|
SYSTEMTIME sTime;
|
2011-04-27 10:05:43 +00:00
|
|
|
FileTimeToSystemTime(time, &sTime);
|
2016-12-14 10:18:50 +00:00
|
|
|
return QDateTime(QDate(sTime.wYear, sTime.wMonth, sTime.wDay),
|
|
|
|
QTime(sTime.wHour, sTime.wMinute, sTime.wSecond, sTime.wMilliseconds),
|
|
|
|
Qt::UTC);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 18:14:57 +00:00
|
|
|
QDateTime QFileSystemMetaData::birthTime() const
|
|
|
|
{
|
|
|
|
return fileTimeToQDateTime(&birthTime_);
|
|
|
|
}
|
|
|
|
QDateTime QFileSystemMetaData::metadataChangeTime() const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2016-07-23 18:14:57 +00:00
|
|
|
return fileTimeToQDateTime(&changeTime_);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
QDateTime QFileSystemMetaData::modificationTime() const
|
|
|
|
{
|
|
|
|
return fileTimeToQDateTime(&lastWriteTime_);
|
|
|
|
}
|
|
|
|
QDateTime QFileSystemMetaData::accessTime() const
|
|
|
|
{
|
|
|
|
return fileTimeToQDateTime(&lastAccessTime_);
|
|
|
|
}
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|