forked from uroni/urbackup_frontend_wx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RestoreWindowsComponents.cpp
1090 lines (902 loc) · 29 KB
/
RestoreWindowsComponents.cpp
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "RestoreWindowsComponents.h"
#include "stringtools.h"
#include "Connector.h"
#include "SelectRestoreWindowsComponents.h"
#include "TranslationHelper.h"
#include "md5.h"
namespace
{
std::string sortHex(UINT i)
{
UINT bi = big_endian(i);
std::string ret = bytesToHex(reinterpret_cast<unsigned char*>(&bi), sizeof(bi));
strupper(&ret);
return ret;
}
class ScopedServiceHandle
{
public:
ScopedServiceHandle(SC_HANDLE& h)
: h(h)
{}
~ScopedServiceHandle() {
if (h != NULL) {
CloseServiceHandle(h);
}
}
private:
SC_HANDLE& h;
};
#define SCOPED_DECLARE_RELEASE_SERVICE_HANDLE(x) SC_HANDLE x = NULL; ScopedServiceHandle TOKENPASTE(ScopedServiceHandle_, __LINE__) (x)
}
RestoreWindowsComponentsThread::RestoreWindowsComponentsThread(int backupid, std::vector<SComponent> selected_components, wxString componentConfigDir)
: wxThread(wxTHREAD_JOINABLE), backupid(backupid), selected_components(selected_components), componentConfigDir(componentConfigDir), progress_bar_pc(-1)
{
}
RestoreWindowsComponentsThread::~RestoreWindowsComponentsThread()
{
}
void RestoreWindowsComponentsThread::log(const std::string & message)
{
std::string fullmsg = wxDateTime::Now().Format().ToStdString() + ": " + message;
wxCriticalSectionLocker lock(log_section);
log_messages.push_back(fullmsg);
}
void RestoreWindowsComponentsThread::log(const wxString & message)
{
wxString fullmsg = wxDateTime::Now().Format() + ": " + message;
wxCriticalSectionLocker lock(log_section);
log_messages.push_back(fullmsg.ToStdString());
}
std::vector<std::string> RestoreWindowsComponentsThread::getNewLogMessages()
{
std::vector<std::string> ret;
wxCriticalSectionLocker lock(log_section);
ret = log_messages;
log_messages.clear();
return ret;
}
wxString RestoreWindowsComponentsThread::getMessage1()
{
wxCriticalSectionLocker lock(log_section);
return message1;
}
void RestoreWindowsComponentsThread::setMessage1(wxString msg)
{
wxCriticalSectionLocker lock(log_section);
message1 = msg;
}
void RestoreWindowsComponentsThread::setMessage2(wxString msg)
{
wxCriticalSectionLocker lock(log_section);
message2 = msg;
}
int RestoreWindowsComponentsThread::getProgressBarPc()
{
wxCriticalSectionLocker lock(log_section);
return progress_bar_pc;
}
void RestoreWindowsComponentsThread::setProgressBarPc(int pc)
{
wxCriticalSectionLocker lock(log_section);
progress_bar_pc = pc;
}
wxString RestoreWindowsComponentsThread::getMessage2()
{
wxCriticalSectionLocker lock(log_section);
return message2;
}
wxThread::ExitCode RestoreWindowsComponentsThread::Entry()
{
setMessage1(_("Preparing restore"));
log(_("Starting restore operation..."));
for (size_t i = 0; i < selected_components.size(); ++i)
{
selected_components[i].is_root = false;
}
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (hr != S_OK)
{
log("CoInitializeEx failed " + GetErrorHResErrStr(hr));
return NULL;
}
SCOPED_DECLARE_RELEASE_IUNKNOWN(IVssBackupComponents, backupcom);
hr = CreateVssBackupComponents(&backupcom);
if (hr != S_OK || backupcom == NULL)
{
log("Error creating backup components " + GetErrorHResErrStr(hr));
return NULL;
}
std::string restoreXml = getFile((componentConfigDir + "\\backupcom.xml").ToStdWstring());
hr = backupcom->InitializeForRestore(const_cast<BSTR>(ConvertToUnicode(restoreXml).c_str()));
if (hr != S_OK)
{
log("Error initializing components for restore " + GetErrorHResErrStr(hr));
return false;
}
SCOPED_DECLARE_RELEASE_IUNKNOWN(IVssAsync, pb_result);
hr = backupcom->GatherWriterMetadata(&pb_result);
if (hr != S_OK)
{
log("Error gathering writer metadata " + GetErrorHResErrStr(hr));
return false;
}
log("Collecting information about running writers...");
std::string werrmsg;
if (!WindowsComponentReader::wait_for(pb_result, "Gathering writer metadata failed", werrmsg))
{
log(werrmsg);
return false;
}
UINT nwriters;
hr = backupcom->GetWriterMetadataCount(&nwriters);
if (hr != S_OK)
{
log(std::string("Error getting writer metadata count"));
return false;
}
log("Selecting components to restore...");
std::vector<std::string> post_restart_services;
std::vector<std::string> stop_services;
for (UINT i = 0; i < nwriters; ++i)
{
VSS_ID writerInstance;
SCOPED_DECLARE_RELEASE_IUNKNOWN(IVssExamineWriterMetadata, writerMetadata);
hr = backupcom->GetWriterMetadata(i, &writerInstance, &writerMetadata);
if (hr != S_OK)
{
log("Getting metadata of writer " + convert(i) + " failed. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
VSS_ID instanceId;
VSS_ID writerId;
SCOPED_DECLARE_FREE_BSTR(writerName);
VSS_USAGE_TYPE usageType;
VSS_SOURCE_TYPE sourceType;
hr = writerMetadata->GetIdentity(&instanceId, &writerId, &writerName, &usageType, &sourceType);
if (hr != S_OK)
{
log("Getting identity of writer " + convert(i) + " failed. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
bool writer_selected = false;
for (size_t j = 0; j < selected_components.size(); ++j)
{
if (selected_components[j].writerId == writerId)
{
writer_selected = true;
break;
}
}
if (!writer_selected)
continue;
//Always skip System writer and ASR writer
if (convert(writerId) == "{E8132975-6F93-4464-A53E-1050253AE220}"
|| convert(writerId) == "{BE000CBE-11FE-4426-9C58-531AA6355FC4}")
{
continue;
}
std::string writerData = getFile((componentConfigDir + "\\" + convert(writerId) + ".xml").ToStdWstring());
if (writerData.empty())
{
log("Cannot read writer config xml " + convert(writerId) + ".xml");
continue;
}
SCOPED_DECLARE_RELEASE_IUNKNOWN(IVssExamineWriterMetadata, writerMetadataFromBackup);
hr = CreateVssExamineWriterMetadata(const_cast<BSTR>(ConvertToUnicode(writerData).c_str()), &writerMetadataFromBackup);
if (hr != S_OK)
{
log("Getting metadata of writer " + convert(i) + " from XML failed. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
std::string writerNameStr = ConvertFromWchar(writerName);
UINT nIncludeFiles, nExcludeFiles, nComponents;
hr = writerMetadataFromBackup->GetFileCounts(&nIncludeFiles, &nExcludeFiles, &nComponents);
if (hr != S_OK)
{
log("GetFileCounts of writer \"" + writerNameStr + "\" failed. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
UINT nCurrIncludeFiles, nCurrExcludeFiles, nCurrComponents;
hr = writerMetadata->GetFileCounts(&nCurrIncludeFiles, &nCurrExcludeFiles, &nCurrComponents);
if (hr != S_OK)
{
log("GetFileCounts of writer \"" + writerNameStr + "\" failed -2. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
VSS_RESTOREMETHOD_ENUM method;
SCOPED_DECLARE_FREE_BSTR(service);
SCOPED_DECLARE_FREE_BSTR(userProcedure);
VSS_WRITERRESTORE_ENUM writerRestore;
bool rebootRequired = false;
UINT nMappings;
hr = writerMetadataFromBackup->GetRestoreMethod(&method, &service, &userProcedure, &writerRestore, &rebootRequired, &nMappings);
if (hr != S_OK)
{
log("GetRestoreMethod of writer \"" + writerNameStr + "\" failed. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
UINT nWriterComponents;
hr = backupcom->GetWriterComponentsCount(&nWriterComponents);
if (hr != S_OK)
{
log("Error getting writer components failed. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
SCOPED_DECLARE_RELEASE_IUNKNOWN(IVssWriterComponentsExt, writerComponents);
for (UINT j = 0; j < nWriterComponents; ++j)
{
SCOPED_DECLARE_RELEASE_IUNKNOWN(IVssWriterComponentsExt, writerComponentsLoc);
hr = backupcom->GetWriterComponents(j, &writerComponentsLoc);
if (hr != S_OK)
{
log("Error getting writer component " + convert(j) + " for writer \"" + writerNameStr + "\" failed. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
VSS_ID writerCompInstance;
VSS_ID writerCompId;
hr = writerComponentsLoc->GetWriterInfo(&writerCompInstance, &writerCompId);
if (hr != S_OK)
{
log("Error getting writer component info " + convert(j) + " for writer \"" + writerNameStr + "\" failed. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
if (writerCompId == writerId)
{
writerComponents = writerComponentsLoc;
writerComponentsLoc = NULL;
break;
}
}
std::vector<SFileSpec> redirected_locations;
for (UINT j = 0; j < nMappings; ++j)
{
SCOPED_DECLARE_RELEASE_IUNKNOWN(IVssWMFiledesc, wmFile);
hr = writerMetadataFromBackup->GetAlternateLocationMapping(j, &wmFile);
if (hr != S_OK)
{
log("Error getting alternate location mapping " + convert(j) + " of writer \"" + writerNameStr + "\" failed. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
SFileSpec spec;
if (!getFilespec(wmFile, spec))
{
continue;
}
redirected_locations.push_back(spec);
}
for (UINT j = 0; j < nComponents; ++j)
{
SCOPED_DECLARE_RELEASE_IUNKNOWN(IVssWMComponent, wmComponent);
hr = writerMetadataFromBackup->GetComponent(j, &wmComponent);
if (hr != S_OK)
{
log("Error getting component " + convert(j) + " of writer \"" + writerNameStr + "\" failed. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
SCOPED_DECLARE_FREE_COMPONENTINFO(wmComponent, componentInfo);
hr = wmComponent->GetComponentInfo(&componentInfo);
if (hr != S_OK)
{
log("Error getting component info " + convert(j) + " of writer \"" + writerNameStr + "\" failed. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
std::string componentNameStr = ConvertFromWchar(componentInfo->bstrComponentName);
std::string logicalPathStr;
if (componentInfo->bstrLogicalPath != NULL)
{
logicalPathStr = ConvertFromWchar(componentInfo->bstrLogicalPath);
}
bool restoreComponent = false;
for (size_t k = 0; k < selected_components.size(); ++k)
{
if (selected_components[k].writerId == writerId
&& selected_components[k].logicalPath == logicalPathStr
&& selected_components[k].name == componentNameStr)
{
restoreComponent = true;
selected_components[k].is_root = true;
break;
}
}
if (restoreComponent)
{
UINT writerComponentsCount;
hr = writerComponents->GetComponentCount(&writerComponentsCount);
if (hr != S_OK)
{
log("Error writer component count " + convert(j) + " of writer \"" + writerNameStr + "\" failed. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
bool hasCurrComponent = false;
for (UINT k = 0; k < writerComponentsCount; ++k)
{
SCOPED_DECLARE_RELEASE_IUNKNOWN(IVssComponent, vssComponent);
hr = writerComponents->GetComponent(k, &vssComponent);
if (hr != S_OK)
{
log("Error getting component " + convert(k) + " of writer \"" + writerNameStr + "\" failed -2. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
SCOPED_DECLARE_FREE_BSTR(bstrComponentName);
hr = vssComponent->GetComponentName(&bstrComponentName);
if (hr != S_OK)
{
log("Error getting component name " + convert(k) + " of writer \"" + writerNameStr + "\" failed -2. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
std::string currComponentNameStr = ConvertFromWchar(bstrComponentName);
SCOPED_DECLARE_FREE_BSTR(bstrLogicalPath);
vssComponent->GetLogicalPath(&bstrLogicalPath);
std::string currLogicalPathStr;
if (bstrLogicalPath != NULL)
{
currLogicalPathStr = ConvertFromWchar(bstrLogicalPath);
}
if (currComponentNameStr == componentNameStr
&& currLogicalPathStr == logicalPathStr)
{
VSS_RESTORE_TARGET target;
hr = vssComponent->GetRestoreTarget(&target);
if (hr != S_OK)
{
log("Error getting restore target of component \"" + componentNameStr + "\" of writer \"" + writerNameStr + "\" failed -2. VSS error code " + GetErrorHResErrStr(hr));
continue;
}
if (target != VSS_RT_ORIGINAL)
{
log("Alternate restore target is used. Not implemented.");
return false;
}
hasCurrComponent = true;
break;
}
}
if (hasCurrComponent)
{
hr = backupcom->SetSelectedForRestore(writerId, componentInfo->type,
componentInfo->bstrLogicalPath, componentInfo->bstrComponentName, true);
if (hr != S_OK)
{
log("Error selecting component \"" + componentNameStr + "\" with logical path \"" + logicalPathStr + "\" of writer \"" + writerNameStr + "\" for restore. VSS error code " + GetErrorHResErrStr(hr));
return false;
}
}
SRestoreComponent comp;
comp.componentIdx = j;
comp.componentName = componentNameStr;
comp.redirected_locations = redirected_locations;
comp.writerId = writerId;
comp.writerName = writerNameStr;
comp.logicalPath = logicalPathStr;
comp.type = componentInfo->type;
comp.hasComponent = hasCurrComponent;
if (method == VSS_RME_RESTORE_IF_NOT_THERE)
{
comp.restoreFlags = Connector::restore_flag_open_all_files_first | Connector::restore_flag_no_overwrite | Connector::restore_flag_mapping_is_alternative;
}
else if (method == VSS_RME_RESTORE_IF_CAN_REPLACE)
{
comp.restoreFlags = Connector::restore_flag_open_all_files_first | Connector::restore_flag_no_reboot_overwrite | Connector::restore_flag_ignore_overwrite_failures;
}
else if (method == VSS_RME_STOP_RESTORE_START
|| method == VSS_RME_RESTORE_TO_ALTERNATE_LOCATION
|| method == VSS_RME_RESTORE_STOP_START )
{
if (method == VSS_RME_STOP_RESTORE_START
&& service!=NULL)
{
stop_services.push_back(ConvertFromWchar(service));
}
else if (method == VSS_RME_RESTORE_STOP_START
&& service!=NULL)
{
post_restart_services.push_back(ConvertFromWchar(service));
}
comp.restoreFlags = Connector::restore_flag_no_reboot_overwrite;
}
else if (method == VSS_RME_RESTORE_AT_REBOOT)
{
comp.restoreFlags = Connector::restore_flag_reboot_overwrite_all;
}
else if (method == VSS_RME_RESTORE_AT_REBOOT_IF_CANNOT_REPLACE)
{
comp.restoreFlags = 0;
}
else
{
log("Unhandled restore method "+convert(method)+" of component \"" + componentNameStr + "\" with logical path \"" + logicalPathStr + "\" of writer \"" + writerNameStr + "\".");
return false;
}
for (UINT k = 0; k < componentInfo->cFileCount; ++k)
{
SCOPED_DECLARE_RELEASE_IUNKNOWN(IVssWMFiledesc, wmFile);
hr = wmComponent->GetFile(k, &wmFile);
if (hr != S_OK)
{
log("Error getting component file desc " + convert(k) + " of component \"" +
componentNameStr + "\" of writer \"" + writerNameStr + "\" failed. VSS error code " + GetErrorHResErrStr(hr));
return false;
}
if (!getFilespec(wmFile, comp.currspec))
{
return false;
}
comp.filesIdx = k;
comp.filesPrefix = "files";
restore_components.push_back(comp);
}
for (UINT k = 0; k < componentInfo->cDatabases; ++k)
{
SCOPED_DECLARE_RELEASE_IUNKNOWN(IVssWMFiledesc, wmFile);
hr = wmComponent->GetDatabaseFile(k, &wmFile);
if (hr != S_OK)
{
log("Error getting component database file desc " + convert(k) + " of component \"" +
componentNameStr + "\" of writer \"" + writerNameStr + "\" failed. VSS error code " + GetErrorHResErrStr(hr));
return false;
}
if (!getFilespec(wmFile, comp.currspec))
{
return false;
}
comp.filesIdx = k;
comp.filesPrefix = "database";
restore_components.push_back(comp);
}
for (UINT k = 0; k < componentInfo->cLogFiles; ++k)
{
SCOPED_DECLARE_RELEASE_IUNKNOWN(IVssWMFiledesc, wmFile);
hr = wmComponent->GetDatabaseLogFile(k, &wmFile);
if (hr != S_OK)
{
log("Error getting component database log file desc " + convert(k) + " of component \"" +
componentNameStr + "\" of writer \"" + writerNameStr + "\" failed. VSS error code " + GetErrorHResErrStr(hr));
return false;
}
if (!getFilespec(wmFile, comp.currspec))
{
return false;
}
comp.filesIdx = k;
comp.filesPrefix = "database_log";
restore_components.push_back(comp);
}
}
}
}
if (restore_components.empty())
{
log("No components selected. Did not restore anything.");
return false;
}
setMessage1(_("Restore operation in progress"));
log("Starting restore operation (pre restore)...");
SCOPED_DECLARE_RELEASE_IUNKNOWN(IVssAsync, prepare_wait);
hr = backupcom->PreRestore(&prepare_wait);
if (hr != S_OK)
{
log("Error during pre restore. VSS error code " + GetErrorHResErrStr(hr));
return false;
}
if (!WindowsComponentReader::wait_for(prepare_wait, "Pre restore failed", werrmsg))
{
log(werrmsg);
return false;
}
for (size_t i = 0; i < stop_services.size(); ++i)
{
log("Stopping service \"" + stop_services[i] + "\"...");
if (!stopService(stop_services[i]))
{
log("Failed to stop service \"" + stop_services[i] + "\"");
return false;
}
}
bool has_restore_err = false;
for (size_t i = 0; i < restore_components.size(); ++i)
{
SRestoreComponent& comp = restore_components[i];
if (!restoreFiles(comp))
{
has_restore_err = true;
}
bool more_files = false;
for (size_t j = i + 1; j < restore_components.size(); ++j)
{
if (restore_components[j].writerId == comp.writerId
&& restore_components[j].componentName == comp.componentName
&& restore_components[j].logicalPath == comp.logicalPath)
{
more_files = true;
break;
}
}
if (!more_files)
{
if (has_restore_err)
{
log("Restore of component \"" + comp.componentName +
"\" of writer \"" + comp.writerName + "\" failed.");
}
else
{
log("Restore of component \"" + comp.componentName +
"\" of writer \"" + comp.writerName + "\" finished.");
}
if (comp.hasComponent)
{
hr = backupcom->SetFileRestoreStatus(comp.writerId,
comp.type, comp.logicalPath.empty() ? NULL : ConvertToUnicode(comp.logicalPath).c_str(),
ConvertToUnicode(comp.componentName).c_str(),
has_restore_err ? VSS_RS_FAILED : VSS_RS_ALL);
if (hr != S_OK)
{
log("Error setting restore status of component \"" + comp.componentName +
"\" of writer \"" + comp.writerName + "\". VSS error code " + GetErrorHResErrStr(hr));
return false;
}
}
has_restore_err = false;
}
}
for (size_t i = 0; i < stop_services.size(); ++i)
{
log("Starting service \"" + stop_services[i] + "\"...");
if (!startService(stop_services[i]))
{
log("Failed to start service \"" + stop_services[i] + "\". Maybe start it manually?");
}
}
log("Finalizing restore operation (post restore)...");
SCOPED_DECLARE_RELEASE_IUNKNOWN(IVssAsync, post_wait);
hr = backupcom->PostRestore(&post_wait);
if (hr != S_OK)
{
log("Error during post restore. VSS error code " + GetErrorHResErrStr(hr));
return false;
}
if (!WindowsComponentReader::wait_for(post_wait, "Post restore failed", werrmsg))
{
log(werrmsg);
return false;
}
for (size_t i = 0; i < post_restart_services.size(); ++i)
{
log("Restarting service \"" + post_restart_services[i] + "\"...");
if (stopService(post_restart_services[i]))
{
if (!startService(post_restart_services[i]))
{
log("Failed to start service \"" + post_restart_services[i] + "\" after stopping it. Maybe start it manually?");
}
}
else
{
log("Failed to stop service \"" + post_restart_services[i] + "\"");
return false;
}
}
setMessage1(_("Restore finished"));
log("Restore finished.");
return ExitCode();
}
bool RestoreWindowsComponentsThread::restoreFiles(const SRestoreComponent& comp)
{
std::string logicalPathHash;
if (!comp.logicalPath.empty())
{
MD5 md((unsigned char*)comp.logicalPath.c_str());
char *p = md.hex_digest();
std::string md5 = p;
delete[]p;
logicalPathHash = "_" + md5;
}
std::string path = ".symlink_"+conv_filename(comp.writerName) + "_" +
convert(comp.writerId) + logicalPathHash + "_" + conv_filename(comp.componentName) + "_" + comp.filesPrefix + sortHex(comp.filesIdx);
std::vector<SPathMap> map_paths;
std::string target = comp.currspec.path;
std::vector<SFileSpec>::const_iterator it_alt = std::find(comp.redirected_locations.begin(),
comp.redirected_locations.end(), comp.currspec);
if (it_alt != comp.redirected_locations.end())
{
SPathMap m;
m.source = comp.currspec.path;
m.target = it_alt->altPath;
target = it_alt->altPath;
map_paths.push_back(m);
}
int pcdone = -1;
setMessage2(trans_2(_("Restoring component \"_1_\" of writer \"_2_\". Indexing..."), wxString(comp.componentName), wxString(comp.writerName)) );
Connector::EAccessError access_error;
SStartRestore restore = Connector::startRestore(path, backupid, map_paths, access_error, false, true, false, comp.restoreFlags);
if (!restore.ok)
{
log("Restoring files " + convert(comp.filesIdx) + " of component \"" + comp.componentName + "\" of writer \"" + comp.writerName + "\" failed. "
+ accessErrorToString(access_error));
return false;
}
log("Restoring file set " + convert(comp.filesIdx) + " of of component \"" + comp.componentName + "\" of writer \"" + comp.writerName + "\" to \"" + target + "\"...");
SConnection connection;
bool restore_finished = false;
std::string restore_details;
int detail_pc = -1;
bool status_error = false;
while (!restore_finished)
{
SStatusDetails status = Connector::getStatusDetails(&connection);
if (status.ok)
{
status_error = false;
for (size_t i = 0; i < status.finished_processes.size(); ++i)
{
if (status.finished_processes[i].id == restore.process_id)
{
restore_finished = true;
if (!status.finished_processes[i].success)
{
setMessage2("");
log("Restoring file set failed. See restore log for details.");
return false;
}
}
}
for (size_t i = 0; i < status.running_processes.size(); ++i)
{
SRunningProcess& proc = status.running_processes[i];
if (proc.process_id == restore.process_id)
{
if (!proc.details.empty()
&& (restore_details != proc.details
|| detail_pc != proc.detail_pc))
{
restore_details = proc.details;
detail_pc = proc.detail_pc;
log("Restoring \"" + restore_details + "\"... " + convert(detail_pc) + "%");
}
if (getProgressBarPc() != proc.percent_done)
{
setProgressBarPc(proc.percent_done);
setMessage2(trans_3(_("Restoring component \"_1_\" of writer \"_2_\". _3_% finished."),
wxString(comp.componentName), wxString(comp.writerName), convert(proc.percent_done)));
}
break;
}
}
}
else
{
if (!status_error)
{
log(std::string("Error getting restore status."));
}
status_error = true;
}
Sleep(1000);
}
setMessage2("");
return true;
}
bool RestoreWindowsComponentsThread::getFilespec(IVssWMFiledesc * wmFile, SFileSpec & filespec)
{
SCOPED_DECLARE_FREE_BSTR(path);
HRESULT hr = wmFile->GetPath(&path);
if (hr != S_OK)
{
log("Getting path of file spec failed. VSS error code " + GetErrorHResErrStr(hr));
return false;
}
SCOPED_DECLARE_FREE_BSTR(spec);
hr = wmFile->GetFilespec(&spec);
if (hr != S_OK)
{
log("Getting spec of file spec failed. VSS error code " + GetErrorHResErrStr(hr));
return false;
}
SCOPED_DECLARE_FREE_BSTR(altPath);
wmFile->GetAlternateLocation(&altPath);
hr = wmFile->GetRecursive(&filespec.recursive);
if (hr == S_FALSE)
{
filespec.recursive = false;
}
else if (hr != S_OK)
{
log("Getting recursive flag of file spec failed. VSS error code " + GetErrorHResErrStr(hr));
return false;
}
filespec.path = ConvertFromWchar(path);
filespec.spec = ConvertFromWchar(spec);
if (altPath != NULL)
{
filespec.altPath = ConvertFromWchar(altPath);
}
return true;
}
bool RestoreWindowsComponentsThread::readAltLocations(std::vector<SFileSpec> alt_locations)
{
return false;
}
RestoreWindowsComponents::RestoreWindowsComponents(wxWindow * parent, int backupid, std::vector<SComponent> selected_components, wxString componentConfigDir)
: GUIRestoreComponents(parent), restoreComponentsThread(backupid, selected_components, componentConfigDir)
{
restoreComponentsThread.Run();
Start(200);
}
void RestoreWindowsComponents::Notify(void)
{
wxString msg1 = restoreComponentsThread.getMessage1();
if (m_staticText1->GetLabel() != msg1)
{
m_staticText1->SetLabel(msg1);
}
wxString msg2 = restoreComponentsThread.getMessage2();
if (m_staticText2->GetLabel() != msg2)
{
m_staticText2->SetLabel(msg2);
}
std::vector<std::string> msgs = restoreComponentsThread.getNewLogMessages();
for (size_t i = 0; i < msgs.size(); ++i)
{
m_textCtrl1->AppendText(wxString(msgs[i]) + "\n");
}
int pc = restoreComponentsThread.getProgressBarPc();
if (pc == -1)
{
m_gauge1->Disable();
}
else
{
m_gauge1->Enable();
if (m_gauge1->GetRange() != 100)
{
m_gauge1->SetRange(100);
}
if (m_gauge1->GetValue() != pc)
{
m_gauge1->SetValue(pc);
}
}
if (!restoreComponentsThread.IsAlive())
{
Stop();
m_button3->Enable();
}
}
void RestoreWindowsComponents::onOkClick(wxCommandEvent & event)
{
Close();
}
bool RestoreWindowsComponentsThread::stopService(const std::string& service_name)
{
ULONGLONG startTime = GetTickCount64();
SCOPED_DECLARE_RELEASE_SERVICE_HANDLE(hsc);
hsc = OpenSCManagerW(
NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (NULL == hsc)
{
log("Could not open service manager");
return false;
}
SCOPED_DECLARE_RELEASE_SERVICE_HANDLE(service);
service = OpenServiceW(
hsc, ConvertToUnicode(service_name).c_str(),
SERVICE_STOP | SERVICE_QUERY_STATUS);
if (service == NULL)
{
log("Could not open handle to service.");
return false;
}
SERVICE_STATUS_PROCESS ssp;
DWORD bytesNeeded;
if (!QueryServiceStatusEx(service,
SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp,
sizeof(SERVICE_STATUS_PROCESS), &bytesNeeded))
{
log("QueryServiceStatusEx failed");
return false;
}
if (ssp.dwCurrentState == SERVICE_STOPPED)
{
return true;
}
while (ssp.dwCurrentState == SERVICE_STOP_PENDING)
{
DWORD dwWaitTime = ssp.dwWaitHint / 10;
if (dwWaitTime < 1000)
dwWaitTime = 1000;
else if (dwWaitTime > 10000)
dwWaitTime = 10000;
Sleep(dwWaitTime);
if (!QueryServiceStatusEx(
service,
SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp,
sizeof(SERVICE_STATUS_PROCESS), &bytesNeeded))
{
log("QueryServiceStatusEx failed");
return false;
}
if (ssp.dwCurrentState == SERVICE_STOPPED)
{
return true;
}
if (GetTickCount64() - startTime > 30000)
{
log("Timeout while waiting for service to stop. Stopping service failed.");
return false;
}
}