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>
|
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"
|
|
|
|
|
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>
|
2018-02-11 00:29:43 +00:00
|
|
|
#include <QTextStream>
|
2022-07-30 18:42:33 +01:00
|
|
|
#include <QUrl>
|
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
|
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>
|
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__
|
|
|
|
#include <Availability.h> // for deployment target to support pre-catalina targets without std::fs
|
|
|
|
#endif // __APPLE__
|
|
|
|
|
|
|
|
#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;
|
|
|
|
#endif // MacOS min version check
|
|
|
|
#endif // Other OSes version check
|
|
|
|
|
|
|
|
#ifndef GHC_USE_STD_FS
|
|
|
|
#include <ghc/filesystem.hpp>
|
|
|
|
namespace fs = ghc::filesystem;
|
|
|
|
#endif
|
2022-07-30 23:38:56 +01:00
|
|
|
|
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
|
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;
|
|
|
|
}
|
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-07 07:05:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief links a directory and it's contents from src to dest
|
|
|
|
* @param offset subdirectory form src to link to dest
|
|
|
|
* @return if there was an error during the attempt to link
|
|
|
|
*/
|
|
|
|
bool create_link::operator()(const QString& offset, bool dryRun)
|
|
|
|
{
|
|
|
|
m_linked = 0; // reset counter
|
|
|
|
|
|
|
|
auto src = PathCombine(m_src.absolutePath(), offset);
|
|
|
|
auto dst = PathCombine(m_dst.absolutePath(), offset);
|
|
|
|
|
|
|
|
std::error_code err;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// 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))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto dst_path = PathCombine(dst, relative_dst_path);
|
|
|
|
if (!dryRun) {
|
|
|
|
|
|
|
|
ensureFilePathExists(dst_path);
|
|
|
|
if (m_useHardLinks) {
|
|
|
|
if (m_debug)
|
|
|
|
qDebug() << "making hard link:" << src_path << "to" << dst_path;
|
|
|
|
fs::create_hard_link(StringUtils::toStdString(src_path), StringUtils::toStdString(dst_path), err);
|
|
|
|
} else if (fs::is_directory(StringUtils::toStdString(src_path))) {
|
|
|
|
if (m_debug)
|
|
|
|
qDebug() << "making directory_symlink:" << src_path << "to" << dst_path;
|
|
|
|
fs::create_directory_symlink(StringUtils::toStdString(src_path), StringUtils::toStdString(dst_path), err);
|
|
|
|
} else {
|
|
|
|
if (m_debug)
|
|
|
|
qDebug() << "making symlink:" << src_path << "to" << dst_path;
|
|
|
|
fs::create_symlink(StringUtils::toStdString(src_path), StringUtils::toStdString(dst_path), err);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
qWarning() << "Failed to link files:" << QString::fromStdString(err.message());
|
|
|
|
qDebug() << "Source file:" << src_path;
|
|
|
|
qDebug() << "Destination file:" << dst_path;
|
|
|
|
qDebug() << "Error catagory:" << err.category().name();
|
|
|
|
qDebug() << "Error code:" << err.value();
|
|
|
|
m_last_os_err = err.value();
|
|
|
|
emit linkFailed(src_path, dst_path, err);
|
|
|
|
} else {
|
|
|
|
m_linked++;
|
|
|
|
emit fileLinked(relative_dst_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
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)
|
|
|
|
qDebug() << "linking recursivly:" << src << "to" << dst;
|
|
|
|
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);
|
|
|
|
|
|
|
|
link_file(src_path, relative_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err.value() == 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-14 15:02:04 +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
|
|
|
}
|
|
|
|
|
2015-10-10 04:55:55 +01:00
|
|
|
QString AbsolutePath(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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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";
|
2022-11-13 13:47:37 +00:00
|
|
|
stream << "\""
|
|
|
|
<< target
|
|
|
|
<< "\" "
|
2022-11-12 18:36:49 +00:00
|
|
|
<< argstring
|
|
|
|
<< "\n";
|
|
|
|
|
|
|
|
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)
|
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";
|
2022-10-22 15:56:27 +01:00
|
|
|
if (!icon.isEmpty())
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (!targetInfo.exists())
|
|
|
|
{
|
|
|
|
qWarning() << "Target file does not exist!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
target = targetInfo.absoluteFilePath();
|
|
|
|
|
|
|
|
if (target.length() >= MAX_PATH)
|
|
|
|
{
|
|
|
|
qWarning() << "Target file path is too long!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!icon.isEmpty() && icon.length() >= MAX_PATH)
|
|
|
|
{
|
|
|
|
qWarning() << "Icon path is too long!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
destination += ".lnk";
|
|
|
|
|
|
|
|
if (destination.length() >= MAX_PATH)
|
|
|
|
{
|
|
|
|
qWarning() << "Destination path is too long!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString argStr;
|
|
|
|
int argCount = args.count();
|
|
|
|
for (int i = 0; i < argCount; i++)
|
|
|
|
{
|
|
|
|
if (args[i].contains(' '))
|
|
|
|
{
|
|
|
|
argStr.append('"').append(args[i]).append('"');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
argStr.append(args[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < argCount - 1)
|
|
|
|
{
|
|
|
|
argStr.append(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argStr.length() >= MAX_PATH)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
if (FAILED(hres))
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
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);
|
2022-10-28 13:46:54 +01:00
|
|
|
psl->SetWorkingDirectory(wsz); // "Starts in" attribute
|
2022-10-22 15:56:27 +01:00
|
|
|
|
|
|
|
if (!icon.isEmpty())
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
wmemset(wsz, 0, MAX_PATH);
|
|
|
|
destination.toWCharArray(wsz);
|
|
|
|
hres = ppf->Save(wsz, TRUE);
|
|
|
|
if (FAILED(hres))
|
|
|
|
{
|
|
|
|
qWarning() << "IPresistFile->Save() failed";
|
|
|
|
qWarning() << "hres = " << hres;
|
|
|
|
}
|
|
|
|
ppf->Release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning() << "Failed to query IPersistFile interface from IShellLink instance";
|
|
|
|
qWarning() << "hres = " << hres;
|
|
|
|
}
|
|
|
|
psl->Release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-05 06:32:52 +00:00
|
|
|
}
|