From 8b207363ce680529b39464c6209d1ad8cfc8c86c Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 14 Sep 2020 20:09:54 +0300 Subject: Plugin Updater: code cleaning --- plugins/PluginUpdater/src/DlgListNew.cpp | 2 -- plugins/PluginUpdater/src/DlgUpdate.cpp | 3 --- plugins/PluginUpdater/src/Options.cpp | 3 ++- plugins/PluginUpdater/src/Utils.cpp | 34 +++++++++++++++++++------------- plugins/PluginUpdater/src/stdafx.h | 17 +--------------- plugins/PluginUpdater/src/unzipfile.cpp | 4 ++-- 6 files changed, 25 insertions(+), 38 deletions(-) (limited to 'plugins/PluginUpdater') diff --git a/plugins/PluginUpdater/src/DlgListNew.cpp b/plugins/PluginUpdater/src/DlgListNew.cpp index 721934af09..b470cff31c 100644 --- a/plugins/PluginUpdater/src/DlgListNew.cpp +++ b/plugins/PluginUpdater/src/DlgListNew.cpp @@ -291,8 +291,6 @@ public: void Unpack() { - AutoHandle pipe(hPipe); - // create needed folders after escalating priviledges. Folders creates when we actually install updates TFileName wszTempFolder, wszBackupFolder; CreateWorkFolders(wszTempFolder, wszBackupFolder); diff --git a/plugins/PluginUpdater/src/DlgUpdate.cpp b/plugins/PluginUpdater/src/DlgUpdate.cpp index 732a6e04d6..d27012b5b7 100644 --- a/plugins/PluginUpdater/src/DlgUpdate.cpp +++ b/plugins/PluginUpdater/src/DlgUpdate.cpp @@ -47,7 +47,6 @@ class CUpdateDLg : public CDlgBase VARSW wszMirandaPath(L"%miranda_path%"); { ThreadWatch threadId(pDlg->dwThreadId); - AutoHandle pipe(hPipe); // Create needed folders after escalating priviledges. Folders creates when we actually install updates TFileName wszTempFolder, wszBackupFolder; @@ -400,8 +399,6 @@ static void DlgUpdateSilent(void *param) return; } - AutoHandle pipe(hPipe); - // Create needed folders after escalating priviledges. Folders creates when we actually install updates TFileName wszTempFolder, wszBackupFolder; CreateWorkFolders(wszTempFolder, wszBackupFolder); diff --git a/plugins/PluginUpdater/src/Options.cpp b/plugins/PluginUpdater/src/Options.cpp index 7880d0269c..c796524d7f 100644 --- a/plugins/PluginUpdater/src/Options.cpp +++ b/plugins/PluginUpdater/src/Options.cpp @@ -288,7 +288,8 @@ public: FindClose(hFind); } - RemoveBackupFolders(); + if (PrepareEscalation()) + RemoveBackupFolders(); // if user tried to change the channel, run the update dialog immediately if (bStartUpdate) diff --git a/plugins/PluginUpdater/src/Utils.cpp b/plugins/PluginUpdater/src/Utils.cpp index 3b346ffd46..5126bc04b8 100644 --- a/plugins/PluginUpdater/src/Utils.cpp +++ b/plugins/PluginUpdater/src/Utils.cpp @@ -20,7 +20,7 @@ Boston, MA 02111-1307, USA. #include "stdafx.h" HNETLIBUSER hNetlibUser = nullptr; -HANDLE hPipe = nullptr; +HANDLE g_hPipe = nullptr; ///////////////////////////////////////////////////////////////////////////////////// @@ -398,12 +398,16 @@ bool PrepareEscalation() if (IsRunAsAdmin()) return true; + // if pipe already opened? + if (g_hPipe != nullptr) + return true; + // Elevate the process. Create a pipe for a stub first TFileName wzPipeName; mir_snwprintf(wzPipeName, L"\\\\.\\pipe\\Miranda_Pu_%d", GetCurrentProcessId()); - hPipe = CreateNamedPipe(wzPipeName, PIPE_ACCESS_DUPLEX, PIPE_READMODE_BYTE | PIPE_WAIT, 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, nullptr); - if (hPipe == INVALID_HANDLE_VALUE) { - hPipe = nullptr; + g_hPipe = CreateNamedPipe(wzPipeName, PIPE_ACCESS_DUPLEX, PIPE_READMODE_BYTE | PIPE_WAIT, 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, nullptr); + if (g_hPipe == INVALID_HANDLE_VALUE) { + g_hPipe = nullptr; } else { wchar_t cmdLine[100], *p; @@ -420,8 +424,8 @@ bool PrepareEscalation() sei.hwnd = nullptr; sei.nShow = SW_NORMAL; if (ShellExecuteEx(&sei)) { - if (hPipe != nullptr) - ConnectNamedPipe(hPipe, nullptr); + if (g_hPipe != nullptr) + ConnectNamedPipe(g_hPipe, nullptr); return true; } @@ -472,6 +476,8 @@ void RemoveBackupFolders() } while (FindNextFileW(hFind, &fdata)); // 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); @@ -500,11 +506,11 @@ int TransactPipe(int opcode, const wchar_t *p1, const wchar_t *p2) else *dst++ = 0; DWORD dwBytes = 0, dwError; - if (!WriteFile(hPipe, buf, (DWORD)((BYTE *)dst - buf), &dwBytes, nullptr)) + if (!WriteFile(g_hPipe, buf, (DWORD)((BYTE *)dst - buf), &dwBytes, nullptr)) return GetLastError(); dwError = 0; - if (!ReadFile(hPipe, &dwError, sizeof(DWORD), &dwBytes, nullptr)) + if (!ReadFile(g_hPipe, &dwError, sizeof(DWORD), &dwBytes, nullptr)) return GetLastError(); if (dwBytes != sizeof(DWORD)) return ERROR_BAD_ARGUMENTS; @@ -514,7 +520,7 @@ int TransactPipe(int opcode, const wchar_t *p1, const wchar_t *p2) int SafeCopyFile(const wchar_t *pSrc, const wchar_t *pDst) { - if (hPipe == nullptr) + if (g_hPipe == nullptr) return CopyFileW(pSrc, pDst, FALSE); return TransactPipe(1, pSrc, pDst); @@ -522,7 +528,7 @@ int SafeCopyFile(const wchar_t *pSrc, const wchar_t *pDst) int SafeMoveFile(const wchar_t *pSrc, const wchar_t *pDst) { - if (hPipe == nullptr) { + if (g_hPipe == nullptr) { if (!DeleteFileW(pDst)) { DWORD dwError = GetLastError(); if (dwError != ERROR_ACCESS_DENIED && dwError != ERROR_FILE_NOT_FOUND) @@ -560,7 +566,7 @@ int SafeMoveFile(const wchar_t *pSrc, const wchar_t *pDst) int SafeDeleteFile(const wchar_t *pwszFile) { - if (hPipe == nullptr) + if (g_hPipe == nullptr) return DeleteFile(pwszFile); return TransactPipe(3, pwszFile, nullptr); @@ -568,7 +574,7 @@ int SafeDeleteFile(const wchar_t *pwszFile) int SafeCreateDirectory(const wchar_t *pwszFolder) { - if (hPipe == nullptr) + if (g_hPipe == nullptr) return CreateDirectoryTreeW(pwszFolder); return TransactPipe(4, pwszFolder, nullptr); @@ -576,7 +582,7 @@ int SafeCreateDirectory(const wchar_t *pwszFolder) int SafeDeleteDirectory(const wchar_t *pwszDirName) { - if (hPipe == nullptr) + if (g_hPipe == nullptr) return DeleteDirectoryTreeW(pwszDirName); return TransactPipe(6, pwszDirName, nullptr); @@ -584,7 +590,7 @@ int SafeDeleteDirectory(const wchar_t *pwszDirName) int SafeCreateFilePath(const wchar_t *pwszFolder) { - if (hPipe == nullptr) { + if (g_hPipe == nullptr) { CreatePathToFileW(pwszFolder); return 0; } diff --git a/plugins/PluginUpdater/src/stdafx.h b/plugins/PluginUpdater/src/stdafx.h index efaf4787a3..28525666c9 100644 --- a/plugins/PluginUpdater/src/stdafx.h +++ b/plugins/PluginUpdater/src/stdafx.h @@ -153,7 +153,7 @@ using namespace std; extern DWORD g_mirandaVersion; extern wchar_t g_wszRoot[MAX_PATH], g_wszTempPath[MAX_PATH]; -extern HANDLE hPipe; +extern HANDLE g_hPipe; extern HNETLIBUSER hNetlibUser; extern IconItem iconList[]; @@ -181,21 +181,6 @@ void UninitListNew(void); int OptInit(WPARAM, LPARAM); -class AutoHandle : private MNonCopyable -{ - HANDLE &m_handle; - -public: - AutoHandle(HANDLE &_handle) : m_handle(_handle) {} - ~AutoHandle() - { - if (m_handle) { - ::CloseHandle(m_handle); - m_handle = nullptr; - } - } -}; - class ThreadWatch { DWORD &pId; diff --git a/plugins/PluginUpdater/src/unzipfile.cpp b/plugins/PluginUpdater/src/unzipfile.cpp index e837509ef3..fb3bb2ae5a 100644 --- a/plugins/PluginUpdater/src/unzipfile.cpp +++ b/plugins/PluginUpdater/src/unzipfile.cpp @@ -69,7 +69,7 @@ int extractCurrentFile(unzFile uf, wchar_t *pwszDestPath, wchar_t *pwszBackPath, SafeCreateFilePath(wszDestFile); wchar_t *pwszFile2unzip; - if (hPipe == nullptr) // direct mode + if (g_hPipe == nullptr) // direct mode pwszFile2unzip = wszDestFile; else { TFileName wszTempPath; @@ -103,7 +103,7 @@ int extractCurrentFile(unzFile uf, wchar_t *pwszDestPath, wchar_t *pwszBackPath, CloseHandle(hFile); unzCloseCurrentFile(uf); /* don't lose the error */ - if (hPipe) + if (g_hPipe) SafeMoveFile(pwszFile2unzip, wszDestFile); } return err; -- cgit v1.2.3