summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2020-09-17 18:14:24 +0300
committerGeorge Hazan <ghazan@miranda.im>2020-09-17 18:14:24 +0300
commitdcd1fc6f6662c6c513f2e15e42473372e6a0240f (patch)
treeae128e273290576479d0b49c903d45013aeef595 /plugins
parentf513c977a39875aa8a180c2d70567957567f1724 (diff)
fixes #2554 (PluginUpdater: делать откат в случае неудачи при замене файлов)
Diffstat (limited to 'plugins')
-rw-r--r--plugins/PluginUpdater/src/DlgUpdate.cpp2
-rw-r--r--plugins/PluginUpdater/src/Utils.cpp59
-rw-r--r--plugins/PluginUpdater/src/stdafx.h1
3 files changed, 53 insertions, 9 deletions
diff --git a/plugins/PluginUpdater/src/DlgUpdate.cpp b/plugins/PluginUpdater/src/DlgUpdate.cpp
index d27012b5b7..5396683204 100644
--- a/plugins/PluginUpdater/src/DlgUpdate.cpp
+++ b/plugins/PluginUpdater/src/DlgUpdate.cpp
@@ -91,6 +91,7 @@ class CUpdateDLg : public CDlgBase
mir_snwprintf(wszBackFile, L"%s\\%s", wszBackupFolder, it->wszNewName + wcslen(wszMirandaPath) + 1);
if (dwErrorCode = BackupFile(it->wszNewName, wszBackFile)) {
LBL_Error:
+ RollbackChanges(wszBackupFolder);
Skin_PlaySound("updatefailed");
CMStringW wszError(FORMAT, TranslateT("Unpack operation failed with error code=%d, update terminated"), dwErrorCode);
MessageBox(pDlg->GetHwnd(), wszError, TranslateT("Plugin Updater"), MB_OK | MB_ICONERROR);
@@ -441,6 +442,7 @@ static void DlgUpdateSilent(void *param)
mir_snwprintf(wszBackFile, L"%s\\%s", wszBackupFolder, it->wszNewName + wcslen(wszMirandaPath) + 1);
if (dwErrorCode = BackupFile(it->wszNewName, wszBackFile)) {
LBL_Error:
+ RollbackChanges(wszBackupFolder);
Skin_PlaySound("updatefailed");
delete &UpdateFiles;
return;
diff --git a/plugins/PluginUpdater/src/Utils.cpp b/plugins/PluginUpdater/src/Utils.cpp
index 5126bc04b8..63c1ede5b4 100644
--- a/plugins/PluginUpdater/src/Utils.cpp
+++ b/plugins/PluginUpdater/src/Utils.cpp
@@ -467,17 +467,20 @@ void RemoveBackupFolders()
WIN32_FIND_DATAW fdata;
HANDLE hFind = FindFirstFileW(wszMask, &fdata);
- if (hFind) {
- // sort folder names alphabetically
- OBJLIST<CMStringW> arNames(1, CompareDirs);
- do {
- if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- arNames.insert(new CMStringW(fdata.cFileName));
- } while (FindNextFileW(hFind, &fdata));
+ if (!hFind)
+ return;
- // remove all folders with lesser dates if there're more than 10 folders
- if (PrepareEscalation())
+ // sort folder names alphabetically
+ OBJLIST<CMStringW> arNames(1, CompareDirs);
+ do {
+ if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
+ arNames.insert(new CMStringW(fdata.cFileName));
+ } while (FindNextFileW(hFind, &fdata));
+ FindClose(hFind);
+
+ // remove all folders with lesser dates if there're more than 10 folders
+ if (PrepareEscalation()) {
while (arNames.getCount() > g_plugin.iNumberBackups) {
mir_snwprintf(wszMask, L"%s\\Backups\\%s", g_wszRoot, arNames[0].c_str());
SafeDeleteDirectory(wszMask);
@@ -487,6 +490,44 @@ void RemoveBackupFolders()
}
/////////////////////////////////////////////////////////////////////////////////////////
+// moves updated files back to Miranda's folder
+
+static void SafeMoveFolder(const wchar_t *wszSrc, const wchar_t *wszDest)
+{
+ TFileName wszNewSrc, wszNewDest;
+ mir_snwprintf(wszNewSrc, L"%s\\*", wszSrc);
+
+ WIN32_FIND_DATAW fdata;
+ HANDLE hFind = FindFirstFileW(wszNewSrc, &fdata);
+ if (!hFind)
+ return;
+
+ do {
+ if (!mir_wstrcmp(fdata.cFileName, L".") || !mir_wstrcmp(fdata.cFileName, L".."))
+ continue;
+
+ mir_snwprintf(wszNewSrc, L"%s\\%s", wszSrc, fdata.cFileName);
+ mir_snwprintf(wszNewDest, L"%s\\%s", wszDest, fdata.cFileName);
+
+ if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
+ SafeMoveFolder(wszNewSrc, wszNewDest);
+ else
+ SafeMoveFile(wszNewSrc, wszNewDest);
+ }
+ while (FindNextFileW(hFind, &fdata));
+
+ FindClose(hFind);
+}
+
+void RollbackChanges(TFileName &pwszBackupFolder)
+{
+ VARSW dirname(L"%miranda_path%");
+ SafeMoveFolder(pwszBackupFolder, dirname);
+
+ SafeDeleteDirectory(pwszBackupFolder);
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
int TransactPipe(int opcode, const wchar_t *p1, const wchar_t *p2)
{
diff --git a/plugins/PluginUpdater/src/stdafx.h b/plugins/PluginUpdater/src/stdafx.h
index 28525666c9..8a0aea883e 100644
--- a/plugins/PluginUpdater/src/stdafx.h
+++ b/plugins/PluginUpdater/src/stdafx.h
@@ -259,6 +259,7 @@ bool PrepareEscalation(void);
void CreateWorkFolders(TFileName &wszTempFolder, TFileName &wszBackupFolder);
void RemoveBackupFolders(void);
+void RollbackChanges(TFileName &pwszBackupFolder);
int SafeCreateDirectory(const wchar_t *pwszDirName);
int SafeDeleteDirectory(const wchar_t *pwszDirName);