aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_disc/copyfiles.cpp
blob: 426bd79d9f3b743b404b2647035377438aa11e58 (plain) (blame)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#include "main.h"
#include "./copyfiles.h"
#include "./copyinternal.h"
#include "./resource.h"
#include "./settings.h"
#include "../nu/trace.h"
#include <api/service/waServiceFactory.h>
#include <shlwapi.h>
#include <strsafe.h>

static LONG szBusyDrive[26] = {0, };
static CRITICAL_SECTION	cs_copy = { 0,}; 

static void NotifyDialog(COPYDATA *pcd, UINT uTask, UINT uOperation, LPARAM lParam)
{
	if(pcd->hDialog) PostMessage(pcd->hDialog, CFM_NOTIFY, MAKEWPARAM(uTask, uOperation), lParam);
}

static INT_PTR QueryDialog(COPYDATA *pcd, UINT uTask, UINT uOperation, LPARAM lParam)
{
	return (pcd->hDialog) ? SendMessage(pcd->hDialog, CFM_NOTIFY, MAKEWPARAM(uTask, uOperation), lParam) : FALSE;
}

static void CopyFiles_MarkDrivesBusy(LPCTSTR *ppsz, INT count, BOOL bBusy)
{
	INT i, n;
	DWORD driveMask = 0x00;

	EnterCriticalSection(&cs_copy);
	if (bBusy)
	{
		for(i = 0; i < count; i++)
		{
			n = PathGetDriveNumber(ppsz[i]);
			if (-1 != n) 
			{
				if (0 == szBusyDrive[n]) driveMask |= (((DWORD)0x01) << n);
				szBusyDrive[n]++;
			}
		}
	}
	else
	{
		for(i = 0; i < count; i++)
		{
			n = PathGetDriveNumber(ppsz[i]);
			if (-1 != n) 
			{
				if (szBusyDrive[n] <= 0) continue;
				szBusyDrive[n]--;
				if (0 == szBusyDrive[n]) driveMask |= (((DWORD)0x01) << n);
			}
		}
	}
	LeaveCriticalSection(&cs_copy);

	if (0x00 != driveMask)
	{
		static UINT uMsgCopyNotify = 0;
		if (!uMsgCopyNotify) uMsgCopyNotify = RegisterWindowMessageA("WACOPY_BROADCAST_MSG");
		if (uMsgCopyNotify)
		{
			for (CHAR c = 'A'; 0x00 != driveMask; c++, driveMask>>=1)
			{
				if (0 == (0x01 & driveMask)) continue;
				if (bBusy) SendNotifyMessage(HWND_BROADCAST, uMsgCopyNotify, MAKEWPARAM(c, 0), (LPARAM)TRUE);
				else 
				{
					SendNotifyMessage(HWND_BROADCAST, uMsgCopyNotify, MAKEWPARAM(c, 0), (LPARAM)FALSE);
					SendNotifyMessage(HWND_BROADCAST, uMsgCopyNotify, MAKEWPARAM(0, 0xffff), (LPARAM)plugin.hwndWinampParent);
				}
			}
		}
	}
}
static BOOL ReadCopyParameters(COPYDATA *pcd)
{
	BOOL bVal;
	HRESULT hr = S_OK;
	pcd->uFlags = 0;
	if (S_OK == hr) hr = Settings_ReadString(C_COPY, CF_PATH, pcd->szDestination, ARRAYSIZE(pcd->szDestination));
	if (S_OK == hr) hr = Settings_ReadString(C_COPY, CF_TITLEFMT, pcd->szTitleFormat, ARRAYSIZE(pcd->szTitleFormat));
	if (S_OK == hr && S_OK == (hr = Settings_GetBool(C_COPY, CF_ADDTOMLDB, &bVal)) && bVal) pcd->uFlags |= FCF_ADDTOMLDB;
	if (S_OK == hr && S_OK == (hr = Settings_GetBool(C_COPY, CF_USETITLEFMT, &bVal)) && bVal) pcd->uFlags |= FCF_USETITLEFMT;
	CleanupDirectoryString(pcd->szDestination);

	return (S_OK == hr);

}

void MLDisc_InitializeCopyData()
{
	InitializeCriticalSection(&cs_copy);
}

void MLDisc_ReleaseCopyData()
{
	DeleteCriticalSection(&cs_copy);
}

BOOL MLDisc_IsDiscCopying(CHAR cLetter)
{
	if (cLetter >= 'a' && cLetter <= 'z') cLetter -= 0x20;
	return (cLetter >= 'A' && cLetter <= 'Z' && szBusyDrive[cLetter - 'A'] > 0);
}


BOOL MLDisc_CopyFiles(HWND hParent, LPWSTR *ppszFiles, ULONGLONG *pFSizes, INT count)
{
	if (!ppszFiles || !count) return FALSE;
	if (NULL == hParent) hParent = plugin.hwndLibraryParent;

	COPYDATA *pcd = (COPYDATA*)CoTaskMemAlloc(sizeof(COPYDATA));
	if (!pcd) return FALSE;
	ZeroMemory(pcd, sizeof(COPYDATA));

	CopyFiles_AddRef(pcd);

	pcd->hOwner = hParent;
	pcd->ppszFiles = ppszFiles;
	pcd->pFSizes = pFSizes;
	pcd->count = count;
	pcd->bCancel = FALSE;

	waServiceFactory *factory = plugin.service->service_getServiceByGuid(api_metadataGUID);
	if (factory) pcd->pMetaReader = (api_metadata*) factory->getInterface();
	factory = plugin.service->service_getServiceByGuid(mldbApiGuid);
	if (factory) pcd->pMlDb = (api_mldb*) factory->getInterface();

	HWND hRoot = GetAncestor(hParent, GA_ROOT);
	if (NULL == hRoot) hRoot = hParent;
	
	INT_PTR result = WASABI_API_DIALOGBOXPARAMW(IDD_FILECOPY_PREPARE, hRoot, CopyPrepare_DialogProc, (LPARAM)pcd);
	if (IDCANCEL != result)
	{
		if (ReadCopyParameters(pcd))
		{
			HWND hCopy = WASABI_API_CREATEDIALOGPARAMW(IDD_FILECOPY_PROGRESS, hRoot, CopyProgress_DialogProc, (LPARAM)pcd);
			if (hCopy)
			{
				pcd->hDialog = hCopy;
				ShowWindow(hCopy, SW_SHOWNORMAL);
				RedrawWindow(hCopy, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
			}
			CopyFiles_StartCopy(pcd);
		}
	}

	CopyFiles_Release(pcd);
	return TRUE;
}

BOOL CopyFiles_CancelCopy(COPYDATA *pcd)
{
	if (pcd) pcd->bCancel = TRUE;
	return (NULL != pcd);
}

LONG CopyFiles_AddRef(COPYDATA *pcd)
{
	return (pcd) ? InterlockedIncrement(&pcd->ref) : 0;

}
LONG CopyFiles_Release(COPYDATA *pcd)
{	
	if (pcd && pcd->ref > 0)
	{
		LONG r = InterlockedDecrement(&pcd->ref);
        if ( 0 == r)
		{
			if (pcd->ppszFiles)
			{
				for (int i = 0; i < pcd->count; i++) CoTaskMemFree(pcd->ppszFiles[i]);
				CoTaskMemFree(pcd->ppszFiles);
			}
			if (pcd->pFSizes) CoTaskMemFree(pcd->pFSizes);
			if (pcd->hThread) CloseHandle(pcd->hThread);

			if(pcd->pMetaReader)
			{
				waServiceFactory *factory = plugin.service->service_getServiceByGuid(api_metadataGUID);
				if (factory) factory->releaseInterface(pcd->pMetaReader);
			}
			if(pcd->pMlDb)
			{				
				waServiceFactory *factory = plugin.service->service_getServiceByGuid(mldbApiGuid);
				if (factory) factory->releaseInterface(pcd->pMlDb);
			}
			CoTaskMemFree(pcd);
		}
		return r;
	}
	return 0;
}

static ULONGLONG CopyFiles_CalculateTotalSize(COPYDATA *pcd)
{
	ULONGLONG total = 0;
	if (!pcd->pFSizes) 
	{
		pcd->pFSizes = (ULONGLONG*)CoTaskMemAlloc(sizeof(ULONGLONG)*pcd->count);
		if (!pcd->pFSizes)
		{
			pcd->errorCode = ERROR_NOT_ENOUGH_MEMORY;
			return 0;
		}
		LARGE_INTEGER fs;
		for(int i = 0; i < pcd->count; i++)
		{
			HANDLE hFile = CreateFile(pcd->ppszFiles[i], FILE_READ_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
			if (INVALID_HANDLE_VALUE == hFile) 
			{
				pcd->errorCode = GetLastError();
				return 0;
			}
			if (!GetFileSizeEx(hFile, &fs)) { pcd->errorCode = GetLastError(); CloseHandle(hFile); return 0; }
			CloseHandle(hFile);
			pcd->pFSizes[i] = fs.QuadPart;
		}
	}
	for(int i = 0; i < pcd->count; i++) total += pcd->pFSizes[i];
	return total;
}

BOOL CopyFiles_CreateDirectory(LPCTSTR pszDirectory)
{
	DWORD ec = ERROR_SUCCESS;
	if (!CreateDirectory(pszDirectory, NULL))
	{
		ec = GetLastError();
		if (ERROR_PATH_NOT_FOUND == ec)
		{
			LPCTSTR pszBlock = pszDirectory;
			TCHAR szBuffer[MAX_PATH] = {0};
			
			LPCTSTR pszCursor = PathFindNextComponent(pszBlock);
			ec = (pszCursor == pszBlock || S_OK != StringCchCopyN(szBuffer, ARRAYSIZE(szBuffer), pszBlock, (pszCursor - pszBlock))) ?
					ERROR_INVALID_NAME : ERROR_SUCCESS;
			
			pszBlock = pszCursor;
			
			while (ERROR_SUCCESS == ec && NULL != (pszCursor = PathFindNextComponent(pszBlock)))
			{
				if (pszCursor == pszBlock || S_OK != StringCchCatN(szBuffer, ARRAYSIZE(szBuffer), pszBlock, (pszCursor - pszBlock)))
					ec = ERROR_INVALID_NAME;

				if (ERROR_SUCCESS == ec && !CreateDirectory(szBuffer, NULL))
				{
					ec = GetLastError();
					if (ERROR_ALREADY_EXISTS == ec) ec = ERROR_SUCCESS;
				}
				pszBlock = pszCursor;
			}
		}

		if (ERROR_ALREADY_EXISTS == ec) ec = ERROR_SUCCESS;
	}
	SetLastError(ec);
	return (ERROR_SUCCESS == ec);
}


static BOOL CopyFiles_CheckDestination(COPYDATA *pcd, ULONGLONG needSize)
{
	TCHAR szRoot[MAX_PATH] = {0};
	if (S_OK != StringCchCopy(szRoot, ARRAYSIZE(szRoot), pcd->szDestination))
	{
		pcd->errorCode = ERROR_OUTOFMEMORY;
		return FALSE;
	}
	PathStripToRoot(szRoot);
	ULARGE_INTEGER free, total;
	if (!GetDiskFreeSpaceEx(szRoot, &free, &total, NULL))
	{
		pcd->errorCode = GetLastError();
		return FALSE;
	}
	if (needSize > free.QuadPart)  
	{
		pcd->errorCode = ERROR_DISK_FULL;
		return FALSE;
	}

	if (INVALID_FILE_ATTRIBUTES == GetFileAttributes(pcd->szDestination))
	{
		DWORD ec = GetLastError();
		if (ERROR_PATH_NOT_FOUND == ec || ERROR_FILE_NOT_FOUND == ec)
		{
			if (TRUE == QueryDialog(pcd, CFT_CONFLICT, CFO_DESTNOTEXIST, (LPARAM)pcd->szDestination))
				pcd->errorCode = ERROR_REQUEST_ABORTED;
		}
		else pcd->errorCode = ec;
		
		if (ERROR_SUCCESS != pcd->errorCode)
		{
			pcd->errorMsgId = IDS_COPY_ERRMSG_DIRECTORYCREATE_FAILED;
			return FALSE;
		}
		
	}

	if (!CopyFiles_CreateDirectory(pcd->szDestination))
	{
		pcd->errorMsgId = IDS_COPY_ERRMSG_DIRECTORYCREATE_FAILED;
		pcd->errorCode = ERROR_CANNOT_MAKE;
		return FALSE;
	}
	return TRUE;
}

static BOOL CopyFiles_FixCdAttributes(LPCTSTR pszFileName)
{
	DWORD attr = GetFileAttributes(pszFileName);
	if (INVALID_FILE_ATTRIBUTES == attr) return FALSE;
	return SetFileAttributes(pszFileName, (attr & ~FILE_ATTRIBUTE_READONLY) | FILE_ATTRIBUTE_ARCHIVE);
}



static BOOL CopyFiles_DeleteFile(LPCTSTR pszFileName, COPYDATA *pcd)
{
	DWORD attr = GetFileAttributes(pszFileName);
	if (INVALID_FILE_ATTRIBUTES == attr) return FALSE;
	if (FILE_ATTRIBUTE_READONLY & attr)
	{
		BOOL bReset = FALSE;
		if (FCF_DELETEREADONLY & pcd->uFlags) bReset = TRUE;
		else 
		{
			INT_PTR r = QueryDialog(pcd, CFT_CONFLICT, CFO_READONLY, (LPARAM)pszFileName);
			switch(r)
			{
				case READONLY_CANCELCOPY:	
					pcd->errorMsgId = IDS_COPY_ERRMSG_COPYFILE_USERABORT;
					SetLastError(ERROR_REQUEST_ABORTED);
					return FALSE;
				case READONLY_DELETEALL:		pcd->uFlags |= FCF_DELETEREADONLY; // no break
				case READONLY_DELETE:		bReset = TRUE; break;
			}
		}

		if (bReset)
		{
			if (!SetFileAttributes(pszFileName, (attr & ~FILE_ATTRIBUTE_READONLY)))
			return FALSE;
		}
	}
	return DeleteFile(pszFileName);
}

#define NOT_EXIST			0
#define EXIST_AND_SKIP		1
#define EXIST_AND_OVERWRITE 	2
#define EXIST_AND_CANCEL 	3

static DWORD CopyFiles_CheckIfExist(LPCTSTR pszFileNameDest, LPCTSTR pszFileNameSource, COPYDATA *pcd)
{
    if (INVALID_FILE_ATTRIBUTES == GetFileAttributes(pszFileNameDest))
	{
		return NOT_EXIST;
	}
	

	if (FCF_SKIPFILE & pcd->uFlags) return EXIST_AND_SKIP;
	if (FCF_OVERWRITEFILE & pcd->uFlags) return EXIST_AND_OVERWRITE;

	FILECONFLICT conflict;
	ZeroMemory(&conflict, sizeof(FILECONFLICT));

	pcd->uFlags &= ~(FCF_SKIPFILE | FCF_OVERWRITEFILE);

	conflict.pszNameExisting = pszFileNameDest;
	conflict.pszNameNew	= pszFileNameSource;
		
	INT_PTR  r = QueryDialog(pcd, CFT_CONFLICT, CFO_FILEALREDYEXIST, (LPARAM)&conflict);
		
	switch(0xFF & r)
	{
		case EXISTFILE_CANCELCOPY:	return EXIST_AND_CANCEL;
		case EXISTFILE_SKIP:		
			if (EXISTFILE_APPLY_TO_ALL & r) pcd->uFlags |= FCF_SKIPFILE;
			return EXIST_AND_SKIP;
		case EXISTFILE_OVERWRITE:	
			if (EXISTFILE_APPLY_TO_ALL & r) pcd->uFlags |= FCF_OVERWRITEFILE;
			return EXIST_AND_OVERWRITE;
	}

	return NOT_EXIST;
}

typedef struct _COPYPROGRESS
{
	ULONGLONG total;
	ULONGLONG completed;
	HWND hDialog;
	INT percent;
} COPYPROGRESS;

static DWORD CALLBACK CopyFiles_ProgressRoutine(LARGE_INTEGER TotalFileSize, LARGE_INTEGER TotalBytesTransferred, LARGE_INTEGER StreamSize,
												LARGE_INTEGER StreamBytesTransferred, DWORD dwStreamNumber, DWORD dwCallbackReason, 
												HANDLE hSourceFile, HANDLE hDestinationFile, LPVOID lpData)
{
	COPYPROGRESS *pProgress = (COPYPROGRESS*)lpData;
	if (pProgress)
	{
		switch(dwCallbackReason)
		{
			case CALLBACK_STREAM_SWITCH:
				pProgress->completed += TotalFileSize.QuadPart;
				break;

		}
		INT p = (INT)((pProgress->completed - TotalFileSize.QuadPart + TotalBytesTransferred.QuadPart) * 100 / pProgress->total);
		if (p != pProgress->percent)
		{
			pProgress->percent = p;
			if (pProgress->hDialog) 
				PostMessage(pProgress->hDialog, CFM_NOTIFY, MAKEWPARAM(CFT_COPYING, CFO_PROGRESS), p);
		}
	}
	return PROGRESS_CONTINUE;
};

static DWORD WINAPI CopyFiles_ThreadProc(LPVOID param)
{
	
	COPYDATA *pcd = (COPYDATA*)param;
	if (!pcd) return 1;
	
	CopyFiles_MarkDrivesBusy((LPCTSTR*)pcd->ppszFiles, pcd->count, TRUE);

	ULONGLONG needSize;
	NotifyDialog(pcd, CFT_INITIALIZING, CFO_INIT, 0L);
	NotifyDialog(pcd, CFT_INITIALIZING, CFO_CACLSIZE, 0L);
	needSize = CopyFiles_CalculateTotalSize(pcd);
	BOOL bSuccess = (ERROR_SUCCESS == pcd->errorCode);
	if (bSuccess)
	{
		NotifyDialog(pcd, CFT_INITIALIZING, CFO_CHECKDESTINATION, 0L);
		bSuccess = CopyFiles_CheckDestination(pcd, needSize);
	}

	if (!bSuccess) 
	{
		if (0 == pcd->errorMsgId) 
			pcd->errorMsgId = IDS_COPY_ERRMSG_INITIALIZATION_FAILED;
		NotifyDialog(pcd, CFT_FINISHED, CFO_FAILED, pcd->errorCode);
		CopyFiles_MarkDrivesBusy((LPCTSTR*)pcd->ppszFiles, pcd->count, FALSE);
		CopyFiles_Release(pcd);
		return 0;
	}

	COPYPROGRESS progress;
	ZeroMemory(&progress, sizeof(COPYPROGRESS));
	progress.total = needSize;
	progress.hDialog = pcd->hDialog;

	NotifyDialog(pcd, CFT_COPYING, CFO_INIT, 0L);

	TCHAR szFile[MAX_PATH] = {0};

	for (int i = 0; i < pcd->count && ERROR_SUCCESS == pcd->errorCode; i++)
	{	
		if (pcd->bCancel)
		{
			pcd->errorMsgId = IDS_COPY_ERRMSG_COPYFILE_USERABORT;
			pcd->errorCode = ERROR_REQUEST_ABORTED;
		}

		LPCTSTR pszOrigFileName = PathFindFileName(pcd->ppszFiles[i]);
		
		NotifyDialog(pcd, CFT_COPYING, CFO_NEXTFILE, MAKELPARAM(i, pcd->count));
		
		if (NULL == PathCombine(szFile, pcd->szDestination, pszOrigFileName))
		{
			pcd->errorMsgId = IDS_COPY_ERRMSG_COPYFILE_FAILED;
			pcd->errorCode = ERROR_BAD_PATHNAME;
		}

		DWORD r = CopyFiles_CheckIfExist(szFile, pcd->ppszFiles[i], pcd);
		switch(r)
		{			
			case EXIST_AND_SKIP:
				continue;
				break;
			case EXIST_AND_OVERWRITE:
				if (!CopyFiles_DeleteFile(szFile, pcd))
				{
					pcd->errorMsgId = IDS_COPY_ERRMSG_DELETEFILE_FAILED;
					pcd->errorCode = GetLastError();
				}
				break;
			case EXIST_AND_CANCEL:
				pcd->errorMsgId = IDS_COPY_ERRMSG_COPYFILE_USERABORT;
				pcd->errorCode = ERROR_REQUEST_ABORTED;
				break;
		}	

		// copy
		if (ERROR_SUCCESS == pcd->errorCode && !CopyFileEx(pcd->ppszFiles[i], szFile, CopyFiles_ProgressRoutine, &progress, &pcd->bCancel, 0))
		{
			pcd->errorMsgId = IDS_COPY_ERRMSG_COPYFILE_FAILED;
			pcd->errorCode = GetLastError();
		}

		if (pcd->bCancel)
		{
			pcd->errorMsgId = IDS_COPY_ERRMSG_COPYFILE_USERABORT;
			pcd->errorCode = ERROR_REQUEST_ABORTED;
		}
		
		if (ERROR_SUCCESS == pcd->errorCode) // post copy
		{
			// fix attributes
			if (ERROR_SUCCESS == pcd->errorCode && !CopyFiles_FixCdAttributes(szFile))
			{
				pcd->errorMsgId = IDS_COPY_ERRMSG_SETATTRIBUTES_FAILED;
				pcd->errorCode = GetLastError();
			}

			// format title & rename
			if (ERROR_SUCCESS == pcd->errorCode && (FCF_USETITLEFMT & pcd->uFlags) && pcd->pMetaReader)
			{				
				TCHAR szBuffer[MAX_PATH] = {0};
				if (!CopyFiles_FormatFileName(szBuffer, ARRAYSIZE(szBuffer), szFile, pszOrigFileName, 
						pcd->szDestination, pcd->szTitleFormat, pcd->pMetaReader))
				{
					pcd->errorMsgId = IDS_COPY_ERRMSG_TITLEFORMAT_FAILED;
					pcd->errorCode = GetLastError();
				}

				if (pcd->bCancel)
				{
					pcd->errorMsgId = IDS_COPY_ERRMSG_COPYFILE_USERABORT;
					pcd->errorCode = ERROR_REQUEST_ABORTED;
				}
				
				if (ERROR_SUCCESS == pcd->errorCode && 
					CSTR_EQUAL != CompareString(STRCOMP_INVARIANT, 0, szBuffer, -1, szFile, -1))
				{

					DWORD r = CopyFiles_CheckIfExist(szBuffer, szFile, pcd);
					switch(r)
					{			
						case EXIST_AND_SKIP:
							CopyFiles_DeleteFile(szFile, pcd);
							continue;
							break;
						case EXIST_AND_OVERWRITE:
							if (!CopyFiles_DeleteFile(szBuffer, pcd))
							{
								pcd->errorMsgId = IDS_COPY_ERRMSG_DELETEFILE_FAILED;
								pcd->errorCode = GetLastError();
							}
							break;
						case EXIST_AND_CANCEL:
							pcd->errorMsgId = IDS_COPY_ERRMSG_COPYFILE_USERABORT;
							pcd->errorCode = ERROR_REQUEST_ABORTED;
							break;
					}	

					if (ERROR_SUCCESS == pcd->errorCode)
					{
						if (!MoveFileEx(szFile, szBuffer, MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED)) 
						{
							pcd->errorMsgId = IDS_COPY_ERRMSG_TITLEFORMAT_FAILED;
							pcd->errorCode = GetLastError();
						}
						else StringCchCopy(szFile, ARRAYSIZE(szFile), szBuffer);
					}
					
				}
			}
			
			
			if (ERROR_SUCCESS != pcd->errorCode)
			{
				CopyFiles_DeleteFile(szFile, pcd);
			}
			else if ((FCF_ADDTOMLDB & pcd->uFlags) && pcd->pMlDb)
			{
				if (0 == pcd->pMlDb->AddFile(szFile))
				{
					pcd->errorMsgId = IDS_COPY_ERRMSG_ADDTOMLDB_FAILED;
					pcd->errorCode = ERROR_FILE_NOT_FOUND;
				}
			}
		}
	
	}
	  
	NotifyDialog(pcd, CFT_FINISHED, (ERROR_SUCCESS == pcd->errorCode) ? CFO_SUCCESS : CFO_FAILED, pcd->errorCode);
	if ((FCF_ADDTOMLDB & pcd->uFlags) && pcd->pMlDb) pcd->pMlDb->Sync();
	CopyFiles_MarkDrivesBusy((LPCTSTR*)pcd->ppszFiles, pcd->count, FALSE);
	CopyFiles_Release(pcd);
	return 0;
}

BOOL CopyFiles_StartCopy(COPYDATA *pcd)
{
	DWORD threadId;
	
	CopyFiles_AddRef(pcd);
	
	pcd->hThread = CreateThread(NULL, 0, CopyFiles_ThreadProc, pcd, 0, &threadId);
	if (pcd->hThread) return TRUE;
	

	pcd->errorCode = GetLastError();
	NotifyDialog(pcd, CFT_FINISHED, CFO_FAILED, pcd->errorCode);
	CopyFiles_Release(pcd);
	return FALSE;
}