forked from qt-creator/qt-creator
Utils: Add FilePath::isSameFile()
FilePath::isSameFile() checks if two files are the same file. It first checks if its on the same device. If it is, it will try to read the fileId of the files and compare them. Change-Id: I83668955cacd4e5ed03d43a3fee2be29e9d0a6f0 Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -8,9 +8,20 @@
|
||||
#include "hostosinfo.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QOperatingSystemVersion>
|
||||
#include <QRegularExpression>
|
||||
#include <QStorageInfo>
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#ifdef QTCREATOR_PCH_H
|
||||
#define CALLBACK WINAPI
|
||||
#endif
|
||||
#include <qt_windows.h>
|
||||
#include <shlobj.h>
|
||||
#else
|
||||
#include <qplatformdefs.h>
|
||||
#endif
|
||||
|
||||
namespace Utils {
|
||||
|
||||
// DeviceFileAccess
|
||||
@@ -220,6 +231,13 @@ qint64 DeviceFileAccess::bytesAvailable(const FilePath &filePath) const
|
||||
return -1;
|
||||
}
|
||||
|
||||
QByteArray DeviceFileAccess::fileId(const FilePath &filePath) const
|
||||
{
|
||||
Q_UNUSED(filePath);
|
||||
QTC_CHECK(false);
|
||||
return {};
|
||||
}
|
||||
|
||||
void DeviceFileAccess::asyncFileContents(
|
||||
const FilePath &filePath,
|
||||
const Continuation<std::optional<QByteArray>> &cont,
|
||||
@@ -509,6 +527,74 @@ qint64 DesktopDeviceFileAccess::bytesAvailable(const FilePath &filePath) const
|
||||
return QStorageInfo(filePath.path()).bytesAvailable();
|
||||
}
|
||||
|
||||
// Copied from qfilesystemengine_win.cpp
|
||||
#ifdef Q_OS_WIN
|
||||
|
||||
// File ID for Windows up to version 7.
|
||||
static inline QByteArray fileIdWin7(HANDLE handle)
|
||||
{
|
||||
BY_HANDLE_FILE_INFORMATION info;
|
||||
if (GetFileInformationByHandle(handle, &info)) {
|
||||
char buffer[sizeof "01234567:0123456701234567\0"];
|
||||
qsnprintf(buffer, sizeof(buffer), "%lx:%08lx%08lx",
|
||||
info.dwVolumeSerialNumber,
|
||||
info.nFileIndexHigh,
|
||||
info.nFileIndexLow);
|
||||
return QByteArray(buffer);
|
||||
}
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
// File ID for Windows starting from version 8.
|
||||
static QByteArray fileIdWin8(HANDLE handle)
|
||||
{
|
||||
QByteArray result;
|
||||
FILE_ID_INFO infoEx;
|
||||
if (GetFileInformationByHandleEx(handle,
|
||||
static_cast<FILE_INFO_BY_HANDLE_CLASS>(18), // FileIdInfo in Windows 8
|
||||
&infoEx, sizeof(FILE_ID_INFO))) {
|
||||
result = QByteArray::number(infoEx.VolumeSerialNumber, 16);
|
||||
result += ':';
|
||||
// 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();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static QByteArray fileIdWin(HANDLE fHandle)
|
||||
{
|
||||
return QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows8 ?
|
||||
fileIdWin8(HANDLE(fHandle)) : fileIdWin7(HANDLE(fHandle));
|
||||
}
|
||||
#endif
|
||||
|
||||
QByteArray DesktopDeviceFileAccess::fileId(const FilePath &filePath) const
|
||||
{
|
||||
QByteArray result;
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
const HANDLE handle =
|
||||
CreateFile((wchar_t*)filePath.toUserOutput().utf16(), 0,
|
||||
FILE_SHARE_READ, NULL, OPEN_EXISTING,
|
||||
FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
||||
if (handle != INVALID_HANDLE_VALUE) {
|
||||
result = fileIdWin(handle);
|
||||
CloseHandle(handle);
|
||||
}
|
||||
#else // Copied from qfilesystemengine_unix.cpp
|
||||
if (Q_UNLIKELY(filePath.isEmpty()))
|
||||
return result;
|
||||
|
||||
QT_STATBUF statResult;
|
||||
if (QT_STAT(filePath.toString().toLocal8Bit().constData(), &statResult))
|
||||
return result;
|
||||
result = QByteArray::number(quint64(statResult.st_dev), 16);
|
||||
result += ':';
|
||||
result += QByteArray::number(quint64(statResult.st_ino));
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
OsType DesktopDeviceFileAccess::osType(const FilePath &filePath) const
|
||||
{
|
||||
Q_UNUSED(filePath);
|
||||
@@ -714,6 +800,15 @@ qint64 UnixDeviceFileAccess::bytesAvailable(const FilePath &filePath) const
|
||||
return FileUtils::bytesAvailableFromDFOutput(result.stdOut);
|
||||
}
|
||||
|
||||
QByteArray UnixDeviceFileAccess::fileId(const FilePath &filePath) const
|
||||
{
|
||||
const RunResult result = runInShell("stat", {"-L", "-c", "%D:%i", filePath.path()});
|
||||
if (result.exitCode != 0)
|
||||
return {};
|
||||
|
||||
return result.stdOut;
|
||||
}
|
||||
|
||||
FilePathInfo UnixDeviceFileAccess::filePathInfo(const FilePath &filePath) const
|
||||
{
|
||||
const RunResult stat = runInShell("stat", {"-L", "-c", "%f %Y %s", filePath.path()});
|
||||
|
||||
Reference in New Issue
Block a user