summaryrefslogtreecommitdiff
path: root/plugins/PluginUpdater/src/Utils.cpp
blob: 8634b156e1f12a7383cc8073f20002b21c75db65 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
Copyright (C) 2010 Mataes

This is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this file; see the file license.txt.  If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/

#include "stdafx.h"

HNETLIBUSER hNetlibUser = nullptr;
HANDLE hPipe = nullptr;

/////////////////////////////////////////////////////////////////////////////////////

IconItem iconList[] =
{
	{ LPGEN("Check for updates"),"check_update", IDI_MENU },
	{ LPGEN("Plugin info"), "info", IDI_INFO },
	{ LPGEN("Component list"),"plg_list", IDI_PLGLIST }
};

void InitIcoLib()
{
	g_plugin.registerIcon(MODULEA, iconList);
}

void InitNetlib()
{
	NETLIBUSER nlu = {};
	nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_HTTPCONNS | NUF_UNICODE;
	nlu.szDescriptiveName.w = TranslateT("Plugin Updater HTTP connections");
	nlu.szSettingsModule = MODULENAME;
	hNetlibUser = Netlib_RegisterUser(&nlu);
}

void UnloadNetlib()
{
	Netlib_CloseHandle(hNetlibUser);
	hNetlibUser = nullptr;
}

int CompareHashes(const ServListEntry *p1, const ServListEntry *p2)
{
	return _wcsicmp(p1->m_name, p2->m_name);
}

bool ParseHashes(const wchar_t *pwszUrl, ptrW &baseUrl, SERVLIST &arHashes)
{
	REPLACEVARSARRAY vars[2];
	vars[0].key.w = L"platform";
	#ifdef _WIN64
		vars[0].value.w = L"64";
	#else
		vars[0].value.w = L"32";
	#endif
	vars[1].key.w = vars[1].value.w = nullptr;

	baseUrl = Utils_ReplaceVarsW(pwszUrl, 0, vars);

	// Download version info
	FILEURL pFileUrl;
	mir_snwprintf(pFileUrl.wszDownloadURL, L"%s/hashes.zip", baseUrl.get());
	mir_snwprintf(pFileUrl.wszDiskPath, L"%s\\hashes.zip", g_wszTempPath);
	pFileUrl.CRCsum = 0;

	HNETLIBCONN nlc = nullptr;
	bool ret = DownloadFile(&pFileUrl, nlc);
	Netlib_CloseHandle(nlc);

	if (!ret) {
		Netlib_LogfW(hNetlibUser, L"Downloading list of available updates from %s failed", baseUrl.get());
		ShowPopup(TranslateT("Plugin Updater"), TranslateT("An error occurred while checking for new updates."), POPUP_TYPE_ERROR);
		Skin_PlaySound("updatefailed");
		return false;
	}

	if (unzip(pFileUrl.wszDiskPath, g_wszTempPath, nullptr, true)) {
		Netlib_LogfW(hNetlibUser, L"Unzipping list of available updates from %s failed", baseUrl.get());
		ShowPopup(TranslateT("Plugin Updater"), TranslateT("An error occurred while checking for new updates."), POPUP_TYPE_ERROR);
		Skin_PlaySound("updatefailed");
		return false;
	}

	DeleteFile(pFileUrl.wszDiskPath);

	TFileName wszTmpIni;
	mir_snwprintf(wszTmpIni, L"%s\\hashes.txt", g_wszTempPath);
	FILE *fp = _wfopen(wszTmpIni, L"r");
	if (!fp) {
		Netlib_LogfW(hNetlibUser, L"Opening %s failed", g_wszTempPath);
		ShowPopup(TranslateT("Plugin Updater"), TranslateT("An error occurred while checking for new updates."), POPUP_TYPE_ERROR);
		return false;
	}

	bool bDoNotSwitchToStable = false;
	char str[200];
	while (fgets(str, _countof(str), fp) != nullptr) {
		rtrim(str);
		// Do not allow the user to switch back to stable
		if (!strcmp(str, "DoNotSwitchToStable")) {
			bDoNotSwitchToStable = true;
		}
		else if (str[0] != ';') { // ';' marks a comment
			Netlib_Logf(hNetlibUser, "Update: %s", str);
			char *p = strchr(str, ' ');
			if (p != nullptr) {
				*p++ = 0;
				_strlwr(p);

				int dwCrc32;
				char *p1 = strchr(p, ' ');
				if (p1 == nullptr)
					dwCrc32 = 0;
				else {
					*p1++ = 0;
					sscanf(p1, "%08x", &dwCrc32);
				}
				arHashes.insert(new ServListEntry(str, p, dwCrc32));
			}
		}
	}
	fclose(fp);
	DeleteFileW(wszTmpIni);

	if (bDoNotSwitchToStable) {
		g_plugin.setByte(DB_SETTING_DONT_SWITCH_TO_STABLE, 1);
		// Reset setting if needed
		int UpdateMode = g_plugin.getByte(DB_SETTING_UPDATE_MODE, UPDATE_MODE_STABLE);
		if (UpdateMode == UPDATE_MODE_STABLE)
			g_plugin.setByte(DB_SETTING_UPDATE_MODE, UPDATE_MODE_TRUNK);
	}
	else g_plugin.setByte(DB_SETTING_DONT_SWITCH_TO_STABLE, 0);

	return true;
}


bool DownloadFile(FILEURL *pFileURL, HNETLIBCONN &nlc)
{
	char szMirVer[100];
	Miranda_GetVersionText(szMirVer, _countof(szMirVer));
	if (auto *p = strchr(szMirVer, '('))
		*p = 0;
	rtrim(szMirVer);

	char szOsVer[100];
	OS_GetShortString(szOsVer, _countof(szOsVer));

	CMStringA szUserAgent("Miranda NG/");
	szUserAgent.Append(szMirVer);
	szUserAgent.AppendFormat(" (%s", szOsVer);
	#ifdef _WIN64
		szUserAgent.Append("; Win64; x64");
	#endif
	szUserAgent.Append(")");

	NETLIBHTTPHEADER headers[4] = {
		{ "User-Agent", szUserAgent.GetBuffer() },
		{ "Connection", "close" },
		{ "Cache-Control", "no-cache" },
		{ "Pragma", "no-cache" }
	};

	ptrA szUrl(mir_u2a(pFileURL->wszDownloadURL));

	NETLIBHTTPREQUEST nlhr = {};
	nlhr.cbSize = sizeof(nlhr);
	nlhr.flags = NLHRF_DUMPASTEXT | NLHRF_HTTP11 | NLHRF_PERSISTENT;
	nlhr.requestType = REQUEST_GET;
	nlhr.nlc = nlc;
	nlhr.szUrl = szUrl;
	nlhr.headersCount = _countof(headers);
	nlhr.headers = headers;

	for (int i = 0; i < MAX_RETRIES; i++) {
		Netlib_LogfW(hNetlibUser, L"Downloading file %s to %s (attempt %d)", pFileURL->wszDownloadURL, pFileURL->wszDiskPath, i + 1);
		NLHR_PTR pReply(Netlib_HttpTransaction(hNetlibUser, &nlhr));
		if (pReply) {
			nlc = pReply->nlc;
			if ((200 == pReply->resultCode) && (pReply->dataLength > 0)) {
				// Check CRC sum
				if (pFileURL->CRCsum) {
					int crc = crc32(0, (unsigned char *)pReply->pData, pReply->dataLength);
					if (crc != pFileURL->CRCsum) {
						// crc check failed, try again
						Netlib_LogfW(hNetlibUser, L"crc check failed for file %s", pFileURL->wszDiskPath);
						continue;
					}
				}

				DWORD dwBytes;
				HANDLE hFile = CreateFile(pFileURL->wszDiskPath, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
				if (hFile != INVALID_HANDLE_VALUE) {
					// write the downloaded file directly
					WriteFile(hFile, pReply->pData, (DWORD)pReply->dataLength, &dwBytes, nullptr);
					CloseHandle(hFile);
				}
				else {
					// try to write it via PU stub
					TFileName wszTempFile;
					mir_snwprintf(wszTempFile, L"%s\\pulocal.tmp", g_wszTempPath);
					hFile = CreateFile(wszTempFile, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
					if (hFile != INVALID_HANDLE_VALUE) {
						WriteFile(hFile, pReply->pData, (DWORD)pReply->dataLength, &dwBytes, nullptr);
						CloseHandle(hFile);
						SafeMoveFile(wszTempFile, pFileURL->wszDiskPath);
					}
				}
				return true;
			}
			Netlib_LogfW(hNetlibUser, L"Downloading file %s failed with error %d", pFileURL->wszDownloadURL, pReply->resultCode);
		}
		else {
			Netlib_LogfW(hNetlibUser, L"Downloading file %s failed, host is propably temporary down.", pFileURL->wszDownloadURL);
			nlc = nullptr;
		}
	}

	Netlib_LogfW(hNetlibUser, L"Downloading file %s failed, giving up", pFileURL->wszDownloadURL);
	return false;
}

/////////////////////////////////////////////////////////////////////////////////////
// FUNCTION: IsRunAsAdmin()
//
// PURPOSE: The function checks whether the current process is run as
// administrator. In other words, it dictates whether the primary access
// token of the process belongs to user account that is a member of the
// local Administrators group and it is elevated.
//
// RETURN VALUE: Returns TRUE if the primary access token of the process
// belongs to user account that is a member of the local Administrators
// group and it is elevated. Returns FALSE if the token does not.
//
// EXCEPTION: If this function fails, it throws a C++ DWORD exception which
// contains the Win32 error code of the failure.
//
// EXAMPLE CALL:
//   try
//   {
//       if (IsRunAsAdmin())
//           wprintf (L"Process is run as administrator\n");
//       else
//           wprintf (L"Process is not run as administrator\n");
//   }
//   catch (DWORD dwError)
//   {
//       wprintf(L"IsRunAsAdmin failed w/err %lu\n", dwError);
//   }

BOOL IsRunAsAdmin()
{
	BOOL fIsRunAsAdmin = FALSE;
	DWORD dwError = ERROR_SUCCESS;
	PSID pAdministratorsGroup = nullptr;

	// Allocate and initialize a SID of the administrators group.
	SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
	if (!AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &pAdministratorsGroup)) {
		dwError = GetLastError();
		goto Cleanup;
	}

	// Determine whether the SID of administrators group is bEnabled in
	// the primary access token of the process.
	if (!CheckTokenMembership(nullptr, pAdministratorsGroup, &fIsRunAsAdmin)) {
		dwError = GetLastError();
		goto Cleanup;
	}

Cleanup:
	// Centralized cleanup for all allocated resources.
	if (pAdministratorsGroup) {
		FreeSid(pAdministratorsGroup);
		pAdministratorsGroup = nullptr;
	}

	// Throw the error if something failed in the function.
	if (ERROR_SUCCESS != dwError) {
		throw dwError;
	}

	return fIsRunAsAdmin;
}

/////////////////////////////////////////////////////////////////////////////////////
// FUNCTION: IsProcessElevated()
//
// PURPOSE: The function gets the elevation information of the current
// process. It dictates whether the process is elevated or not. Token
// elevation is only available on Windows Vista and newer operating
// systems, thus IsProcessElevated throws a C++ exception if it is called
// on systems prior to Windows Vista. It is not appropriate to use this
// function to determine whether a process is run as administartor.
//
// RETURN VALUE: Returns TRUE if the process is elevated. Returns FALSE if
// it is not.
//
// EXCEPTION: If this function fails, it throws a C++ DWORD exception
// which contains the Win32 error code of the failure. For example, if
// IsProcessElevated is called on systems prior to Windows Vista, the error
// code will be ERROR_INVALID_PARAMETER.
//
// NOTE: TOKEN_INFORMATION_CLASS provides TokenElevationType to check the
// elevation type (TokenElevationTypeDefault / TokenElevationTypeLimited /
// TokenElevationTypeFull) of the process. It is different from
// TokenElevation in that, when UAC is turned off, elevation type always
// returns TokenElevationTypeDefault even though the process is elevated
// (Integrity Level == High). In other words, it is not safe to say if the
// process is elevated based on elevation type. Instead, we should use
// TokenElevation.
//
// EXAMPLE CALL:
//   try
//   {
//       if (IsProcessElevated())
//           wprintf (L"Process is elevated\n");
//       else
//           wprintf (L"Process is not elevated\n");
//   }
//   catch (DWORD dwError)
//   {
//       wprintf(L"IsProcessElevated failed w/err %lu\n", dwError);
//   }

BOOL IsProcessElevated()
{
	BOOL fIsElevated = FALSE;
	DWORD dwError = ERROR_SUCCESS;
	HANDLE hToken = nullptr;

	// Open the primary access token of the process with TOKEN_QUERY.
	if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
		dwError = GetLastError();
		goto Cleanup;
	}

	// Retrieve token elevation information.
	TOKEN_ELEVATION elevation;
	DWORD dwSize;
	if (!GetTokenInformation(hToken, TokenElevation, &elevation, sizeof(elevation), &dwSize)) {
		// When the process is run on operating systems prior to Windows
		// Vista, GetTokenInformation returns FALSE with the
		// ERROR_INVALID_PARAMETER error code because TokenElevation is
		// not supported on those operating systems.
		dwError = GetLastError();
		goto Cleanup;
	}

	fIsElevated = elevation.TokenIsElevated;

Cleanup:
	// Centralized cleanup for all allocated resources.
	if (hToken) {
		CloseHandle(hToken);
		hToken = nullptr;
	}

	// Throw the error if something failed in the function.
	if (ERROR_SUCCESS != dwError) {
		throw dwError;
	}

	return fIsElevated;
}

bool PrepareEscalation()
{
	// First try to create a file near Miranda32.exe
	TFileName szPath;
	GetModuleFileName(nullptr, szPath, _countof(szPath));
	wchar_t *ext = wcsrchr(szPath, '.');
	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
		CloseHandle(hFile);
		DeleteFile(szPath);
		return true;
	}

	// Check the current process's "run as administrator" status.
	if (IsRunAsAdmin())
		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;
	}
	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 ...
		}
	}
	return false;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Folder creation

static int __cdecl CompareDirs(const CMStringW *s1, const CMStringW *s2)
{
	return mir_wstrcmp(s1->c_str(), s2->c_str());
}

void CreateWorkFolders(TFileName &wszTempFolder, TFileName &wszBackupFolder)
{
	TFileName wszMask;
	mir_snwprintf(wszMask, L"%s\\Backups\\BKP*", g_wszRoot);

	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));

		// remove all folders with lesser dates if there're more than 10 folders
		while (arNames.getCount() > 9) {
			SafeDeleteDirectory(arNames[0]);
			arNames.remove(00);
		}
	}

	SYSTEMTIME st;
	GetLocalTime(&st);
	mir_snwprintf(wszBackupFolder, L"%s\\Backups\\BKP%04d-%02d-%02d %02d-%02d-%02d-%03d", g_wszRoot, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
	SafeCreateDirectory(wszBackupFolder);

	mir_snwprintf(wszTempFolder, L"%s\\Temp", g_wszRoot);
	SafeCreateDirectory(wszTempFolder);
}

/////////////////////////////////////////////////////////////////////////////////////////

int TransactPipe(int opcode, const wchar_t *p1, const wchar_t *p2)
{
	BYTE buf[1024];
	DWORD l1 = lstrlen(p1), l2 = lstrlen(p2);
	if (l1 > MAX_PATH || l2 > MAX_PATH)
		return ERROR_BAD_ARGUMENTS;

	*(DWORD *)buf = opcode;
	wchar_t *dst = (wchar_t *)&buf[sizeof(DWORD)];
	lstrcpy(dst, p1);
	dst += l1 + 1;
	if (p2) {
		lstrcpy(dst, p2);
		dst += l2 + 1;
	}
	else *dst++ = 0;

	DWORD dwBytes = 0, dwError;
	if (!WriteFile(hPipe, buf, (DWORD)((BYTE *)dst - buf), &dwBytes, nullptr))
		return GetLastError();

	dwError = 0;
	if (!ReadFile(hPipe, &dwError, sizeof(DWORD), &dwBytes, nullptr))
		return GetLastError();
	if (dwBytes != sizeof(DWORD))
		return ERROR_BAD_ARGUMENTS;

	return dwError;
}

int SafeCopyFile(const wchar_t *pSrc, const wchar_t *pDst)
{
	if (hPipe == nullptr)
		return CopyFileW(pSrc, pDst, FALSE);

	return TransactPipe(1, pSrc, pDst);
}

int SafeMoveFile(const wchar_t *pSrc, const wchar_t *pDst)
{
	if (hPipe == nullptr) {
		if (!DeleteFileW(pDst)) {
			DWORD dwError = GetLastError();
			if (dwError != ERROR_ACCESS_DENIED && dwError != ERROR_FILE_NOT_FOUND)
				return dwError;
		}

		if (!MoveFileW(pSrc, pDst)) { // use copy on error
			switch (DWORD dwError = GetLastError()) {
			case ERROR_ALREADY_EXISTS:
			case ERROR_FILE_NOT_FOUND:
				return 0; // this file was included into many archives, so Miranda tries to move it again & again

			case ERROR_ACCESS_DENIED:
			case ERROR_SHARING_VIOLATION:
			case ERROR_LOCK_VIOLATION:
				// use copy routine if a move operation isn't available
				// for example, when files are on different disks
				if (!CopyFileW(pSrc, pDst, FALSE))
					return GetLastError();

				if (!DeleteFileW(pSrc))
					return GetLastError();
				break;

			default:
				return dwError;
			}
		}

		return ERROR_SUCCESS;
	}

	return TransactPipe(2, pSrc, pDst);
}

int SafeDeleteFile(const wchar_t *pwszFile)
{
	if (hPipe == nullptr)
		return DeleteFile(pwszFile);

	return TransactPipe(3, pwszFile, nullptr);
}

int SafeCreateDirectory(const wchar_t *pwszFolder)
{
	if (hPipe == nullptr)
		return CreateDirectoryTreeW(pwszFolder);

	return TransactPipe(4, pwszFolder, nullptr);
}

int SafeDeleteDirectory(const wchar_t *pwszDirName)
{
	if (hPipe == nullptr)
		return DeleteDirectoryTreeW(pwszDirName);

	return TransactPipe(6, pwszDirName, nullptr);
}

int SafeCreateFilePath(const wchar_t *pwszFolder)
{
	if (hPipe == nullptr) {
		CreatePathToFileW(pwszFolder);
		return 0;
	}

	return TransactPipe(5, pwszFolder, nullptr);
}

int BackupFile(wchar_t *pwszSrcFileName, wchar_t *pwszBackFileName)
{
	SafeCreateFilePath(pwszBackFileName);

	if (int iErrorCode = SafeMoveFile(pwszSrcFileName, pwszBackFileName))
		return iErrorCode;
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

char* StrToLower(char *str)
{
	for (int i = 0; str[i]; i++)
		str[i] = tolower(str[i]);

	return str;
}