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
|
/*
Miranda IM: the free IM client for Microsoft* Windows*
Copyright 2000-2003 Miranda ICQ/IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.
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 "commonheaders.h"
#include "options.h"
#define SHOW_PROGRESS_TIME 1000 // show progress window when backup time exceeds this many milliseconds
extern HANDLE hDbFile;
extern CRITICAL_SECTION csDbAccess;
extern HINSTANCE g_hInst;
extern Options options;
UINT timer_id = 0;
void Map();
void UnMap();
BOOL CALLBACK DlgProcProgress(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg) {
case WM_INITDIALOG:
{
HWND prog = GetDlgItem(hwndDlg, IDC_PROGRESS);
TranslateDialogDefault( hwndDlg );
SendMessage(prog, PBM_SETPOS, 0, 0);
}
break;
case WM_COMMAND:
if ( HIWORD( wParam ) == BN_CLICKED ) {
switch( LOWORD( wParam )) {
case IDCANCEL:
// in the progress dialog, use the user data to indicate that the user has pressed cancel
SetWindowLong(hwndDlg, GWL_USERDATA, (LONG)1);
return TRUE;
}
}
break;
}
return FALSE;
}
int RotateBackups(HWND progress_dialog, DWORD start_time) {
char num1[4], num2[4];
char backupfilename1[MAX_PATH], backupfilename2[MAX_PATH];
int i;
HWND prog = GetDlgItem(progress_dialog, IDC_PROGRESS);
MSG msg;
// delete the last file, so that the rest can be rename operations
strcpy(backupfilename1, options.folder);
strcat(backupfilename1, "\\db_backup");
itoa(options.num_backups - 1, num1, 10);
strcat(backupfilename1, num1);
strcat(backupfilename1, ".dat");
DeleteFileA(backupfilename1);
for(i = options.num_backups - 1; i > 0 && GetWindowLong(progress_dialog, GWL_USERDATA) == 0; i--) {
strcpy(backupfilename1, options.folder);
strcat(backupfilename1, "\\db_backup");
itoa(i - 1, num1, 10);
strcat(backupfilename1, num1);
strcat(backupfilename1, ".dat");
strcpy(backupfilename2, options.folder);
strcat(backupfilename2, "\\db_backup");
itoa(i, num2, 10);
strcat(backupfilename2, num2);
strcat(backupfilename2, ".dat");
MoveFileA(backupfilename1, backupfilename2);
if(GetTickCount() - start_time >= SHOW_PROGRESS_TIME)
ShowWindow(progress_dialog, SW_SHOW);
while(PeekMessage(&msg, progress_dialog, 0, 0, PM_REMOVE) != 0) {
if(!IsDialogMessage(progress_dialog, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
SendMessage(prog, PBM_SETPOS, (WPARAM)(int)(100 * (options.num_backups - i) / options.num_backups), 0);
UpdateWindow(progress_dialog);
}
return 0;
}
int Backup() {
HANDLE hBackupFile;
char backup_filename[MAX_PATH];
char buff[8192];
DWORD bytes_read, bytes_written, file_size, total_bytes_copied = 0;
HWND progress_dialog = 0, prog;
MSG msg;
DWORD start_time = GetTickCount();
// ensure the backup folder exists (either create it or return non-zero signifying error)
if(!CreateDirectoryA(options.folder, 0)) {
DWORD err = GetLastError();
if(err != ERROR_ALREADY_EXISTS) {
return 1;
}
}
if(!options.disable_progress) {
progress_dialog = CreateDialog(g_hInst, MAKEINTRESOURCE(IDD_COPYPROGRESS), 0, DlgProcProgress);
prog = GetDlgItem(progress_dialog, IDC_PROGRESS);
SetDlgItemText(progress_dialog, IDC_PROGRESSMESSAGE, TranslateT("Rotating backup files..."));
}
RotateBackups(progress_dialog, start_time);
strcpy(backup_filename, options.folder);
strcat(backup_filename, "\\db_backup0.dat");
hBackupFile = CreateFileA(backup_filename, GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if(hBackupFile != INVALID_HANDLE_VALUE) {
if(!options.disable_progress) {
SetDlgItemText(progress_dialog, IDC_PROGRESSMESSAGE, TranslateT("Copying database file..."));
SendMessage(prog, PBM_SETPOS, (WPARAM)(int)(0), 0);
UpdateWindow(progress_dialog);
}
EnterCriticalSection(&csDbAccess);
UnMap();
file_size = GetFileSize(hDbFile, 0);
if(file_size == 0) {
Map();
LeaveCriticalSection(&csDbAccess);
CloseHandle(hBackupFile);
DestroyWindow(progress_dialog);
return 1;
}
SetFilePointer(hDbFile, 0, 0, FILE_BEGIN);
while(ReadFile(hDbFile, buff, sizeof(buff), &bytes_read, 0) == TRUE && bytes_read > 0
&& GetWindowLong(progress_dialog, GWL_USERDATA) == 0)
{
if(!WriteFile(hBackupFile, buff, bytes_read, &bytes_written, 0)) {
Map();
LeaveCriticalSection(&csDbAccess);
CloseHandle(hBackupFile);
DestroyWindow(progress_dialog);
return 1;
}
total_bytes_copied += bytes_written;
if(!options.disable_progress) {
if(GetTickCount() - start_time >= SHOW_PROGRESS_TIME)
ShowWindow(progress_dialog, SW_SHOW);
while(PeekMessage(&msg, progress_dialog, 0, 0, PM_REMOVE) != 0) {
if(!IsDialogMessage(progress_dialog, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
SendMessage(prog, PBM_SETPOS, (WPARAM)(int)(100.0 * total_bytes_copied / file_size), 0);
UpdateWindow(progress_dialog);
}
}
Map();
LeaveCriticalSection(&csDbAccess);
CloseHandle(hBackupFile);
if(!options.disable_progress && GetWindowLong(progress_dialog, GWL_USERDATA) != 0)
DeleteFileA(backup_filename);
else
DBWriteContactSettingDword(0, "db3x", "LastBackupTimestamp", (DWORD)time(0));
if(!options.disable_progress)
DestroyWindow(progress_dialog);
return 0;
}
if(!options.disable_progress)
DestroyWindow(progress_dialog);
return 1;
}
VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) {
time_t t = time(0), diff = t - (time_t)DBGetContactSettingDword(0, "db3x", "LastBackupTimestamp", (DWORD)t);
if(diff > (time_t)(options.period * (options.period_type == PT_HOURS ? 60 * 60 : 60 * 60 * 24)))
Backup();
}
int SetBackupTimer(void) {
if(options.backup_types & BT_PERIODIC) {
if(timer_id == 0) {
time_t t = time(0), diff = t - (time_t)DBGetContactSettingDword(0, "db3x", "LastBackupTimestamp", (DWORD)t);
if(diff > (time_t)(options.period * (options.period_type == PT_HOURS ? 60 * 60 : 60 * 60 * 24)))
Backup();
timer_id = SetTimer(0, 0, 1000 * 60 * 60, TimerProc);
}
} else {
if(timer_id != 0) {
KillTimer(0, timer_id);
timer_id = 0;
}
}
return 0;
}
int BackupService(WPARAM wParam, LPARAM lParam) {
return Backup();
}
|