summaryrefslogtreecommitdiff
path: root/NSCmpts/BackupActions.cpp
blob: 0dff71ed7af46f2a26c827ca087cfe71f6828b0f (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
// BackupActions.cpp: implementation of the CBackupActions class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BackupActions.h"

#include "resource.h"
#include <appmisc.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

bool CBackupActions::ArchivePrepare::Do(CActionEngine* pEngine, 
	  								    HWND hwndUpdates)
{
	// Some Checks
	ASSERT(pEngine != NULL);

	if(!m_dest.valid())
		throw CActionError(E_INVALIDARG, IDS_BACKUP_ERR_DEST, m_dest);

	// Create destination folder
	HRESULT hr = m_dest.create();
	if(FAILED(hr))
		throw CActionError(hr, IDS_BACKUP_ERR_CREATE_DEST, m_dest);

	// This could take a while so message
	Update(hwndUpdates, _T("Listing files to backup..."), NULL);

	// Iterate through source folders
	string_array::const_iterator iterSrc;
	for(iterSrc = m_src.begin(); iterSrc != m_src.end(); iterSrc++)
	{
		Update(hwndUpdates, NULL, (LPCTSTR)*iterSrc);

		// Variables Used below
		file_path srcPath = *iterSrc;
		file_path srcFile;
		file_path destFile;

		file_iterator fileIter(srcPath.files_begin(file_iterator::sub_folders));
		file_iterator end;

		for( ; fileIter != end; fileIter++)
		{
			// Check if it has the ARCHIVE attribute set
			if(!(fileIter->dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE))
				continue;

			if(fileIter->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				continue;

			// Make File Paths
			srcFile = *iterSrc;

			// Destination is root destination + same folder structure as 
			// original folder
			destFile = m_dest;

			if(srcFile.attributes() & FILE_ATTRIBUTE_DIRECTORY)
				destFile += *(srcFile.end(false) - 1);

			destFile += fileIter->cFileName;

			srcFile = srcFile.folder() + fileIter->cFileName;


			// Make sure extension is not in list
			if(m_asIgnore.find(srcFile.ext().make_lower()) == m_asIgnore.end())
			{
				// Add the file
				ArchiveCopy* pCopy = new ArchiveCopy(srcFile, destFile);
				pEngine->Add(pCopy);
			}
		}
		
		// Error from file_iterator (listing folders)
		if(fileIter.state() != ERROR_SUCCESS)
			throw CActionError(HRESULT_FROM_WIN32(fileIter.state()), IDS_BACKUP_ERR_LIST, (file_path)*iterSrc);
	}

	return false;
}

HRESULT CBackupActions::BasePrepare::Initialize(const CPropertyBag& data)
{
	m_dest = data.GetString(NS_BACKUP_REG_DEST, _T(""));
	data.GetStringSet(NS_BACKUP_REG_SOURCE, m_src);

	string_array asTemp;
	data.GetStringSet(NS_IGNORE_REG_EXT, asTemp);

	// Make them all lower
	for_each(asTemp.begin(), asTemp.end(), Lower);

	// And copy them to the set (removes doubles)
	copy(asTemp.begin(), asTemp.end(), inserter(m_asIgnore, m_asIgnore.begin()));

	// Remove any blanks from ignore
	m_asIgnore.erase(_T(""));

	if(!m_dest.valid())
		return E_INVALIDARG;

	return S_OK;
}

#define FILETIME_TO_64(ft)						\
	((__int64) (ft.dwLowDateTime | ((__int64)(ft.dwHighDateTime  << 32))))


bool CBackupActions::DatePrepare::Do(CActionEngine* pEngine, 
	  					 		     HWND hwndUpdates)
{
	// Some Checks
	ASSERT(pEngine != NULL);

	if(!m_dest.valid())
		throw CActionError(E_INVALIDARG, IDS_BACKUP_ERR_DEST, m_dest);

	// Create destination folder
	HRESULT hr = m_dest.create();
	if(FAILED(hr))
		throw CActionError(hr, IDS_BACKUP_ERR_CREATE_DEST, m_dest);

	// This could take a while so message
	Update(hwndUpdates, _T("Comparing dates on backup files..."), NULL);

	HANDLE hFile;
	FILETIME ftModify;

	// Iterate through source folders
	string_array::const_iterator iterSrc;
	for(iterSrc = m_src.begin(); iterSrc != m_src.end(); iterSrc++)
	{
		Update(hwndUpdates, NULL, (LPCTSTR)*iterSrc);

		// Variables Used below
		file_path srcPath = *iterSrc;
		file_path srcFile;
		file_path destFile;

		file_iterator fileIter(srcPath.files_begin(file_iterator::sub_folders));
		file_iterator end;

		for( ; fileIter != end; fileIter++)
		{
			if(fileIter->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				continue;

			// Make File Paths
			srcFile = *iterSrc;

			// Destination is root destination + same folder structure as 
			// original folder
			destFile = m_dest;

			if(srcFile.attributes() & FILE_ATTRIBUTE_DIRECTORY)
				destFile += *(srcFile.end(false) - 1);

			destFile += fileIter->cFileName;

			srcFile = srcFile.folder() + fileIter->cFileName;


			// Make sure extension is not in list
			if(m_asIgnore.find(srcFile.ext().make_lower()) != m_asIgnore.end())
				continue;

			// Open the file
			hFile = CreateFile(destFile, GENERIC_READ, FILE_SHARE_READ, NULL, 
				               OPEN_EXISTING, 0, NULL);

			// Check return Value
			if(hFile == INVALID_HANDLE_VALUE)
			{
				// If no file then Backup
				switch(::GetLastError())
				{
				case ERROR_FILE_NOT_FOUND:
				case ERROR_PATH_NOT_FOUND:
					break;
				default:
					throw CActionError(HRESULT_FROM_WIN32(::GetLastError()), IDS_BACKUP_ERR_FILE, m_dest);
				}
			}
			else
			{
				// Get the filetime
				ftModify.dwHighDateTime = 0;
				ftModify.dwLowDateTime = 0;

				if(!GetFileTime(hFile, NULL, NULL, &ftModify))
					throw CActionError(HRESULT_FROM_WIN32(::GetLastError()), IDS_BACKUP_ERR_FILE, destFile);

				CloseHandle(hFile);

				// Are we newer?
				if(FILETIME_TO_64(fileIter->ftLastWriteTime) <= FILETIME_TO_64(ftModify))
					continue;
			}

			// Add the file
			ArchiveCopy* pCopy = new ArchiveCopy(srcFile, destFile);
			pEngine->Add(pCopy);
			
		}
		
		// Error from file_iterator (listing folders)
		if(fileIter.state() != ERROR_SUCCESS)
			throw CActionError(HRESULT_FROM_WIN32(fileIter.state()), IDS_BACKUP_ERR_LIST, (file_path)*iterSrc);
	}

	return false;
}

bool CBackupActions::ArchiveCopy::Do(CActionEngine* pEngine, 
	  						         HWND hwndUpdates)
{
start:
	try
	{
		// Update any monitors
		Update(hwndUpdates, _T("Copying..."), m_src);

		// Create the folder this file is in
		HRESULT hr = m_dest.folder().create();
		if(FAILED(hr))
			throw CActionError(hr, IDS_BACKUP_ERR_CREATE_DEST, m_dest.folder());

		// SetAttributes of the file to make sure it's not read-only
		// when read-only CopyFile fails (CopyFile copies attributes)
		::SetFileAttributes(m_dest, FILE_ATTRIBUTE_NORMAL);

		// Copy. Don't worry about overwriting old files
		if(!CopyFile(m_src, m_dest, FALSE))
			throw CActionError(HRESULT_FROM_WIN32(::GetLastError()), IDS_BACKUP_ERR_COPY, m_src);
		
		// Remove the Archive Attribute of the file
		SetFileAttributes(m_src, m_src.attributes() ^ FILE_ATTRIBUTE_ARCHIVE);

		return false;
	}
	catch(CActionError& err)
	{
		switch(err.m_hRes)
		{
		case ERROR_HANDLE_DISK_FULL:
		case ERROR_DISK_FULL:
		case ERROR_DESTINATION_ELEMENT_FULL:
		case STG_E_MEDIUMFULL:
			{
				string sTemp;
				sTemp.format(IDS_BACKUP_ERR_DISKFULL, (LPCTSTR)m_src.root());

				if(IDYES == MessageBoxTimeout(hwndUpdates, sTemp, _T("Secure Shutdown"), MB_OKCANCEL | MB_ICONQUESTION, 15000, 5000))
					break;
			}
		default:
			throw;
		}

	}

	goto start;
}

bool CBackupActions::DateCopy::Do(CActionEngine* pEngine, 
	  					          HWND hwndUpdates)
{
	// Update any monitors
	Update(hwndUpdates, _T("Copying..."), m_src);

	// Create the folder this file is in
	HRESULT hr = m_dest.folder().create();
	if(FAILED(hr))
		throw CActionError(hr, IDS_BACKUP_ERR_CREATE_DEST, m_dest.folder());

	// SetAttributes of the file to make sure it's not read-only
	// when read-only CopyFile fails (CopyFile copies attributes)
	::SetFileAttributes(m_dest, FILE_ATTRIBUTE_NORMAL);

	// Copy. Don't worry about overwriting old files
	if(!CopyFile(m_src, m_dest, FALSE))
		throw CActionError(HRESULT_FROM_WIN32(::GetLastError()), IDS_BACKUP_ERR_COPY, m_src);
	
	return false;

	// We don't try to catch disk full errors (and prompt for another disk)
	// because that doesn't make any sense in a date based backup
}