summaryrefslogtreecommitdiff
path: root/plugins/Db_autobackups/src/backup.cpp
blob: df31953a223a6b886b50beadabee376ac0bebbe9 (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
#include "stdafx.h"

static UINT_PTR	timer_id = 0;
volatile long m_state = 0;


LRESULT CALLBACK DlgProcPopup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_COMMAND:
	{
		wchar_t* ptszPath = (wchar_t*)PUGetPluginData(hWnd);
		if (ptszPath != nullptr)
			ShellExecute(nullptr, L"open", ptszPath, nullptr, nullptr, SW_SHOW);

		PUDeletePopup(hWnd);
		break;
	}
	case WM_CONTEXTMENU:
		PUDeletePopup(hWnd);
		break;
	case UM_FREEPLUGINDATA:
		mir_free(PUGetPluginData(hWnd));
		break;
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}

void ShowPopup(wchar_t* ptszText, wchar_t* ptszHeader, wchar_t* ptszPath)
{
	POPUPDATAT ppd = { 0 };

	wcsncpy_s(ppd.lptzText, ptszText, _TRUNCATE);
	wcsncpy_s(ppd.lptzContactName, ptszHeader, _TRUNCATE);
	if (ptszPath != nullptr)
		ppd.PluginData = (void*)mir_wstrdup(ptszPath);
	ppd.PluginWindowProc = DlgProcPopup;
	ppd.lchIcon = IcoLib_GetIcon(iconList[0].szName);

	PUAddPopupT(&ppd);
}

INT_PTR CALLBACK DlgProcProgress(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM)
{
	switch (msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hwndDlg);
		SendDlgItemMessage(hwndDlg, IDC_PROGRESS, PBM_SETPOS, 0, 0);
		break;
	case WM_COMMAND:
		if (HIWORD(wParam) != BN_CLICKED || LOWORD(wParam) != IDCANCEL)
			break;
		// in the progress dialog, use the user data to indicate that the user has pressed cancel
		SetWindowLongPtr(hwndDlg, GWLP_USERDATA, 1);
		return TRUE;
		break;
	}
	return FALSE;
}

wchar_t* DoubleSlash(wchar_t *sorce)
{
	wchar_t *ret, *r, *s;

	ret = (wchar_t*)mir_alloc((MAX_PATH * sizeof(wchar_t)));
	if (ret == nullptr)
		return nullptr;
	for (s = sorce, r = ret; *s && (r - ret) < (MAX_PATH - 1); s++, r++) {
		if (*s != '\\')
			*r = *s;
		else {
			*r = '\\';
			r++;
			*r = '\\';
		}
	}
	r[0] = 0;
	return ret;
}

bool MakeZip_Dir(LPCSTR szDir, LPCTSTR szDest, LPCSTR /* szDbName */, HWND progress_dialog)
{
	HWND hProgBar = GetDlgItem(progress_dialog, IDC_PROGRESS);
	size_t count = 0;
	OBJLIST<ZipFile> lstFiles(15);

	for (auto it = fs::recursive_directory_iterator(fs::path(szDir)); it != fs::recursive_directory_iterator(); ++it)
	{
		const auto& file = it->path();
		if (!fs::is_directory(file) && file.string().find(fs::path((char*)_T2A(szDest)).string().c_str()) == std::string::npos)
		{
			const std::string &filepath = file.string();
			const std::string rpath = filepath.substr(filepath.find(szDir) + mir_strlen(szDir) + 1);

			lstFiles.insert(new ZipFile(filepath, rpath));
			count++;
		}
	}
	if (count == 0) 
		return 1;

	CreateZipFile(_T2A(szDest), lstFiles, [&](size_t i)->bool
	{ 
		SendMessage(hProgBar, PBM_SETPOS, (WPARAM)(100 * i / count), 0); 
		return GetWindowLongPtr(progress_dialog, GWLP_USERDATA) != 1;
	});

	return 1;
}

bool MakeZip(wchar_t *tszSource, wchar_t *tszDest, wchar_t *dbname, HWND progress_dialog)
{
	HWND hProgBar = GetDlgItem(progress_dialog, IDC_PROGRESS);

	ptrA szSourceName(mir_u2a(dbname));
	ptrW tszDestPath(DoubleSlash(tszDest));
	OBJLIST<ZipFile> lstFiles(15);
	lstFiles.insert(new ZipFile((char*)_T2A(tszSource), (char*)szSourceName));

	CreateZipFile(_T2A(tszDest), lstFiles, [&](size_t)->bool{ SendMessage(hProgBar, PBM_SETPOS, (WPARAM)(100), 0); return true; });

	return true;
}


struct backupFile
{
	wchar_t Name[MAX_PATH];
	FILETIME CreationTime;
};

int Comp(const void *i, const void *j)
{
	backupFile *pi = (backupFile*)i;
	backupFile *pj = (backupFile*)j;

	if (pi->CreationTime.dwHighDateTime > pj->CreationTime.dwHighDateTime ||
		(pi->CreationTime.dwHighDateTime == pj->CreationTime.dwHighDateTime && pi->CreationTime.dwLowDateTime > pj->CreationTime.dwLowDateTime))
		return -1;
	else
		return 1;
}

int RotateBackups(wchar_t *backupfolder, wchar_t *dbname)
{
	backupFile *bf = nullptr, *bftmp;
	HANDLE hFind;
	wchar_t backupfolderTmp[MAX_PATH];
	WIN32_FIND_DATA FindFileData;

	if (options.num_backups == 0)
		return 0; /* Roration disabled. */
	mir_snwprintf(backupfolderTmp, L"%s\\%s*", backupfolder, dbname);
	hFind = FindFirstFile(backupfolderTmp, &FindFileData);
	if (hFind == INVALID_HANDLE_VALUE)
		return 0;

	int i = 0;
	do {
		if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			continue;
		bftmp = (backupFile*)mir_realloc(bf, ((i + 1) * sizeof(backupFile)));
		if (bftmp == nullptr)
			goto err_out;
		bf = bftmp;
		wcsncpy_s(bf[i].Name, FindFileData.cFileName, _TRUNCATE);
		bf[i].CreationTime = FindFileData.ftCreationTime;
		i ++;
	} while (FindNextFile(hFind, &FindFileData));
	if (i > 0)
		qsort(bf, i, sizeof(backupFile), Comp); /* Sort the list of found files by date in descending order. */
	for (; i >= options.num_backups; i --) {
		mir_snwprintf(backupfolderTmp, L"%s\\%s", backupfolder, bf[(i - 1)].Name);
		DeleteFile(backupfolderTmp);
	}
err_out:
	FindClose(hFind);
	mir_free(bf);
	return 0;
}


int Backup(wchar_t *backup_filename)
{
	bool bZip = false;
	wchar_t dbname[MAX_PATH], source_file[MAX_PATH] = { 0 }, dest_file[MAX_PATH];
	HWND progress_dialog = nullptr;
	SYSTEMTIME st;

	Profile_GetNameW(_countof(dbname), dbname);

	if (backup_filename == nullptr) {
		int err;
		wchar_t backupfolder[MAX_PATH], buffer[MAX_COMPUTERNAME_LENGTH + 1];
		DWORD size = _countof(buffer);

		bZip = options.use_zip != 0;
		PathToAbsoluteW(VARSW(options.folder), backupfolder);
		// ensure the backup folder exists (either create it or return non-zero signifying error)
		err = CreateDirectoryTreeW(backupfolder);
		if (err != ERROR_ALREADY_EXISTS && err != 0) {
			mir_free(backupfolder);
			return 1;
		}
		RotateBackups(backupfolder, dbname);

		GetLocalTime(&st);
		GetComputerName(buffer, &size);
		mir_snwprintf(dest_file, L"%s\\%s_%02d.%02d.%02d@%02d-%02d-%02d_%s.%s", backupfolder, dbname, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, buffer, bZip ? L"zip" : L"dat");
	}
	else {
		wcsncpy_s(dest_file, backup_filename, _TRUNCATE);
		if (!mir_wstrcmp(wcsrchr(backup_filename, '.'), L".zip"))
			bZip = true;
	}
	if (!options.disable_popups)
		ShowPopup(dbname, TranslateT("Backup in progress"), nullptr);

	if (!options.disable_progress)
		progress_dialog = CreateDialog(g_hInstance, MAKEINTRESOURCE(IDD_COPYPROGRESS), nullptr, DlgProcProgress);

	SetDlgItemText(progress_dialog, IDC_PROGRESSMESSAGE, TranslateT("Copying database file..."));

	VARSW profile_path(L"%miranda_userdata%");
	mir_snwprintf(source_file, L"%s\\%s", profile_path, dbname);
	BOOL res = 0;
	if (bZip)
	{
		res = options.backup_profile 
			? MakeZip_Dir(_T2A(profile_path), dest_file, _T2A(dbname), progress_dialog)
			: MakeZip(source_file, dest_file, dbname, progress_dialog);
	}
	else
		res = CopyFile(source_file, dest_file, 0);
	if (res) {
		if (!bZip) { // Set the backup file to the current time for rotator's correct  work
			FILETIME ft;
			HANDLE hFile = CreateFile(dest_file, FILE_WRITE_ATTRIBUTES, NULL, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
			GetSystemTime(&st);
			SystemTimeToFileTime(&st, &ft);
			SetFileTime(hFile, nullptr, nullptr, &ft);
			CloseHandle(hFile);
		}
		SendDlgItemMessage(progress_dialog, IDC_PROGRESS, PBM_SETPOS, (WPARAM)(100), 0);
		UpdateWindow(progress_dialog);
		db_set_dw(0, MODULE, "LastBackupTimestamp", (DWORD)time(nullptr));

		if (options.use_cloudfile)
		{
			CFUPLOADDATA ui =
			{
				options.cloudfile_service,
				dest_file,
				L"Backups"
			};
			if (CallService(MS_CLOUDFILE_UPLOAD, (LPARAM)&ui))
				ShowPopup(TranslateT("Uploading to cloud failed"), TranslateT("Error"), nullptr);
		}

		if (!options.disable_popups) {
			size_t dest_file_len = mir_wstrlen(dest_file);
			wchar_t *puText;

			if (dest_file_len > 50) {
				size_t i;
				puText = (wchar_t*)mir_alloc(sizeof(wchar_t) * (dest_file_len + 2));
				for (i = (dest_file_len - 1); dest_file[i] != '\\'; i--)
					;
				//wcsncpy_s(dest_file, backup_filename, _TRUNCATE);
				mir_wstrncpy(puText, dest_file, (i + 2));
				mir_wstrcat(puText, L"\n");
				mir_wstrcat(puText, (dest_file + i + 1));
			}
			else
				puText = mir_wstrdup(dest_file);

			// Now we need to know, which folder we made a backup. Let's break unnecessary variables :)
			while (dest_file[--dest_file_len] != L'\\')
				;
			dest_file[dest_file_len] = 0;
			ShowPopup(puText, TranslateT("Database backed up"), dest_file);
			mir_free(puText);
		}
	}
	else
		DeleteFile(dest_file);

	DestroyWindow(progress_dialog);
	return 0;
}

void BackupThread(void *backup_filename)
{
	Backup((wchar_t*)backup_filename);
	InterlockedExchange(&m_state, 0); /* Backup done. */
	mir_free(backup_filename);
}

void BackupStart(wchar_t *backup_filename)
{
	wchar_t *tm = nullptr;
	LONG cur_state;

	cur_state = InterlockedCompareExchange(&m_state, 1, 0);
	if (cur_state != 0) { /* Backup allready in process. */
		ShowPopup(TranslateT("Database back up in process..."), TranslateT("Error"), nullptr); /* Show error message :) */
		return;
	}
	if (backup_filename != nullptr)
		tm = mir_wstrdup(backup_filename);
	if (mir_forkthread(BackupThread, (void*)tm) == INVALID_HANDLE_VALUE) {
		InterlockedExchange(&m_state, 0); /* Backup done. */
		mir_free(tm);
	}
}

VOID CALLBACK TimerProc(HWND, UINT, UINT_PTR, DWORD)
{
	time_t t = time(nullptr);
	time_t diff = t - (time_t)db_get_dw(0, "AutoBackups", "LastBackupTimestamp", 0);
	if (diff > (time_t)(options.period * (options.period_type == PT_MINUTES ? 60 : (options.period_type == PT_HOURS ? (60 * 60) : (60 * 60 * 24)))))
		BackupStart(nullptr);
}

int SetBackupTimer(void)
{
	if (timer_id != 0) {
		KillTimer(nullptr, timer_id);
		timer_id = 0;
	}
	if (options.backup_types & BT_PERIODIC)
		timer_id = SetTimer(nullptr, 0, (1000 * 60), TimerProc);
	return 0;
}