summaryrefslogtreecommitdiff
path: root/plugins/DbChecker/src/worker.cpp
blob: 704fa325cb3b1c45a611206a90a446571ec45e56 (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
/*
Miranda Database Tool
Copyright (C) 2001-2005  Richard Hughes

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

This program 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 General Public License for more details.

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

#include "stdafx.h"

void ProcessingDone(void);

static void Finalize(time_t& ts)
{
	opts.dbChecker->Destroy();
	opts.dbChecker = nullptr;

	if (opts.hOutFile) {
		CloseHandle(opts.hOutFile);
		opts.hOutFile = nullptr;
	}

	if (errorCount && !opts.bBackup && !opts.bCheckOnly) {
		time_t dlg_ts = time(nullptr);
		if (IDYES == MessageBox(nullptr,
			TranslateT("Errors were encountered, however you selected not to backup the original database. It is strongly recommended that you do so in case important data was omitted. Do you wish to keep a backup of the original database?"),
			TranslateT("Miranda Database Tool"), MB_YESNO))
			opts.bBackup = 1;
		ts += time(nullptr) - dlg_ts;
	}

	if (opts.bBackup) {
		wchar_t dbPath[MAX_PATH], dbFile[MAX_PATH];
		mir_wstrcpy(dbPath, opts.filename);
		wchar_t* str2 = wcsrchr(dbPath, '\\');
		if (str2 != nullptr) {
			mir_wstrcpy(dbFile, str2 + 1);
			*str2 = 0;
		}
		else {
			mir_wstrcpy(dbFile, dbPath);
			dbPath[0] = 0;
		}
		for (int i = 1;; i++) {
			if (i == 1)
				mir_snwprintf(opts.backupFilename, TranslateT("%s\\Backup of %s"), dbPath, dbFile);
			else
				mir_snwprintf(opts.backupFilename, TranslateT("%s\\Backup (%d) of %s"), dbPath, i, dbFile);
			if (_waccess(opts.backupFilename, 0) == -1) break;
		}

		if (!MoveFile(opts.filename, opts.backupFilename))
			AddToStatus(STATUS_WARNING, TranslateT("Unable to rename original file"));
	}
	else if (!opts.bCheckOnly)
		if (!DeleteFile(opts.filename))
			AddToStatus(STATUS_WARNING, TranslateT("Unable to delete original file"));

	if (!opts.bCheckOnly)
		if (!MoveFile(opts.outputFilename, opts.filename))
			AddToStatus(STATUS_WARNING, TranslateT("Unable to rename output file"));
}

void __cdecl WorkerThread(void *)
{
	int task, firstTime;
	time_t ts = time(nullptr);

	AddToStatus(STATUS_MESSAGE, TranslateT("Database worker thread activated"));

	mir_wstrcpy(opts.workingFilename, opts.filename);

	if (opts.bCheckOnly) {
		mir_wstrcpy(opts.outputFilename, TranslateT("<check only>"));
		opts.hOutFile = INVALID_HANDLE_VALUE;
	}
	else {
		mir_wstrcpy(opts.outputFilename, opts.filename);
		*wcsrchr(opts.outputFilename, '.') = 0;
		mir_wstrcat(opts.outputFilename, TranslateT(" (Output).dat"));
		opts.hOutFile = CreateFile(opts.outputFilename, GENERIC_WRITE, FILE_SHARE_READ, nullptr, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
		if (opts.hOutFile == INVALID_HANDLE_VALUE) {
			AddToStatus(STATUS_FATAL, TranslateT("Can't create output file (%u)"), GetLastError());
			ProcessingDone();
			return;
		}
	}

	DWORD sp = 0;
	firstTime = 0;

	DBCHeckCallback callback;
	callback.cbSize = sizeof(callback);
	callback.spaceUsed = 1;
	callback.spaceProcessed = 0;
	callback.pfnAddLogMessage = AddToStatus;
	callback.hOutFile = opts.hOutFile;
	callback.bCheckOnly = opts.bCheckOnly;
	callback.bBackup = opts.bBackup;
	callback.bAggressive = opts.bAggressive;
	callback.bEraseHistory = opts.bEraseHistory;
	callback.bMarkRead = opts.bMarkRead;
	callback.bConvertUtf = opts.bConvertUtf;
	opts.dbChecker->Start(&callback);

	for (task = 0;;) {
		if (callback.spaceProcessed / (callback.spaceUsed / 1000 + 1) > sp) {
			sp = callback.spaceProcessed / (callback.spaceUsed / 1000 + 1);
			SetProgressBar(sp);
		}
		WaitForSingleObject(hEventRun, INFINITE);
		if (WaitForSingleObject(hEventAbort, 0) == WAIT_OBJECT_0) {
			AddToStatus(STATUS_FATAL, TranslateT("Processing aborted by user"));
			break;
		}

		int ret = opts.dbChecker->CheckDb(task, firstTime);
		firstTime = 0;
		if (ret == ERROR_OUT_OF_PAPER) {
			Finalize(ts);
			AddToStatus(STATUS_MESSAGE, TranslateT("Elapsed time: %d sec"), time(nullptr) - ts);
			if (errorCount)
				AddToStatus(STATUS_SUCCESS, TranslateT("All tasks completed but with errors (%d)"), errorCount);
			else
				AddToStatus(STATUS_SUCCESS, TranslateT("All tasks completed successfully"));
			break;
		}
		else if (ret == ERROR_NO_MORE_ITEMS) {
			task++;
			firstTime = 1;
		}
		else if (ret != ERROR_SUCCESS)
			break;
	}

	ProcessingDone();
}