summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2020-08-27 21:26:06 +0300
committerGeorge Hazan <ghazan@miranda.im>2020-08-27 21:26:06 +0300
commit4be5fbc996a104b1ec99327a2e46f45d1aaeb58f (patch)
tree9c2463031ef6c6957f6037e07bdf5a689d3f78e6
parent3c74a59033f723e6c209284cbaed97425136c627 (diff)
Plugin Updater: more code cleaning
-rw-r--r--plugins/PluginUpdater/pu_stub/src/pu_stub.cpp12
-rw-r--r--plugins/PluginUpdater/src/Utils.cpp66
2 files changed, 41 insertions, 37 deletions
diff --git a/plugins/PluginUpdater/pu_stub/src/pu_stub.cpp b/plugins/PluginUpdater/pu_stub/src/pu_stub.cpp
index cca42b5c10..f93a07484d 100644
--- a/plugins/PluginUpdater/pu_stub/src/pu_stub.cpp
+++ b/plugins/PluginUpdater/pu_stub/src/pu_stub.cpp
@@ -85,13 +85,15 @@ int APIENTRY wWinMain(HINSTANCE /*hInstance*/, HINSTANCE, LPTSTR lpCmdLine, int)
case 2: // move
if (!DeleteFileW(ptszFile2)) {
- dwError = GetLastError();
- if (dwError != ERROR_ACCESS_DENIED && dwError != ERROR_FILE_NOT_FOUND)
+ DWORD err = GetLastError();
+ if (err != ERROR_ACCESS_DENIED && err != ERROR_FILE_NOT_FOUND) {
+ dwError = err;
break;
+ }
}
if (!MoveFileW(ptszFile1, ptszFile2)) { // use copy on error
- switch (dwError = GetLastError()) {
+ switch (DWORD err = GetLastError()) {
case ERROR_ALREADY_EXISTS:
dwError = 0;
break; // this file was included into many archives, so Miranda tries to move it again & again
@@ -106,8 +108,10 @@ int APIENTRY wWinMain(HINSTANCE /*hInstance*/, HINSTANCE, LPTSTR lpCmdLine, int)
if (!DeleteFileW(ptszFile1))
dwError = GetLastError();
+ break;
- dwError = 0;
+ default:
+ dwError = err;
break;
}
}
diff --git a/plugins/PluginUpdater/src/Utils.cpp b/plugins/PluginUpdater/src/Utils.cpp
index 5121ab8df3..33acef4b87 100644
--- a/plugins/PluginUpdater/src/Utils.cpp
+++ b/plugins/PluginUpdater/src/Utils.cpp
@@ -389,6 +389,7 @@ bool PrepareEscalation()
if (ext != nullptr)
*ext = '\0';
wcscat(szPath, L".test");
+
HANDLE hFile = CreateFile(szPath, GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile != INVALID_HANDLE_VALUE) {
// we are admins or UAC is disable, cool
@@ -396,46 +397,45 @@ bool PrepareEscalation()
DeleteFile(szPath);
return true;
}
- else if (IsRunAsAdmin()) {
- // Check the current process's "run as administrator" status.
+
+ // Check the current process's "run as administrator" status.
+ if (IsRunAsAdmin())
return true;
+
+ // Elevate the process. Create a pipe for a stub first
+ wchar_t tszPipeName[MAX_PATH];
+ mir_snwprintf(tszPipeName, L"\\\\.\\pipe\\Miranda_Pu_%d", GetCurrentProcessId());
+ hPipe = CreateNamedPipe(tszPipeName, PIPE_ACCESS_DUPLEX, PIPE_READMODE_BYTE | PIPE_WAIT, 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, nullptr);
+ if (hPipe == INVALID_HANDLE_VALUE) {
+ hPipe = nullptr;
}
else {
- // Elevate the process. Create a pipe for a stub first
- wchar_t tszPipeName[MAX_PATH];
- mir_snwprintf(tszPipeName, L"\\\\.\\pipe\\Miranda_Pu_%d", GetCurrentProcessId());
- hPipe = CreateNamedPipe(tszPipeName, PIPE_ACCESS_DUPLEX, PIPE_READMODE_BYTE | PIPE_WAIT, 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, nullptr);
- if (hPipe == INVALID_HANDLE_VALUE) {
- hPipe = nullptr;
+ wchar_t cmdLine[100], *p;
+ GetModuleFileName(nullptr, szPath, ARRAYSIZE(szPath));
+ if ((p = wcsrchr(szPath, '\\')) != nullptr)
+ wcscpy(p + 1, L"pu_stub.exe");
+ mir_snwprintf(cmdLine, L"%d", GetCurrentProcessId());
+
+ // Launch a stub
+ SHELLEXECUTEINFO sei = { sizeof(sei) };
+ sei.lpVerb = L"runas";
+ sei.lpFile = szPath;
+ sei.lpParameters = cmdLine;
+ sei.hwnd = nullptr;
+ sei.nShow = SW_NORMAL;
+ if (ShellExecuteEx(&sei)) {
+ if (hPipe != nullptr)
+ ConnectNamedPipe(hPipe, nullptr);
+ return true;
}
- else {
- wchar_t cmdLine[100], *p;
- GetModuleFileName(nullptr, szPath, ARRAYSIZE(szPath));
- if ((p = wcsrchr(szPath, '\\')) != nullptr)
- wcscpy(p + 1, L"pu_stub.exe");
- mir_snwprintf(cmdLine, L"%d", GetCurrentProcessId());
-
- // Launch a stub
- SHELLEXECUTEINFO sei = { sizeof(sei) };
- sei.lpVerb = L"runas";
- sei.lpFile = szPath;
- sei.lpParameters = cmdLine;
- sei.hwnd = nullptr;
- sei.nShow = SW_NORMAL;
- if (ShellExecuteEx(&sei)) {
- if (hPipe != nullptr)
- ConnectNamedPipe(hPipe, nullptr);
- return true;
- }
- DWORD dwError = GetLastError();
- if (dwError == ERROR_CANCELLED) {
- // The user refused to allow privileges elevation.
- // Do nothing ...
- }
+ DWORD dwError = GetLastError();
+ if (dwError == ERROR_CANCELLED) {
+ // The user refused to allow privileges elevation.
+ // Do nothing ...
}
- return false;
}
+ return false;
}
/////////////////////////////////////////////////////////////////////////////////////////