2022-05-27 02:48:54 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
|
|
|
*
|
|
|
|
* 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 23:08:29 +05:30
|
|
|
|
|
|
|
#include "FileSystem.h"
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
#include <QDebug>
|
2015-05-28 23:08:29 +05:30
|
|
|
#include <QDir>
|
2022-07-31 04:08:56 +05:30
|
|
|
#include <QDirIterator>
|
2018-02-11 05:59:43 +05:30
|
|
|
#include <QFile>
|
2015-05-28 23:08:29 +05:30
|
|
|
#include <QFileInfo>
|
2022-07-30 23:12:33 +05:30
|
|
|
#include <QSaveFile>
|
2016-01-05 12:02:52 +05:30
|
|
|
#include <QStandardPaths>
|
2018-02-11 05:59:43 +05:30
|
|
|
#include <QTextStream>
|
2022-07-30 23:12:33 +05:30
|
|
|
#include <QUrl>
|
2022-10-30 23:24:52 +05:30
|
|
|
#include "DesktopServices.h"
|
2015-05-28 23:08:29 +05:30
|
|
|
|
2018-02-11 05:05:56 +05:30
|
|
|
#if defined Q_OS_WIN32
|
2022-07-30 23:12:33 +05:30
|
|
|
#include <objbase.h>
|
|
|
|
#include <objidl.h>
|
|
|
|
#include <shlguid.h>
|
|
|
|
#include <shlobj.h>
|
|
|
|
#include <shobjidl.h>
|
|
|
|
#include <sys/utime.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winnls.h>
|
|
|
|
#include <string>
|
2018-02-11 05:05:56 +05:30
|
|
|
#else
|
2022-07-30 23:12:33 +05:30
|
|
|
#include <utime.h>
|
2018-02-11 05:05:56 +05:30
|
|
|
#endif
|
|
|
|
|
2022-10-14 03:08:52 +05:30
|
|
|
// 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-31 04:08:56 +05:30
|
|
|
#include <filesystem>
|
2022-10-14 03:08:52 +05:30
|
|
|
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-31 04:08:56 +05:30
|
|
|
|
2022-08-07 23:00:38 +05:30
|
|
|
#if defined Q_OS_WIN32
|
|
|
|
|
|
|
|
std::wstring toStdString(QString s)
|
|
|
|
{
|
|
|
|
return s.toStdWString();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
std::string toStdString(QString s)
|
|
|
|
{
|
|
|
|
return s.toStdString();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
namespace FS {
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
void ensureExists(const QDir& dir)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!QDir().mkpath(dir.absolutePath())) {
|
|
|
|
throw FileSystemException("Unable to create folder " + dir.dirName() + " (" + dir.absolutePath() + ")");
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
void write(const QString& filename, const QByteArray& data)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
ensureExists(QFileInfo(filename).dir());
|
|
|
|
QSaveFile file(filename);
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!file.open(QSaveFile::WriteOnly)) {
|
|
|
|
throw FileSystemException("Couldn't open " + filename + " for writing: " + file.errorString());
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2022-07-30 23:12:33 +05:30
|
|
|
if (data.size() != file.write(data)) {
|
|
|
|
throw FileSystemException("Error writing data to " + filename + ": " + file.errorString());
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!file.commit()) {
|
|
|
|
throw FileSystemException("Error while committing data to " + filename + ": " + file.errorString());
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
QByteArray read(const QString& filename)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QFile file(filename);
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
|
|
|
throw FileSystemException("Unable to open " + filename + " for reading: " + file.errorString());
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
const qint64 size = file.size();
|
|
|
|
QByteArray data(int(size), 0);
|
|
|
|
const qint64 ret = file.read(data.data(), size);
|
2022-07-30 23:12:33 +05:30
|
|
|
if (ret == -1 || ret != size) {
|
|
|
|
throw FileSystemException("Error reading data from " + filename + ": " + file.errorString());
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
return data;
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
2015-10-05 05:17:27 +05:30
|
|
|
|
2016-11-17 08:39:24 +05:30
|
|
|
bool updateTimestamp(const QString& filename)
|
|
|
|
{
|
2018-02-11 05:05:56 +05:30
|
|
|
#ifdef Q_OS_WIN32
|
2018-07-15 18:21:05 +05:30
|
|
|
std::wstring filename_utf_16 = filename.toStdWString();
|
|
|
|
return (_wutime64(filename_utf_16.c_str(), nullptr) == 0);
|
2018-02-11 05:05:56 +05:30
|
|
|
#else
|
2018-07-15 18:21:05 +05:30
|
|
|
QByteArray filenameBA = QFile::encodeName(filename);
|
|
|
|
return (utime(filenameBA.data(), nullptr) == 0);
|
2018-02-11 05:05:56 +05:30
|
|
|
#endif
|
2016-11-17 08:39:24 +05:30
|
|
|
}
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
bool ensureFilePathExists(QString filenamepath)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QFileInfo a(filenamepath);
|
|
|
|
QDir dir;
|
|
|
|
QString ensuredPath = a.path();
|
|
|
|
bool success = dir.mkpath(ensuredPath);
|
|
|
|
return success;
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
bool ensureFolderPathExists(QString foldernamepath)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QFileInfo a(foldernamepath);
|
|
|
|
QDir dir;
|
|
|
|
QString ensuredPath = a.filePath();
|
|
|
|
bool success = dir.mkpath(ensuredPath);
|
|
|
|
return success;
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
bool copy::operator()(const QString& offset)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2022-10-14 03:08:52 +05:30
|
|
|
using copy_opts = fs::copy_options;
|
2022-07-31 04:08:56 +05:30
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
// NOTE always deep copy on windows. the alternatives are too messy.
|
|
|
|
#if defined Q_OS_WIN32
|
2018-07-15 18:21:05 +05:30
|
|
|
m_followSymlinks = true;
|
2022-07-30 23:12:33 +05:30
|
|
|
#endif
|
2018-07-15 18:21:05 +05:30
|
|
|
|
|
|
|
auto src = PathCombine(m_src.absolutePath(), offset);
|
|
|
|
auto dst = PathCombine(m_dst.absolutePath(), offset);
|
|
|
|
|
2022-07-31 04:08:56 +05:30
|
|
|
std::error_code err;
|
2018-07-15 18:21:05 +05:30
|
|
|
|
2022-10-14 03:08:52 +05:30
|
|
|
fs::copy_options opt = copy_opts::none;
|
2022-07-31 04:08:56 +05:30
|
|
|
|
|
|
|
// The default behavior is to follow symlinks
|
|
|
|
if (!m_followSymlinks)
|
|
|
|
opt |= copy_opts::copy_symlinks;
|
|
|
|
|
|
|
|
|
|
|
|
// 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 17:50:31 +05:30
|
|
|
QDirIterator source_it(src, QDir::Filter::Files | QDir::Filter::Hidden, QDirIterator::Subdirectories);
|
2022-07-31 04:08:56 +05:30
|
|
|
|
|
|
|
while (source_it.hasNext()) {
|
|
|
|
auto src_path = source_it.next();
|
|
|
|
auto relative_path = src_dir.relativeFilePath(src_path);
|
|
|
|
|
|
|
|
if (m_blacklist && m_blacklist->matches(relative_path))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto dst_path = PathCombine(dst, relative_path);
|
|
|
|
ensureFilePathExists(dst_path);
|
|
|
|
|
2022-10-14 03:08:52 +05:30
|
|
|
fs::copy(toStdString(src_path), toStdString(dst_path), opt, err);
|
2022-07-31 04:08:56 +05:30
|
|
|
if (err) {
|
|
|
|
qWarning() << "Failed to copy files:" << QString::fromStdString(err.message());
|
|
|
|
qDebug() << "Source file:" << src_path;
|
|
|
|
qDebug() << "Destination file:" << dst_path;
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
}
|
2022-07-31 04:08:56 +05:30
|
|
|
|
|
|
|
return err.value() == 0;
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
2015-10-10 09:25:55 +05:30
|
|
|
|
|
|
|
bool deletePath(QString path)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2022-07-31 05:10:25 +05:30
|
|
|
std::error_code err;
|
2019-08-05 04:14:56 +05:30
|
|
|
|
2022-10-14 03:08:52 +05:30
|
|
|
fs::remove_all(toStdString(path), err);
|
2018-07-15 18:21:05 +05:30
|
|
|
|
2022-07-31 05:10:25 +05:30
|
|
|
if (err) {
|
|
|
|
qWarning() << "Failed to remove files:" << QString::fromStdString(err.message());
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
|
2022-07-31 05:10:25 +05:30
|
|
|
return err.value() == 0;
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
bool trash(QString path, QString *pathInTrash = nullptr)
|
|
|
|
{
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
|
|
|
|
return false;
|
|
|
|
#else
|
2022-10-30 23:24:52 +05:30
|
|
|
// FIXME: Figure out trash in Flatpak. Qt seemingly doesn't use the Trash portal
|
|
|
|
if (DesktopServices::isFlatpak())
|
|
|
|
return false;
|
2022-07-30 23:12:33 +05:30
|
|
|
return QFile::moveToTrash(path, pathInTrash);
|
|
|
|
#endif
|
|
|
|
}
|
2015-10-05 05:17:27 +05:30
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
QString PathCombine(const QString& path1, const QString& path2)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!path1.size())
|
2018-07-15 18:21:05 +05:30
|
|
|
return path2;
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!path2.size())
|
2018-07-15 18:21:05 +05:30
|
|
|
return path1;
|
2015-10-05 05:17:27 +05:30
|
|
|
return QDir::cleanPath(path1 + QDir::separator() + path2);
|
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
QString PathCombine(const QString& path1, const QString& path2, const QString& path3)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return PathCombine(PathCombine(path1, path2), path3);
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
QString PathCombine(const QString& path1, const QString& path2, const QString& path3, const QString& path4)
|
2017-09-08 12:32:27 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return PathCombine(PathCombine(path1, path2, path3), path4);
|
2017-09-08 12:32:27 +05:30
|
|
|
}
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
QString AbsolutePath(QString path)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return QFileInfo(path).absolutePath();
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
QString ResolveExecutable(QString path)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2022-07-30 23:12:33 +05:30
|
|
|
if (path.isEmpty()) {
|
2018-07-15 18:21:05 +05:30
|
|
|
return QString();
|
|
|
|
}
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!path.contains('/')) {
|
2018-07-15 18:21:05 +05:30
|
|
|
path = QStandardPaths::findExecutable(path);
|
|
|
|
}
|
|
|
|
QFileInfo pathInfo(path);
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!pathInfo.exists() || !pathInfo.isExecutable()) {
|
2018-07-15 18:21:05 +05:30
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
return pathInfo.absoluteFilePath();
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize path
|
|
|
|
*
|
2017-01-17 03:12:22 +05:30
|
|
|
* Any paths inside the current folder will be normalized to relative paths (to current)
|
2015-10-05 05:17:27 +05:30
|
|
|
* Other paths will be made absolute
|
|
|
|
*/
|
2015-10-10 09:25:55 +05:30
|
|
|
QString NormalizePath(QString path)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QDir a = QDir::currentPath();
|
|
|
|
QString currentAbsolute = a.absolutePath();
|
|
|
|
|
|
|
|
QDir b(path);
|
|
|
|
QString newAbsolute = b.absolutePath();
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
if (newAbsolute.startsWith(currentAbsolute)) {
|
2018-07-15 18:21:05 +05:30
|
|
|
return a.relativeFilePath(newAbsolute);
|
2022-07-30 23:12:33 +05:30
|
|
|
} else {
|
2018-07-15 18:21:05 +05:30
|
|
|
return newAbsolute;
|
|
|
|
}
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
|
|
|
|
2019-08-13 11:09:00 +05:30
|
|
|
QString badFilenameChars = "\"\\/?<>:;*|!+\r\n";
|
2015-10-05 05:17:27 +05:30
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
QString RemoveInvalidFilenameChars(QString string, QChar replaceWith)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2022-07-30 23:12:33 +05:30
|
|
|
for (int i = 0; i < string.length(); i++) {
|
|
|
|
if (badFilenameChars.contains(string[i])) {
|
2018-07-15 18:21:05 +05:30
|
|
|
string[i] = replaceWith;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string;
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
QString DirNameFromString(QString string, QString inDir)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
int num = 0;
|
|
|
|
QString baseName = RemoveInvalidFilenameChars(string, '-');
|
|
|
|
QString dirName;
|
2022-07-30 23:12:33 +05:30
|
|
|
do {
|
|
|
|
if (num == 0) {
|
2018-07-15 18:21:05 +05:30
|
|
|
dirName = baseName;
|
2022-07-30 23:12:33 +05:30
|
|
|
} else {
|
2022-08-07 15:49:29 +05:30
|
|
|
dirName = baseName + "(" + QString::number(num) + ")";
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// If it's over 9000
|
|
|
|
if (num > 9000)
|
|
|
|
return "";
|
|
|
|
num++;
|
|
|
|
} while (QFileInfo(PathCombine(inDir, dirName)).exists());
|
|
|
|
return dirName;
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
|
|
|
|
2017-01-17 03:12:22 +05:30
|
|
|
// Does the folder path contain any '!'? If yes, return true, otherwise false.
|
2015-10-05 05:17:27 +05:30
|
|
|
// (This is a problem for Java)
|
2015-10-10 09:25:55 +05:30
|
|
|
bool checkProblemticPathJava(QDir folder)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QString pathfoldername = folder.absolutePath();
|
|
|
|
return pathfoldername.contains("!", Qt::CaseInsensitive);
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
QString getDesktopDir()
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Cross-platform Shortcut creation
|
2022-07-30 23:12:33 +05:30
|
|
|
bool createShortCut(QString location, QString dest, QStringList args, QString name, QString icon)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2021-12-12 06:05:46 +05:30
|
|
|
#if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD)
|
2018-07-15 18:21:05 +05:30
|
|
|
location = PathCombine(location, name + ".desktop");
|
2015-10-05 05:17:27 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
QFile f(location);
|
|
|
|
f.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
QTextStream stream(&f);
|
2015-10-05 05:17:27 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
QString argstring;
|
|
|
|
if (!args.empty())
|
|
|
|
argstring = " '" + args.join("' '") + "'";
|
2015-10-05 05:17:27 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
stream << "[Desktop Entry]"
|
|
|
|
<< "\n";
|
|
|
|
stream << "Type=Application"
|
|
|
|
<< "\n";
|
|
|
|
stream << "TryExec=" << dest.toLocal8Bit() << "\n";
|
|
|
|
stream << "Exec=" << dest.toLocal8Bit() << argstring.toLocal8Bit() << "\n";
|
|
|
|
stream << "Name=" << name.toLocal8Bit() << "\n";
|
|
|
|
stream << "Icon=" << icon.toLocal8Bit() << "\n";
|
2015-10-05 05:17:27 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
stream.flush();
|
|
|
|
f.close();
|
2015-10-05 05:17:27 +05:30
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
f.setPermissions(f.permissions() | QFileDevice::ExeOwner | QFileDevice::ExeGroup | QFileDevice::ExeOther);
|
2015-10-05 05:17:27 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
return true;
|
2015-10-05 05:17:27 +05:30
|
|
|
#elif defined Q_OS_WIN
|
2018-07-15 18:21:05 +05:30
|
|
|
// TODO: Fix
|
|
|
|
// QFile file(PathCombine(location, name + ".lnk"));
|
|
|
|
// WCHAR *file_w;
|
|
|
|
// WCHAR *dest_w;
|
|
|
|
// WCHAR *args_w;
|
|
|
|
// file.fileName().toWCharArray(file_w);
|
|
|
|
// dest.toWCharArray(dest_w);
|
|
|
|
|
|
|
|
// QString argStr;
|
|
|
|
// for (int i = 0; i < args.count(); i++)
|
|
|
|
// {
|
|
|
|
// argStr.append(args[i]);
|
|
|
|
// argStr.append(" ");
|
|
|
|
// }
|
|
|
|
// argStr.toWCharArray(args_w);
|
|
|
|
|
|
|
|
// return SUCCEEDED(CreateLink(file_w, dest_w, args_w));
|
|
|
|
return false;
|
2015-10-05 05:17:27 +05:30
|
|
|
#else
|
2018-07-15 18:21:05 +05:30
|
|
|
qWarning("Desktop Shortcuts not supported on your platform!");
|
|
|
|
return false;
|
2015-10-05 05:17:27 +05:30
|
|
|
#endif
|
|
|
|
}
|
2022-06-11 22:13:09 +05:30
|
|
|
|
|
|
|
bool overrideFolder(QString overwritten_path, QString override_path)
|
|
|
|
{
|
2022-10-14 03:08:52 +05:30
|
|
|
using copy_opts = fs::copy_options;
|
2022-08-07 17:20:39 +05:30
|
|
|
|
2022-06-11 22:13:09 +05:30
|
|
|
if (!FS::ensureFolderPathExists(overwritten_path))
|
|
|
|
return false;
|
|
|
|
|
2022-08-07 17:20:39 +05:30
|
|
|
std::error_code err;
|
2022-10-14 03:08:52 +05:30
|
|
|
fs::copy_options opt = copy_opts::recursive | copy_opts::overwrite_existing;
|
2022-06-11 22:13:09 +05:30
|
|
|
|
2022-10-28 00:44:30 +05:30
|
|
|
// FIXME: hello traveller! Apparently std::copy does NOT overwrite existing files on GNU libstdc++ on Windows?
|
2022-10-14 03:08:52 +05:30
|
|
|
fs::copy(toStdString(override_path), toStdString(overwritten_path), opt, err);
|
2022-06-11 22:13:09 +05:30
|
|
|
|
2022-08-07 17:20:39 +05:30
|
|
|
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 22:13:09 +05:30
|
|
|
}
|
|
|
|
|
2022-08-07 17:20:39 +05:30
|
|
|
return err.value() == 0;
|
2022-06-11 22:13:09 +05:30
|
|
|
}
|
|
|
|
|
2016-01-05 12:02:52 +05:30
|
|
|
}
|