Merge pull request 'feat: Backup Paths & Disable Option for Linux' (#68) from feat/custom-path-appimage-backup into main

Reviewed-on: https://git.citron-emu.org/Citron/Emulator/pulls/68
This commit is contained in:
Collecting
2025-12-11 07:39:18 +00:00
6 changed files with 380 additions and 77 deletions

View File

@@ -3,7 +3,9 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include "citron/configuration/configure_filesystem.h"
#include <QDir>
#include <QFileDialog>
#include <QFutureWatcher>
#include <QMessageBox>
#include <QProgressDialog>
#include <QStringList>
@@ -34,19 +36,253 @@ ConfigureFilesystem::ConfigureFilesystem(QWidget* parent)
connect(ui->reset_game_list_cache, &QPushButton::pressed, this, &ConfigureFilesystem::ResetMetadata);
connect(ui->gamecard_inserted, &QCheckBox::checkStateChanged, this, &ConfigureFilesystem::UpdateEnabledControls);
connect(ui->gamecard_current_game, &QCheckBox::checkStateChanged, this, &ConfigureFilesystem::UpdateEnabledControls);
connect(this, &ConfigureFilesystem::UpdateInstallProgress, this, &ConfigureFilesystem::OnUpdateInstallProgress);
#ifdef __linux__
connect(ui->enable_backups_checkbox, &QCheckBox::toggled, this, &ConfigureFilesystem::UpdateEnabledControls);
connect(ui->custom_backup_location_checkbox, &QCheckBox::toggled, this, &ConfigureFilesystem::UpdateEnabledControls);
connect(ui->custom_backup_location_button, &QToolButton::pressed, this, [this] {
QString dir = QFileDialog::getExistingDirectory(this, tr("Select Backup Directory"));
if (!dir.isEmpty()) {
ui->custom_backup_location_edit->setText(dir);
}
});
#endif
}
ConfigureFilesystem::~ConfigureFilesystem() = default;
void ConfigureFilesystem::changeEvent(QEvent* event) { if (event->type() == QEvent::LanguageChange) { RetranslateUI(); } QWidget::changeEvent(event); }
void ConfigureFilesystem::SetConfiguration() { ui->nand_directory_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::NANDDir))); ui->sdmc_directory_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::SDMCDir))); ui->gamecard_path_edit->setText(QString::fromStdString(Settings::values.gamecard_path.GetValue())); ui->dump_path_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::DumpDir))); ui->load_path_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::LoadDir))); ui->gamecard_inserted->setChecked(Settings::values.gamecard_inserted.GetValue()); ui->gamecard_current_game->setChecked(Settings::values.gamecard_current_game.GetValue()); ui->dump_exefs->setChecked(Settings::values.dump_exefs.GetValue()); ui->dump_nso->setChecked(Settings::values.dump_nso.GetValue()); ui->cache_game_list->setChecked(UISettings::values.cache_game_list.GetValue()); ui->prompt_for_autoloader->setChecked(UISettings::values.prompt_for_autoloader.GetValue()); UpdateEnabledControls(); }
void ConfigureFilesystem::ApplyConfiguration() { Common::FS::SetCitronPath(Common::FS::CitronPath::NANDDir, ui->nand_directory_edit->text().toStdString()); Common::FS::SetCitronPath(Common::FS::CitronPath::SDMCDir, ui->sdmc_directory_edit->text().toStdString()); Common::FS::SetCitronPath(Common::FS::CitronPath::DumpDir, ui->dump_path_edit->text().toStdString()); Common::FS::SetCitronPath(Common::FS::CitronPath::LoadDir, ui->load_path_edit->text().toStdString()); Settings::values.gamecard_inserted = ui->gamecard_inserted->isChecked(); Settings::values.gamecard_current_game = ui->gamecard_current_game->isChecked(); Settings::values.dump_exefs = ui->dump_exefs->isChecked(); Settings::values.dump_nso = ui->dump_nso->isChecked(); UISettings::values.cache_game_list = ui->cache_game_list->isChecked(); UISettings::values.prompt_for_autoloader = ui->prompt_for_autoloader->isChecked(); }
void ConfigureFilesystem::SetDirectory(DirectoryTarget target, QLineEdit* edit) { QString caption; switch (target) { case DirectoryTarget::NAND: caption = tr("Select Emulated NAND Directory..."); break; case DirectoryTarget::SD: caption = tr("Select Emulated SD Directory..."); break; case DirectoryTarget::Gamecard: caption = tr("Select Gamecard Path..."); break; case DirectoryTarget::Dump: caption = tr("Select Dump Directory..."); break; case DirectoryTarget::Load: caption = tr("Select Mod Load Directory..."); break; } QString str; if (target == DirectoryTarget::Gamecard) { str = QFileDialog::getOpenFileName(this, caption, QFileInfo(edit->text()).dir().path(), QStringLiteral("NX Gamecard;*.xci")); } else { str = QFileDialog::getExistingDirectory(this, caption, edit->text()); } if (str.isNull() || str.isEmpty()) { return; } if (str.back() != QChar::fromLatin1('/')) { str.append(QChar::fromLatin1('/')); } edit->setText(str); }
void ConfigureFilesystem::ResetMetadata() { if (!Common::FS::Exists(Common::FS::GetCitronPath(Common::FS::CitronPath::CacheDir) / "game_list/")) { QMessageBox::information(this, tr("Reset Metadata Cache"), tr("The metadata cache is already empty.")); } else if (Common::FS::RemoveDirRecursively(Common::FS::GetCitronPath(Common::FS::CitronPath::CacheDir) / "game_list")) { QMessageBox::information(this, tr("Reset Metadata Cache"), tr("The operation completed successfully.")); UISettings::values.is_game_list_reload_pending.exchange(true); } else { QMessageBox::warning(this, tr("Reset Metadata Cache"), tr("The metadata cache couldn't be deleted. It might be in use or non-existent.")); } }
void ConfigureFilesystem::UpdateEnabledControls() { ui->gamecard_current_game->setEnabled(ui->gamecard_inserted->isChecked()); ui->gamecard_path_edit->setEnabled(ui->gamecard_inserted->isChecked() && !ui->gamecard_current_game->isChecked()); ui->gamecard_path_button->setEnabled(ui->gamecard_inserted->isChecked() && !ui->gamecard_current_game->isChecked()); }
void ConfigureFilesystem::RetranslateUI() { ui->retranslateUi(this); }
void ConfigureFilesystem::changeEvent(QEvent* event) {
if (event->type() == QEvent::LanguageChange) {
RetranslateUI();
}
QWidget::changeEvent(event);
}
void ConfigureFilesystem::SetConfiguration() {
ui->nand_directory_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::NANDDir)));
ui->sdmc_directory_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::SDMCDir)));
ui->gamecard_path_edit->setText(QString::fromStdString(Settings::values.gamecard_path.GetValue()));
ui->dump_path_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::DumpDir)));
ui->load_path_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::LoadDir)));
ui->gamecard_inserted->setChecked(Settings::values.gamecard_inserted.GetValue());
ui->gamecard_current_game->setChecked(Settings::values.gamecard_current_game.GetValue());
ui->dump_exefs->setChecked(Settings::values.dump_exefs.GetValue());
ui->dump_nso->setChecked(Settings::values.dump_nso.GetValue());
ui->cache_game_list->setChecked(UISettings::values.cache_game_list.GetValue());
ui->prompt_for_autoloader->setChecked(UISettings::values.prompt_for_autoloader.GetValue());
#ifdef __linux__
ui->enable_backups_checkbox->setChecked(UISettings::values.updater_enable_backups.GetValue());
const std::string& backup_path = UISettings::values.updater_backup_path.GetValue();
if (!backup_path.empty()) {
ui->custom_backup_location_checkbox->setChecked(true);
ui->custom_backup_location_edit->setText(QString::fromStdString(backup_path));
} else {
ui->custom_backup_location_checkbox->setChecked(false);
}
m_old_custom_backup_enabled = ui->custom_backup_location_checkbox->isChecked();
m_old_backup_path = ui->custom_backup_location_edit->text();
#endif
UpdateEnabledControls();
}
void ConfigureFilesystem::ApplyConfiguration() {
Common::FS::SetCitronPath(Common::FS::CitronPath::NANDDir, ui->nand_directory_edit->text().toStdString());
Common::FS::SetCitronPath(Common::FS::CitronPath::SDMCDir, ui->sdmc_directory_edit->text().toStdString());
Common::FS::SetCitronPath(Common::FS::CitronPath::DumpDir, ui->dump_path_edit->text().toStdString());
Common::FS::SetCitronPath(Common::FS::CitronPath::LoadDir, ui->load_path_edit->text().toStdString());
Settings::values.gamecard_inserted = ui->gamecard_inserted->isChecked();
Settings::values.gamecard_current_game = ui->gamecard_current_game->isChecked();
Settings::values.dump_exefs = ui->dump_exefs->isChecked();
Settings::values.dump_nso = ui->dump_nso->isChecked();
UISettings::values.cache_game_list = ui->cache_game_list->isChecked();
UISettings::values.prompt_for_autoloader = ui->prompt_for_autoloader->isChecked();
#ifdef __linux__
UISettings::values.updater_enable_backups = ui->enable_backups_checkbox->isChecked();
const bool new_custom_backup_enabled = ui->custom_backup_location_checkbox->isChecked();
const QString new_backup_path = ui->custom_backup_location_edit->text();
if (new_custom_backup_enabled) {
UISettings::values.updater_backup_path = new_backup_path.toStdString();
} else {
UISettings::values.updater_backup_path = "";
}
QByteArray appimage_path_env = qgetenv("APPIMAGE");
const QString default_path = appimage_path_env.isEmpty() ? QString() : QFileInfo(QString::fromUtf8(appimage_path_env)).dir().filePath(QStringLiteral("backup"));
QString old_path_to_check;
if (m_old_custom_backup_enabled && !m_old_backup_path.isEmpty()) {
old_path_to_check = m_old_backup_path;
} else if (!default_path.isEmpty()) {
old_path_to_check = default_path;
}
QString new_path_to_check;
if (new_custom_backup_enabled && !new_backup_path.isEmpty()) {
new_path_to_check = new_backup_path;
} else if (!default_path.isEmpty()) {
new_path_to_check = default_path;
}
if (!old_path_to_check.isEmpty() && !new_path_to_check.isEmpty() && old_path_to_check != new_path_to_check) {
QDir old_dir(old_path_to_check);
if (old_dir.exists() && !old_dir.entryInfoList({QStringLiteral("citron-backup-*.AppImage")}, QDir::Files).isEmpty()) {
QMessageBox::StandardButton reply = QMessageBox::question(this, tr("Migrate AppImage Backups?"),
tr("The backup location has changed. Would you like to move your existing backups from the old location to the new one?"),
QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes) {
MigrateBackups(old_path_to_check, new_path_to_check);
}
}
}
#endif
}
void ConfigureFilesystem::SetDirectory(DirectoryTarget target, QLineEdit* edit) {
QString caption;
switch (target) {
case DirectoryTarget::NAND:
caption = tr("Select Emulated NAND Directory...");
break;
case DirectoryTarget::SD:
caption = tr("Select Emulated SD Directory...");
break;
case DirectoryTarget::Gamecard:
caption = tr("Select Gamecard Path...");
break;
case DirectoryTarget::Dump:
caption = tr("Select Dump Directory...");
break;
case DirectoryTarget::Load:
caption = tr("Select Mod Load Directory...");
break;
}
QString str;
if (target == DirectoryTarget::Gamecard) {
str = QFileDialog::getOpenFileName(this, caption, QFileInfo(edit->text()).dir().path(),
QStringLiteral("NX Gamecard;*.xci"));
} else {
str = QFileDialog::getExistingDirectory(this, caption, edit->text());
}
if (str.isNull() || str.isEmpty()) {
return;
}
if (str.back() != QChar::fromLatin1('/')) {
str.append(QChar::fromLatin1('/'));
}
edit->setText(str);
}
void ConfigureFilesystem::ResetMetadata() {
if (!Common::FS::Exists(Common::FS::GetCitronPath(Common::FS::CitronPath::CacheDir) / "game_list/")) {
QMessageBox::information(this, tr("Reset Metadata Cache"), tr("The metadata cache is already empty."));
} else if (Common::FS::RemoveDirRecursively(Common::FS::GetCitronPath(Common::FS::CitronPath::CacheDir) / "game_list")) {
QMessageBox::information(this, tr("Reset Metadata Cache"), tr("The operation completed successfully."));
UISettings::values.is_game_list_reload_pending.exchange(true);
} else {
QMessageBox::warning(this, tr("Reset Metadata Cache"), tr("The metadata cache couldn't be deleted. It might be in use or non-existent."));
}
}
void ConfigureFilesystem::UpdateEnabledControls() {
ui->gamecard_current_game->setEnabled(ui->gamecard_inserted->isChecked());
ui->gamecard_path_edit->setEnabled(ui->gamecard_inserted->isChecked() && !ui->gamecard_current_game->isChecked());
ui->gamecard_path_button->setEnabled(ui->gamecard_inserted->isChecked() && !ui->gamecard_current_game->isChecked());
#ifdef __linux__
ui->updater_group->setVisible(true);
bool backups_enabled = ui->enable_backups_checkbox->isChecked();
ui->custom_backup_location_checkbox->setEnabled(backups_enabled);
bool useCustomBackup = backups_enabled && ui->custom_backup_location_checkbox->isChecked();
ui->custom_backup_location_edit->setEnabled(useCustomBackup);
ui->custom_backup_location_button->setEnabled(useCustomBackup);
#else
ui->updater_group->setVisible(false);
#endif
}
void ConfigureFilesystem::RetranslateUI() {
ui->retranslateUi(this);
}
#ifdef __linux__
void ConfigureFilesystem::MigrateBackups(const QString& old_path, const QString& new_path) {
QDir old_dir(old_path);
if (!old_dir.exists()) {
QMessageBox::warning(this, tr("Migration Error"), tr("The old backup location does not exist."));
return;
}
QStringList name_filters;
name_filters << QStringLiteral("citron-backup-*.AppImage");
QFileInfoList files_to_move = old_dir.entryInfoList(name_filters, QDir::Files);
if (files_to_move.isEmpty()) {
QMessageBox::information(this, tr("Migration Complete"), tr("No backup files were found to migrate."));
return;
}
auto progress = new QProgressDialog(tr("Moving backup files..."), tr("Cancel"), 0, files_to_move.count(), this);
progress->setWindowModality(Qt::WindowModal);
progress->setMinimumDuration(1000);
progress->show();
auto watcher = new QFutureWatcher<bool>(this);
connect(watcher, &QFutureWatcher<bool>::finished, this, [this, watcher, progress] {
progress->close();
if (watcher->future().isCanceled()) {
QMessageBox::warning(this, tr("Migration Canceled"), tr("The migration was canceled. Some files may have been moved."));
} else if (watcher->future().result()) {
QMessageBox::information(this, tr("Migration Complete"), tr("All backup files were successfully moved to the new location."));
} else {
QMessageBox::critical(this, tr("Migration Failed"), tr("An error occurred while moving files. Some files may not have been moved. Please check both locations."));
}
watcher->deleteLater();
});
connect(progress, &QProgressDialog::canceled, watcher, &QFutureWatcher<void>::cancel);
QFuture<bool> future = QtConcurrent::run([=] {
QDir new_dir(new_path);
if (!new_dir.exists()) {
if (!new_dir.mkpath(QStringLiteral("."))) {
return false;
}
}
for (int i = 0; i < files_to_move.count(); ++i) {
if (progress->wasCanceled()) {
return false;
}
progress->setValue(i);
const auto& file_info = files_to_move.at(i);
QString new_file_path = new_dir.filePath(file_info.fileName());
if (QFile::exists(new_file_path)) {
if (!QFile::remove(new_file_path)) {
return false; // Failed to remove existing file
}
}
if (!QFile::copy(file_info.absoluteFilePath(), new_file_path)) {
return false; // Copy operation failed
}
if (!QFile::remove(file_info.absoluteFilePath())) {
return false; // Delete operation failed
}
}
return true;
});
watcher->setFuture(future);
}
#endif
void ConfigureFilesystem::OnUpdateInstallProgress() {
if (install_progress) {
@@ -67,9 +303,8 @@ void ConfigureFilesystem::OnRunAutoloader(bool skip_confirmation) {
}
}
GMainWindow* main_window = qobject_cast<GMainWindow*>(this->parent()); // Try direct parent first
GMainWindow* main_window = qobject_cast<GMainWindow*>(this->parent());
if (!main_window) {
// Fallback for when it's nested in the config dialog
main_window = qobject_cast<GMainWindow*>(this->window()->parent());
}
@@ -116,7 +351,6 @@ void ConfigureFilesystem::OnRunAutoloader(bool skip_confirmation) {
install_progress->setWindowFlags(install_progress->windowFlags() & ~Qt::WindowContextHelpButtonHint);
install_progress->setAttribute(Qt::WA_DeleteOnClose, true);
install_progress->setFixedWidth(400);
// When the dialog is destroyed (e.g., user clicks X), set the pointer to nullptr
connect(install_progress, &QObject::destroyed, this, [this]() { install_progress = nullptr; });
install_progress->show();
@@ -147,9 +381,15 @@ void ConfigureFilesystem::OnRunAutoloader(bool skip_confirmation) {
ContentManager::InstallResult result = future.result();
switch (result) {
case ContentManager::InstallResult::Success: new_files.append(QFileInfo(file).fileName()); break;
case ContentManager::InstallResult::Overwrite: overwritten_files.append(QFileInfo(file).fileName()); break;
case ContentManager::InstallResult::Failure: failed_files.append(QFileInfo(file).fileName()); break;
case ContentManager::InstallResult::Success:
new_files.append(QFileInfo(file).fileName());
break;
case ContentManager::InstallResult::Overwrite:
overwritten_files.append(QFileInfo(file).fileName());
break;
case ContentManager::InstallResult::Failure:
failed_files.append(QFileInfo(file).fileName());
break;
case ContentManager::InstallResult::BaseInstallAttempted:
failed_files.append(QFileInfo(file).fileName());
detected_base_install = true;
@@ -173,9 +413,12 @@ void ConfigureFilesystem::OnRunAutoloader(bool skip_confirmation) {
} else {
QString install_results = tr("Installation Complete!");
install_results.append(QLatin1String("\n\n"));
if (!new_files.isEmpty()) install_results.append(tr("%n file(s) were newly installed.", nullptr, new_files.size()));
if (!overwritten_files.isEmpty()) install_results.append(tr("\n%n file(s) were overwritten.", nullptr, overwritten_files.size()));
if (!failed_files.isEmpty()) install_results.append(tr("\n%n file(s) failed to install.", nullptr, failed_files.size()));
if (!new_files.isEmpty())
install_results.append(tr("%n file(s) were newly installed.", nullptr, new_files.size()));
if (!overwritten_files.isEmpty())
install_results.append(tr("\n%n file(s) were overwritten.", nullptr, overwritten_files.size()));
if (!failed_files.isEmpty())
install_results.append(tr("\n%n file(s) failed to install.", nullptr, failed_files.size()));
QMessageBox::information(this, tr("Install Results"), install_results);
}

View File

@@ -40,6 +40,11 @@ private:
void ResetMetadata();
void UpdateEnabledControls();
void MigrateBackups(const QString& old_path, const QString& new_path);
std::unique_ptr<Ui::ConfigureFilesystem> ui;
QProgressDialog* install_progress = nullptr;
bool m_old_custom_backup_enabled{};
QString m_old_backup_path;
};

View File

@@ -218,6 +218,39 @@
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="updater_group">
<property name="title">
<string>Updater</string>
</property>
<layout class="QGridLayout" name="gridLayout_updater">
<item row="0" column="0" colspan="2">
<widget class="QCheckBox" name="enable_backups_checkbox">
<property name="text">
<string>Enable AppImage Backups</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="custom_backup_location_checkbox">
<property name="text">
<string>Use Custom Backup Location for AppImage Updates</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLineEdit" name="custom_backup_location_edit"/>
</item>
<item row="2" column="1">
<widget class="QToolButton" name="custom_backup_location_button">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_5">
<property name="title">

View File

@@ -150,6 +150,13 @@ namespace UISettings {
Setting<bool> check_for_updates_on_start{linkage, true, "check_for_updates_on_start", Category::Ui};
// User might not want backups. Allow them to disable/re-enable accordingly.
Setting<bool> updater_enable_backups{linkage, true, "updater/enableBackups", Category::Ui};
// The custom directory to store AppImage backups on Linux. If empty, backups are stored
// in a 'backup' folder next to the main AppImage.
Setting<std::string> updater_backup_path{linkage, "", "updater/backupPath", Category::Ui};
// Discord RPC
Setting<bool> enable_discord_presence{linkage, true, "enable_discord_presence", Category::Ui};

View File

@@ -4,6 +4,7 @@
#include <filesystem>
#include "citron/updater/updater_dialog.h"
#include "citron/uisettings.h"
#include "ui_updater_dialog.h"
#include <QApplication>
@@ -14,6 +15,7 @@
#include <QMessageBox>
#include <QProcess>
#include <QRegularExpression>
#include <QSettings>
#include <QTimer>
#include <QUrl>
@@ -371,18 +373,32 @@ void UpdaterDialog::ShowCompletedState() {
"The update has been downloaded and prepared successfully. "
"The update will be applied when you restart Citron.");
// Check for the APPIMAGE env var to locate the backup directory.
QByteArray appimage_path_env = qgetenv("APPIMAGE");
if (!appimage_path_env.isEmpty()) {
// Only show backup information if backups are enabled and we're in an AppImage.
if (!appimage_path_env.isEmpty() && UISettings::values.updater_enable_backups.GetValue()) {
const std::string& custom_path = UISettings::values.updater_backup_path.GetValue();
std::filesystem::path backup_dir;
QString native_backup_path;
if (!custom_path.empty()) {
// User HAS set a custom path.
backup_dir = custom_path;
native_backup_path = QDir::toNativeSeparators(QString::fromStdString(backup_dir.string()));
status_message.append(
QStringLiteral("\n\nA backup of the previous version has been saved to your custom location:\n%1")
.arg(native_backup_path));
} else {
// User has NOT set a custom path, use the default.
std::filesystem::path appimage_path(appimage_path_env.constData());
std::filesystem::path backup_dir = appimage_path.parent_path() / "backup";
// Use QDir to present the path in a native format for the user.
QString native_backup_path = QDir::toNativeSeparators(QString::fromStdString(backup_dir.string()));
backup_dir = appimage_path.parent_path() / "backup";
native_backup_path = QDir::toNativeSeparators(QString::fromStdString(backup_dir.string()));
status_message.append(
QStringLiteral("\n\nA backup of the previous version has been saved to:\n%1")
.arg(native_backup_path));
// Add the helpful tip.
status_message.append(
QStringLiteral("\n\nP.S. You can change the backup location or disable backups in Emulation > Configure > Filesystem."));
}
}
ui->statusLabel->setText(status_message);

View File

@@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include "citron/updater/updater_service.h"
#include "citron/uisettings.h"
#include "common/logging/log.h"
#include "common/fs/path_util.h"
#include "common/scm_rev.h"
@@ -271,8 +272,6 @@ void UpdaterService::OnDownloadFinished() {
QSettings settings;
QString channel = settings.value(QStringLiteral("updater/channel"), QStringLiteral("Stable")).toString();
// This logic has been simplified for clarity. The checksum part can be re-added later.
#if defined(_WIN32)
QString filename = QStringLiteral("citron_update_%1.zip").arg(QString::fromStdString(current_update_info.version));
std::filesystem::path download_path = temp_download_path / filename.toStdString();
@@ -323,16 +322,27 @@ void UpdaterService::OnDownloadFinished() {
std::filesystem::path original_appimage_path = appimage_path_env;
std::filesystem::path appimage_dir = original_appimage_path.parent_path();
std::filesystem::path backup_dir = appimage_dir / "backup";
std::error_code ec;
// 1. Create the backup directory
// Check if backups are enabled before doing anything.
if (UISettings::values.updater_enable_backups.GetValue()) {
const std::string& custom_backup_path = UISettings::values.updater_backup_path.GetValue();
std::filesystem::path backup_dir;
if (!custom_backup_path.empty()) {
// User has specified a custom path.
backup_dir = custom_backup_path;
} else {
// Default behavior: create 'backup' folder next to the AppImage.
backup_dir = appimage_dir / "backup";
}
// Create the backup directory
std::filesystem::create_directories(backup_dir, ec);
if (ec) {
LOG_ERROR(Frontend, "Failed to create backup directory: {}", ec.message());
// Do not stop the update; the backup is a convenience, not critical.
} else {
// 2. Create the backup copy of the old AppImage
// Create the backup copy of the old AppImage
std::string current_version = GetCurrentVersion();
std::string backup_filename = "citron-backup-" + (current_version.empty() ? "unknown" : current_version) + ".AppImage";
std::filesystem::path backup_filepath = backup_dir / backup_filename;
@@ -343,8 +353,8 @@ void UpdaterService::OnDownloadFinished() {
LOG_INFO(Frontend, "Created backup of old AppImage at: {}", backup_filepath.string());
}
}
}
// 3. Save the new AppImage to a temporary file
std::filesystem::path new_appimage_path = original_appimage_path.string() + ".new";
QFile new_file(QString::fromStdString(new_appimage_path.string()));
if (!new_file.open(QIODevice::WriteOnly)) {
@@ -355,17 +365,15 @@ void UpdaterService::OnDownloadFinished() {
new_file.write(downloaded_data);
new_file.close();
// 4. Make the new file executable
if (!new_file.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner |
QFileDevice::ReadGroup | QFileDevice::ExeGroup |
QFileDevice::ReadOther | QFileDevice::ExeOther)) {
emit UpdateError(QStringLiteral("Failed to make the new AppImage executable."));
std::filesystem::remove(new_appimage_path, ec); // Clean up temp file
std::filesystem::remove(new_appimage_path, ec);
update_in_progress.store(false);
return;
}
// 5. Atomically replace the old AppImage with the new one
std::filesystem::rename(new_appimage_path, original_appimage_path, ec);
if (ec) {
LOG_ERROR(Frontend, "Failed to replace old AppImage: {}", ec.message());
@@ -374,7 +382,6 @@ void UpdaterService::OnDownloadFinished() {
return;
}
// 6. Update or remove the version file as before
std::filesystem::path version_file_path = appimage_dir / CITRON_VERSION_FILE;
if (channel == QStringLiteral("Stable")) {
LOG_INFO(Frontend, "Writing stable version marker: {}", current_update_info.version);
@@ -626,49 +633,49 @@ bool UpdaterService::CreateUpdateHelperScript(const std::filesystem::path& stagi
std::filesystem::path script_path = staging_path / "apply_update.bat";
LOG_INFO(Frontend, "Creating update helper script at: {}", script_path.string());
// Ensure staging directory exists
if (!std::filesystem::exists(staging_path)) {
LOG_ERROR(Frontend, "Staging path does not exist: {}", staging_path.string());
return false;
}
std::ofstream script(script_path, std::ios::out | std::ios::trunc);
if (!script.is_open()) {
LOG_ERROR(Frontend, "Failed to open file for writing: {}", script_path.string());
return false;
}
// Convert paths to Windows-style paths for the batch script
std::string staging_path_str = staging_path.string();
std::string app_path_str = app_directory.string();
std::string exe_path_str = (app_directory / "citron.exe").string();
// Replace forward slashes with backslashes
for (auto& ch : staging_path_str) if (ch == '/') ch = '\\';
for (auto& ch : app_path_str) if (ch == '/') ch = '\\';
for (auto& ch : exe_path_str) if (ch == '/') ch = '\\';
// Write batch script
script << "@echo off\n";
script << "REM Citron Auto-Updater Helper Script\n";
script << "REM This script applies staged updates after the main application exits\n\n";
script << "REM Citron Auto-Updater Helper Script\n\n";
script << "echo Waiting for Citron to close...\n";
script << "timeout /t 3 /nobreak >nul\n\n";
script << "echo Applying update...\n";
// This loop will continuously check if citron.exe is running.
// It will only proceed once the process is no longer found.
script << ":wait_loop\n";
script << "tasklist /FI \"IMAGENAME eq citron.exe\" | find /I \"citron.exe\" >nul\n";
script << "if not errorlevel 1 (\n";
script << " timeout /t 1 /nobreak >nul\n";
script << " goto wait_loop\n";
script << ")\n\n";
script << "echo Citron has closed. Applying update...\n";
script << "xcopy /E /Y /I \"" << staging_path_str << "\" \"" << app_path_str << "\" >nul 2>&1\n\n";
script << "if errorlevel 1 (\n";
script << " echo Update failed. Please restart Citron manually.\n";
script << " timeout /t 5\n";
script << " pause\n"; // Pause to let the user see the error
script << " exit /b 1\n";
script << ")\n\n";
script << "echo Update applied successfully!\n";
script << "timeout /t 1 /nobreak >nul\n\n";
script << "echo Restarting Citron...\n";
script << "start \"\" \"" << exe_path_str << "\"\n\n";
@@ -676,20 +683,12 @@ bool UpdaterService::CreateUpdateHelperScript(const std::filesystem::path& stagi
script << "rd /s /q \"" << staging_path_str << "\" >nul 2>&1\n\n";
script << "REM Delete this script\n";
script << "del \"%~f0\"\n";
script << "(goto) 2>nul & del \"%~f0\"\n";
script.flush();
script.close();
// Verify the file was created
if (!std::filesystem::exists(script_path)) {
LOG_ERROR(Frontend, "Script file was not created despite successful write!");
return false;
}
auto file_size = std::filesystem::file_size(script_path);
LOG_INFO(Frontend, "Update helper script created successfully: {} ({} bytes)",
script_path.string(), file_size);
LOG_INFO(Frontend, "Update helper script created successfully: {}", script_path.string());
return true;
} catch (const std::exception& e) {
LOG_ERROR(Frontend, "Exception creating update helper script: {}", e.what());