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
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Exception.h"
|
2015-10-10 04:55:55 +01:00
|
|
|
#include "pathmatcher/IPathMatcher.h"
|
2015-05-28 18:38:29 +01:00
|
|
|
|
2023-02-08 08:35:03 +00:00
|
|
|
#include <system_error>
|
|
|
|
|
2015-10-05 00:47:27 +01:00
|
|
|
#include <QDir>
|
2023-06-27 20:41:36 +01:00
|
|
|
#include <QPair>
|
2015-10-10 04:55:55 +01:00
|
|
|
#include <QFlags>
|
2023-03-20 23:38:40 +00:00
|
|
|
#include <QLocalServer>
|
2022-10-22 22:36:47 +01:00
|
|
|
#include <QObject>
|
2023-02-08 08:35:03 +00:00
|
|
|
#include <QThread>
|
2015-09-05 17:46:57 +01:00
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
namespace FS {
|
2015-05-28 18:38:29 +01:00
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
class FileSystemException : public ::Exception {
|
|
|
|
public:
|
|
|
|
FileSystemException(const QString& message) : Exception(message) {}
|
2015-09-05 17:46:57 +01:00
|
|
|
};
|
|
|
|
|
2015-10-05 00:47:27 +01:00
|
|
|
/**
|
|
|
|
* write data to a file safely
|
|
|
|
*/
|
2022-07-30 18:42:33 +01:00
|
|
|
void write(const QString& filename, const QByteArray& data);
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2023-06-19 06:34:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* append data to a file safely
|
|
|
|
*/
|
|
|
|
void appendSafe(const QString& filename, const QByteArray& data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* append data to a file
|
|
|
|
*/
|
|
|
|
void append(const QString& filename, const QByteArray& data);
|
|
|
|
|
|
|
|
|
2015-10-05 00:47:27 +01:00
|
|
|
/**
|
|
|
|
* read data from a file safely\
|
|
|
|
*/
|
2022-07-30 18:42:33 +01:00
|
|
|
QByteArray read(const QString& filename);
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2016-11-17 03:09:24 +00:00
|
|
|
/**
|
|
|
|
* Update the last changed timestamp of an existing file
|
|
|
|
*/
|
2022-07-30 18:42:33 +01:00
|
|
|
bool updateTimestamp(const QString& filename);
|
2016-11-17 03:09:24 +00:00
|
|
|
|
2015-10-05 00:47:27 +01:00
|
|
|
/**
|
|
|
|
* Creates all the folders in a path for the specified path
|
|
|
|
* last segment of the path is treated as a file name and is ignored!
|
|
|
|
*/
|
2021-07-25 18:11:59 +01:00
|
|
|
bool ensureFilePathExists(QString filenamepath);
|
2015-10-05 00:47:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates all the folders in a path for the specified path
|
|
|
|
* last segment of the path is treated as a folder name and is created!
|
|
|
|
*/
|
2021-07-25 18:11:59 +01:00
|
|
|
bool ensureFolderPathExists(QString filenamepath);
|
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
|
2023-03-20 23:38:40 +00:00
|
|
|
*/
|
2022-10-22 22:36:47 +01:00
|
|
|
class copy : public QObject {
|
|
|
|
Q_OBJECT
|
2022-07-30 18:42:33 +01:00
|
|
|
public:
|
2022-10-22 22:36:47 +01:00
|
|
|
copy(const QString& src, const QString& dst, QObject* parent = nullptr) : QObject(parent)
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
2022-05-02 18:10:45 +01:00
|
|
|
m_src.setPath(src);
|
|
|
|
m_dst.setPath(dst);
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2022-07-30 18:42:33 +01:00
|
|
|
copy& followSymlinks(const bool follow)
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
|
|
|
m_followSymlinks = follow;
|
|
|
|
return *this;
|
|
|
|
}
|
2022-10-22 22:25:14 +01:00
|
|
|
copy& matcher(const IPathMatcher* filter)
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
2022-10-22 22:25:14 +01:00
|
|
|
m_matcher = filter;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
copy& whitelist(bool whitelist)
|
|
|
|
{
|
|
|
|
m_whitelist = whitelist;
|
2018-07-15 13:51:05 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2023-06-28 21:51:58 +01:00
|
|
|
copy& overwrite(const bool overwrite)
|
|
|
|
{
|
|
|
|
m_overwrite = overwrite;
|
|
|
|
return *this;
|
|
|
|
}
|
2022-10-22 22:36:47 +01:00
|
|
|
|
|
|
|
bool operator()(bool dryRun = false) { return operator()(QString(), dryRun); }
|
|
|
|
|
|
|
|
int totalCopied() { return m_copied; }
|
2023-06-27 20:41:36 +01:00
|
|
|
int totalFailed() { return m_failedPaths.length(); }
|
|
|
|
QStringList failed() { return m_failedPaths; }
|
2022-10-22 22:36:47 +01:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void fileCopied(const QString& relativeName);
|
2023-06-27 20:41:36 +01:00
|
|
|
void copyFailed(const QString& relativeName);
|
2022-10-22 22:36:47 +01:00
|
|
|
// TODO: maybe add a "shouldCopy" signal in the future?
|
2015-10-10 04:55:55 +01:00
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
private:
|
2022-10-22 22:36:47 +01:00
|
|
|
bool operator()(const QString& offset, bool dryRun = false);
|
2015-10-10 04:55:55 +01:00
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
private:
|
2018-07-15 13:51:05 +01:00
|
|
|
bool m_followSymlinks = true;
|
2022-10-22 22:25:14 +01:00
|
|
|
const IPathMatcher* m_matcher = nullptr;
|
|
|
|
bool m_whitelist = false;
|
2023-06-28 21:51:58 +01:00
|
|
|
bool m_overwrite = false;
|
2018-07-15 13:51:05 +01:00
|
|
|
QDir m_src;
|
|
|
|
QDir m_dst;
|
2022-10-22 22:36:47 +01:00
|
|
|
int m_copied;
|
2023-06-27 20:41:36 +01:00
|
|
|
QStringList m_failedPaths;
|
2015-10-10 04:55:55 +01:00
|
|
|
};
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2023-02-08 08:35:03 +00:00
|
|
|
struct LinkPair {
|
|
|
|
QString src;
|
|
|
|
QString dst;
|
|
|
|
};
|
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
struct LinkResult {
|
|
|
|
QString src;
|
|
|
|
QString dst;
|
|
|
|
QString err_msg;
|
|
|
|
int err_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ExternalLinkFileProcess : public QThread {
|
2023-02-08 08:35:03 +00:00
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2023-02-08 20:36:15 +00:00
|
|
|
ExternalLinkFileProcess(QString server, bool useHardLinks, QObject* parent = nullptr)
|
2023-02-09 01:52:50 +00:00
|
|
|
: QThread(parent), m_useHardLinks(useHardLinks), m_server(server)
|
2023-02-08 20:36:15 +00:00
|
|
|
{}
|
2023-02-08 08:35:03 +00:00
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
void run() override
|
|
|
|
{
|
2023-02-08 08:35:03 +00:00
|
|
|
runLinkFile();
|
|
|
|
emit processExited();
|
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void processExited();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void runLinkFile();
|
|
|
|
|
2023-02-08 20:36:15 +00:00
|
|
|
bool m_useHardLinks = false;
|
|
|
|
|
2023-02-08 08:35:03 +00:00
|
|
|
QString m_server;
|
|
|
|
};
|
|
|
|
|
2023-02-07 07:05:06 +00:00
|
|
|
/**
|
2023-02-08 08:35:03 +00:00
|
|
|
* @brief links (a file / a directory and it's contents) from src to dest
|
2023-03-20 23:38:40 +00:00
|
|
|
*/
|
2023-02-07 07:05:06 +00:00
|
|
|
class create_link : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2023-03-20 23:38:40 +00:00
|
|
|
create_link(const QList<LinkPair> path_pairs, QObject* parent = nullptr) : QObject(parent) { m_path_pairs.append(path_pairs); }
|
2023-02-07 07:05:06 +00:00
|
|
|
create_link(const QString& src, const QString& dst, QObject* parent = nullptr) : QObject(parent)
|
|
|
|
{
|
2023-03-20 23:38:40 +00:00
|
|
|
LinkPair pair = { src, dst };
|
2023-02-08 08:35:03 +00:00
|
|
|
m_path_pairs.append(pair);
|
2023-02-07 07:05:06 +00:00
|
|
|
}
|
|
|
|
create_link& useHardLinks(const bool useHard)
|
|
|
|
{
|
|
|
|
m_useHardLinks = useHard;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
create_link& matcher(const IPathMatcher* filter)
|
|
|
|
{
|
|
|
|
m_matcher = filter;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
create_link& whitelist(bool whitelist)
|
|
|
|
{
|
|
|
|
m_whitelist = whitelist;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
create_link& linkRecursively(bool recursive)
|
|
|
|
{
|
|
|
|
m_recursive = recursive;
|
|
|
|
return *this;
|
|
|
|
}
|
2023-02-10 02:48:40 +00:00
|
|
|
create_link& setMaxDepth(int depth)
|
|
|
|
{
|
|
|
|
m_max_depth = depth;
|
|
|
|
return *this;
|
|
|
|
}
|
2023-02-07 07:05:06 +00:00
|
|
|
create_link& debug(bool d)
|
|
|
|
{
|
|
|
|
m_debug = d;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-03-20 23:38:40 +00:00
|
|
|
std::error_code getOSError() { return m_os_err; }
|
2023-02-07 07:05:06 +00:00
|
|
|
|
|
|
|
bool operator()(bool dryRun = false) { return operator()(QString(), dryRun); }
|
|
|
|
|
2023-02-08 22:30:45 +00:00
|
|
|
int totalLinked() { return m_linked; }
|
|
|
|
|
2023-02-23 00:40:07 +00:00
|
|
|
void runPrivileged() { runPrivileged(QString()); }
|
|
|
|
void runPrivileged(const QString& offset);
|
2023-02-08 08:35:03 +00:00
|
|
|
|
2023-02-08 22:30:45 +00:00
|
|
|
QList<LinkResult> getResults() { return m_path_results; }
|
|
|
|
|
2023-02-07 07:05:06 +00:00
|
|
|
signals:
|
2023-02-08 20:36:15 +00:00
|
|
|
void fileLinked(const QString& srcName, const QString& dstName);
|
|
|
|
void linkFailed(const QString& srcName, const QString& dstName, const QString& err_msg, int err_value);
|
|
|
|
void finished();
|
2023-02-23 00:40:07 +00:00
|
|
|
void finishedPrivileged(bool gotResults);
|
2023-02-08 22:30:45 +00:00
|
|
|
|
2023-02-07 07:05:06 +00:00
|
|
|
private:
|
|
|
|
bool operator()(const QString& offset, bool dryRun = false);
|
2023-02-08 20:36:15 +00:00
|
|
|
void make_link_list(const QString& offset);
|
|
|
|
bool make_links();
|
2023-02-07 07:05:06 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_useHardLinks = false;
|
|
|
|
const IPathMatcher* m_matcher = nullptr;
|
|
|
|
bool m_whitelist = false;
|
|
|
|
bool m_recursive = true;
|
2023-02-08 08:35:03 +00:00
|
|
|
|
2023-02-10 02:48:40 +00:00
|
|
|
/// @brief >= -1 = infinite, 0 = link files at src/* to dest/*, 1 = link files at src/*/* to dest/*/*, etc.
|
|
|
|
int m_max_depth = -1;
|
|
|
|
|
2023-02-08 08:35:03 +00:00
|
|
|
QList<LinkPair> m_path_pairs;
|
2023-02-08 20:36:15 +00:00
|
|
|
QList<LinkResult> m_path_results;
|
|
|
|
QList<LinkPair> m_links_to_make;
|
2023-02-08 08:35:03 +00:00
|
|
|
|
2023-02-07 07:05:06 +00:00
|
|
|
int m_linked;
|
|
|
|
bool m_debug = false;
|
2023-02-08 08:35:03 +00:00
|
|
|
std::error_code m_os_err;
|
|
|
|
|
|
|
|
QLocalServer m_linkServer;
|
2023-02-07 07:05:06 +00:00
|
|
|
};
|
|
|
|
|
2022-12-30 00:21:54 +00:00
|
|
|
/**
|
|
|
|
* @brief moves a file by renaming it
|
|
|
|
* @param source source file path
|
|
|
|
* @param dest destination filepath
|
2023-03-20 23:38:40 +00:00
|
|
|
*
|
2022-12-30 00:21:54 +00:00
|
|
|
*/
|
2023-03-20 23:38:40 +00:00
|
|
|
bool move(const QString& source, const QString& dest);
|
2022-12-30 00:21:54 +00:00
|
|
|
|
2015-10-05 00:47:27 +01:00
|
|
|
/**
|
|
|
|
* Delete a folder recursively
|
|
|
|
*/
|
2021-07-25 18:11:59 +01:00
|
|
|
bool deletePath(QString path);
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2022-07-30 18:42:33 +01:00
|
|
|
/**
|
|
|
|
* Trash a folder / file
|
|
|
|
*/
|
2023-03-20 23:38:40 +00:00
|
|
|
bool trash(QString path, QString* pathInTrash = nullptr);
|
2022-07-30 18:42:33 +01:00
|
|
|
|
|
|
|
QString PathCombine(const QString& path1, const QString& path2);
|
|
|
|
QString PathCombine(const QString& path1, const QString& path2, const QString& path3);
|
|
|
|
QString PathCombine(const QString& path1, const QString& path2, const QString& path3, const QString& path4);
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2023-02-10 02:48:40 +00:00
|
|
|
QString AbsolutePath(const QString& path);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief depth of path. "foo.txt" -> 0 , "bar/foo.txt" -> 1, /baz/bar/foo.txt -> 2, etc.
|
2023-03-20 23:38:40 +00:00
|
|
|
*
|
2023-02-10 02:48:40 +00:00
|
|
|
* @param path path to measure
|
2023-03-20 23:38:40 +00:00
|
|
|
* @return int number of components before base path
|
2023-02-10 02:48:40 +00:00
|
|
|
*/
|
2023-04-01 04:22:07 +01:00
|
|
|
int pathDepth(const QString& path);
|
2023-02-10 02:48:40 +00:00
|
|
|
|
|
|
|
/**
|
2023-03-20 23:38:40 +00:00
|
|
|
* @brief cut off segments of path until it is a max of length depth
|
|
|
|
*
|
2023-02-10 02:48:40 +00:00
|
|
|
* @param path path to truncate
|
|
|
|
* @param depth max depth of new path
|
|
|
|
* @return QString truncated path
|
|
|
|
*/
|
2023-04-01 04:22:07 +01:00
|
|
|
QString pathTruncate(const QString& path, int depth);
|
2015-10-05 00:47:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve an executable
|
|
|
|
*
|
|
|
|
* Will resolve:
|
|
|
|
* single executable (by name)
|
|
|
|
* relative path
|
|
|
|
* absolute path
|
|
|
|
*
|
|
|
|
* @return absolute path to executable or null string
|
|
|
|
*/
|
2021-07-25 18:11:59 +01:00
|
|
|
QString ResolveExecutable(QString path);
|
2015-10-05 00:47:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize path
|
|
|
|
*
|
|
|
|
* Any paths inside the current directory will be normalized to relative paths (to current)
|
|
|
|
* Other paths will be made absolute
|
|
|
|
*
|
|
|
|
* Returns false if the path logic somehow filed (and normalizedPath in invalid)
|
|
|
|
*/
|
2021-07-25 18:11:59 +01:00
|
|
|
QString NormalizePath(QString path);
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2021-07-25 18:11:59 +01:00
|
|
|
QString RemoveInvalidFilenameChars(QString string, QChar replaceWith = '-');
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2021-07-25 18:11:59 +01:00
|
|
|
QString DirNameFromString(QString string, QString inDir = ".");
|
2015-10-05 00:47:27 +01:00
|
|
|
|
|
|
|
/// Checks if the a given Path contains "!"
|
2021-07-25 18:11:59 +01:00
|
|
|
bool checkProblemticPathJava(QDir folder);
|
2015-10-05 00:47:27 +01:00
|
|
|
|
|
|
|
// Get the Directory representing the User's Desktop
|
2021-07-25 18:11:59 +01:00
|
|
|
QString getDesktopDir();
|
2015-10-05 00:47:27 +01:00
|
|
|
|
2022-06-11 17:43:09 +01:00
|
|
|
// Overrides one folder with the contents of another, preserving items exclusive to the first folder
|
|
|
|
// Equivalent to doing QDir::rename, but allowing for overrides
|
|
|
|
bool overrideFolder(QString overwritten_path, QString override_path);
|
2022-10-22 15:56:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a shortcut to the specified target file at the specified destination path.
|
|
|
|
*/
|
|
|
|
bool createShortcut(QString destination, QString target, QStringList args, QString name, QString icon);
|
2023-02-09 06:42:13 +00:00
|
|
|
|
|
|
|
enum class FilesystemType {
|
|
|
|
FAT,
|
|
|
|
NTFS,
|
2023-02-10 13:34:48 +00:00
|
|
|
REFS,
|
2023-02-09 06:42:13 +00:00
|
|
|
EXT,
|
|
|
|
EXT_2_OLD,
|
|
|
|
EXT_2_3_4,
|
|
|
|
XFS,
|
|
|
|
BTRFS,
|
|
|
|
NFS,
|
|
|
|
ZFS,
|
|
|
|
APFS,
|
|
|
|
HFS,
|
|
|
|
HFSPLUS,
|
|
|
|
HFSX,
|
2023-02-09 09:02:40 +00:00
|
|
|
FUSEBLK,
|
2023-02-09 23:19:38 +00:00
|
|
|
F2FS,
|
2023-02-09 06:42:13 +00:00
|
|
|
UNKNOWN
|
|
|
|
};
|
|
|
|
|
2023-02-13 23:48:58 +00:00
|
|
|
/**
|
|
|
|
* @brief Ordered Mapping of enum types to reported filesystem names
|
2023-03-20 23:38:40 +00:00
|
|
|
* this mapping is non exsaustive, it just attempts to capture the filesystems which could be reasonalbly be in use .
|
2023-02-13 23:48:58 +00:00
|
|
|
* all string values are in uppercase, use `QString.toUpper()` or equivalent during lookup.
|
2023-03-20 23:38:40 +00:00
|
|
|
*
|
2023-02-13 23:48:58 +00:00
|
|
|
* QMap is ordered
|
2023-03-20 23:38:40 +00:00
|
|
|
*
|
2023-02-13 23:48:58 +00:00
|
|
|
*/
|
2023-04-04 01:14:06 +01:00
|
|
|
static const QMap<FilesystemType, QStringList> s_filesystem_type_names = {
|
|
|
|
{FilesystemType::FAT, { "FAT" }},
|
|
|
|
{FilesystemType::NTFS, { "NTFS" }},
|
|
|
|
{FilesystemType::REFS, { "REFS" }},
|
|
|
|
{FilesystemType::EXT_2_OLD, { "EXT_2_OLD", "EXT2_OLD" }},
|
|
|
|
{FilesystemType::EXT_2_3_4, { "EXT2/3/4", "EXT_2_3_4", "EXT2", "EXT3", "EXT4" }},
|
|
|
|
{FilesystemType::EXT, { "EXT" }},
|
|
|
|
{FilesystemType::XFS, { "XFS" }},
|
|
|
|
{FilesystemType::BTRFS, { "BTRFS" }},
|
|
|
|
{FilesystemType::NFS, { "NFS" }},
|
|
|
|
{FilesystemType::ZFS, { "ZFS" }},
|
|
|
|
{FilesystemType::APFS, { "APFS" }},
|
|
|
|
{FilesystemType::HFS, { "HFS" }},
|
|
|
|
{FilesystemType::HFSPLUS, { "HFSPLUS" }},
|
|
|
|
{FilesystemType::HFSX, { "HFSX" }},
|
|
|
|
{FilesystemType::FUSEBLK, { "FUSEBLK" }},
|
|
|
|
{FilesystemType::F2FS, { "F2FS" }},
|
|
|
|
{FilesystemType::UNKNOWN, { "UNKNOWN" }}
|
2023-02-09 06:42:13 +00:00
|
|
|
};
|
|
|
|
|
2023-02-13 23:48:58 +00:00
|
|
|
/**
|
|
|
|
* @brief Get the string name of Filesystem enum object
|
2023-03-20 23:38:40 +00:00
|
|
|
*
|
|
|
|
* @param type
|
|
|
|
* @return QString
|
2023-02-13 23:48:58 +00:00
|
|
|
*/
|
|
|
|
QString getFilesystemTypeName(FilesystemType type);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the Filesystem enum object from a name
|
2023-03-20 23:38:40 +00:00
|
|
|
* Does a lookup of the type name and returns an exact match
|
2023-02-13 23:48:58 +00:00
|
|
|
*
|
2023-03-20 23:38:40 +00:00
|
|
|
* @param name
|
|
|
|
* @return FilesystemType
|
2023-02-13 23:48:58 +00:00
|
|
|
*/
|
|
|
|
FilesystemType getFilesystemType(const QString& name);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the Filesystem enum object from a name
|
|
|
|
* Does a fuzzy lookup of the type name and returns an apropreate match
|
2023-03-20 23:38:40 +00:00
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @return FilesystemType
|
2023-02-13 23:48:58 +00:00
|
|
|
*/
|
|
|
|
FilesystemType getFilesystemTypeFuzzy(const QString& name);
|
|
|
|
|
2023-02-09 06:42:13 +00:00
|
|
|
struct FilesystemInfo {
|
|
|
|
FilesystemType fsType = FilesystemType::UNKNOWN;
|
2023-02-11 00:41:48 +00:00
|
|
|
QString fsTypeName;
|
2023-03-20 23:38:40 +00:00
|
|
|
int blockSize;
|
|
|
|
qint64 bytesAvailable;
|
|
|
|
qint64 bytesFree;
|
|
|
|
qint64 bytesTotal;
|
2023-02-09 06:42:13 +00:00
|
|
|
QString name;
|
|
|
|
QString rootPath;
|
|
|
|
};
|
|
|
|
|
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-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-02-09 06:42:13 +00:00
|
|
|
/**
|
|
|
|
* @brief colect information about the filesystem under a file
|
2023-03-20 23:38:40 +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
|
|
|
|
2023-03-20 23:38:40 +00:00
|
|
|
static const QList<FilesystemType> s_clone_filesystems = { FilesystemType::BTRFS, FilesystemType::APFS, FilesystemType::ZFS,
|
|
|
|
FilesystemType::XFS, FilesystemType::REFS };
|
2023-02-09 06:42:13 +00:00
|
|
|
|
|
|
|
/**
|
2023-03-20 23:38:40 +00:00
|
|
|
* @brief if the Filesystem is reflink/clone capable
|
|
|
|
*
|
2023-02-09 06:42:13 +00:00
|
|
|
*/
|
|
|
|
bool canCloneOnFS(const QString& path);
|
|
|
|
bool canCloneOnFS(const FilesystemInfo& info);
|
|
|
|
bool canCloneOnFS(FilesystemType type);
|
|
|
|
|
|
|
|
/**
|
2023-02-09 23:19:38 +00:00
|
|
|
* @brief if the Filesystems are reflink/clone capable and both are on the same device
|
2023-03-20 23:38:40 +00:00
|
|
|
*
|
2023-02-09 06:42:13 +00:00
|
|
|
*/
|
|
|
|
bool canClone(const QString& src, const QString& dst);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Copies a directory and it's contents from src to dest
|
2023-03-20 23:38:40 +00:00
|
|
|
*/
|
2023-02-09 06:42:13 +00:00
|
|
|
class clone : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
clone(const QString& src, const QString& dst, QObject* parent = nullptr) : QObject(parent)
|
|
|
|
{
|
|
|
|
m_src.setPath(src);
|
|
|
|
m_dst.setPath(dst);
|
|
|
|
}
|
|
|
|
clone& matcher(const IPathMatcher* filter)
|
|
|
|
{
|
|
|
|
m_matcher = filter;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
clone& whitelist(bool whitelist)
|
|
|
|
{
|
|
|
|
m_whitelist = whitelist;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(bool dryRun = false) { return operator()(QString(), dryRun); }
|
|
|
|
|
|
|
|
int totalCloned() { return m_cloned; }
|
2023-06-27 20:41:36 +01:00
|
|
|
int totalFailed() { return m_failedClones.length(); }
|
|
|
|
|
|
|
|
QList<QPair<QString, QString>> failed() { return m_failedClones; }
|
2023-02-09 06:42:13 +00:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void fileCloned(const QString& src, const QString& dst);
|
|
|
|
void cloneFailed(const QString& src, const QString& dst);
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool operator()(const QString& offset, bool dryRun = false);
|
|
|
|
|
|
|
|
private:
|
|
|
|
const IPathMatcher* m_matcher = nullptr;
|
|
|
|
bool m_whitelist = false;
|
|
|
|
QDir m_src;
|
|
|
|
QDir m_dst;
|
|
|
|
int m_cloned;
|
2023-06-27 20:41:36 +01:00
|
|
|
QList<QPair<QString, QString>> m_failedClones;
|
2023-02-09 06:42:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief clone/reflink file from src to dst
|
2023-03-20 23:38:40 +00:00
|
|
|
*
|
2023-02-09 06:42:13 +00:00
|
|
|
*/
|
|
|
|
bool clone_file(const QString& src, const QString& dst, std::error_code& ec);
|
|
|
|
|
2023-02-10 13:34:48 +00:00
|
|
|
#if defined(Q_OS_WIN)
|
2023-02-13 23:48:58 +00:00
|
|
|
bool win_ioctl_clone(const std::wstring& src_path, const std::wstring& dst_path, std::error_code& ec);
|
2023-02-10 13:34:48 +00:00
|
|
|
#elif defined(Q_OS_LINUX)
|
|
|
|
bool linux_ficlone(const std::string& src_path, const std::string& dst_path, std::error_code& ec);
|
|
|
|
#elif defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD)
|
|
|
|
bool macos_bsd_clonefile(const std::string& src_path, const std::string& dst_path, std::error_code& ec);
|
|
|
|
#endif
|
2023-02-09 23:19:38 +00:00
|
|
|
|
|
|
|
static const QList<FilesystemType> s_non_link_filesystems = {
|
|
|
|
FilesystemType::FAT,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2023-03-20 23:38:40 +00:00
|
|
|
* @brief if the Filesystem is symlink capable
|
|
|
|
*
|
2023-02-09 23:19:38 +00:00
|
|
|
*/
|
|
|
|
bool canLinkOnFS(const QString& path);
|
|
|
|
bool canLinkOnFS(const FilesystemInfo& info);
|
|
|
|
bool canLinkOnFS(FilesystemType type);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief if the Filesystem is symlink capable on both ends
|
2023-03-20 23:38:40 +00:00
|
|
|
*
|
2023-02-09 23:19:38 +00:00
|
|
|
*/
|
|
|
|
bool canLink(const QString& src, const QString& dst);
|
|
|
|
|
2023-02-12 09:44:39 +00:00
|
|
|
uintmax_t hardLinkCount(const QString& path);
|
|
|
|
|
2023-03-20 23:38:40 +00:00
|
|
|
} // namespace FS
|