format: clang-format to fix windows fallout

it looked fine over in vscod on windows but
as soon as I opened it on linux via Helix
the chaos was clear

Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
This commit is contained in:
Rachel Powers 2023-03-03 07:28:59 -07:00
parent a96519cbdc
commit 0bec0046bb

View File

@ -44,9 +44,9 @@
#include <QDirIterator> #include <QDirIterator>
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
#include <QStorageInfo>
#include <QSaveFile> #include <QSaveFile>
#include <QStandardPaths> #include <QStandardPaths>
#include <QStorageInfo>
#include <QTextStream> #include <QTextStream>
#include <QUrl> #include <QUrl>
#include <QtNetwork> #include <QtNetwork>
@ -68,10 +68,10 @@
#include <windows.h> #include <windows.h>
#include <winnls.h> #include <winnls.h>
#include <string> #include <string>
//for ShellExecute // for ShellExecute
#include <shlobj.h>
#include <objbase.h>
#include <Shellapi.h> #include <Shellapi.h>
#include <objbase.h>
#include <shlobj.h>
#else #else
#include <utime.h> #include <utime.h>
#endif #endif
@ -95,28 +95,27 @@ namespace fs = std::filesystem;
namespace fs = ghc::filesystem; namespace fs = ghc::filesystem;
#endif #endif
// clone // clone
#if defined(Q_OS_LINUX) #if defined(Q_OS_LINUX)
#include <linux/fs.h>
#include <fcntl.h> /* Definition of FICLONE* constants */
#include <sys/ioctl.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> /* Definition of FICLONE* constants */
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <unistd.h> #include <unistd.h>
#elif defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) #elif defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD)
#include <sys/attr.h> #include <sys/attr.h>
#include <sys/clonefile.h> #include <sys/clonefile.h>
#elif defined(Q_OS_WIN) #elif defined(Q_OS_WIN)
// winbtrfs clone vs rundll32 shellbtrfs.dll,ReflinkCopy // winbtrfs clone vs rundll32 shellbtrfs.dll,ReflinkCopy
#include <windows.h> #include <fileapi.h>
#include <stdio.h> #include <stdio.h>
#include <tchar.h> #include <tchar.h>
#include <fileapi.h> #include <windows.h>
// refs // refs
#include <winioctl.h> #include <winioctl.h>
# if defined(__MINGW32__) #if defined(__MINGW32__)
# include <crtdbg.h> #include <crtdbg.h>
# endif #endif
#endif #endif
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
@ -138,7 +137,6 @@ typedef struct _FSCTL_GET_INTEGRITY_INFORMATION_BUFFER {
DWORD ClusterSizeInBytes; DWORD ClusterSizeInBytes;
} FSCTL_GET_INTEGRITY_INFORMATION_BUFFER, *PFSCTL_GET_INTEGRITY_INFORMATION_BUFFER; } FSCTL_GET_INTEGRITY_INFORMATION_BUFFER, *PFSCTL_GET_INTEGRITY_INFORMATION_BUFFER;
typedef struct _FSCTL_SET_INTEGRITY_INFORMATION_BUFFER { typedef struct _FSCTL_SET_INTEGRITY_INFORMATION_BUFFER {
WORD ChecksumAlgorithm; // Checksum algorithm. e.g. CHECKSUM_TYPE_UNCHANGED, CHECKSUM_TYPE_NONE, CHECKSUM_TYPE_CRC32 WORD ChecksumAlgorithm; // Checksum algorithm. e.g. CHECKSUM_TYPE_UNCHANGED, CHECKSUM_TYPE_NONE, CHECKSUM_TYPE_CRC32
WORD Reserved; // Must be 0 WORD Reserved; // Must be 0
@ -147,17 +145,18 @@ typedef struct _FSCTL_SET_INTEGRITY_INFORMATION_BUFFER {
#endif #endif
#ifndef FSCTL_DUPLICATE_EXTENTS_TO_FILE #ifndef FSCTL_DUPLICATE_EXTENTS_TO_FILE
#define FSCTL_DUPLICATE_EXTENTS_TO_FILE CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 209, METHOD_BUFFERED, FILE_WRITE_DATA ) #define FSCTL_DUPLICATE_EXTENTS_TO_FILE CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 209, METHOD_BUFFERED, FILE_WRITE_DATA)
#endif #endif
#ifndef FSCTL_GET_INTEGRITY_INFORMATION #ifndef FSCTL_GET_INTEGRITY_INFORMATION
#define FSCTL_GET_INTEGRITY_INFORMATION CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 159, METHOD_BUFFERED, FILE_ANY_ACCESS) // FSCTL_GET_INTEGRITY_INFORMATION_BUFFER #define FSCTL_GET_INTEGRITY_INFORMATION \
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 159, METHOD_BUFFERED, FILE_ANY_ACCESS) // FSCTL_GET_INTEGRITY_INFORMATION_BUFFER
#endif #endif
#ifndef FSCTL_SET_INTEGRITY_INFORMATION #ifndef FSCTL_SET_INTEGRITY_INFORMATION
#define FSCTL_SET_INTEGRITY_INFORMATION CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 160, METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA) // FSCTL_SET_INTEGRITY_INFORMATION_BUFFER #define FSCTL_SET_INTEGRITY_INFORMATION \
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 160, METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA) // FSCTL_SET_INTEGRITY_INFORMATION_BUFFER
#endif #endif
#ifndef ERROR_NOT_CAPABLE #ifndef ERROR_NOT_CAPABLE
@ -170,7 +169,6 @@ typedef struct _FSCTL_SET_INTEGRITY_INFORMATION_BUFFER {
#endif #endif
namespace FS { namespace FS {
void ensureExists(const QDir& dir) void ensureExists(const QDir& dir)
@ -329,7 +327,6 @@ bool create_link::operator()(const QString& offset, bool dryRun)
return true; return true;
} }
/** /**
* @brief make a list off all the links ot make * @brief make a list off all the links ot make
* @param offset subdirectory form src to link to dest * @param offset subdirectory form src to link to dest
@ -355,9 +352,8 @@ void create_link::make_link_list(const QString& offset)
return; return;
} }
auto dst_path = PathCombine(dst, relative_dst_path); auto dst_path = PathCombine(dst, relative_dst_path);
LinkPair link = {src_path, dst_path}; LinkPair link = { src_path, dst_path };
m_links_to_make.append(link); m_links_to_make.append(link);
}; };
@ -377,7 +373,7 @@ void create_link::make_link_list(const QString& offset)
auto src_path = source_it.next(); auto src_path = source_it.next();
auto relative_path = src_dir.relativeFilePath(src_path); auto relative_path = src_dir.relativeFilePath(src_path);
if (m_max_depth >= 0 && PathDepth(relative_path) > m_max_depth){ if (m_max_depth >= 0 && PathDepth(relative_path) > m_max_depth) {
relative_path = PathTruncate(relative_path, m_max_depth); relative_path = PathTruncate(relative_path, m_max_depth);
src_path = src_dir.filePath(relative_path); src_path = src_dir.filePath(relative_path);
if (linkedPaths.contains(src_path)) { if (linkedPaths.contains(src_path)) {
@ -396,7 +392,6 @@ void create_link::make_link_list(const QString& offset)
bool create_link::make_links() bool create_link::make_links()
{ {
for (auto link : m_links_to_make) { for (auto link : m_links_to_make) {
QString src_path = link.src; QString src_path = link.src;
QString dst_path = link.dst; QString dst_path = link.dst;
@ -415,7 +410,6 @@ bool create_link::make_links()
fs::create_symlink(StringUtils::toStdString(src_path), StringUtils::toStdString(dst_path), m_os_err); fs::create_symlink(StringUtils::toStdString(src_path), StringUtils::toStdString(dst_path), m_os_err);
} }
if (m_os_err) { if (m_os_err) {
qWarning() << "Failed to link files:" << QString::fromStdString(m_os_err.message()); qWarning() << "Failed to link files:" << QString::fromStdString(m_os_err.message());
qDebug() << "Source file:" << src_path; qDebug() << "Source file:" << src_path;
@ -427,7 +421,8 @@ bool create_link::make_links()
m_linked++; m_linked++;
emit fileLinked(src_path, dst_path); emit fileLinked(src_path, dst_path);
} }
if (m_os_err) return false; if (m_os_err)
return false;
} }
return true; return true;
} }
@ -444,8 +439,7 @@ void create_link::runPrivileged(const QString& offset)
QString serverName = BuildConfig.LAUNCHER_APP_BINARY_NAME + "_filelink_server" + StringUtils::getRandomAlphaNumeric(); QString serverName = BuildConfig.LAUNCHER_APP_BINARY_NAME + "_filelink_server" + StringUtils::getRandomAlphaNumeric();
connect(&m_linkServer, &QLocalServer::newConnection, this, [&](){ connect(&m_linkServer, &QLocalServer::newConnection, this, [&]() {
qDebug() << "Client connected, sending out pairs"; qDebug() << "Client connected, sending out pairs";
// construct block of data to send // construct block of data to send
QByteArray block; QByteArray block;
@ -466,11 +460,10 @@ void create_link::runPrivileged(const QString& offset)
out << link.dst; out << link.dst;
} }
QLocalSocket *clientConnection = m_linkServer.nextPendingConnection(); QLocalSocket* clientConnection = m_linkServer.nextPendingConnection();
connect(clientConnection, &QLocalSocket::disconnected, connect(clientConnection, &QLocalSocket::disconnected, clientConnection, &QLocalSocket::deleteLater);
clientConnection, &QLocalSocket::deleteLater);
connect(clientConnection, &QLocalSocket::readyRead, this, [&, clientConnection](){ connect(clientConnection, &QLocalSocket::readyRead, this, [&, clientConnection]() {
QDataStream in; QDataStream in;
quint32 blockSize = 0; quint32 blockSize = 0;
in.setDevice(clientConnection); in.setDevice(clientConnection);
@ -494,7 +487,7 @@ void create_link::runPrivileged(const QString& offset)
in >> numResults; in >> numResults;
qDebug() << "numResults" << numResults; qDebug() << "numResults" << numResults;
for(quint32 i = 0; i < numResults; i++) { for (quint32 i = 0; i < numResults; i++) {
FS::LinkResult result; FS::LinkResult result;
in >> result.src; in >> result.src;
in >> result.dst; in >> result.dst;
@ -520,7 +513,6 @@ void create_link::runPrivileged(const QString& offset)
qint64 byteswritten = clientConnection->write(block); qint64 byteswritten = clientConnection->write(block);
bool bytesflushed = clientConnection->flush(); bool bytesflushed = clientConnection->flush();
qDebug() << "block flushed" << byteswritten << bytesflushed; qDebug() << "block flushed" << byteswritten << bytesflushed;
}); });
qDebug() << "Listening on pipe" << serverName; qDebug() << "Listening on pipe" << serverName;
@ -534,11 +526,12 @@ void create_link::runPrivileged(const QString& offset)
connect(linkFileProcess, &ExternalLinkFileProcess::finished, linkFileProcess, &QObject::deleteLater); connect(linkFileProcess, &ExternalLinkFileProcess::finished, linkFileProcess, &QObject::deleteLater);
linkFileProcess->start(); linkFileProcess->start();
} }
void ExternalLinkFileProcess::runLinkFile() { void ExternalLinkFileProcess::runLinkFile()
QString fileLinkExe = PathCombine(QCoreApplication::instance()->applicationDirPath(), BuildConfig.LAUNCHER_APP_BINARY_NAME + "_filelink"); {
QString fileLinkExe =
PathCombine(QCoreApplication::instance()->applicationDirPath(), BuildConfig.LAUNCHER_APP_BINARY_NAME + "_filelink");
QString params = "-s " + m_server; QString params = "-s " + m_server;
params += " -H " + QVariant(m_useHardLinks).toString(); params += " -H " + QVariant(m_useHardLinks).toString();
@ -550,13 +543,14 @@ void ExternalLinkFileProcess::runLinkFile() {
qDebug() << "Running: runas" << fileLinkExe << params; qDebug() << "Running: runas" << fileLinkExe << params;
LPCWSTR programNameWin = (const wchar_t*) fileLinkExe.utf16(); LPCWSTR programNameWin = (const wchar_t*)fileLinkExe.utf16();
LPCWSTR paramsWin = (const wchar_t*) params.utf16(); LPCWSTR paramsWin = (const wchar_t*)params.utf16();
// https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-shellexecuteinfoa // https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-shellexecuteinfoa
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL; // Optional. A handle to the owner window, used to display and position any UI that the system might produce while executing this function. ShExecInfo.hwnd = NULL; // Optional. A handle to the owner window, used to display and position any UI that the system might produce
// while executing this function.
ShExecInfo.lpVerb = L"runas"; // elevate to admin, show UAC ShExecInfo.lpVerb = L"runas"; // elevate to admin, show UAC
ShExecInfo.lpFile = programNameWin; ShExecInfo.lpFile = programNameWin;
ShExecInfo.lpParameters = paramsWin; ShExecInfo.lpParameters = paramsWin;
@ -602,7 +596,7 @@ bool deletePath(QString path)
return err.value() == 0; return err.value() == 0;
} }
bool trash(QString path, QString *pathInTrash) bool trash(QString path, QString* pathInTrash)
{ {
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
return false; return false;
@ -644,7 +638,8 @@ QString AbsolutePath(const QString& path)
int PathDepth(const QString& path) int PathDepth(const QString& path)
{ {
if (path.isEmpty()) return 0; if (path.isEmpty())
return 0;
QFileInfo info(path); QFileInfo info(path);
@ -663,11 +658,12 @@ int PathDepth(const QString& path)
QString PathTruncate(const QString& path, int depth) QString PathTruncate(const QString& path, int depth)
{ {
if (path.isEmpty() || (depth < 0) ) return ""; if (path.isEmpty() || (depth < 0))
return "";
QString trunc = QFileInfo(path).path(); QString trunc = QFileInfo(path).path();
if (PathDepth(trunc) > depth ) { if (PathDepth(trunc) > depth) {
return PathTruncate(trunc, depth); return PathTruncate(trunc, depth);
} }
@ -786,11 +782,7 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri
stream << "#!/bin/bash" stream << "#!/bin/bash"
<< "\n"; << "\n";
stream << "\"" stream << "\"" << target << "\" " << argstring << "\n";
<< target
<< "\" "
<< argstring
<< "\n";
stream.flush(); stream.flush();
f.close(); f.close();
@ -813,8 +805,7 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri
<< "\n"; << "\n";
stream << "Exec=\"" << target.toLocal8Bit() << "\"" << argstring.toLocal8Bit() << "\n"; stream << "Exec=\"" << target.toLocal8Bit() << "\"" << argstring.toLocal8Bit() << "\n";
stream << "Name=" << name.toLocal8Bit() << "\n"; stream << "Name=" << name.toLocal8Bit() << "\n";
if (!icon.isEmpty()) if (!icon.isEmpty()) {
{
stream << "Icon=" << icon.toLocal8Bit() << "\n"; stream << "Icon=" << icon.toLocal8Bit() << "\n";
} }
@ -827,55 +818,45 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri
#elif defined(Q_OS_WIN) #elif defined(Q_OS_WIN)
QFileInfo targetInfo(target); QFileInfo targetInfo(target);
if (!targetInfo.exists()) if (!targetInfo.exists()) {
{
qWarning() << "Target file does not exist!"; qWarning() << "Target file does not exist!";
return false; return false;
} }
target = targetInfo.absoluteFilePath(); target = targetInfo.absoluteFilePath();
if (target.length() >= MAX_PATH) if (target.length() >= MAX_PATH) {
{
qWarning() << "Target file path is too long!"; qWarning() << "Target file path is too long!";
return false; return false;
} }
if (!icon.isEmpty() && icon.length() >= MAX_PATH) if (!icon.isEmpty() && icon.length() >= MAX_PATH) {
{
qWarning() << "Icon path is too long!"; qWarning() << "Icon path is too long!";
return false; return false;
} }
destination += ".lnk"; destination += ".lnk";
if (destination.length() >= MAX_PATH) if (destination.length() >= MAX_PATH) {
{
qWarning() << "Destination path is too long!"; qWarning() << "Destination path is too long!";
return false; return false;
} }
QString argStr; QString argStr;
int argCount = args.count(); int argCount = args.count();
for (int i = 0; i < argCount; i++) for (int i = 0; i < argCount; i++) {
{ if (args[i].contains(' ')) {
if (args[i].contains(' '))
{
argStr.append('"').append(args[i]).append('"'); argStr.append('"').append(args[i]).append('"');
} } else {
else
{
argStr.append(args[i]); argStr.append(args[i]);
} }
if (i < argCount - 1) if (i < argCount - 1) {
{
argStr.append(" "); argStr.append(" ");
} }
} }
if (argStr.length() >= MAX_PATH) if (argStr.length() >= MAX_PATH) {
{
qWarning() << "Arguments string is too long!"; qWarning() << "Arguments string is too long!";
return false; return false;
} }
@ -884,8 +865,7 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri
// ...yes, you need to initialize the entire COM stack just to make a shortcut // ...yes, you need to initialize the entire COM stack just to make a shortcut
hres = CoInitialize(nullptr); hres = CoInitialize(nullptr);
if (FAILED(hres)) if (FAILED(hres)) {
{
qWarning() << "Failed to initialize COM!"; qWarning() << "Failed to initialize COM!";
return false; return false;
} }
@ -896,8 +876,7 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri
// create an IShellLink instance - this stores the shortcut's attributes // create an IShellLink instance - this stores the shortcut's attributes
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres)) if (SUCCEEDED(hres)) {
{
wmemset(wsz, 0, MAX_PATH); wmemset(wsz, 0, MAX_PATH);
target.toWCharArray(wsz); target.toWCharArray(wsz);
psl->SetPath(wsz); psl->SetPath(wsz);
@ -910,8 +889,7 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri
targetInfo.absolutePath().toWCharArray(wsz); targetInfo.absolutePath().toWCharArray(wsz);
psl->SetWorkingDirectory(wsz); // "Starts in" attribute psl->SetWorkingDirectory(wsz); // "Starts in" attribute
if (!icon.isEmpty()) if (!icon.isEmpty()) {
{
wmemset(wsz, 0, MAX_PATH); wmemset(wsz, 0, MAX_PATH);
icon.toWCharArray(wsz); icon.toWCharArray(wsz);
psl->SetIconLocation(wsz, 0); psl->SetIconLocation(wsz, 0);
@ -921,27 +899,21 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri
// this is the interface that will actually let us save the shortcut to disk! // this is the interface that will actually let us save the shortcut to disk!
IPersistFile* ppf; IPersistFile* ppf;
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if (SUCCEEDED(hres)) if (SUCCEEDED(hres)) {
{
wmemset(wsz, 0, MAX_PATH); wmemset(wsz, 0, MAX_PATH);
destination.toWCharArray(wsz); destination.toWCharArray(wsz);
hres = ppf->Save(wsz, TRUE); hres = ppf->Save(wsz, TRUE);
if (FAILED(hres)) if (FAILED(hres)) {
{
qWarning() << "IPresistFile->Save() failed"; qWarning() << "IPresistFile->Save() failed";
qWarning() << "hres = " << hres; qWarning() << "hres = " << hres;
} }
ppf->Release(); ppf->Release();
} } else {
else
{
qWarning() << "Failed to query IPersistFile interface from IShellLink instance"; qWarning() << "Failed to query IPersistFile interface from IShellLink instance";
qWarning() << "hres = " << hres; qWarning() << "hres = " << hres;
} }
psl->Release(); psl->Release();
} } else {
else
{
qWarning() << "Failed to create IShellLink instance"; qWarning() << "Failed to create IShellLink instance";
qWarning() << "hres = " << hres; qWarning() << "hres = " << hres;
} }
@ -977,9 +949,10 @@ bool overrideFolder(QString overwritten_path, QString override_path)
return err.value() == 0; return err.value() == 0;
} }
QString getFilesystemTypeName(FilesystemType type) { QString getFilesystemTypeName(FilesystemType type)
{
auto iter = s_filesystem_type_names.constFind(type); auto iter = s_filesystem_type_names.constFind(type);
if (iter != s_filesystem_type_names.constEnd()){ if (iter != s_filesystem_type_names.constEnd()) {
return iter.value(); return iter.value();
} }
return getFilesystemTypeName(FilesystemType::UNKNOWN); return getFilesystemTypeName(FilesystemType::UNKNOWN);
@ -988,7 +961,7 @@ QString getFilesystemTypeName(FilesystemType type) {
FilesystemType getFilesystemTypeFuzzy(const QString& name) FilesystemType getFilesystemTypeFuzzy(const QString& name)
{ {
auto iter = s_filesystem_type_names_inverse.constFind(name.toUpper()); auto iter = s_filesystem_type_names_inverse.constFind(name.toUpper());
if (iter != s_filesystem_type_names_inverse.constEnd()){ if (iter != s_filesystem_type_names_inverse.constEnd()) {
return iter.value(); return iter.value();
} }
return FilesystemType::UNKNOWN; return FilesystemType::UNKNOWN;
@ -1000,9 +973,8 @@ FilesystemType getFilesystemType(const QString& name)
auto fs_type_name = fs_type_pair.first; auto fs_type_name = fs_type_pair.first;
auto fs_type = fs_type_pair.second; auto fs_type = fs_type_pair.second;
if(name.toUpper().contains(fs_type_name.toUpper())) { if (name.toUpper().contains(fs_type_name.toUpper())) {
return fs_type; return fs_type;
} }
} }
return FilesystemType::UNKNOWN; return FilesystemType::UNKNOWN;
@ -1014,15 +986,15 @@ FilesystemType getFilesystemType(const QString& name)
*/ */
QString NearestExistentAncestor(const QString& path) QString NearestExistentAncestor(const QString& path)
{ {
if(QFileInfo::exists(path)) return path; if (QFileInfo::exists(path))
return path;
QDir dir(path); QDir dir(path);
if(!dir.makeAbsolute()) return {}; if (!dir.makeAbsolute())
do return {};
{ do {
dir.setPath(QDir::cleanPath(dir.filePath(QStringLiteral("..")))); dir.setPath(QDir::cleanPath(dir.filePath(QStringLiteral(".."))));
} } while (!dir.exists() && !dir.isRoot());
while(!dir.exists() && !dir.isRoot());
return dir.exists() ? dir.path() : QString(); return dir.exists() ? dir.path() : QString();
} }
@ -1033,7 +1005,6 @@ QString NearestExistentAncestor(const QString& path)
*/ */
FilesystemInfo statFS(const QString& path) FilesystemInfo statFS(const QString& path)
{ {
FilesystemInfo info; FilesystemInfo info;
QStorageInfo storage_info(NearestExistentAncestor(path)); QStorageInfo storage_info(NearestExistentAncestor(path));
@ -1092,7 +1063,6 @@ bool canClone(const QString& src, const QString& dst)
*/ */
bool clone::operator()(const QString& offset, bool dryRun) bool clone::operator()(const QString& offset, bool dryRun)
{ {
if (!canClone(m_src.absolutePath(), m_dst.absolutePath())) { if (!canClone(m_src.absolutePath(), m_dst.absolutePath())) {
qWarning() << "Can not clone: not same device or not clone/reflink filesystem"; qWarning() << "Can not clone: not same device or not clone/reflink filesystem";
qDebug() << "Source path:" << m_src.absolutePath(); qDebug() << "Source path:" << m_src.absolutePath();
@ -1159,10 +1129,7 @@ bool clone_file(const QString& src, const QString& dst, std::error_code& ec)
FilesystemInfo srcinfo = statFS(src); FilesystemInfo srcinfo = statFS(src);
FilesystemInfo dstinfo = statFS(dst); FilesystemInfo dstinfo = statFS(dst);
if ((srcinfo.rootPath != dstinfo.rootPath) || (srcinfo.fsType != dstinfo.fsType)) {
if ((srcinfo.rootPath != dstinfo.rootPath) || (srcinfo.fsType != dstinfo.fsType))
{
ec = std::make_error_code(std::errc::not_supported); ec = std::make_error_code(std::errc::not_supported);
qWarning() << "reflink/clone must be to the same device and filesystem! src and dst root filesystems do not match."; qWarning() << "reflink/clone must be to the same device and filesystem! src and dst root filesystems do not match.";
return false; return false;
@ -1177,18 +1144,16 @@ bool clone_file(const QString& src, const QString& dst, std::error_code& ec)
return false; return false;
} }
#elif defined(Q_OS_LINUX) #elif defined(Q_OS_LINUX)
if(!linux_ficlone(src_path, dst_path, ec)) { if (!linux_ficlone(src_path, dst_path, ec)) {
qDebug() << "failed linux_ficlone:"; qDebug() << "failed linux_ficlone:";
return false; return false;
} }
#elif defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) #elif defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD)
if(!macos_bsd_clonefile(src_path, dst_path, ec)) { if (!macos_bsd_clonefile(src_path, dst_path, ec)) {
qDebug() << "failed macos_bsd_clonefile:"; qDebug() << "failed macos_bsd_clonefile:";
return false; return false;
} }
@ -1224,23 +1189,20 @@ bool win_ioctl_clone(const std::wstring& src_path, const std::wstring& dst_path,
*/ */
HANDLE hSourceFile = CreateFileW(src_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); HANDLE hSourceFile = CreateFileW(src_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (hSourceFile == INVALID_HANDLE_VALUE) if (hSourceFile == INVALID_HANDLE_VALUE) {
{
ec = std::error_code(GetLastError(), std::system_category()); ec = std::error_code(GetLastError(), std::system_category());
qDebug() << "Failed to open source file" << src_path.c_str(); qDebug() << "Failed to open source file" << src_path.c_str();
return false; return false;
} }
ULONG fs_flags; ULONG fs_flags;
if (!GetVolumeInformationByHandleW(hSourceFile, nullptr, 0, nullptr, nullptr, &fs_flags, nullptr, 0)) if (!GetVolumeInformationByHandleW(hSourceFile, nullptr, 0, nullptr, nullptr, &fs_flags, nullptr, 0)) {
{
ec = std::error_code(GetLastError(), std::system_category()); ec = std::error_code(GetLastError(), std::system_category());
qDebug() << "Failed to get Filesystem information for " << src_path.c_str(); qDebug() << "Failed to get Filesystem information for " << src_path.c_str();
CloseHandle(hSourceFile); CloseHandle(hSourceFile);
return false; return false;
} }
if (!(fs_flags & FILE_SUPPORTS_BLOCK_REFCOUNTING)) if (!(fs_flags & FILE_SUPPORTS_BLOCK_REFCOUNTING)) {
{
SetLastError(ERROR_NOT_CAPABLE); SetLastError(ERROR_NOT_CAPABLE);
ec = std::error_code(GetLastError(), std::system_category()); ec = std::error_code(GetLastError(), std::system_category());
qWarning() << "Filesystem at " << src_path.c_str() << " does not support reflink"; qWarning() << "Filesystem at " << src_path.c_str() << " does not support reflink";
@ -1249,16 +1211,14 @@ bool win_ioctl_clone(const std::wstring& src_path, const std::wstring& dst_path,
} }
FILE_END_OF_FILE_INFO sourceFileLength; FILE_END_OF_FILE_INFO sourceFileLength;
if (!GetFileSizeEx(hSourceFile, &sourceFileLength.EndOfFile)) if (!GetFileSizeEx(hSourceFile, &sourceFileLength.EndOfFile)) {
{
ec = std::error_code(GetLastError(), std::system_category()); ec = std::error_code(GetLastError(), std::system_category());
qDebug() << "Failed to size of source file" << src_path.c_str(); qDebug() << "Failed to size of source file" << src_path.c_str();
CloseHandle(hSourceFile); CloseHandle(hSourceFile);
return false; return false;
} }
FILE_BASIC_INFO sourceFileBasicInfo; FILE_BASIC_INFO sourceFileBasicInfo;
if (!GetFileInformationByHandleEx(hSourceFile, FileBasicInfo, &sourceFileBasicInfo, sizeof(sourceFileBasicInfo))) if (!GetFileInformationByHandleEx(hSourceFile, FileBasicInfo, &sourceFileBasicInfo, sizeof(sourceFileBasicInfo))) {
{
ec = std::error_code(GetLastError(), std::system_category()); ec = std::error_code(GetLastError(), std::system_category());
qDebug() << "Failed to source file info" << src_path.c_str(); qDebug() << "Failed to source file info" << src_path.c_str();
CloseHandle(hSourceFile); CloseHandle(hSourceFile);
@ -1266,8 +1226,8 @@ bool win_ioctl_clone(const std::wstring& src_path, const std::wstring& dst_path,
} }
ULONG junk; ULONG junk;
FSCTL_GET_INTEGRITY_INFORMATION_BUFFER sourceFileIntegrity; FSCTL_GET_INTEGRITY_INFORMATION_BUFFER sourceFileIntegrity;
if (!DeviceIoControl(hSourceFile, FSCTL_GET_INTEGRITY_INFORMATION, nullptr, 0, &sourceFileIntegrity, sizeof(sourceFileIntegrity), &junk, nullptr)) if (!DeviceIoControl(hSourceFile, FSCTL_GET_INTEGRITY_INFORMATION, nullptr, 0, &sourceFileIntegrity, sizeof(sourceFileIntegrity), &junk,
{ nullptr)) {
ec = std::error_code(GetLastError(), std::system_category()); ec = std::error_code(GetLastError(), std::system_category());
qDebug() << "Failed to source file integrity info" << src_path.c_str(); qDebug() << "Failed to source file integrity info" << src_path.c_str();
CloseHandle(hSourceFile); CloseHandle(hSourceFile);
@ -1276,16 +1236,14 @@ bool win_ioctl_clone(const std::wstring& src_path, const std::wstring& dst_path,
HANDLE hDestFile = CreateFileW(dst_path.c_str(), GENERIC_READ | GENERIC_WRITE | DELETE, 0, nullptr, CREATE_NEW, 0, hSourceFile); HANDLE hDestFile = CreateFileW(dst_path.c_str(), GENERIC_READ | GENERIC_WRITE | DELETE, 0, nullptr, CREATE_NEW, 0, hSourceFile);
if (hDestFile == INVALID_HANDLE_VALUE) if (hDestFile == INVALID_HANDLE_VALUE) {
{
ec = std::error_code(GetLastError(), std::system_category()); ec = std::error_code(GetLastError(), std::system_category());
qDebug() << "Failed to open dest file" << dst_path.c_str(); qDebug() << "Failed to open dest file" << dst_path.c_str();
CloseHandle(hSourceFile); CloseHandle(hSourceFile);
return false; return false;
} }
FILE_DISPOSITION_INFO destFileDispose = { TRUE }; FILE_DISPOSITION_INFO destFileDispose = { TRUE };
if (!SetFileInformationByHandle(hDestFile, FileDispositionInfo, &destFileDispose, sizeof(destFileDispose))) if (!SetFileInformationByHandle(hDestFile, FileDispositionInfo, &destFileDispose, sizeof(destFileDispose))) {
{
ec = std::error_code(GetLastError(), std::system_category()); ec = std::error_code(GetLastError(), std::system_category());
qDebug() << "Failed to set dest file info" << dst_path.c_str(); qDebug() << "Failed to set dest file info" << dst_path.c_str();
CloseHandle(hSourceFile); CloseHandle(hSourceFile);
@ -1293,25 +1251,24 @@ bool win_ioctl_clone(const std::wstring& src_path, const std::wstring& dst_path,
return false; return false;
} }
if (!DeviceIoControl(hDestFile, FSCTL_SET_SPARSE, nullptr, 0, nullptr, 0, &junk, nullptr)) if (!DeviceIoControl(hDestFile, FSCTL_SET_SPARSE, nullptr, 0, nullptr, 0, &junk, nullptr)) {
{
ec = std::error_code(GetLastError(), std::system_category()); ec = std::error_code(GetLastError(), std::system_category());
qDebug() << "Failed to set dest sparseness" << dst_path.c_str(); qDebug() << "Failed to set dest sparseness" << dst_path.c_str();
CloseHandle(hSourceFile); CloseHandle(hSourceFile);
CloseHandle(hDestFile); CloseHandle(hDestFile);
return false; return false;
} }
FSCTL_SET_INTEGRITY_INFORMATION_BUFFER setDestFileintegrity = { sourceFileIntegrity.ChecksumAlgorithm, sourceFileIntegrity.Reserved, sourceFileIntegrity.Flags }; FSCTL_SET_INTEGRITY_INFORMATION_BUFFER setDestFileintegrity = { sourceFileIntegrity.ChecksumAlgorithm, sourceFileIntegrity.Reserved,
if (!DeviceIoControl(hDestFile, FSCTL_SET_INTEGRITY_INFORMATION, &setDestFileintegrity, sizeof(setDestFileintegrity), nullptr, 0, nullptr, nullptr)) sourceFileIntegrity.Flags };
{ if (!DeviceIoControl(hDestFile, FSCTL_SET_INTEGRITY_INFORMATION, &setDestFileintegrity, sizeof(setDestFileintegrity), nullptr, 0,
nullptr, nullptr)) {
ec = std::error_code(GetLastError(), std::system_category()); ec = std::error_code(GetLastError(), std::system_category());
qDebug() << "Failed to set dest file integrity info" << dst_path.c_str(); qDebug() << "Failed to set dest file integrity info" << dst_path.c_str();
CloseHandle(hSourceFile); CloseHandle(hSourceFile);
CloseHandle(hDestFile); CloseHandle(hDestFile);
return false; return false;
} }
if (!SetFileInformationByHandle(hDestFile, FileEndOfFileInfo, &sourceFileLength, sizeof(sourceFileLength))) if (!SetFileInformationByHandle(hDestFile, FileEndOfFileInfo, &sourceFileLength, sizeof(sourceFileLength))) {
{
ec = std::error_code(GetLastError(), std::system_category()); ec = std::error_code(GetLastError(), std::system_category());
qDebug() << "Failed to set dest file size" << dst_path.c_str(); qDebug() << "Failed to set dest file size" << dst_path.c_str();
CloseHandle(hSourceFile); CloseHandle(hSourceFile);
@ -1323,38 +1280,36 @@ bool win_ioctl_clone(const std::wstring& src_path, const std::wstring& dst_path,
DUPLICATE_EXTENTS_DATA dupExtent; DUPLICATE_EXTENTS_DATA dupExtent;
dupExtent.FileHandle = hSourceFile; dupExtent.FileHandle = hSourceFile;
for (LONG64 offset = 0, remain = RoundUpToPowerOf2(sourceFileLength.EndOfFile.QuadPart, sourceFileIntegrity.ClusterSizeInBytes); remain > 0; offset += splitThreshold, remain -= splitThreshold) for (LONG64 offset = 0, remain = RoundUpToPowerOf2(sourceFileLength.EndOfFile.QuadPart, sourceFileIntegrity.ClusterSizeInBytes);
{ remain > 0; offset += splitThreshold, remain -= splitThreshold) {
dupExtent.SourceFileOffset.QuadPart = dupExtent.TargetFileOffset.QuadPart = offset; dupExtent.SourceFileOffset.QuadPart = dupExtent.TargetFileOffset.QuadPart = offset;
dupExtent.ByteCount.QuadPart = std::min(splitThreshold, remain); dupExtent.ByteCount.QuadPart = std::min(splitThreshold, remain);
if (!DeviceIoControl(hDestFile, FSCTL_DUPLICATE_EXTENTS_TO_FILE, &dupExtent, sizeof(dupExtent), nullptr, 0, &junk, nullptr)) if (!DeviceIoControl(hDestFile, FSCTL_DUPLICATE_EXTENTS_TO_FILE, &dupExtent, sizeof(dupExtent), nullptr, 0, &junk, nullptr)) {
{
DWORD err = GetLastError(); DWORD err = GetLastError();
QString additionalMessage; QString additionalMessage;
if (err == ERROR_BLOCK_TOO_MANY_REFERENCES) if (err == ERROR_BLOCK_TOO_MANY_REFERENCES) {
{
static const int MaxClonesPerFile = 8175; static const int MaxClonesPerFile = 8175;
additionalMessage = QString( additionalMessage =
QString(
" This is ERROR_BLOCK_TOO_MANY_REFERENCES and may mean you have surpassed the maximum " " This is ERROR_BLOCK_TOO_MANY_REFERENCES and may mean you have surpassed the maximum "
"allowed %1 references for a single file. " "allowed %1 references for a single file. "
"See https://docs.microsoft.com/en-us/windows-server/storage/refs/block-cloning#functionality-restrictions-and-remarks" "See "
).arg(MaxClonesPerFile); "https://docs.microsoft.com/en-us/windows-server/storage/refs/block-cloning#functionality-restrictions-and-remarks")
.arg(MaxClonesPerFile);
} }
ec = std::error_code(err, std::system_category()); ec = std::error_code(err, std::system_category());
qDebug() << "Failed copy-on-write cloning of" << src_path.c_str() << "to" << dst_path.c_str() << "with error" << err << additionalMessage; qDebug() << "Failed copy-on-write cloning of" << src_path.c_str() << "to" << dst_path.c_str() << "with error" << err
<< additionalMessage;
CloseHandle(hSourceFile); CloseHandle(hSourceFile);
CloseHandle(hDestFile); CloseHandle(hDestFile);
return false; return false;
} }
} }
if (!(sourceFileBasicInfo.FileAttributes & FILE_ATTRIBUTE_SPARSE_FILE)) if (!(sourceFileBasicInfo.FileAttributes & FILE_ATTRIBUTE_SPARSE_FILE)) {
{
FILE_SET_SPARSE_BUFFER setDestSparse = { FALSE }; FILE_SET_SPARSE_BUFFER setDestSparse = { FALSE };
if (!DeviceIoControl(hDestFile, FSCTL_SET_SPARSE, &setDestSparse, sizeof(setDestSparse), nullptr, 0, &junk, nullptr)) if (!DeviceIoControl(hDestFile, FSCTL_SET_SPARSE, &setDestSparse, sizeof(setDestSparse), nullptr, 0, &junk, nullptr)) {
{
qDebug() << "Failed to set dest file sparseness" << dst_path.c_str(); qDebug() << "Failed to set dest file sparseness" << dst_path.c_str();
CloseHandle(hSourceFile); CloseHandle(hSourceFile);
CloseHandle(hDestFile); CloseHandle(hDestFile);
@ -1363,15 +1318,13 @@ bool win_ioctl_clone(const std::wstring& src_path, const std::wstring& dst_path,
} }
sourceFileBasicInfo.CreationTime.QuadPart = 0; sourceFileBasicInfo.CreationTime.QuadPart = 0;
if (!SetFileInformationByHandle(hDestFile, FileBasicInfo, &sourceFileBasicInfo, sizeof(sourceFileBasicInfo))) if (!SetFileInformationByHandle(hDestFile, FileBasicInfo, &sourceFileBasicInfo, sizeof(sourceFileBasicInfo))) {
{
qDebug() << "Failed to set dest file creation time" << dst_path.c_str(); qDebug() << "Failed to set dest file creation time" << dst_path.c_str();
CloseHandle(hSourceFile); CloseHandle(hSourceFile);
CloseHandle(hDestFile); CloseHandle(hDestFile);
return false; return false;
} }
if (!FlushFileBuffers(hDestFile)) if (!FlushFileBuffers(hDestFile)) {
{
qDebug() << "Failed to flush dest file buffer" << dst_path.c_str(); qDebug() << "Failed to flush dest file buffer" << dst_path.c_str();
CloseHandle(hSourceFile); CloseHandle(hSourceFile);
CloseHandle(hDestFile); CloseHandle(hDestFile);
@ -1393,14 +1346,14 @@ bool linux_ficlone(const std::string& src_path, const std::string& dst_path, std
// https://man7.org/linux/man-pages/man2/ioctl_ficlone.2.html // https://man7.org/linux/man-pages/man2/ioctl_ficlone.2.html
int src_fd = open(src_path.c_str(), O_RDONLY); int src_fd = open(src_path.c_str(), O_RDONLY);
if(src_fd == -1) { if (src_fd == -1) {
qDebug() << "Failed to open file:" << src_path.c_str(); qDebug() << "Failed to open file:" << src_path.c_str();
qDebug() << "Error:" << strerror(errno); qDebug() << "Error:" << strerror(errno);
ec = std::make_error_code(static_cast<std::errc>(errno)); ec = std::make_error_code(static_cast<std::errc>(errno));
return false; return false;
} }
int dst_fd = open(dst_path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); int dst_fd = open(dst_path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if(dst_fd == -1) { if (dst_fd == -1) {
qDebug() << "Failed to open file:" << dst_path.c_str(); qDebug() << "Failed to open file:" << dst_path.c_str();
qDebug() << "Error:" << strerror(errno); qDebug() << "Error:" << strerror(errno);
ec = std::make_error_code(static_cast<std::errc>(errno)); ec = std::make_error_code(static_cast<std::errc>(errno));
@ -1408,7 +1361,7 @@ bool linux_ficlone(const std::string& src_path, const std::string& dst_path, std
return false; return false;
} }
// attempt to clone // attempt to clone
if(ioctl(dst_fd, FICLONE, src_fd) == -1){ if (ioctl(dst_fd, FICLONE, src_fd) == -1) {
qDebug() << "Failed to clone file:" << src_path.c_str() << "to" << dst_path.c_str(); qDebug() << "Failed to clone file:" << src_path.c_str() << "to" << dst_path.c_str();
qDebug() << "Error:" << strerror(errno); qDebug() << "Error:" << strerror(errno);
ec = std::make_error_code(static_cast<std::errc>(errno)); ec = std::make_error_code(static_cast<std::errc>(errno));
@ -1416,11 +1369,11 @@ bool linux_ficlone(const std::string& src_path, const std::string& dst_path, std
close(dst_fd); close(dst_fd);
return false; return false;
} }
if(close(src_fd)) { if (close(src_fd)) {
qDebug() << "Failed to close file:" << src_path.c_str(); qDebug() << "Failed to close file:" << src_path.c_str();
qDebug() << "Error:" << strerror(errno); qDebug() << "Error:" << strerror(errno);
} }
if(close(dst_fd)) { if (close(dst_fd)) {
qDebug() << "Failed to close file:" << dst_path.c_str(); qDebug() << "Failed to close file:" << dst_path.c_str();
qDebug() << "Error:" << strerror(errno); qDebug() << "Error:" << strerror(errno);
} }
@ -1482,4 +1435,4 @@ uintmax_t hardLinkCount(const QString& path)
return count; return count;
} }
} } // namespace FS