1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Windows updater: Create backup and restore it on failure

This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-11-16 11:24:15 +01:00
parent 007a10346d
commit 546b90c4c9
No known key found for this signature in database
GPG Key ID: 7E086DD661263264
2 changed files with 31 additions and 6 deletions

View File

@ -65,6 +65,7 @@ void Widget::fatalError(QString message)
{ {
QMessageBox::critical(this,tr("Error"), message+'\n'+tr("qTox will restart now.")); QMessageBox::critical(this,tr("Error"), message+'\n'+tr("qTox will restart now."));
deleteUpdate(); deleteUpdate();
restoreBackups();
startQToxAndExit(); startQToxAndExit();
} }
@ -80,6 +81,18 @@ void Widget::startQToxAndExit()
exit(0); exit(0);
} }
void Widget::deleteBackups()
{
for (QString file : backups)
QFile(file+".bak").remove();
}
void Widget::restoreBackups()
{
for (QString file : backups)
QFile(file+".bak").rename(file);
}
void Widget::update() void Widget::update()
{ {
/// 1. Find and parse the update (0-5%) /// 1. Find and parse the update (0-5%)
@ -138,17 +151,26 @@ void Widget::update()
float installProgress = 50; float installProgress = 50;
for (UpdateFileMeta fileMeta : diff) for (UpdateFileMeta fileMeta : diff)
{ {
QFile fileFile(updateDirStr+fileMeta.installpath); // Backup old files
if (!fileFile.copy(fileMeta.installpath)) if (QFile(fileMeta.installpath).exists())
fatalError(tr("Unable to copy the update's files.")); {
QFile(fileMeta.installpath).rename(fileMeta.installpath+".bak");
backups.append(fileMeta.installpath);
}
installProgress += installProgressStep; // Install new ones
setProgress(installProgress); QFile fileFile(updateDirStr+fileMeta.installpath);
if (!fileFile.copy(fileMeta.installpath))
fatalError(tr("Unable to copy the update's files."));
installProgress += installProgressStep;
setProgress(installProgress);
} }
setProgress(95); setProgress(95);
/// 4. Delete the update (95-100%) /// 4. Delete the update and backups (95-100%)
deleteUpdate(); deleteUpdate();
setProgress(97);
deleteBackups();
setProgress(100); setProgress(100);
/// 5. Start qTox and exit /// 5. Start qTox and exit

View File

@ -33,6 +33,8 @@ public:
~Widget(); ~Widget();
// Utilities // Utilities
void deleteBackups();
void restoreBackups();
void setProgress(int value); void setProgress(int value);
// Noreturn // Noreturn
@ -46,6 +48,7 @@ public slots:
private: private:
Ui::Widget *ui; Ui::Widget *ui;
QStringList backups;
}; };
#endif // WIDGET_H #endif // WIDGET_H