Debian Codegraph @

bypy/windows/portable-installer.cpp

raw
1 #ifndef UNICODE
2 #define UNICODE
3 #endif
4
5 #ifndef _UNICODE
6 #define _UNICODE
7 #endif
8
9 #include <Windows.h>
10 #include <Shlobj.h>
11 #include <Shlwapi.h>
12 #include <Shellapi.h>
13 #include <Psapi.h>
14 #include <wchar.h>
15 #include <stdio.h>
16 #include <io.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <VersionHelpers.h>
20
21 #include <easylzma/decompress.h>
22 #include "XUnzip.h"
23
24 #define BUFSIZE 4096
25 #define arraysz(x) (sizeof(x) / sizeof(x[0]))
26
27 static const LPCWSTR calibre_processes[] = {L"calibre.exe", L"calibre-parallel.exe", L"ebook-viewer.exe", L"ebook-edit.exe"};
28
29 // Error handling {{{
30
31 static void
32 show_error(LPCWSTR msg) {
33 MessageBeep(MB_ICONERROR);
34 MessageBox(NULL, msg, L"Error", MB_OK | MB_ICONERROR);
35 }
36
37 static void
38 show_detailed_error(LPCWSTR preamble, LPCWSTR msg, int code) {
39 LPWSTR buf;
40 buf = (LPWSTR)LocalAlloc(LMEM_ZEROINIT, sizeof(WCHAR) * (wcslen(msg) + wcslen(preamble) + 80));
41
42 _snwprintf_s(buf, LocalSize(buf) / sizeof(WCHAR), _TRUNCATE, L"%s\r\n %s (Error Code: %d)\r\n", preamble, msg, code);
43
44 show_error(buf);
45 LocalFree(buf);
46 }
47
48 static void
49 show_zip_error(LPCWSTR preamble, LPCWSTR msg, ZRESULT code) {
50 LPWSTR buf;
51 char msgbuf[1024] = {0};
52
53 FormatZipMessage(code, msgbuf, 1024);
54
55 buf = (LPWSTR)LocalAlloc(LMEM_ZEROINIT, sizeof(WCHAR) * (wcslen(preamble) + wcslen(msg) + 1100));
56
57 _snwprintf_s(buf, LocalSize(buf) / sizeof(WCHAR), _TRUNCATE, L"%s\r\n %s (Error: %S)\r\n", preamble, msg, msgbuf);
58
59 show_error(buf);
60 LocalFree(buf);
61 }
62
63 static void
64 show_last_error(LPCWSTR preamble) {
65 WCHAR *msg = NULL;
66 DWORD dw = GetLastError();
67
68 FormatMessage(
69 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
70 NULL,
71 dw,
72 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
73 (LPWSTR)&msg,
74 0,
75 NULL);
76
77 show_detailed_error(preamble, msg, (int)dw);
78 }
79
80 // }}}
81
82 // Load, decompress and extract data {{{
83
84 static BOOL
85 load_data(LPVOID *data, DWORD *sz) {
86 HRSRC rsrc;
87 HGLOBAL h;
88
89 rsrc = FindResourceW(NULL, L"extra", L"extra");
90 if (rsrc == NULL) {
91 show_last_error(L"Failed to find portable data in exe");
92 return false;
93 }
94
95 h = LoadResource(NULL, rsrc);
96 if (h == NULL) {
97 show_last_error(L"Failed to load portable data from exe");
98 return false;
99 }
100
101 *data = LockResource(h);
102 if (*data == NULL) {
103 show_last_error(L"Failed to lock portable data in exe");
104 return false;
105 }
106
107 *sz = SizeofResource(NULL, rsrc);
108 if (sz == 0) {
109 show_last_error(L"Failed to get size of portable data in exe");
110 return false;
111 }
112
113 return true;
114 }
115
116 static BOOL
117 unzip(HZIP zipf, int nitems, IProgressDialog *pd) {
118 int i = 0;
119 ZRESULT res;
120 ZIPENTRYW ze;
121
122 for (i = 0; i < nitems; i++) {
123 res = GetZipItem(zipf, i, &ze);
124 if (res != ZR_OK) {
125 show_zip_error(L"Failed to get zip item", L"", res);
126 return false;
127 }
128
129 res = UnzipItem(zipf, i, ze.name, 0, ZIP_FILENAME);
130 if (res != ZR_OK) {
131 CloseZip(zipf);
132 show_zip_error(L"Failed to extract zip item (is your disk full?):", ze.name, res);
133 return false;
134 }
135
136 pd->SetLine(2, ze.name, true, NULL);
137 pd->SetProgress(i, nitems);
138 }
139
140 CloseZip(zipf);
141
142 return true;
143 }
144
145 static HANDLE
146 temp_file(LPWSTR name) {
147 UINT res;
148 HANDLE h;
149
150 res = GetTempFileNameW(L".", L"portable_data", 0, name);
151
152 if (res == 0) {
153 show_last_error(L"Failed to create temporary file to decompress portable data");
154 return INVALID_HANDLE_VALUE;
155 }
156
157 h = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
158 if (h == INVALID_HANDLE_VALUE) { show_last_error(L"Failed to open temp file to decompress portable data"); }
159 return h;
160 }
161
162 struct DataStream {
163 const unsigned char *in_data;
164 size_t in_len;
165
166 HANDLE out;
167 IProgressDialog *pd;
168 };
169
170 static int
171 input_callback(void *ctx, void *buf, size_t *size) {
172 size_t rd = 0;
173 struct DataStream *ds = (struct DataStream *)ctx;
174
175 rd = (ds->in_len < *size) ? ds->in_len : *size;
176
177 if (rd > 0) {
178 memcpy(buf, (void *)ds->in_data, rd);
179 ds->in_data += rd;
180 ds->in_len -= rd;
181 }
182
183 *size = rd;
184
185 return 0;
186 }
187
188 static int output_error_shown = 0;
189
190 static size_t
191 output_callback(void *ctx, const void *buf, size_t size) {
192 struct DataStream *ds = (struct DataStream *)ctx;
193 DWORD written = 0;
194
195 if (size > 0) {
196 if (!WriteFile(ds->out, buf, (DWORD)size, &written, NULL)) {
197 show_last_error(L"Failed to write uncompressed data to temp file");
198 output_error_shown = 1;
199 return 0;
200 }
201 written = SetFilePointer(ds->out, 0, NULL, FILE_CURRENT);
202 ds->pd->SetProgress(written, UNCOMPRESSED_SIZE);
203 }
204
205 return size;
206 }
207
208 static BOOL
209 decompress(LPVOID src, DWORD src_sz, HANDLE out, IProgressDialog *pd) {
210 elzma_decompress_handle h;
211 struct DataStream ds;
212 int rc;
213
214 h = elzma_decompress_alloc();
215
216 if (h == NULL) {
217 show_error(L"Out of memory");
218 return false;
219 }
220
221 ds.in_data = (unsigned char *)src;
222 ds.in_len = src_sz;
223 ds.out = out;
224 ds.pd = pd;
225
226 rc = elzma_decompress_run(h, input_callback, (void *)&ds, output_callback, (void *)&ds, ELZMA_lzip);
227
228 if (rc != ELZMA_E_OK) {
229 if (!output_error_shown) show_detailed_error(L"Failed to decompress portable data", L"", rc);
230 elzma_decompress_free(&h);
231 return false;
232 }
233
234 elzma_decompress_free(&h);
235
236 return true;
237 }
238
239 static BOOL
240 extract(LPVOID cdata, DWORD csz) {
241 HANDLE h;
242 WCHAR tempnam[MAX_PATH + 1] = {0};
243 BOOL ret = true;
244 HZIP zipf;
245 ZIPENTRYW ze;
246 ZRESULT res;
247 int nitems;
248 HRESULT hr;
249 IProgressDialog *pd = NULL;
250
251 hr = CoCreateInstance(CLSID_ProgressDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pd));
252
253 if (FAILED(hr)) {
254 show_error(L"Failed to create progress dialog");
255 return false;
256 }
257 pd->SetTitle(L"Extracting Calibre Portable");
258 pd->SetLine(1, L"Decompressing data...", true, NULL);
259
260 h = temp_file(tempnam);
261 if (h == INVALID_HANDLE_VALUE) return false;
262
263 pd->StartProgressDialog(NULL, NULL, PROGDLG_NORMAL | PROGDLG_AUTOTIME | PROGDLG_NOCANCEL, NULL);
264 if (!decompress(cdata, csz, h, pd)) {
265 ret = false;
266 goto end;
267 }
268 SetFilePointer(h, 0, NULL, FILE_BEGIN);
269 zipf = OpenZip(h, 0, ZIP_HANDLE);
270 if (zipf == 0) {
271 show_last_error(L"Failed to open zipped portable data");
272 ret = false;
273 goto end;
274 }
275
276 res = GetZipItem(zipf, -1, &ze);
277 if (res != ZR_OK) {
278 show_zip_error(L"Failed to get count of items in portable data", L"", res);
279 ret = false;
280 goto end;
281 }
282 nitems = ze.index;
283
284 pd->SetLine(1, L"Copying files...", true, NULL);
285 if (!unzip(zipf, nitems, pd)) {
286 ret = false;
287 goto end;
288 }
289 end:
290 pd->StopProgressDialog();
291 pd->Release();
292 CloseHandle(h);
293 DeleteFile(tempnam);
294 return ret;
295 }
296
297 // }}}
298
299 // Find calibre portable folder and install/upgrade into it {{{
300
301 static BOOL
302 directory_exists(LPCWSTR path) {
303 if (_waccess_s(path, 0) == 0) {
304 struct _stat status;
305 _wstat(path, &status);
306 return (status.st_mode & S_IFDIR) != 0;
307 }
308
309 return FALSE;
310 }
311
312 static BOOL
313 file_exists(LPCWSTR path) {
314 if (_waccess_s(path, 0) == 0) {
315 struct _stat status;
316 _wstat(path, &status);
317 return (status.st_mode & S_IFREG) != 0;
318 }
319
320 return FALSE;
321 }
322
323 static LPWSTR
324 get_directory_from_user() {
325 WCHAR name[MAX_PATH + 1] = {0};
326 LPWSTR path = NULL;
327 PIDLIST_ABSOLUTE ret;
328
329 path = (LPWSTR)calloc(2 * MAX_PATH, sizeof(WCHAR));
330 if (path == NULL) {
331 show_error(L"Out of memory");
332 return NULL;
333 }
334
335 int image = 0;
336 BROWSEINFO bi = {
337 NULL,
338 NULL,
339 name,
340 L"Select the folder where you want to install or update Calibre Portable",
341 BIF_RETURNONLYFSDIRS | BIF_DONTGOBELOWDOMAIN | BIF_USENEWUI,
342 NULL,
343 NULL,
344 image};
345
346 ret = SHBrowseForFolder(&bi);
347 if (ret == NULL) { return NULL; }
348
349 if (!SHGetPathFromIDList(ret, path)) {
350 show_detailed_error(L"The selected folder is not valid: ", name, 0);
351 return NULL;
352 }
353
354 return path;
355 }
356
357 static bool
358 is_dots(LPCWSTR name) {
359 return wcscmp(name, L".") == 0 || wcscmp(name, L"..") == 0;
360 }
361
362 static bool
363 rmtree(LPCWSTR path) {
364 SHFILEOPSTRUCTW op;
365 WCHAR buf[4 * MAX_PATH + 2] = {0};
366
367 if (GetFullPathName(path, 4 * MAX_PATH, buf, NULL) == 0) return false;
368
369 op.hwnd = NULL;
370 op.wFunc = FO_DELETE;
371 op.pFrom = buf;
372 op.pTo = NULL;
373 op.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMMKDIR;
374 op.fAnyOperationsAborted = false;
375 op.hNameMappings = NULL;
376 op.lpszProgressTitle = NULL;
377
378 return SHFileOperationW(&op) == 0;
379 }
380
381 static BOOL
382 find_portable_dir(LPCWSTR base, LPWSTR *result, BOOL *existing) {
383 WCHAR buf[4 * MAX_PATH] = {0};
384
385 _snwprintf_s(buf, 4 * MAX_PATH, _TRUNCATE, L"%s\\calibre-portable.exe", base);
386 *existing = true;
387
388 if (file_exists(buf)) {
389 *result = _wcsdup(base);
390 if (*result == NULL) {
391 show_error(L"Out of memory");
392 return false;
393 }
394 return true;
395 }
396
397 WIN32_FIND_DATA fdFile;
398 HANDLE hFind = NULL;
399 _snwprintf_s(buf, 4 * MAX_PATH, _TRUNCATE, L"%s\\*", base);
400
401 if ((hFind = FindFirstFileEx(buf, FindExInfoStandard, &fdFile, FindExSearchLimitToDirectories, NULL, 0)) != INVALID_HANDLE_VALUE) {
402 do {
403 if (is_dots(fdFile.cFileName)) continue;
404
405 if (fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
406 _snwprintf_s(buf, 4 * MAX_PATH, _TRUNCATE, L"%s\\%s\\calibre-portable.exe", base, fdFile.cFileName);
407 if (file_exists(buf)) {
408 *result = _wcsdup(buf);
409 if (*result == NULL) {
410 show_error(L"Out of memory");
411 return false;
412 }
413 PathRemoveFileSpec(*result);
414 FindClose(hFind);
415 return true;
416 }
417 }
418 } while (FindNextFile(hFind, &fdFile));
419 FindClose(hFind);
420 }
421
422 *existing = false;
423 _snwprintf_s(buf, 4 * MAX_PATH, _TRUNCATE, L"%s\\Calibre Portable", base);
424 if (!CreateDirectory(buf, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) {
425 show_last_error(L"Failed to create Calibre Portable folder");
426 return false;
427 }
428 *result = _wcsdup(buf);
429 if (*result == NULL) {
430 show_error(L"Out of memory");
431 return false;
432 }
433
434 return true;
435 }
436
437 static LPWSTR
438 make_unpack_dir() {
439 WCHAR buf[4 * MAX_PATH] = {0};
440 LPWSTR ans = NULL;
441
442 if (directory_exists(L"_unpack_calibre_portable")) rmtree(L"_unpack_calibre_portable");
443
444 if (!CreateDirectory(L"_unpack_calibre_portable", NULL) && GetLastError() != ERROR_ALREADY_EXISTS) {
445 show_last_error(L"Failed to create temporary folder to unpack into");
446 return ans;
447 }
448
449 if (!GetFullPathName(L"_unpack_calibre_portable", 4 * MAX_PATH, buf, NULL)) {
450 show_last_error(L"Failed to resolve path");
451 return NULL;
452 }
453
454 ans = _wcsdup(buf);
455 if (ans == NULL) show_error(L"Out of memory");
456 return ans;
457 }
458
459 static BOOL
460 move_program() {
461 if (MoveFileEx(L"Calibre Portable\\calibre-portable.exe", L"..\\calibre-portable.exe", MOVEFILE_REPLACE_EXISTING) == 0) {
462 show_last_error(L"Failed to move calibre-portable.exe, make sure calibre is not running");
463 return false;
464 }
465 if (MoveFileEx(L"Calibre Portable\\ebook-viewer-portable.exe", L"..\\ebook-viewer-portable.exe", MOVEFILE_REPLACE_EXISTING) == 0) {
466 show_last_error(L"Failed to move ebook-viewer-portable.exe, make sure calibre is not running");
467 return false;
468 }
469 if (MoveFileEx(L"Calibre Portable\\ebook-edit-portable.exe", L"..\\ebook-edit-portable.exe", MOVEFILE_REPLACE_EXISTING) == 0) {
470 show_last_error(L"Failed to move ebook-edit-portable.exe, make sure calibre is not running");
471 return false;
472 }
473
474 if (directory_exists(L"..\\Calibre")) {
475 if (!rmtree(L"..\\Calibre")) {
476 show_error(L"Failed to delete the Calibre program folder. Make sure calibre is not running.");
477 return false;
478 }
479 }
480
481 if (MoveFileEx(L"Calibre Portable\\Calibre", L"..\\Calibre", 0) == 0) {
482 Sleep(4000); // Sleep and try again
483 if (MoveFileEx(L"Calibre Portable\\Calibre", L"..\\Calibre", 0) == 0) {
484 show_last_error(
485 L"Failed to move calibre program folder. This is usually caused by an antivirus program or a file sync program like DropBox. Turn them off "
486 L"temporarily and try again. Underlying error: ");
487 return false;
488 }
489 }
490
491 if (!directory_exists(L"..\\Calibre Library")) { MoveFileEx(L"Calibre Portable\\Calibre Library", L"..\\Calibre Library", 0); }
492
493 if (!directory_exists(L"..\\Calibre Settings")) { MoveFileEx(L"Calibre Portable\\Calibre Settings", L"..\\Calibre Settings", 0); }
494
495 return true;
496 }
497 // }}}
498
499 static BOOL
500 find_running_calibre_processes(LPWSTR *running_processes, int *count) {
501 DWORD processes[4096], needed, num;
502 unsigned int i;
503 WCHAR name[4 * MAX_PATH] = L"<unknown>";
504 HANDLE h;
505 DWORD len;
506 LPWSTR fname = NULL;
507 BOOL found[arraysz(calibre_processes)] = {FALSE};
508 *count = 0;
509
510 if (!EnumProcesses(processes, sizeof(processes), &needed)) return true;
511 num = needed / sizeof(DWORD);
512
513 for (i = 0; i < num; i++) {
514 if (processes[i] == 0) continue;
515 h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, processes[i]);
516 if (h != NULL) {
517 len = GetProcessImageFileNameW(h, name, 4 * MAX_PATH);
518 CloseHandle(h);
519 if (len != 0) {
520 name[len] = 0;
521 fname = PathFindFileName(name);
522 for (int j = 0; j < arraysz(calibre_processes); j++) {
523 if (!found[j] && wcscmp(fname, calibre_processes[j]) == 0) {
524 found[j] = TRUE;
525 running_processes[*count] = _wcsdup(calibre_processes[j]);
526 (*count)++;
527 break;
528 }
529 }
530 }
531 }
532 }
533
534 return *count > 0;
535 }
536
537 static BOOL
538 ensure_not_running() {
539 LPWSTR running_processes[arraysz(calibre_processes)] = {NULL};
540 int count = 0;
541 WCHAR msg[4 * MAX_PATH] = {0};
542 WCHAR *msg_ptr;
543 size_t remaining;
544 int result;
545
546 while (TRUE) {
547 // Reset buffer pointers for each iteration
548 msg_ptr = msg;
549 remaining = arraysz(msg);
550 wmemset(msg, 0, remaining);
551
552 // Check for running processes
553 if (!find_running_calibre_processes(running_processes, &count)) return true;
554
555 // Build the message with list of running processes
556 int written = _snwprintf_s(msg_ptr, remaining, _TRUNCATE, L"The following calibre processes are currently running:\r\n\r\n");
557 if (written > 0) {
558 msg_ptr += written;
559 remaining -= written;
560 }
561
562 for (int i = 0; i < count; i++) {
563 written = _snwprintf_s(msg_ptr, remaining, _TRUNCATE, L" - %s\r\n", running_processes[i]);
564 if (written > 0) {
565 msg_ptr += written;
566 remaining -= written;
567 }
568 }
569
570 written = _snwprintf_s(
571 msg_ptr,
572 remaining,
573 _TRUNCATE,
574 L"\r\nPlease quit calibre before installing or upgrading Calibre Portable.\r\n\r\n"
575 L"Click 'Retry' after quitting calibre, or 'Abort' to cancel installation.");
576
577 // Show dialog with Retry and Abort buttons
578 result = MessageBox(NULL, msg, L"calibre is running", MB_RETRYCANCEL | MB_ICONEXCLAMATION | MB_TOPMOST);
579
580 // Free allocated memory
581 for (int i = 0; i < count; i++) {
582 if (running_processes[i] != NULL) {
583 free(running_processes[i]);
584 running_processes[i] = NULL;
585 }
586 }
587 count = 0;
588
589 if (result == IDCANCEL) return false; // User chose to abort
590 // If IDRETRY, loop continues to check again
591 }
592
593 return true;
594 }
595
596 static void
597 launch_calibre() {
598 STARTUPINFO si;
599 PROCESS_INFORMATION pi;
600
601 ZeroMemory(&si, sizeof(si));
602 si.cb = sizeof(si);
603 ZeroMemory(&pi, sizeof(pi));
604
605
606 if (CreateProcess(_wcsdup(L"calibre-portable.exe"), NULL, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT | CREATE_NEW_PROCESS_GROUP, NULL, NULL, &si, &pi) ==
607 0) {
608 show_last_error(L"Failed to launch calibre portable");
609 }
610
611 // Close process and thread handles.
612 CloseHandle(pi.hProcess);
613 CloseHandle(pi.hThread);
614 }
615
616 void
617 makedirs(LPWSTR path) {
618 WCHAR *p = path;
619 while (*p) {
620 if ((*p == L'\\' || *p == L'/') && p != path && *(p - 1) != L':') {
621 *p = 0;
622 CreateDirectory(path, NULL);
623 *p = L'\\';
624 }
625 p++;
626 }
627 CreateDirectory(path, NULL);
628 }
629
630 int WINAPI
631 wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
632 (void)hPrevInstance;
633 (void)pCmdLine;
634 (void)nCmdShow;
635 (void)hInstance;
636 LPVOID cdata = NULL;
637 DWORD csz = 0;
638 int ret = 1, argc;
639 HRESULT hr;
640 LPWSTR tgt = NULL, dest = NULL, *argv, unpack_dir = NULL;
641 BOOL existing = false, launch = false, automated = false;
642 WCHAR buf[4 * MAX_PATH] = {0}, mb_msg[4 * MAX_PATH] = {0}, fdest[4 * MAX_PATH] = {0};
643
644 if (!load_data(&cdata, &csz)) return ret;
645 if (!IsWindows10OrGreater()) {
646 show_error(L"Your version of Windows is too old. calibre requires Windows 10 or greater.");
647 return ret;
648 }
649
650 hr = CoInitialize(NULL);
651 if (FAILED(hr)) {
652 show_error(L"Failed to initialize COM");
653 return ret;
654 }
655
656 // Get the target folder for installation
657 argv = CommandLineToArgvW(GetCommandLine(), &argc);
658 if (argv == NULL) {
659 show_last_error(L"Failed to get command line");
660 return ret;
661 }
662 if (argc > 1) {
663 tgt = argv[1];
664 automated = true;
665 if (!directory_exists(tgt)) {
666 if (GetFullPathName(tgt, MAX_PATH * 4, fdest, NULL) == 0) {
667 show_last_error(L"Failed to resolve target folder");
668 goto end;
669 }
670 makedirs(fdest);
671 }
672
673 } else {
674 tgt = get_directory_from_user();
675 if (tgt == NULL) goto end;
676 }
677
678 if (!directory_exists(tgt)) {
679 show_detailed_error(L"The specified folder does not exist: ", tgt, 1);
680 goto end;
681 }
682
683 // Ensure the path to Calibre Portable is not too long
684 do {
685 if (!find_portable_dir(tgt, &dest, &existing)) goto end;
686
687 if (GetFullPathName(dest, MAX_PATH * 4, fdest, NULL) == 0) {
688 show_last_error(L"Failed to resolve target folder");
689 goto end;
690 }
691 free(dest);
692 dest = NULL;
693
694 if (wcslen(fdest) > 58) {
695 _snwprintf_s(buf, 4 * MAX_PATH, _TRUNCATE, L"Path to Calibre Portable (%s) too long. It must be less than 59 characters.", fdest);
696 if (!existing) RemoveDirectory(fdest);
697 show_error(buf);
698 tgt = get_directory_from_user();
699 if (tgt == NULL) goto end;
700 }
701 } while (wcslen(fdest) > 58);
702
703 // Confirm the user wants to upgrade
704 if (existing && !automated) {
705 _snwprintf_s(mb_msg, 4 * MAX_PATH, _TRUNCATE, L"An existing install of Calibre Portable was found at %s. Do you want to upgrade it?", fdest);
706 if (MessageBox(NULL, mb_msg, L"Upgrade Calibre Portable?", MB_ICONEXCLAMATION | MB_YESNO | MB_TOPMOST) != IDYES) goto end;
707 }
708
709 if (existing) {
710 if (!ensure_not_running()) goto end;
711 }
712
713 // Make a temp dir to unpack into
714 if (!SetCurrentDirectoryW(fdest)) {
715 show_detailed_error(L"Failed to change to unzip folder: ", fdest, 0);
716 goto end;
717 }
718
719 if ((unpack_dir = make_unpack_dir()) == NULL) goto end;
720 if (!SetCurrentDirectoryW(unpack_dir)) {
721 show_detailed_error(L"Failed to change to unpack folder: ", fdest, 0);
722 goto end;
723 }
724
725 // Extract files
726 if (!extract(cdata, csz)) goto end;
727
728 // Move files from temp dir to the install dir
729 if (!move_program()) goto end;
730
731 ret = 0;
732 if (!automated) {
733 _snwprintf_s(mb_msg, 4 * MAX_PATH, _TRUNCATE, L"Calibre Portable successfully installed to %s. Launch calibre?", fdest);
734 launch = MessageBox(NULL, mb_msg, L"Success", MB_ICONINFORMATION | MB_YESNO | MB_TOPMOST) == IDYES;
735 }
736
737 end:
738 if (unpack_dir != NULL) {
739 SetCurrentDirectoryW(L"..");
740 rmtree(unpack_dir);
741 free(unpack_dir);
742 }
743 CoUninitialize();
744 if (launch) launch_calibre();
745 return ret;
746 }