forked from Squirrel/Squirrel.Windows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateRunner.cpp
More file actions
100 lines (78 loc) · 2.38 KB
/
Copy pathUpdateRunner.cpp
File metadata and controls
100 lines (78 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "stdafx.h"
#include "unzip.h"
#include "Resource.h"
#include "UpdateRunner.h"
void CUpdateRunner::DisplayErrorMessage(CString& errorMessage)
{
CTaskDialog dlg;
// TODO: Something about contacting support?
dlg.SetCommonButtons(TDCBF_OK_BUTTON);
dlg.SetMainInstructionText(L"Installation has failed");
dlg.SetContentText(errorMessage);
dlg.SetMainIcon(TD_ERROR_ICON);
dlg.DoModal();
}
int CUpdateRunner::ExtractUpdaterAndRun(wchar_t* lpCommandLine)
{
PROCESS_INFORMATION pi = { 0 };
STARTUPINFO si = { 0 };
CResource zipResource;
wchar_t targetDir[MAX_PATH];
ExpandEnvironmentStrings(L"%LocalAppData%\\SquirrelTemp", targetDir, _countof(targetDir));
if (!CreateDirectory(targetDir, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) {
goto failedExtract;
}
if (!zipResource.Load(L"DATA", IDR_UPDATE_ZIP)) {
goto failedExtract;
}
DWORD dwSize = zipResource.GetSize();
if (dwSize < 0x100) {
goto failedExtract;
}
BYTE* pData = (BYTE*)zipResource.Lock();
HZIP zipFile = OpenZip(pData, dwSize, NULL);
SetUnzipBaseDir(zipFile, targetDir);
// NB: This library is kind of a disaster
ZRESULT zr;
int index = 0;
do {
ZIPENTRY zentry;
zr = GetZipItem(zipFile, index, &zentry);
if (zr != ZR_OK && zr != ZR_MORE) {
break;
}
if (UnzipItem(zipFile, index, zentry.name) != ZR_OK) break;
index++;
} while (zr == ZR_MORE || zr == ZR_OK);
CloseZip(zipFile);
zipResource.Release();
// nfi if the zip extract actually worked, check for Update.exe
wchar_t updateExePath[MAX_PATH];
swprintf_s(updateExePath, L"%s\\%s", targetDir, L"Update.exe");
if (GetFileAttributes(updateExePath) == INVALID_FILE_ATTRIBUTES) {
goto failedExtract;
}
// Run Update.exe
si.cb = sizeof(STARTUPINFO);
si.wShowWindow = SW_SHOW;
si.dwFlags = STARTF_USESHOWWINDOW;
if (!lpCommandLine || wcsnlen_s(lpCommandLine, MAX_PATH) < 1) {
lpCommandLine = L"--install .";
}
wchar_t cmd[MAX_PATH];
swprintf_s(cmd, L"%s %s", updateExePath, lpCommandLine);
if (!CreateProcess(NULL, cmd, NULL, NULL, false, 0, NULL, targetDir, &si, &pi)) {
goto failedExtract;
}
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD dwExitCode;
if (!GetExitCodeProcess(pi.hProcess, &dwExitCode)) {
dwExitCode = (DWORD)-1;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return (int) dwExitCode;
failedExtract:
DisplayErrorMessage(CString(L"Failed to extract installer"));
return (int) dwExitCode;
}