2022-05-26 22:18:54 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
2022-12-14 15:02:04 +00:00
|
|
|
* Prism Launcher - Minecraft Launcher
|
2022-05-26 22:18:54 +01:00
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2022-12-14 15:02:04 +00:00
|
|
|
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
|
2023-02-09 06:42:13 +00:00
|
|
|
* Copyright (C) 2022 Rachel Powers <508861+Ryex@users.noreply.github.com>
|
2022-05-26 22:18:54 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2015-05-28 18:38:29 +01:00
|
|
|
|
|
|
|
#include "FileSystem.h"
|
2023-06-27 21:01:28 +01:00
|
|
|
#include <QPair>
|
2015-05-28 18:38:29 +01:00
|
|
|
|
2023-02-08 08:35:03 +00:00
|
|
|
#include "BuildConfig.h"
|
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
#include <QDebug>
|
2015-05-28 18:38:29 +01:00
|
|
|
#include <QDir>
|
2022-07-30 23:38:56 +01:00
|
|
|
#include <QDirIterator>
|
2018-02-11 00:29:43 +00:00
|
|
|
#include <QFile>
|
2015-05-28 18:38:29 +01:00
|
|
|
#include <QFileInfo>
|
2022-07-30 18:42:33 +01:00
|
|
|
#include <QSaveFile>
|
2016-01-05 06:32:52 +00:00
|
|
|
#include <QStandardPaths>
|
2023-03-03 14:28:59 +00:00
|
|
|
#include <QStorageInfo>
|
2018-02-11 00:29:43 +00:00
|
|
|
#include <QTextStream>
|
2022-07-30 18:42:33 +01:00
|
|
|
#include <QUrl>
|
2023-02-08 08:35:03 +00:00
|
|
|
#include <QtNetwork>
|
2023-02-09 06:42:13 +00:00
|
|
|
#include <system_error>
|
2022-11-03 19:44:23 +00:00
|
|
|
|
2022-10-30 17:54:52 +00:00
|
|
|
#include "DesktopServices.h"
|
2022-11-03 19:44:23 +00:00
|
|
|
#include "StringUtils.h"
|
2015-05-28 18:38:29 +01:00
|
|
|
|
2018-02-10 23:35:56 +00:00
|
|
|
#if defined Q_OS_WIN32
|
2023-02-11 00:41:48 +00:00
|
|
|
#define NOMINMAX
|
2022-10-22 15:56:27 +01:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2022-07-30 18:42:33 +01:00
|
|
|
#include <objbase.h>
|
|
|
|
#include <objidl.h>
|
|
|
|
#include <shlguid.h>
|
|
|
|
#include <shlobj.h>
|
|
|
|
#include <shobjidl.h>
|
|
|
|
#include <sys/utime.h>
|
2023-01-10 15:50:56 +00:00
|
|
|
#include <versionhelpers.h>
|
2022-07-30 18:42:33 +01:00
|
|
|
#include <windows.h>
|
|
|
|
#include <winnls.h>
|
|
|
|
#include <string>
|
2023-03-03 14:28:59 +00:00
|
|
|
// for ShellExecute
|
2023-02-08 08:35:03 +00:00
|
|
|
#include <Shellapi.h>
|
2023-03-03 14:28:59 +00:00
|
|
|
#include <objbase.h>
|
|
|
|
#include <shlobj.h>
|
2018-02-10 23:35:56 +00:00
|
|
|
#else
|
2022-07-30 18:42:33 +01:00
|
|
|
#include <utime.h>
|
2018-02-10 23:35:56 +00:00
|
|
|
#endif
|
|
|
|
|
2022-10-13 22:38:52 +01:00
|
|
|
// Snippet from https://github.com/gulrak/filesystem#using-it-as-single-file-header
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
2023-03-03 14:28:59 +00:00
|
|
|
#include <Availability.h> // for deployment target to support pre-catalina targets without std::fs
|
|
|
|
#endif // __APPLE__
|
2022-10-13 22:38:52 +01:00
|
|
|
|
|
|
|
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || (defined(__cplusplus) && __cplusplus >= 201703L)) && defined(__has_include)
|
|
|
|
#if __has_include(<filesystem>) && (!defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500)
|
|
|
|
#define GHC_USE_STD_FS
|
2022-07-30 23:38:56 +01:00
|
|
|
#include <filesystem>
|
2022-10-13 22:38:52 +01:00
|
|
|
namespace fs = std::filesystem;
|
2023-03-03 14:28:59 +00:00
|
|
|
#endif // MacOS min version check
|
|
|
|
#endif // Other OSes version check
|
2022-10-13 22:38:52 +01:00
|
|
|
|
|
|
|
#ifndef GHC_USE_STD_FS
|
|
|
|
#include <ghc/filesystem.hpp>
|
|
|
|
namespace fs = ghc::filesystem;
|
|
|
|
#endif
|
2022-07-30 23:38:56 +01:00
|
|
|
|
2023-02-09 06:42:13 +00:00
|
|
|
// clone
|
|
|
|
#if defined(Q_OS_LINUX)
|
2023-03-03 14:28:59 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h> /* Definition of FICLONE* constants */
|
2023-02-09 06:42:13 +00:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <sys/ioctl.h>
|
2023-02-09 09:02:40 +00:00
|
|
|
#include <unistd.h>
|
2023-07-02 05:35:37 +01:00
|
|
|
#elif defined(Q_OS_MACOS)
|
2023-02-09 06:42:13 +00:00
|
|
|
#include <sys/attr.h>
|
|
|
|
#include <sys/clonefile.h>
|
2023-02-10 13:34:48 +00:00
|
|
|
#elif defined(Q_OS_WIN)
|
|
|
|
// winbtrfs clone vs rundll32 shellbtrfs.dll,ReflinkCopy
|
2023-03-03 14:28:59 +00:00
|
|
|
#include <fileapi.h>
|
2023-02-10 13:34:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <tchar.h>
|
2023-03-03 14:28:59 +00:00
|
|
|
#include <windows.h>
|
2023-02-10 13:34:48 +00:00
|
|
|
// refs
|
|
|
|
#include <winioctl.h>
|
2023-03-03 14:28:59 +00:00
|
|
|
#if defined(__MINGW32__)
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#endif
|
2023-02-09 06:42:13 +00:00
|
|
|
#endif
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
#if defined(Q_OS_WIN)
|
2023-02-14 00:20:44 +00:00
|
|
|
|
2023-02-14 00:39:56 +00:00
|
|
|
#if defined(__MINGW32__)
|
2023-02-11 03:06:49 +00:00
|
|
|
|
|
|
|
typedef struct _DUPLICATE_EXTENTS_DATA {
|
|
|
|
HANDLE FileHandle;
|
|
|
|
LARGE_INTEGER SourceFileOffset;
|
|
|
|
LARGE_INTEGER TargetFileOffset;
|
|
|
|
LARGE_INTEGER ByteCount;
|
|
|
|
} DUPLICATE_EXTENTS_DATA, *PDUPLICATE_EXTENTS_DATA;
|
|
|
|
|
|
|
|
typedef struct _FSCTL_GET_INTEGRITY_INFORMATION_BUFFER {
|
2023-03-03 14:28:59 +00:00
|
|
|
WORD ChecksumAlgorithm; // Checksum algorithm. e.g. CHECKSUM_TYPE_UNCHANGED, CHECKSUM_TYPE_NONE, CHECKSUM_TYPE_CRC32
|
|
|
|
WORD Reserved; // Must be 0
|
|
|
|
DWORD Flags; // FSCTL_INTEGRITY_FLAG_xxx
|
2023-02-11 03:06:49 +00:00
|
|
|
DWORD ChecksumChunkSizeInBytes;
|
|
|
|
DWORD ClusterSizeInBytes;
|
|
|
|
} FSCTL_GET_INTEGRITY_INFORMATION_BUFFER, *PFSCTL_GET_INTEGRITY_INFORMATION_BUFFER;
|
|
|
|
|
|
|
|
typedef struct _FSCTL_SET_INTEGRITY_INFORMATION_BUFFER {
|
2023-03-03 14:28:59 +00:00
|
|
|
WORD ChecksumAlgorithm; // Checksum algorithm. e.g. CHECKSUM_TYPE_UNCHANGED, CHECKSUM_TYPE_NONE, CHECKSUM_TYPE_CRC32
|
|
|
|
WORD Reserved; // Must be 0
|
|
|
|
DWORD Flags; // FSCTL_INTEGRITY_FLAG_xxx
|
2023-02-11 03:06:49 +00:00
|
|
|
} FSCTL_SET_INTEGRITY_INFORMATION_BUFFER, *PFSCTL_SET_INTEGRITY_INFORMATION_BUFFER;
|
|
|
|
|
2023-02-14 00:20:44 +00:00
|
|
|
#endif
|
2023-02-11 03:06:49 +00:00
|
|
|
|
2023-02-14 00:39:56 +00:00
|
|
|
#ifndef FSCTL_DUPLICATE_EXTENTS_TO_FILE
|
2023-03-03 14:28:59 +00:00
|
|
|
#define FSCTL_DUPLICATE_EXTENTS_TO_FILE CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 209, METHOD_BUFFERED, FILE_WRITE_DATA)
|
2023-02-14 00:39:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FSCTL_GET_INTEGRITY_INFORMATION
|
2023-03-03 14:28:59 +00:00
|
|
|
#define FSCTL_GET_INTEGRITY_INFORMATION \
|
|
|
|
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 159, METHOD_BUFFERED, FILE_ANY_ACCESS) // FSCTL_GET_INTEGRITY_INFORMATION_BUFFER
|
2023-02-14 00:39:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FSCTL_SET_INTEGRITY_INFORMATION
|
2023-03-03 14:28:59 +00:00
|
|
|
#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
|
2023-02-14 00:39:56 +00:00
|
|
|
#endif
|
|
|
|
|
2023-02-14 00:20:44 +00:00
|
|
|
#ifndef ERROR_NOT_CAPABLE
|
2023-03-03 14:28:59 +00:00
|
|
|
#define ERROR_NOT_CAPABLE 775L
|
2023-02-14 00:20:44 +00:00
|
|
|
#endif
|
2023-02-14 00:39:56 +00:00
|
|
|
|
2023-02-14 00:20:44 +00:00
|
|
|
#ifndef ERROR_BLOCK_TOO_MANY_REFERENCES
|
2023-03-03 14:28:59 +00:00
|
|
|
#define ERROR_BLOCK_TOO_MANY_REFERENCES 347L
|
2023-02-14 00:20:44 +00:00
|
|
|
#endif
|
2023-02-11 03:06:49 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-10-10 04:55:55 +01:00
|
|
|
namespace FS {
|
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
void ensureExists(const QDir& dir)
|
2015-05-28 18:38:29 +01:00
|
|
|
{
|
2022-07-30 18:42:33 +01:00
|
|
|
if (!QDir().mkpath(dir.absolutePath())) {
|
|
|
|
throw FileSystemException("Unable to create folder " + dir.dirName() + " (" + dir.absolutePath() + ")");
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2015-05-28 18:38:29 +01:00
|
|
|
}
|
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
void write(const QString& filename, const QByteArray& data)
|
2015-05-28 18:38:29 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
ensureExists(QFileInfo(filename).dir());
|
|
|
|
QSaveFile file(filename);
|
2022-07-30 18:42:33 +01:00
|
|
|
if (!file.open(QSaveFile::WriteOnly)) {
|
|
|
|
throw FileSystemException("Couldn't open " + filename + " for writing: " + file.errorString());
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2022-07-30 18:42:33 +01:00
|
|
|
if (data.size() != file.write(data)) {
|
|
|
|
throw FileSystemException("Error writing data to " + filename + ": " + file.errorString());
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2022-07-30 18:42:33 +01:00
|
|
|
if (!file.commit()) {
|
|
|
|
throw FileSystemException("Error while committing data to " + filename + ": " + file.errorString());
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2015-05-28 18:38:29 +01:00
|
|
|
}
|
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
QByteArray read(const QString& filename)
|
2015-05-28 18:38:29 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QFile file(filename);
|
2022-07-30 18:42:33 +01:00
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
|
|
|
throw FileSystemException("Unable to open " + filename + " for reading: " + file.errorString());
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
|
|
|
const qint64 size = file.size();
|
|
|
|
QByteArray data(int(size), 0);
|
|
|
|
const qint64 ret = file.read(data.data(), size);
|
2022-07-30 18:42:33 +01:00
|
|
|
if (ret == -1 || ret != size) {
|
|
|
|
throw FileSystemException("Error reading data from " + filename + ": " + file.errorString());
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
|
|
|
return data;
|
2015-05-28 18:38:29 +01:00
|
|
|
}
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2016-11-17 03:09:24 +00:00
|
|
|
bool updateTimestamp(const QString& filename)
|
|
|
|
{
|
2018-02-10 23:35:56 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
2018-07-15 13:51:05 +01:00
|
|
|
std::wstring filename_utf_16 = filename.toStdWString();
|
|
|
|
return (_wutime64(filename_utf_16.c_str(), nullptr) == 0);
|
2018-02-10 23:35:56 +00:00
|
|
|
#else
|
2018-07-15 13:51:05 +01:00
|
|
|
QByteArray filenameBA = QFile::encodeName(filename);
|
|
|
|
return (utime(filenameBA.data(), nullptr) == 0);
|
2018-02-10 23:35:56 +00:00
|
|
|
#endif
|
2016-11-17 03:09:24 +00:00
|
|
|
}
|
|
|
|
|
2015-10-10 04:55:55 +01:00
|
|
|
bool ensureFilePathExists(QString filenamepath)
|
2015-10-05 00:47:27 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QFileInfo a(filenamepath);
|
|
|
|
QDir dir;
|
|
|
|
QString ensuredPath = a.path();
|
|
|
|
bool success = dir.mkpath(ensuredPath);
|
|
|
|
return success;
|
2015-10-05 00:47:27 +01:00
|
|
|
}
|
|
|
|
|
2015-10-10 04:55:55 +01:00
|
|
|
bool ensureFolderPathExists(QString foldernamepath)
|
2015-10-05 00:47:27 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QFileInfo a(foldernamepath);
|
|
|
|
QDir dir;
|
|
|
|
QString ensuredPath = a.filePath();
|
|
|
|
bool success = dir.mkpath(ensuredPath);
|
|
|
|
return success;
|
2015-10-05 00:47:27 +01:00
|
|
|
}
|
|
|
|
|
2023-02-07 07:05:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Copies a directory and it's contents from src to dest
|
|
|
|
* @param offset subdirectory form src to copy to dest
|
|
|
|
* @return if there was an error during the filecopy
|
|
|
|
*/
|
2022-10-22 22:36:47 +01:00
|
|
|
bool copy::operator()(const QString& offset, bool dryRun)
|
2015-10-05 00:47:27 +01:00
|
|
|
{
|
2022-10-13 22:38:52 +01:00
|
|
|
using copy_opts = fs::copy_options;
|
2022-10-22 22:36:47 +01:00
|
|
|
m_copied = 0; // reset counter
|
2023-06-27 20:41:36 +01:00
|
|
|
m_failedPaths.clear();
|
2022-07-30 23:38:56 +01:00
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
// NOTE always deep copy on windows. the alternatives are too messy.
|
|
|
|
#if defined Q_OS_WIN32
|
2018-07-15 13:51:05 +01:00
|
|
|
m_followSymlinks = true;
|
2022-07-30 18:42:33 +01:00
|
|
|
#endif
|
2018-07-15 13:51:05 +01:00
|
|
|
|
|
|
|
auto src = PathCombine(m_src.absolutePath(), offset);
|
|
|
|
auto dst = PathCombine(m_dst.absolutePath(), offset);
|
|
|
|
|
2022-07-30 23:38:56 +01:00
|
|
|
std::error_code err;
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2022-10-13 22:38:52 +01:00
|
|
|
fs::copy_options opt = copy_opts::none;
|
2022-07-30 23:38:56 +01:00
|
|
|
|
|
|
|
// The default behavior is to follow symlinks
|
|
|
|
if (!m_followSymlinks)
|
|
|
|
opt |= copy_opts::copy_symlinks;
|
|
|
|
|
2022-10-29 00:11:46 +01:00
|
|
|
// Function that'll do the actual copying
|
|
|
|
auto copy_file = [&](QString src_path, QString relative_dst_path) {
|
2022-11-10 18:04:42 +00:00
|
|
|
if (m_matcher && (m_matcher->matches(relative_dst_path) != m_whitelist))
|
2022-10-29 00:11:46 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto dst_path = PathCombine(dst, relative_dst_path);
|
2022-10-22 22:36:47 +01:00
|
|
|
if (!dryRun) {
|
|
|
|
ensureFilePathExists(dst_path);
|
|
|
|
fs::copy(StringUtils::toStdString(src_path), StringUtils::toStdString(dst_path), opt, err);
|
|
|
|
}
|
2022-10-29 00:11:46 +01:00
|
|
|
if (err) {
|
|
|
|
qWarning() << "Failed to copy files:" << QString::fromStdString(err.message());
|
|
|
|
qDebug() << "Source file:" << src_path;
|
|
|
|
qDebug() << "Destination file:" << dst_path;
|
2023-06-27 20:41:36 +01:00
|
|
|
m_failedPaths.append(dst_path);
|
|
|
|
emit copyFailed(relative_dst_path);
|
|
|
|
return;
|
2022-10-29 00:11:46 +01:00
|
|
|
}
|
2022-11-21 14:47:15 +00:00
|
|
|
m_copied++;
|
|
|
|
emit fileCopied(relative_dst_path);
|
2022-10-29 00:11:46 +01:00
|
|
|
};
|
2022-07-30 23:38:56 +01:00
|
|
|
|
|
|
|
// We can't use copy_opts::recursive because we need to take into account the
|
|
|
|
// blacklisted paths, so we iterate over the source directory, and if there's no blacklist
|
|
|
|
// match, we copy the file.
|
|
|
|
QDir src_dir(src);
|
2022-10-15 13:20:31 +01:00
|
|
|
QDirIterator source_it(src, QDir::Filter::Files | QDir::Filter::Hidden, QDirIterator::Subdirectories);
|
2022-07-30 23:38:56 +01:00
|
|
|
|
|
|
|
while (source_it.hasNext()) {
|
|
|
|
auto src_path = source_it.next();
|
|
|
|
auto relative_path = src_dir.relativeFilePath(src_path);
|
|
|
|
|
2022-10-29 00:11:46 +01:00
|
|
|
copy_file(src_path, relative_path);
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2022-07-30 23:38:56 +01:00
|
|
|
|
2022-10-29 00:11:46 +01:00
|
|
|
// If the root src is not a directory, the previous iterator won't run.
|
2022-11-03 19:44:23 +00:00
|
|
|
if (!fs::is_directory(StringUtils::toStdString(src)))
|
2022-10-29 00:11:46 +01:00
|
|
|
copy_file(src, "");
|
|
|
|
|
2022-07-30 23:38:56 +01:00
|
|
|
return err.value() == 0;
|
2015-10-05 00:47:27 +01:00
|
|
|
}
|
2015-10-10 04:55:55 +01:00
|
|
|
|
2023-02-10 02:48:40 +00:00
|
|
|
/// qDebug print support for the LinkPair struct
|
|
|
|
QDebug operator<<(QDebug debug, const LinkPair& lp)
|
|
|
|
{
|
|
|
|
QDebugStateSaver saver(debug);
|
|
|
|
|
|
|
|
debug.nospace() << "LinkPair{ src: " << lp.src << " , dst: " << lp.dst << " }";
|
|
|
|
return debug;
|
|
|
|
}
|
2023-02-07 07:05:06 +00:00
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
bool create_link::operator()(const QString& offset, bool dryRun)
|
2023-02-08 08:35:03 +00:00
|
|
|
{
|
2023-02-08 20:36:15 +00:00
|
|
|
m_linked = 0; // reset counter
|
|
|
|
m_path_results.clear();
|
|
|
|
m_links_to_make.clear();
|
|
|
|
|
|
|
|
m_path_results.clear();
|
2023-03-03 14:28:59 +00:00
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
make_link_list(offset);
|
2023-03-03 14:28:59 +00:00
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
if (!dryRun)
|
|
|
|
return make_links();
|
2023-02-08 08:35:03 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-07 07:05:06 +00:00
|
|
|
/**
|
2023-04-01 04:22:07 +01:00
|
|
|
* @brief Make a list of all the links to make
|
2023-04-30 02:05:48 +01:00
|
|
|
* @param offset subdirectory of src to link to dest
|
2023-02-07 07:05:06 +00:00
|
|
|
*/
|
2023-02-10 02:48:40 +00:00
|
|
|
void create_link::make_link_list(const QString& offset)
|
2023-02-07 07:05:06 +00:00
|
|
|
{
|
2023-02-08 20:36:15 +00:00
|
|
|
for (auto pair : m_path_pairs) {
|
|
|
|
const QString& srcPath = pair.src;
|
|
|
|
const QString& dstPath = pair.dst;
|
2023-02-07 07:05:06 +00:00
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
auto src = PathCombine(QDir(srcPath).absolutePath(), offset);
|
|
|
|
auto dst = PathCombine(QDir(dstPath).absolutePath(), offset);
|
2023-02-07 07:05:06 +00:00
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
// you can't hard link a directory so make sure if we deal with a directory we do so recursively
|
|
|
|
if (m_useHardLinks)
|
|
|
|
m_recursive = true;
|
2023-02-07 07:05:06 +00:00
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
// Function that'll do the actual linking
|
|
|
|
auto link_file = [&](QString src_path, QString relative_dst_path) {
|
|
|
|
if (m_matcher && (m_matcher->matches(relative_dst_path) != m_whitelist)) {
|
|
|
|
qDebug() << "path" << relative_dst_path << "in black list or not in whitelist";
|
|
|
|
return;
|
|
|
|
}
|
2023-02-07 07:05:06 +00:00
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
auto dst_path = PathCombine(dst, relative_dst_path);
|
2023-03-03 14:28:59 +00:00
|
|
|
LinkPair link = { src_path, dst_path };
|
|
|
|
m_links_to_make.append(link);
|
2023-02-08 20:36:15 +00:00
|
|
|
};
|
2023-03-03 14:28:59 +00:00
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
if ((!m_recursive) || !fs::is_directory(StringUtils::toStdString(src))) {
|
|
|
|
if (m_debug)
|
|
|
|
qDebug() << "linking single file or dir:" << src << "to" << dst;
|
|
|
|
link_file(src, "");
|
|
|
|
} else {
|
|
|
|
if (m_debug)
|
2023-04-01 04:22:07 +01:00
|
|
|
qDebug() << "linking recursively:" << src << "to" << dst << ", max_depth:" << m_max_depth;
|
2023-02-08 20:36:15 +00:00
|
|
|
QDir src_dir(src);
|
|
|
|
QDirIterator source_it(src, QDir::Filter::Files | QDir::Filter::Hidden, QDirIterator::Subdirectories);
|
|
|
|
|
2023-02-10 02:48:40 +00:00
|
|
|
QStringList linkedPaths;
|
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
while (source_it.hasNext()) {
|
|
|
|
auto src_path = source_it.next();
|
|
|
|
auto relative_path = src_dir.relativeFilePath(src_path);
|
|
|
|
|
2023-06-27 17:05:32 +01:00
|
|
|
if (m_max_depth >= 0 && pathDepth(relative_path) > m_max_depth) {
|
2023-04-01 04:22:07 +01:00
|
|
|
relative_path = pathTruncate(relative_path, m_max_depth);
|
2023-02-10 02:48:40 +00:00
|
|
|
src_path = src_dir.filePath(relative_path);
|
|
|
|
if (linkedPaths.contains(src_path)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
linkedPaths.append(src_path);
|
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
link_file(src_path, relative_path);
|
2023-02-07 07:05:06 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-03 14:28:59 +00:00
|
|
|
}
|
2023-02-08 20:36:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool create_link::make_links()
|
2023-03-03 14:28:59 +00:00
|
|
|
{
|
2023-02-08 20:36:15 +00:00
|
|
|
for (auto link : m_links_to_make) {
|
|
|
|
QString src_path = link.src;
|
|
|
|
QString dst_path = link.dst;
|
2023-04-01 04:22:07 +01:00
|
|
|
auto src_path_std = StringUtils::toStdString(link.src);
|
|
|
|
auto dst_path_std = StringUtils::toStdString(link.dst);
|
2023-02-08 20:36:15 +00:00
|
|
|
|
|
|
|
ensureFilePathExists(dst_path);
|
|
|
|
if (m_useHardLinks) {
|
|
|
|
if (m_debug)
|
|
|
|
qDebug() << "making hard link:" << src_path << "to" << dst_path;
|
2023-04-01 04:22:07 +01:00
|
|
|
fs::create_hard_link(src_path_std, dst_path_std, m_os_err);
|
|
|
|
} else if (fs::is_directory(src_path_std)) {
|
2023-02-08 20:36:15 +00:00
|
|
|
if (m_debug)
|
|
|
|
qDebug() << "making directory_symlink:" << src_path << "to" << dst_path;
|
2023-04-01 04:22:07 +01:00
|
|
|
fs::create_directory_symlink(src_path_std, dst_path_std, m_os_err);
|
2023-02-08 20:36:15 +00:00
|
|
|
} else {
|
|
|
|
if (m_debug)
|
|
|
|
qDebug() << "making symlink:" << src_path << "to" << dst_path;
|
2023-04-01 04:22:07 +01:00
|
|
|
fs::create_symlink(src_path_std, dst_path_std, m_os_err);
|
2023-02-08 20:36:15 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 08:35:03 +00:00
|
|
|
if (m_os_err) {
|
|
|
|
qWarning() << "Failed to link files:" << QString::fromStdString(m_os_err.message());
|
2023-02-07 07:05:06 +00:00
|
|
|
qDebug() << "Source file:" << src_path;
|
|
|
|
qDebug() << "Destination file:" << dst_path;
|
2023-03-20 23:38:40 +00:00
|
|
|
qDebug() << "Error category:" << m_os_err.category().name();
|
2023-02-08 08:35:03 +00:00
|
|
|
qDebug() << "Error code:" << m_os_err.value();
|
2023-02-08 20:36:15 +00:00
|
|
|
emit linkFailed(src_path, dst_path, QString::fromStdString(m_os_err.message()), m_os_err.value());
|
2023-02-07 07:05:06 +00:00
|
|
|
} else {
|
|
|
|
m_linked++;
|
2023-02-08 20:36:15 +00:00
|
|
|
emit fileLinked(src_path, dst_path);
|
2023-02-07 07:05:06 +00:00
|
|
|
}
|
2023-03-03 14:28:59 +00:00
|
|
|
if (m_os_err)
|
|
|
|
return false;
|
2023-02-07 07:05:06 +00:00
|
|
|
}
|
2023-02-08 20:36:15 +00:00
|
|
|
return true;
|
2023-02-08 08:35:03 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 00:40:07 +00:00
|
|
|
void create_link::runPrivileged(const QString& offset)
|
2023-02-08 08:35:03 +00:00
|
|
|
{
|
2023-02-08 20:36:15 +00:00
|
|
|
m_linked = 0; // reset counter
|
|
|
|
m_path_results.clear();
|
|
|
|
m_links_to_make.clear();
|
|
|
|
|
|
|
|
bool gotResults = false;
|
|
|
|
|
|
|
|
make_link_list(offset);
|
2023-02-08 08:35:03 +00:00
|
|
|
|
2023-02-24 02:08:35 +00:00
|
|
|
QString serverName = BuildConfig.LAUNCHER_APP_BINARY_NAME + "_filelink_server" + StringUtils::getRandomAlphaNumeric();
|
2023-02-08 08:35:03 +00:00
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
connect(&m_linkServer, &QLocalServer::newConnection, this, [&]() {
|
2023-02-08 08:35:03 +00:00
|
|
|
qDebug() << "Client connected, sending out pairs";
|
|
|
|
// construct block of data to send
|
|
|
|
QByteArray block;
|
|
|
|
QDataStream out(&block, QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
qint32 blocksize = quint32(sizeof(quint32));
|
2023-02-08 20:36:15 +00:00
|
|
|
for (auto link : m_links_to_make) {
|
2023-03-03 14:28:59 +00:00
|
|
|
blocksize += quint32(link.src.size());
|
2023-02-08 20:36:15 +00:00
|
|
|
blocksize += quint32(link.dst.size());
|
2023-02-08 08:35:03 +00:00
|
|
|
}
|
|
|
|
qDebug() << "About to write block of size:" << blocksize;
|
|
|
|
out << blocksize;
|
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
out << quint32(m_links_to_make.length());
|
|
|
|
for (auto link : m_links_to_make) {
|
2023-03-03 14:28:59 +00:00
|
|
|
out << link.src;
|
2023-02-08 20:36:15 +00:00
|
|
|
out << link.dst;
|
2023-02-08 08:35:03 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
QLocalSocket* clientConnection = m_linkServer.nextPendingConnection();
|
|
|
|
connect(clientConnection, &QLocalSocket::disconnected, clientConnection, &QLocalSocket::deleteLater);
|
|
|
|
|
|
|
|
connect(clientConnection, &QLocalSocket::readyRead, this, [&, clientConnection]() {
|
2023-02-08 20:36:15 +00:00
|
|
|
QDataStream in;
|
|
|
|
quint32 blockSize = 0;
|
|
|
|
in.setDevice(clientConnection);
|
2023-04-04 02:00:56 +01:00
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
qDebug() << "Reading path results from client";
|
2023-03-20 23:38:40 +00:00
|
|
|
qDebug() << "bytes available" << clientConnection->bytesAvailable();
|
2023-02-08 20:36:15 +00:00
|
|
|
|
|
|
|
// Relies on the fact that QDataStream serializes a quint32 into
|
|
|
|
// sizeof(quint32) bytes
|
|
|
|
if (clientConnection->bytesAvailable() < (int)sizeof(quint32))
|
|
|
|
return;
|
|
|
|
qDebug() << "reading block size";
|
|
|
|
in >> blockSize;
|
|
|
|
|
|
|
|
qDebug() << "blocksize is" << blockSize;
|
2023-03-20 23:38:40 +00:00
|
|
|
qDebug() << "bytes available" << clientConnection->bytesAvailable();
|
2023-02-08 20:36:15 +00:00
|
|
|
if (clientConnection->bytesAvailable() < blockSize || in.atEnd())
|
|
|
|
return;
|
2023-03-03 14:28:59 +00:00
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
quint32 numResults;
|
|
|
|
in >> numResults;
|
|
|
|
qDebug() << "numResults" << numResults;
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
for (quint32 i = 0; i < numResults; i++) {
|
2023-02-08 20:36:15 +00:00
|
|
|
FS::LinkResult result;
|
|
|
|
in >> result.src;
|
|
|
|
in >> result.dst;
|
|
|
|
in >> result.err_msg;
|
|
|
|
qint32 err_value;
|
2023-03-03 14:28:59 +00:00
|
|
|
in >> err_value;
|
2023-02-08 20:36:15 +00:00
|
|
|
result.err_value = err_value;
|
|
|
|
if (result.err_value) {
|
2023-02-23 00:40:07 +00:00
|
|
|
qDebug() << "privileged link fail" << result.src << "to" << result.dst << "code" << result.err_value << result.err_msg;
|
2023-02-08 20:36:15 +00:00
|
|
|
emit linkFailed(result.src, result.dst, result.err_msg, result.err_value);
|
|
|
|
} else {
|
2023-02-23 00:40:07 +00:00
|
|
|
qDebug() << "privileged link success" << result.src << "to" << result.dst;
|
2023-02-08 20:36:15 +00:00
|
|
|
m_linked++;
|
|
|
|
emit fileLinked(result.src, result.dst);
|
|
|
|
}
|
|
|
|
m_path_results.append(result);
|
|
|
|
}
|
|
|
|
gotResults = true;
|
2023-03-20 23:38:40 +00:00
|
|
|
qDebug() << "results received, closing connection";
|
2023-02-08 20:36:15 +00:00
|
|
|
clientConnection->close();
|
|
|
|
});
|
2023-02-08 08:35:03 +00:00
|
|
|
|
|
|
|
qint64 byteswritten = clientConnection->write(block);
|
|
|
|
bool bytesflushed = clientConnection->flush();
|
|
|
|
qDebug() << "block flushed" << byteswritten << bytesflushed;
|
|
|
|
});
|
|
|
|
|
|
|
|
qDebug() << "Listening on pipe" << serverName;
|
|
|
|
if (!m_linkServer.listen(serverName)) {
|
|
|
|
qDebug() << "Unable to start local pipe server on" << serverName << ":" << m_linkServer.errorString();
|
2023-02-08 20:36:15 +00:00
|
|
|
return;
|
2023-02-08 08:35:03 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
ExternalLinkFileProcess* linkFileProcess = new ExternalLinkFileProcess(serverName, m_useHardLinks, this);
|
2023-02-23 00:40:07 +00:00
|
|
|
connect(linkFileProcess, &ExternalLinkFileProcess::processExited, this, [&]() { emit finishedPrivileged(gotResults); });
|
2023-02-08 08:35:03 +00:00
|
|
|
connect(linkFileProcess, &ExternalLinkFileProcess::finished, linkFileProcess, &QObject::deleteLater);
|
|
|
|
|
|
|
|
linkFileProcess->start();
|
|
|
|
}
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
void ExternalLinkFileProcess::runLinkFile()
|
|
|
|
{
|
|
|
|
QString fileLinkExe =
|
|
|
|
PathCombine(QCoreApplication::instance()->applicationDirPath(), BuildConfig.LAUNCHER_APP_BINARY_NAME + "_filelink");
|
2023-02-08 08:35:03 +00:00
|
|
|
QString params = "-s " + m_server;
|
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
params += " -H " + QVariant(m_useHardLinks).toString();
|
|
|
|
|
2023-02-08 08:35:03 +00:00
|
|
|
#if defined Q_OS_WIN32
|
|
|
|
SHELLEXECUTEINFO ShExecInfo;
|
|
|
|
|
|
|
|
fileLinkExe = fileLinkExe + ".exe";
|
|
|
|
|
|
|
|
qDebug() << "Running: runas" << fileLinkExe << params;
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
LPCWSTR programNameWin = (const wchar_t*)fileLinkExe.utf16();
|
|
|
|
LPCWSTR paramsWin = (const wchar_t*)params.utf16();
|
2023-02-08 08:35:03 +00:00
|
|
|
|
|
|
|
// https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-shellexecuteinfoa
|
|
|
|
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
|
|
|
|
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
|
2023-03-03 14:28:59 +00:00
|
|
|
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
|
2023-02-08 08:35:03 +00:00
|
|
|
ShExecInfo.lpFile = programNameWin;
|
|
|
|
ShExecInfo.lpParameters = paramsWin;
|
|
|
|
ShExecInfo.lpDirectory = NULL;
|
2023-02-08 22:30:45 +00:00
|
|
|
ShExecInfo.nShow = SW_HIDE;
|
2023-02-08 08:35:03 +00:00
|
|
|
ShExecInfo.hInstApp = NULL;
|
|
|
|
|
|
|
|
ShellExecuteEx(&ShExecInfo);
|
|
|
|
|
|
|
|
WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
|
|
|
|
CloseHandle(ShExecInfo.hProcess);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
qDebug() << "Process exited";
|
2023-02-07 07:05:06 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 00:21:54 +00:00
|
|
|
bool move(const QString& source, const QString& dest)
|
|
|
|
{
|
|
|
|
std::error_code err;
|
|
|
|
|
|
|
|
ensureFilePathExists(dest);
|
|
|
|
fs::rename(StringUtils::toStdString(source), StringUtils::toStdString(dest), err);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
qWarning() << "Failed to move file:" << QString::fromStdString(err.message());
|
|
|
|
qDebug() << "Source file:" << source;
|
|
|
|
qDebug() << "Destination file:" << dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err.value() == 0;
|
|
|
|
}
|
|
|
|
|
2015-10-10 04:55:55 +01:00
|
|
|
bool deletePath(QString path)
|
2015-10-05 00:47:27 +01:00
|
|
|
{
|
2022-07-31 00:40:25 +01:00
|
|
|
std::error_code err;
|
2019-08-04 23:44:56 +01:00
|
|
|
|
2022-11-03 19:44:23 +00:00
|
|
|
fs::remove_all(StringUtils::toStdString(path), err);
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2022-07-31 00:40:25 +01:00
|
|
|
if (err) {
|
|
|
|
qWarning() << "Failed to remove files:" << QString::fromStdString(err.message());
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
|
|
|
|
2022-07-31 00:40:25 +01:00
|
|
|
return err.value() == 0;
|
2015-10-05 00:47:27 +01:00
|
|
|
}
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
bool trash(QString path, QString* pathInTrash)
|
2022-07-30 18:42:33 +01:00
|
|
|
{
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
|
|
|
|
return false;
|
|
|
|
#else
|
2022-10-30 17:54:52 +00:00
|
|
|
// FIXME: Figure out trash in Flatpak. Qt seemingly doesn't use the Trash portal
|
|
|
|
if (DesktopServices::isFlatpak())
|
|
|
|
return false;
|
2023-01-10 15:50:56 +00:00
|
|
|
#if defined Q_OS_WIN32
|
|
|
|
if (IsWindowsServer())
|
|
|
|
return false;
|
|
|
|
#endif
|
2022-07-30 18:42:33 +01:00
|
|
|
return QFile::moveToTrash(path, pathInTrash);
|
|
|
|
#endif
|
|
|
|
}
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
QString PathCombine(const QString& path1, const QString& path2)
|
2015-10-05 00:47:27 +01:00
|
|
|
{
|
2022-07-30 18:42:33 +01:00
|
|
|
if (!path1.size())
|
2018-07-15 13:51:05 +01:00
|
|
|
return path2;
|
2022-07-30 18:42:33 +01:00
|
|
|
if (!path2.size())
|
2018-07-15 13:51:05 +01:00
|
|
|
return path1;
|
2015-10-05 00:47:27 +01:00
|
|
|
return QDir::cleanPath(path1 + QDir::separator() + path2);
|
|
|
|
}
|
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
QString PathCombine(const QString& path1, const QString& path2, const QString& path3)
|
2015-10-05 00:47:27 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
return PathCombine(PathCombine(path1, path2), path3);
|
2015-10-05 00:47:27 +01:00
|
|
|
}
|
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
QString PathCombine(const QString& path1, const QString& path2, const QString& path3, const QString& path4)
|
2017-09-08 08:02:27 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
return PathCombine(PathCombine(path1, path2, path3), path4);
|
2017-09-08 08:02:27 +01:00
|
|
|
}
|
|
|
|
|
2023-02-10 02:48:40 +00:00
|
|
|
QString AbsolutePath(const QString& path)
|
2015-10-05 00:47:27 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
return QFileInfo(path).absolutePath();
|
2015-10-05 00:47:27 +01:00
|
|
|
}
|
|
|
|
|
2023-04-01 04:22:07 +01:00
|
|
|
int pathDepth(const QString& path)
|
2023-02-10 02:48:40 +00:00
|
|
|
{
|
2023-03-03 14:28:59 +00:00
|
|
|
if (path.isEmpty())
|
|
|
|
return 0;
|
2023-02-10 02:48:40 +00:00
|
|
|
|
|
|
|
QFileInfo info(path);
|
|
|
|
|
2023-02-10 03:02:20 +00:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
|
|
|
|
auto parts = QDir::toNativeSeparators(info.path()).split(QDir::separator(), QString::SkipEmptyParts);
|
|
|
|
#else
|
2023-02-10 02:48:40 +00:00
|
|
|
auto parts = QDir::toNativeSeparators(info.path()).split(QDir::separator(), Qt::SkipEmptyParts);
|
2023-02-10 03:02:20 +00:00
|
|
|
#endif
|
2023-02-10 02:48:40 +00:00
|
|
|
|
2023-02-10 03:02:20 +00:00
|
|
|
int numParts = parts.length();
|
2023-02-10 02:48:40 +00:00
|
|
|
numParts -= parts.count(".");
|
|
|
|
numParts -= parts.count("..") * 2;
|
2023-03-03 14:28:59 +00:00
|
|
|
|
2023-02-10 02:48:40 +00:00
|
|
|
return numParts;
|
|
|
|
}
|
|
|
|
|
2023-04-01 04:22:07 +01:00
|
|
|
QString pathTruncate(const QString& path, int depth)
|
2023-02-10 02:48:40 +00:00
|
|
|
{
|
2023-03-03 14:28:59 +00:00
|
|
|
if (path.isEmpty() || (depth < 0))
|
|
|
|
return "";
|
2023-02-10 02:48:40 +00:00
|
|
|
|
|
|
|
QString trunc = QFileInfo(path).path();
|
|
|
|
|
2023-06-27 17:05:32 +01:00
|
|
|
if (pathDepth(trunc) > depth) {
|
2023-04-01 04:22:07 +01:00
|
|
|
return pathTruncate(trunc, depth);
|
2023-02-10 02:48:40 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 03:02:20 +00:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
|
|
|
|
auto parts = QDir::toNativeSeparators(trunc).split(QDir::separator(), QString::SkipEmptyParts);
|
|
|
|
#else
|
2023-02-10 02:48:40 +00:00
|
|
|
auto parts = QDir::toNativeSeparators(trunc).split(QDir::separator(), Qt::SkipEmptyParts);
|
2023-02-10 03:02:20 +00:00
|
|
|
#endif
|
|
|
|
|
2023-02-10 02:48:40 +00:00
|
|
|
if (parts.startsWith(".") && !path.startsWith(".")) {
|
|
|
|
parts.removeFirst();
|
|
|
|
}
|
2023-02-10 06:07:07 +00:00
|
|
|
if (QDir::toNativeSeparators(path).startsWith(QDir::separator())) {
|
2023-02-10 02:48:40 +00:00
|
|
|
parts.prepend("");
|
|
|
|
}
|
|
|
|
|
|
|
|
trunc = parts.join(QDir::separator());
|
|
|
|
|
|
|
|
return trunc;
|
|
|
|
}
|
|
|
|
|
2015-10-10 04:55:55 +01:00
|
|
|
QString ResolveExecutable(QString path)
|
2015-10-05 00:47:27 +01:00
|
|
|
{
|
2022-07-30 18:42:33 +01:00
|
|
|
if (path.isEmpty()) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return QString();
|
|
|
|
}
|
2022-07-30 18:42:33 +01:00
|
|
|
if (!path.contains('/')) {
|
2018-07-15 13:51:05 +01:00
|
|
|
path = QStandardPaths::findExecutable(path);
|
|
|
|
}
|
|
|
|
QFileInfo pathInfo(path);
|
2022-07-30 18:42:33 +01:00
|
|
|
if (!pathInfo.exists() || !pathInfo.isExecutable()) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
return pathInfo.absoluteFilePath();
|
2015-10-05 00:47:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize path
|
|
|
|
*
|
2017-01-16 21:42:22 +00:00
|
|
|
* Any paths inside the current folder will be normalized to relative paths (to current)
|
2015-10-05 00:47:27 +01:00
|
|
|
* Other paths will be made absolute
|
|
|
|
*/
|
2015-10-10 04:55:55 +01:00
|
|
|
QString NormalizePath(QString path)
|
2015-10-05 00:47:27 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QDir a = QDir::currentPath();
|
|
|
|
QString currentAbsolute = a.absolutePath();
|
|
|
|
|
|
|
|
QDir b(path);
|
|
|
|
QString newAbsolute = b.absolutePath();
|
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
if (newAbsolute.startsWith(currentAbsolute)) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return a.relativeFilePath(newAbsolute);
|
2022-07-30 18:42:33 +01:00
|
|
|
} else {
|
2018-07-15 13:51:05 +01:00
|
|
|
return newAbsolute;
|
|
|
|
}
|
2015-10-05 00:47:27 +01:00
|
|
|
}
|
|
|
|
|
2019-08-13 06:39:00 +01:00
|
|
|
QString badFilenameChars = "\"\\/?<>:;*|!+\r\n";
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2015-10-10 04:55:55 +01:00
|
|
|
QString RemoveInvalidFilenameChars(QString string, QChar replaceWith)
|
2015-10-05 00:47:27 +01:00
|
|
|
{
|
2022-07-30 18:42:33 +01:00
|
|
|
for (int i = 0; i < string.length(); i++) {
|
|
|
|
if (badFilenameChars.contains(string[i])) {
|
2018-07-15 13:51:05 +01:00
|
|
|
string[i] = replaceWith;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string;
|
2015-10-05 00:47:27 +01:00
|
|
|
}
|
|
|
|
|
2015-10-10 04:55:55 +01:00
|
|
|
QString DirNameFromString(QString string, QString inDir)
|
2015-10-05 00:47:27 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
int num = 0;
|
|
|
|
QString baseName = RemoveInvalidFilenameChars(string, '-');
|
|
|
|
QString dirName;
|
2022-07-30 18:42:33 +01:00
|
|
|
do {
|
|
|
|
if (num == 0) {
|
2018-07-15 13:51:05 +01:00
|
|
|
dirName = baseName;
|
2022-07-30 18:42:33 +01:00
|
|
|
} else {
|
2022-08-07 11:19:29 +01:00
|
|
|
dirName = baseName + "(" + QString::number(num) + ")";
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it's over 9000
|
|
|
|
if (num > 9000)
|
|
|
|
return "";
|
|
|
|
num++;
|
|
|
|
} while (QFileInfo(PathCombine(inDir, dirName)).exists());
|
|
|
|
return dirName;
|
2015-10-05 00:47:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-16 21:42:22 +00:00
|
|
|
// Does the folder path contain any '!'? If yes, return true, otherwise false.
|
2015-10-05 00:47:27 +01:00
|
|
|
// (This is a problem for Java)
|
2015-10-10 04:55:55 +01:00
|
|
|
bool checkProblemticPathJava(QDir folder)
|
2015-10-05 00:47:27 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QString pathfoldername = folder.absolutePath();
|
|
|
|
return pathfoldername.contains("!", Qt::CaseInsensitive);
|
2015-10-05 00:47:27 +01:00
|
|
|
}
|
|
|
|
|
2015-10-10 04:55:55 +01:00
|
|
|
QString getDesktopDir()
|
2015-10-05 00:47:27 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
return QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
|
2015-10-05 00:47:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cross-platform Shortcut creation
|
2022-10-22 15:56:27 +01:00
|
|
|
bool createShortcut(QString destination, QString target, QStringList args, QString name, QString icon)
|
2015-10-05 00:47:27 +01:00
|
|
|
{
|
2023-06-27 17:05:32 +01:00
|
|
|
if (destination.isEmpty()) {
|
|
|
|
destination = PathCombine(getDesktopDir(), RemoveInvalidFilenameChars(name));
|
|
|
|
}
|
2022-11-12 18:36:49 +00:00
|
|
|
#if defined(Q_OS_MACOS)
|
2022-11-13 13:47:37 +00:00
|
|
|
destination += ".command";
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2022-11-12 18:36:49 +00:00
|
|
|
QFile f(destination);
|
|
|
|
f.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
QTextStream stream(&f);
|
|
|
|
|
|
|
|
QString argstring;
|
|
|
|
if (!args.empty())
|
|
|
|
argstring = " \"" + args.join("\" \"") + "\"";
|
|
|
|
|
|
|
|
stream << "#!/bin/bash"
|
|
|
|
<< "\n";
|
2023-03-03 14:28:59 +00:00
|
|
|
stream << "\"" << target << "\" " << argstring << "\n";
|
2022-11-12 18:36:49 +00:00
|
|
|
|
|
|
|
stream.flush();
|
|
|
|
f.close();
|
|
|
|
|
|
|
|
f.setPermissions(f.permissions() | QFileDevice::ExeOwner | QFileDevice::ExeGroup | QFileDevice::ExeOther);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#elif defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD)
|
2023-06-27 17:05:32 +01:00
|
|
|
if (!destination.endsWith(".desktop")) // in case of isFlatpak destination is already populated
|
|
|
|
destination += ".desktop";
|
2022-10-22 15:56:27 +01:00
|
|
|
QFile f(destination);
|
2018-07-15 13:51:05 +01:00
|
|
|
f.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
QTextStream stream(&f);
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
QString argstring;
|
|
|
|
if (!args.empty())
|
|
|
|
argstring = " '" + args.join("' '") + "'";
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
stream << "[Desktop Entry]"
|
|
|
|
<< "\n";
|
|
|
|
stream << "Type=Application"
|
|
|
|
<< "\n";
|
2022-11-13 15:51:29 +00:00
|
|
|
stream << "Exec=\"" << target.toLocal8Bit() << "\"" << argstring.toLocal8Bit() << "\n";
|
2018-07-15 13:51:05 +01:00
|
|
|
stream << "Name=" << name.toLocal8Bit() << "\n";
|
2023-03-03 14:28:59 +00:00
|
|
|
if (!icon.isEmpty()) {
|
2022-10-22 15:56:27 +01:00
|
|
|
stream << "Icon=" << icon.toLocal8Bit() << "\n";
|
|
|
|
}
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
stream.flush();
|
|
|
|
f.close();
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
f.setPermissions(f.permissions() | QFileDevice::ExeOwner | QFileDevice::ExeGroup | QFileDevice::ExeOther);
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
return true;
|
2022-11-12 18:41:52 +00:00
|
|
|
#elif defined(Q_OS_WIN)
|
2022-10-22 15:56:27 +01:00
|
|
|
QFileInfo targetInfo(target);
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
if (!targetInfo.exists()) {
|
2022-10-22 15:56:27 +01:00
|
|
|
qWarning() << "Target file does not exist!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
target = targetInfo.absoluteFilePath();
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
if (target.length() >= MAX_PATH) {
|
2022-10-22 15:56:27 +01:00
|
|
|
qWarning() << "Target file path is too long!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
if (!icon.isEmpty() && icon.length() >= MAX_PATH) {
|
2022-10-22 15:56:27 +01:00
|
|
|
qWarning() << "Icon path is too long!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
destination += ".lnk";
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
if (destination.length() >= MAX_PATH) {
|
2022-10-22 15:56:27 +01:00
|
|
|
qWarning() << "Destination path is too long!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString argStr;
|
|
|
|
int argCount = args.count();
|
2023-03-03 14:28:59 +00:00
|
|
|
for (int i = 0; i < argCount; i++) {
|
|
|
|
if (args[i].contains(' ')) {
|
2022-10-22 15:56:27 +01:00
|
|
|
argStr.append('"').append(args[i]).append('"');
|
2023-03-03 14:28:59 +00:00
|
|
|
} else {
|
2022-10-22 15:56:27 +01:00
|
|
|
argStr.append(args[i]);
|
|
|
|
}
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
if (i < argCount - 1) {
|
2022-10-22 15:56:27 +01:00
|
|
|
argStr.append(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
if (argStr.length() >= MAX_PATH) {
|
2022-10-22 15:56:27 +01:00
|
|
|
qWarning() << "Arguments string is too long!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-28 13:46:54 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
// ...yes, you need to initialize the entire COM stack just to make a shortcut
|
|
|
|
hres = CoInitialize(nullptr);
|
2023-03-03 14:28:59 +00:00
|
|
|
if (FAILED(hres)) {
|
2022-10-28 13:46:54 +01:00
|
|
|
qWarning() << "Failed to initialize COM!";
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-22 15:56:27 +01:00
|
|
|
|
2022-10-28 13:46:54 +01:00
|
|
|
WCHAR wsz[MAX_PATH];
|
2022-10-22 15:56:27 +01:00
|
|
|
|
|
|
|
IShellLink* psl;
|
|
|
|
|
2022-10-28 13:46:54 +01:00
|
|
|
// create an IShellLink instance - this stores the shortcut's attributes
|
2022-10-22 15:56:27 +01:00
|
|
|
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
|
2023-03-03 14:28:59 +00:00
|
|
|
if (SUCCEEDED(hres)) {
|
2022-10-22 15:56:27 +01:00
|
|
|
wmemset(wsz, 0, MAX_PATH);
|
|
|
|
target.toWCharArray(wsz);
|
|
|
|
psl->SetPath(wsz);
|
|
|
|
|
|
|
|
wmemset(wsz, 0, MAX_PATH);
|
|
|
|
argStr.toWCharArray(wsz);
|
|
|
|
psl->SetArguments(wsz);
|
|
|
|
|
|
|
|
wmemset(wsz, 0, MAX_PATH);
|
|
|
|
targetInfo.absolutePath().toWCharArray(wsz);
|
2023-03-03 14:28:59 +00:00
|
|
|
psl->SetWorkingDirectory(wsz); // "Starts in" attribute
|
2022-10-22 15:56:27 +01:00
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
if (!icon.isEmpty()) {
|
2022-10-22 15:56:27 +01:00
|
|
|
wmemset(wsz, 0, MAX_PATH);
|
|
|
|
icon.toWCharArray(wsz);
|
|
|
|
psl->SetIconLocation(wsz, 0);
|
|
|
|
}
|
|
|
|
|
2022-10-28 13:46:54 +01:00
|
|
|
// query an IPersistFile interface from our IShellLink instance
|
|
|
|
// this is the interface that will actually let us save the shortcut to disk!
|
2022-10-22 15:56:27 +01:00
|
|
|
IPersistFile* ppf;
|
|
|
|
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
|
2023-03-03 14:28:59 +00:00
|
|
|
if (SUCCEEDED(hres)) {
|
2022-10-22 15:56:27 +01:00
|
|
|
wmemset(wsz, 0, MAX_PATH);
|
|
|
|
destination.toWCharArray(wsz);
|
|
|
|
hres = ppf->Save(wsz, TRUE);
|
2023-03-03 14:28:59 +00:00
|
|
|
if (FAILED(hres)) {
|
2022-10-22 15:56:27 +01:00
|
|
|
qWarning() << "IPresistFile->Save() failed";
|
|
|
|
qWarning() << "hres = " << hres;
|
|
|
|
}
|
|
|
|
ppf->Release();
|
2023-03-03 14:28:59 +00:00
|
|
|
} else {
|
2022-10-22 15:56:27 +01:00
|
|
|
qWarning() << "Failed to query IPersistFile interface from IShellLink instance";
|
|
|
|
qWarning() << "hres = " << hres;
|
|
|
|
}
|
|
|
|
psl->Release();
|
2023-03-03 14:28:59 +00:00
|
|
|
} else {
|
2022-10-22 15:56:27 +01:00
|
|
|
qWarning() << "Failed to create IShellLink instance";
|
|
|
|
qWarning() << "hres = " << hres;
|
|
|
|
}
|
|
|
|
|
2022-10-28 13:46:54 +01:00
|
|
|
// go away COM, nobody likes you
|
2022-10-22 15:56:27 +01:00
|
|
|
CoUninitialize();
|
|
|
|
|
|
|
|
return SUCCEEDED(hres);
|
2015-10-05 00:47:27 +01:00
|
|
|
#else
|
2018-07-15 13:51:05 +01:00
|
|
|
qWarning("Desktop Shortcuts not supported on your platform!");
|
|
|
|
return false;
|
2015-10-05 00:47:27 +01:00
|
|
|
#endif
|
|
|
|
}
|
2022-06-11 17:43:09 +01:00
|
|
|
|
|
|
|
bool overrideFolder(QString overwritten_path, QString override_path)
|
|
|
|
{
|
2022-10-13 22:38:52 +01:00
|
|
|
using copy_opts = fs::copy_options;
|
2022-08-07 12:50:39 +01:00
|
|
|
|
2022-06-11 17:43:09 +01:00
|
|
|
if (!FS::ensureFolderPathExists(overwritten_path))
|
|
|
|
return false;
|
|
|
|
|
2022-08-07 12:50:39 +01:00
|
|
|
std::error_code err;
|
2022-10-13 22:38:52 +01:00
|
|
|
fs::copy_options opt = copy_opts::recursive | copy_opts::overwrite_existing;
|
2022-06-11 17:43:09 +01:00
|
|
|
|
2022-10-27 20:14:30 +01:00
|
|
|
// FIXME: hello traveller! Apparently std::copy does NOT overwrite existing files on GNU libstdc++ on Windows?
|
2022-11-03 19:44:23 +00:00
|
|
|
fs::copy(StringUtils::toStdString(override_path), StringUtils::toStdString(overwritten_path), opt, err);
|
2022-06-11 17:43:09 +01:00
|
|
|
|
2022-08-07 12:50:39 +01:00
|
|
|
if (err) {
|
|
|
|
qCritical() << QString("Failed to apply override from %1 to %2").arg(override_path, overwritten_path);
|
|
|
|
qCritical() << "Reason:" << QString::fromStdString(err.message());
|
2022-06-11 17:43:09 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 12:50:39 +01:00
|
|
|
return err.value() == 0;
|
2022-06-11 17:43:09 +01:00
|
|
|
}
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
QString getFilesystemTypeName(FilesystemType type)
|
|
|
|
{
|
2023-02-13 23:48:58 +00:00
|
|
|
auto iter = s_filesystem_type_names.constFind(type);
|
2023-03-03 14:28:59 +00:00
|
|
|
if (iter != s_filesystem_type_names.constEnd()) {
|
2023-04-04 01:14:06 +01:00
|
|
|
return iter.value().constFirst();
|
2023-02-13 23:48:58 +00:00
|
|
|
}
|
2023-03-03 14:28:59 +00:00
|
|
|
return getFilesystemTypeName(FilesystemType::UNKNOWN);
|
2023-02-13 23:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FilesystemType getFilesystemTypeFuzzy(const QString& name)
|
|
|
|
{
|
2023-04-04 01:14:06 +01:00
|
|
|
for (auto iter = s_filesystem_type_names.constBegin(); iter != s_filesystem_type_names.constEnd(); ++iter) {
|
|
|
|
auto fs_names = iter.value();
|
|
|
|
for (auto fs_name : fs_names) {
|
|
|
|
if (name.toUpper().contains(fs_name.toUpper()))
|
|
|
|
return iter.key();
|
|
|
|
}
|
2023-02-13 23:48:58 +00:00
|
|
|
}
|
2023-03-03 14:28:59 +00:00
|
|
|
return FilesystemType::UNKNOWN;
|
2023-02-13 23:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FilesystemType getFilesystemType(const QString& name)
|
2023-03-03 14:28:59 +00:00
|
|
|
{
|
2023-04-04 01:14:06 +01:00
|
|
|
for (auto iter = s_filesystem_type_names.constBegin(); iter != s_filesystem_type_names.constEnd(); ++iter) {
|
|
|
|
auto fs_names = iter.value();
|
2023-06-27 17:05:32 +01:00
|
|
|
if (fs_names.contains(name.toUpper()))
|
2023-04-04 01:14:06 +01:00
|
|
|
return iter.key();
|
2023-02-13 23:48:58 +00:00
|
|
|
}
|
|
|
|
return FilesystemType::UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2023-02-10 13:34:48 +00:00
|
|
|
/**
|
2023-03-20 23:38:40 +00:00
|
|
|
* @brief path to the near ancestor that exists
|
2023-03-03 14:28:59 +00:00
|
|
|
*
|
2023-02-10 13:34:48 +00:00
|
|
|
*/
|
2023-04-01 04:22:07 +01:00
|
|
|
QString nearestExistentAncestor(const QString& path)
|
2023-02-10 13:34:48 +00:00
|
|
|
{
|
2023-03-03 14:28:59 +00:00
|
|
|
if (QFileInfo::exists(path))
|
|
|
|
return path;
|
2023-02-10 13:34:48 +00:00
|
|
|
|
|
|
|
QDir dir(path);
|
2023-03-03 14:28:59 +00:00
|
|
|
if (!dir.makeAbsolute())
|
|
|
|
return {};
|
|
|
|
do {
|
2023-02-10 13:34:48 +00:00
|
|
|
dir.setPath(QDir::cleanPath(dir.filePath(QStringLiteral(".."))));
|
2023-03-03 14:28:59 +00:00
|
|
|
} while (!dir.exists() && !dir.isRoot());
|
2023-02-10 13:34:48 +00:00
|
|
|
|
|
|
|
return dir.exists() ? dir.path() : QString();
|
|
|
|
}
|
2023-02-09 06:42:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief colect information about the filesystem under a file
|
2023-03-03 14:28:59 +00:00
|
|
|
*
|
2023-02-09 06:42:13 +00:00
|
|
|
*/
|
2023-02-10 13:34:48 +00:00
|
|
|
FilesystemInfo statFS(const QString& path)
|
2023-02-09 06:42:13 +00:00
|
|
|
{
|
|
|
|
FilesystemInfo info;
|
|
|
|
|
2023-04-01 04:22:07 +01:00
|
|
|
QStorageInfo storage_info(nearestExistentAncestor(path));
|
2023-02-09 06:42:13 +00:00
|
|
|
|
2023-02-23 00:40:07 +00:00
|
|
|
info.fsTypeName = storage_info.fileSystemType();
|
2023-02-09 06:42:13 +00:00
|
|
|
|
2023-02-13 23:48:58 +00:00
|
|
|
info.fsType = getFilesystemTypeFuzzy(info.fsTypeName);
|
2023-02-09 06:42:13 +00:00
|
|
|
|
|
|
|
info.blockSize = storage_info.blockSize();
|
|
|
|
info.bytesAvailable = storage_info.bytesAvailable();
|
|
|
|
info.bytesFree = storage_info.bytesFree();
|
|
|
|
info.bytesTotal = storage_info.bytesTotal();
|
|
|
|
|
|
|
|
info.name = storage_info.name();
|
|
|
|
info.rootPath = storage_info.rootPath();
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-03 14:28:59 +00:00
|
|
|
* @brief if the Filesystem is reflink/clone capable
|
|
|
|
*
|
2023-02-09 06:42:13 +00:00
|
|
|
*/
|
|
|
|
bool canCloneOnFS(const QString& path)
|
2023-03-03 14:28:59 +00:00
|
|
|
{
|
2023-02-09 06:42:13 +00:00
|
|
|
FilesystemInfo info = statFS(path);
|
|
|
|
return canCloneOnFS(info);
|
|
|
|
}
|
|
|
|
bool canCloneOnFS(const FilesystemInfo& info)
|
|
|
|
{
|
|
|
|
return canCloneOnFS(info.fsType);
|
|
|
|
}
|
|
|
|
bool canCloneOnFS(FilesystemType type)
|
2023-03-03 14:28:59 +00:00
|
|
|
{
|
2023-02-09 06:42:13 +00:00
|
|
|
return s_clone_filesystems.contains(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief if the Filesystem is reflink/clone capable and both paths are on the same device
|
2023-03-03 14:28:59 +00:00
|
|
|
*
|
2023-02-09 06:42:13 +00:00
|
|
|
*/
|
|
|
|
bool canClone(const QString& src, const QString& dst)
|
|
|
|
{
|
|
|
|
auto srcVInfo = statFS(src);
|
|
|
|
auto dstVInfo = statFS(dst);
|
|
|
|
|
|
|
|
bool sameDevice = srcVInfo.rootPath == dstVInfo.rootPath;
|
|
|
|
|
|
|
|
return sameDevice && canCloneOnFS(srcVInfo) && canCloneOnFS(dstVInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief reflink/clones a directory and it's contents from src to dest
|
|
|
|
* @param offset subdirectory form src to copy to dest
|
|
|
|
* @return if there was an error during the filecopy
|
|
|
|
*/
|
|
|
|
bool clone::operator()(const QString& offset, bool dryRun)
|
|
|
|
{
|
|
|
|
if (!canClone(m_src.absolutePath(), m_dst.absolutePath())) {
|
|
|
|
qWarning() << "Can not clone: not same device or not clone/reflink filesystem";
|
|
|
|
qDebug() << "Source path:" << m_src.absolutePath();
|
|
|
|
qDebug() << "Destination path:" << m_dst.absolutePath();
|
|
|
|
emit cloneFailed(m_src.absolutePath(), m_dst.absolutePath());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_cloned = 0; // reset counter
|
2023-06-27 20:41:36 +01:00
|
|
|
m_failedClones.clear();
|
2023-02-09 06:42:13 +00:00
|
|
|
|
|
|
|
auto src = PathCombine(m_src.absolutePath(), offset);
|
|
|
|
auto dst = PathCombine(m_dst.absolutePath(), offset);
|
|
|
|
|
|
|
|
std::error_code err;
|
|
|
|
|
|
|
|
// Function that'll do the actual cloneing
|
|
|
|
auto cloneFile = [&](QString src_path, QString relative_dst_path) {
|
|
|
|
if (m_matcher && (m_matcher->matches(relative_dst_path) != m_whitelist))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto dst_path = PathCombine(dst, relative_dst_path);
|
|
|
|
if (!dryRun) {
|
|
|
|
ensureFilePathExists(dst_path);
|
|
|
|
clone_file(src_path, dst_path, err);
|
|
|
|
}
|
|
|
|
if (err) {
|
2023-02-13 23:48:58 +00:00
|
|
|
qDebug() << "Failed to clone files: error" << err.value() << "message" << QString::fromStdString(err.message());
|
2023-02-09 06:42:13 +00:00
|
|
|
qDebug() << "Source file:" << src_path;
|
|
|
|
qDebug() << "Destination file:" << dst_path;
|
2023-06-27 20:41:36 +01:00
|
|
|
m_failedClones.append(qMakePair(src_path, dst_path));
|
|
|
|
emit cloneFailed(src_path, dst_path);
|
|
|
|
return;
|
2023-02-09 06:42:13 +00:00
|
|
|
}
|
|
|
|
m_cloned++;
|
|
|
|
emit fileCloned(src_path, dst_path);
|
|
|
|
};
|
|
|
|
|
|
|
|
// We can't use copy_opts::recursive because we need to take into account the
|
|
|
|
// blacklisted paths, so we iterate over the source directory, and if there's no blacklist
|
|
|
|
// match, we copy the file.
|
|
|
|
QDir src_dir(src);
|
|
|
|
QDirIterator source_it(src, QDir::Filter::Files | QDir::Filter::Hidden, QDirIterator::Subdirectories);
|
|
|
|
|
|
|
|
while (source_it.hasNext()) {
|
|
|
|
auto src_path = source_it.next();
|
|
|
|
auto relative_path = src_dir.relativeFilePath(src_path);
|
|
|
|
|
|
|
|
cloneFile(src_path, relative_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the root src is not a directory, the previous iterator won't run.
|
|
|
|
if (!fs::is_directory(StringUtils::toStdString(src)))
|
|
|
|
cloneFile(src, "");
|
|
|
|
|
|
|
|
return err.value() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief clone/reflink file from src to dst
|
2023-03-03 14:28:59 +00:00
|
|
|
*
|
2023-02-09 06:42:13 +00:00
|
|
|
*/
|
|
|
|
bool clone_file(const QString& src, const QString& dst, std::error_code& ec)
|
2023-03-03 14:28:59 +00:00
|
|
|
{
|
2023-02-10 13:34:48 +00:00
|
|
|
auto src_path = StringUtils::toStdString(QDir::toNativeSeparators(QFileInfo(src).absoluteFilePath()));
|
|
|
|
auto dst_path = StringUtils::toStdString(QDir::toNativeSeparators(QFileInfo(dst).absoluteFilePath()));
|
2023-02-09 06:42:13 +00:00
|
|
|
|
2023-02-10 13:34:48 +00:00
|
|
|
FilesystemInfo srcinfo = statFS(src);
|
2023-02-11 00:41:48 +00:00
|
|
|
FilesystemInfo dstinfo = statFS(dst);
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
if ((srcinfo.rootPath != dstinfo.rootPath) || (srcinfo.fsType != dstinfo.fsType)) {
|
2023-02-13 23:48:58 +00:00
|
|
|
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.";
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-11 00:41:48 +00:00
|
|
|
|
2023-02-13 23:48:58 +00:00
|
|
|
#if defined(Q_OS_WIN)
|
2023-02-11 00:41:48 +00:00
|
|
|
|
2023-02-13 23:48:58 +00:00
|
|
|
if (!win_ioctl_clone(src_path, dst_path, ec)) {
|
|
|
|
qDebug() << "failed win_ioctl_clone";
|
|
|
|
qWarning() << "clone/reflink not supported on windows outside of btrfs or ReFS!";
|
2023-02-10 13:34:48 +00:00
|
|
|
qWarning() << "check out https://github.com/maharmstone/btrfs for btrfs support!";
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-13 23:48:58 +00:00
|
|
|
|
2023-02-10 13:34:48 +00:00
|
|
|
#elif defined(Q_OS_LINUX)
|
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
if (!linux_ficlone(src_path, dst_path, ec)) {
|
2023-02-13 23:48:58 +00:00
|
|
|
qDebug() << "failed linux_ficlone:";
|
2023-02-10 13:34:48 +00:00
|
|
|
return false;
|
2023-02-13 23:48:58 +00:00
|
|
|
}
|
2023-03-03 14:28:59 +00:00
|
|
|
|
2023-07-02 05:35:37 +01:00
|
|
|
#elif defined(Q_OS_MACOS)
|
2023-03-03 14:28:59 +00:00
|
|
|
|
|
|
|
if (!macos_bsd_clonefile(src_path, dst_path, ec)) {
|
2023-02-13 23:48:58 +00:00
|
|
|
qDebug() << "failed macos_bsd_clonefile:";
|
2023-02-10 13:34:48 +00:00
|
|
|
return false;
|
2023-02-13 23:48:58 +00:00
|
|
|
}
|
2023-03-03 14:28:59 +00:00
|
|
|
|
2023-02-10 13:34:48 +00:00
|
|
|
#else
|
2023-02-13 23:48:58 +00:00
|
|
|
|
2023-02-10 13:34:48 +00:00
|
|
|
qWarning() << "clone/reflink not supported! unknown OS";
|
2023-02-09 06:42:13 +00:00
|
|
|
ec = std::make_error_code(std::errc::not_supported);
|
|
|
|
return false;
|
2023-02-13 23:48:58 +00:00
|
|
|
|
2023-02-10 13:34:48 +00:00
|
|
|
#endif
|
2023-02-09 06:42:13 +00:00
|
|
|
|
2023-02-10 13:34:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(Q_OS_WIN)
|
2023-02-11 00:41:48 +00:00
|
|
|
|
|
|
|
static long RoundUpToPowerOf2(long originalValue, long roundingMultiplePowerOf2)
|
|
|
|
{
|
|
|
|
long mask = roundingMultiplePowerOf2 - 1;
|
|
|
|
return (originalValue + mask) & ~mask;
|
|
|
|
}
|
|
|
|
|
2023-02-14 00:20:44 +00:00
|
|
|
bool win_ioctl_clone(const std::wstring& src_path, const std::wstring& dst_path, std::error_code& ec)
|
2023-02-11 00:41:48 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* This algorithm inspired from https://github.com/0xbadfca11/reflink
|
|
|
|
* LICENSE MIT
|
2023-03-03 14:28:59 +00:00
|
|
|
*
|
2023-02-13 23:48:58 +00:00
|
|
|
* Additional references
|
|
|
|
* https://learn.microsoft.com/en-us/windows/win32/api/winioctl/ni-winioctl-fsctl_duplicate_extents_to_file
|
|
|
|
* https://github.com/microsoft/CopyOnWrite/blob/main/lib/Windows/WindowsCopyOnWriteFilesystem.cs#L94
|
2023-02-11 00:41:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
HANDLE hSourceFile = CreateFileW(src_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
|
2023-03-03 14:28:59 +00:00
|
|
|
if (hSourceFile == INVALID_HANDLE_VALUE) {
|
|
|
|
ec = std::error_code(GetLastError(), std::system_category());
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to open source file" << src_path.c_str();
|
|
|
|
return false;
|
2023-03-03 14:28:59 +00:00
|
|
|
}
|
2023-02-11 00:41:48 +00:00
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
ULONG fs_flags;
|
|
|
|
if (!GetVolumeInformationByHandleW(hSourceFile, nullptr, 0, nullptr, nullptr, &fs_flags, nullptr, 0)) {
|
|
|
|
ec = std::error_code(GetLastError(), std::system_category());
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to get Filesystem information for " << src_path.c_str();
|
|
|
|
CloseHandle(hSourceFile);
|
2023-03-03 14:28:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!(fs_flags & FILE_SUPPORTS_BLOCK_REFCOUNTING)) {
|
|
|
|
SetLastError(ERROR_NOT_CAPABLE);
|
|
|
|
ec = std::error_code(GetLastError(), std::system_category());
|
2023-02-11 00:41:48 +00:00
|
|
|
qWarning() << "Filesystem at " << src_path.c_str() << " does not support reflink";
|
|
|
|
CloseHandle(hSourceFile);
|
|
|
|
return false;
|
2023-03-03 14:28:59 +00:00
|
|
|
}
|
2023-02-11 00:41:48 +00:00
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
FILE_END_OF_FILE_INFO sourceFileLength;
|
|
|
|
if (!GetFileSizeEx(hSourceFile, &sourceFileLength.EndOfFile)) {
|
|
|
|
ec = std::error_code(GetLastError(), std::system_category());
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to size of source file" << src_path.c_str();
|
|
|
|
CloseHandle(hSourceFile);
|
|
|
|
return false;
|
2023-03-03 14:28:59 +00:00
|
|
|
}
|
|
|
|
FILE_BASIC_INFO sourceFileBasicInfo;
|
|
|
|
if (!GetFileInformationByHandleEx(hSourceFile, FileBasicInfo, &sourceFileBasicInfo, sizeof(sourceFileBasicInfo))) {
|
|
|
|
ec = std::error_code(GetLastError(), std::system_category());
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to source file info" << src_path.c_str();
|
|
|
|
CloseHandle(hSourceFile);
|
2023-03-03 14:28:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ULONG junk;
|
|
|
|
FSCTL_GET_INTEGRITY_INFORMATION_BUFFER sourceFileIntegrity;
|
|
|
|
if (!DeviceIoControl(hSourceFile, FSCTL_GET_INTEGRITY_INFORMATION, nullptr, 0, &sourceFileIntegrity, sizeof(sourceFileIntegrity), &junk,
|
|
|
|
nullptr)) {
|
|
|
|
ec = std::error_code(GetLastError(), std::system_category());
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to source file integrity info" << src_path.c_str();
|
|
|
|
CloseHandle(hSourceFile);
|
2023-03-03 14:28:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-02-11 00:41:48 +00:00
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
HANDLE hDestFile = CreateFileW(dst_path.c_str(), GENERIC_READ | GENERIC_WRITE | DELETE, 0, nullptr, CREATE_NEW, 0, hSourceFile);
|
2023-02-11 00:41:48 +00:00
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
if (hDestFile == INVALID_HANDLE_VALUE) {
|
|
|
|
ec = std::error_code(GetLastError(), std::system_category());
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to open dest file" << dst_path.c_str();
|
|
|
|
CloseHandle(hSourceFile);
|
2023-03-03 14:28:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
FILE_DISPOSITION_INFO destFileDispose = { TRUE };
|
|
|
|
if (!SetFileInformationByHandle(hDestFile, FileDispositionInfo, &destFileDispose, sizeof(destFileDispose))) {
|
|
|
|
ec = std::error_code(GetLastError(), std::system_category());
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to set dest file info" << dst_path.c_str();
|
|
|
|
CloseHandle(hSourceFile);
|
|
|
|
CloseHandle(hDestFile);
|
2023-03-03 14:28:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-02-11 00:41:48 +00:00
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
if (!DeviceIoControl(hDestFile, FSCTL_SET_SPARSE, nullptr, 0, nullptr, 0, &junk, nullptr)) {
|
|
|
|
ec = std::error_code(GetLastError(), std::system_category());
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to set dest sparseness" << dst_path.c_str();
|
|
|
|
CloseHandle(hSourceFile);
|
|
|
|
CloseHandle(hDestFile);
|
2023-03-03 14:28:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
FSCTL_SET_INTEGRITY_INFORMATION_BUFFER setDestFileintegrity = { sourceFileIntegrity.ChecksumAlgorithm, sourceFileIntegrity.Reserved,
|
|
|
|
sourceFileIntegrity.Flags };
|
|
|
|
if (!DeviceIoControl(hDestFile, FSCTL_SET_INTEGRITY_INFORMATION, &setDestFileintegrity, sizeof(setDestFileintegrity), nullptr, 0,
|
|
|
|
nullptr, nullptr)) {
|
|
|
|
ec = std::error_code(GetLastError(), std::system_category());
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to set dest file integrity info" << dst_path.c_str();
|
|
|
|
CloseHandle(hSourceFile);
|
|
|
|
CloseHandle(hDestFile);
|
2023-03-03 14:28:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!SetFileInformationByHandle(hDestFile, FileEndOfFileInfo, &sourceFileLength, sizeof(sourceFileLength))) {
|
|
|
|
ec = std::error_code(GetLastError(), std::system_category());
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to set dest file size" << dst_path.c_str();
|
|
|
|
CloseHandle(hSourceFile);
|
|
|
|
CloseHandle(hDestFile);
|
2023-03-03 14:28:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-02-11 00:41:48 +00:00
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
const LONG64 splitThreshold = (1LL << 32) - sourceFileIntegrity.ClusterSizeInBytes;
|
2023-02-11 00:41:48 +00:00
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
DUPLICATE_EXTENTS_DATA dupExtent;
|
|
|
|
dupExtent.FileHandle = hSourceFile;
|
|
|
|
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.ByteCount.QuadPart = std::min(splitThreshold, remain);
|
2023-02-11 00:41:48 +00:00
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
if (!DeviceIoControl(hDestFile, FSCTL_DUPLICATE_EXTENTS_TO_FILE, &dupExtent, sizeof(dupExtent), nullptr, 0, &junk, nullptr)) {
|
2023-02-11 00:41:48 +00:00
|
|
|
DWORD err = GetLastError();
|
|
|
|
QString additionalMessage;
|
2023-03-03 14:28:59 +00:00
|
|
|
if (err == ERROR_BLOCK_TOO_MANY_REFERENCES) {
|
2023-02-11 00:41:48 +00:00
|
|
|
static const int MaxClonesPerFile = 8175;
|
2023-03-03 14:28:59 +00:00
|
|
|
additionalMessage =
|
|
|
|
QString(
|
|
|
|
" This is ERROR_BLOCK_TOO_MANY_REFERENCES and may mean you have surpassed the maximum "
|
|
|
|
"allowed %1 references for a single file. "
|
|
|
|
"See "
|
|
|
|
"https://docs.microsoft.com/en-us/windows-server/storage/refs/block-cloning#functionality-restrictions-and-remarks")
|
|
|
|
.arg(MaxClonesPerFile);
|
2023-02-11 00:41:48 +00:00
|
|
|
}
|
2023-03-03 14:28:59 +00:00
|
|
|
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;
|
2023-02-11 00:41:48 +00:00
|
|
|
CloseHandle(hSourceFile);
|
|
|
|
CloseHandle(hDestFile);
|
2023-03-03 14:28:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(sourceFileBasicInfo.FileAttributes & FILE_ATTRIBUTE_SPARSE_FILE)) {
|
|
|
|
FILE_SET_SPARSE_BUFFER setDestSparse = { FALSE };
|
|
|
|
if (!DeviceIoControl(hDestFile, FSCTL_SET_SPARSE, &setDestSparse, sizeof(setDestSparse), nullptr, 0, &junk, nullptr)) {
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to set dest file sparseness" << dst_path.c_str();
|
|
|
|
CloseHandle(hSourceFile);
|
|
|
|
CloseHandle(hDestFile);
|
2023-03-03 14:28:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-02-11 00:41:48 +00:00
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
sourceFileBasicInfo.CreationTime.QuadPart = 0;
|
|
|
|
if (!SetFileInformationByHandle(hDestFile, FileBasicInfo, &sourceFileBasicInfo, sizeof(sourceFileBasicInfo))) {
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to set dest file creation time" << dst_path.c_str();
|
|
|
|
CloseHandle(hSourceFile);
|
|
|
|
CloseHandle(hDestFile);
|
2023-03-03 14:28:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!FlushFileBuffers(hDestFile)) {
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to flush dest file buffer" << dst_path.c_str();
|
|
|
|
CloseHandle(hSourceFile);
|
|
|
|
CloseHandle(hDestFile);
|
2023-03-03 14:28:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
destFileDispose = { FALSE };
|
|
|
|
bool result = !!SetFileInformationByHandle(hDestFile, FileDispositionInfo, &destFileDispose, sizeof(destFileDispose));
|
2023-02-11 00:41:48 +00:00
|
|
|
|
|
|
|
CloseHandle(hSourceFile);
|
|
|
|
CloseHandle(hDestFile);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2023-02-10 13:34:48 +00:00
|
|
|
|
|
|
|
#elif defined(Q_OS_LINUX)
|
2023-02-13 23:48:58 +00:00
|
|
|
|
2023-02-10 13:34:48 +00:00
|
|
|
bool linux_ficlone(const std::string& src_path, const std::string& dst_path, std::error_code& ec)
|
|
|
|
{
|
2023-02-09 06:42:13 +00:00
|
|
|
// https://man7.org/linux/man-pages/man2/ioctl_ficlone.2.html
|
|
|
|
|
|
|
|
int src_fd = open(src_path.c_str(), O_RDONLY);
|
2023-03-03 14:28:59 +00:00
|
|
|
if (src_fd == -1) {
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to open file:" << src_path.c_str();
|
2023-02-09 06:42:13 +00:00
|
|
|
qDebug() << "Error:" << strerror(errno);
|
|
|
|
ec = std::make_error_code(static_cast<std::errc>(errno));
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-09 09:02:40 +00:00
|
|
|
int dst_fd = open(dst_path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
2023-03-03 14:28:59 +00:00
|
|
|
if (dst_fd == -1) {
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to open file:" << dst_path.c_str();
|
2023-02-09 06:42:13 +00:00
|
|
|
qDebug() << "Error:" << strerror(errno);
|
|
|
|
ec = std::make_error_code(static_cast<std::errc>(errno));
|
2023-02-09 09:02:40 +00:00
|
|
|
close(src_fd);
|
2023-02-09 06:42:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// attempt to clone
|
2023-03-03 14:28:59 +00:00
|
|
|
if (ioctl(dst_fd, FICLONE, src_fd) == -1) {
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to clone file:" << src_path.c_str() << "to" << dst_path.c_str();
|
2023-02-09 06:42:13 +00:00
|
|
|
qDebug() << "Error:" << strerror(errno);
|
|
|
|
ec = std::make_error_code(static_cast<std::errc>(errno));
|
2023-02-09 09:02:40 +00:00
|
|
|
close(src_fd);
|
|
|
|
close(dst_fd);
|
2023-02-09 06:42:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-03-03 14:28:59 +00:00
|
|
|
if (close(src_fd)) {
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to close file:" << src_path.c_str();
|
2023-02-09 09:02:40 +00:00
|
|
|
qDebug() << "Error:" << strerror(errno);
|
|
|
|
}
|
2023-03-03 14:28:59 +00:00
|
|
|
if (close(dst_fd)) {
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to close file:" << dst_path.c_str();
|
2023-02-09 09:02:40 +00:00
|
|
|
qDebug() << "Error:" << strerror(errno);
|
|
|
|
}
|
2023-02-10 13:34:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
2023-02-09 06:42:13 +00:00
|
|
|
|
2023-07-02 05:35:37 +01:00
|
|
|
#elif defined(Q_OS_MACOS)
|
2023-02-13 23:48:58 +00:00
|
|
|
|
2023-02-10 13:34:48 +00:00
|
|
|
bool macos_bsd_clonefile(const std::string& src_path, const std::string& dst_path, std::error_code& ec)
|
|
|
|
{
|
2023-02-09 06:42:13 +00:00
|
|
|
// clonefile(const char * src, const char * dst, int flags);
|
|
|
|
// https://www.manpagez.com/man/2/clonefile/
|
|
|
|
|
2023-02-10 13:49:52 +00:00
|
|
|
qDebug() << "attempting file clone via clonefile" << src_path.c_str() << "to" << dst_path.c_str();
|
2023-02-09 09:02:40 +00:00
|
|
|
if (clonefile(src_path.c_str(), dst_path.c_str(), 0) == -1) {
|
2023-02-11 00:41:48 +00:00
|
|
|
qDebug() << "Failed to clone file:" << src_path.c_str() << "to" << dst_path.c_str();
|
2023-02-09 06:42:13 +00:00
|
|
|
qDebug() << "Error:" << strerror(errno);
|
|
|
|
ec = std::make_error_code(static_cast<std::errc>(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2023-02-10 13:34:48 +00:00
|
|
|
#endif
|
2023-02-09 23:19:38 +00:00
|
|
|
|
|
|
|
/**
|
2023-03-03 14:28:59 +00:00
|
|
|
* @brief if the Filesystem is symlink capable
|
|
|
|
*
|
2023-02-09 23:19:38 +00:00
|
|
|
*/
|
|
|
|
bool canLinkOnFS(const QString& path)
|
|
|
|
{
|
|
|
|
FilesystemInfo info = statFS(path);
|
|
|
|
return canLinkOnFS(info);
|
|
|
|
}
|
|
|
|
bool canLinkOnFS(const FilesystemInfo& info)
|
|
|
|
{
|
|
|
|
return canLinkOnFS(info.fsType);
|
|
|
|
}
|
|
|
|
bool canLinkOnFS(FilesystemType type)
|
|
|
|
{
|
|
|
|
return !s_non_link_filesystems.contains(type);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @brief if the Filesystem is symlink capable on both ends
|
2023-03-03 14:28:59 +00:00
|
|
|
*
|
2023-02-09 23:19:38 +00:00
|
|
|
*/
|
|
|
|
bool canLink(const QString& src, const QString& dst)
|
|
|
|
{
|
2023-03-03 14:28:59 +00:00
|
|
|
return canLinkOnFS(src) && canLinkOnFS(dst);
|
2023-02-09 23:19:38 +00:00
|
|
|
}
|
|
|
|
|
2023-02-12 09:44:39 +00:00
|
|
|
uintmax_t hardLinkCount(const QString& path)
|
|
|
|
{
|
2023-02-17 04:01:59 +00:00
|
|
|
std::error_code err;
|
2023-03-03 14:28:59 +00:00
|
|
|
int count = fs::hard_link_count(StringUtils::toStdString(path), err);
|
2023-02-17 04:01:59 +00:00
|
|
|
if (err) {
|
2023-03-03 14:28:59 +00:00
|
|
|
qWarning() << "Failed to count hard links for" << path << ":" << QString::fromStdString(err.message());
|
|
|
|
count = 0;
|
2023-02-17 04:01:59 +00:00
|
|
|
}
|
|
|
|
return count;
|
2023-02-12 09:44:39 +00:00
|
|
|
}
|
2023-02-09 23:19:38 +00:00
|
|
|
|
2023-03-03 14:28:59 +00:00
|
|
|
} // namespace FS
|