#include "commonheaders.h" extern HANDLE hDbFile; extern CRITICAL_SECTION csDbAccess; extern HINSTANCE g_hInst; extern BOOL gl_bUnicodeAwareCore; struct DBHeader dbHeader; int BackupService(WPARAM wParam, LPARAM lParam); static HANDLE hEventBackup; int InitBackups() { CreateServiceFunction(MS_DBMMAP_BACKUP,BackupService); hEventBackup = CreateHookableEvent(ME_DB_BACKUPED); return 0; } int Backup(char* backup_filename, HWND progress_dialog) { HANDLE hBackupFile; char buff[8192]; DWORD bytes_read, bytes_written, file_size, total_bytes_copied = 0; HWND prog; MSG msg; DWORD start_time = GetTickCount(); prog = GetDlgItem(progress_dialog, 0xDEAD); hBackupFile = CreateFileA(backup_filename, GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if(hBackupFile != INVALID_HANDLE_VALUE) { if(progress_dialog) { if (gl_bUnicodeAwareCore) SetDlgItemText(progress_dialog, 0xDAED, TranslateT("Copying database file...")); else SetDlgItemTextA(progress_dialog, 0xDAED, Translate("Copying database file...")); SendMessage(prog, PBM_SETPOS, (WPARAM)(int)(0), 0); UpdateWindow(progress_dialog); } EnterCriticalSection(&csDbAccess); file_size = GetFileSize(hDbFile, 0); if(file_size == 0) { LeaveCriticalSection(&csDbAccess); CloseHandle(hBackupFile); DestroyWindow(progress_dialog); return 1; } SetFilePointer(hDbFile, sizeof(dbHeader), 0, FILE_BEGIN); if(WriteFile(hBackupFile,&dbHeader,sizeof(dbHeader),&bytes_written,NULL) == TRUE) { total_bytes_copied += bytes_written; if(progress_dialog) { SendMessage(prog, PBM_SETPOS, (WPARAM)(int)(100.0 * total_bytes_copied / file_size), 0); UpdateWindow(progress_dialog); } } else { LeaveCriticalSection(&csDbAccess); CloseHandle(hBackupFile); return 1; } 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)) { LeaveCriticalSection(&csDbAccess); CloseHandle(hBackupFile); return 1; } total_bytes_copied += bytes_written; if(progress_dialog) { 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); } } LeaveCriticalSection(&csDbAccess); CloseHandle(hBackupFile); if(progress_dialog && GetWindowLong(progress_dialog, GWL_USERDATA) != 0) DeleteFileA(backup_filename); else NotifyEventHooks(hEventBackup, (WPARAM)backup_filename, 0); return 0; } return 1; } int BackupService(WPARAM wParam,LPARAM lParam) { return Backup((char*)wParam, (HWND)lParam); }