-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
2071 lines (1925 loc) · 91.8 KB
/
main.cpp
File metadata and controls
2071 lines (1925 loc) · 91.8 KB
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 <iostream>
#include <filesystem>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <unordered_map>
#include <chrono>
#include <thread>
#include <set>
#include <cctype>
#include <csignal>
#include <windows.h>
#include <thread>
#include <TlHelp32.h>
#include <ranges>
#include <boost\uuid\uuid.hpp>
#include <boost\uuid\uuid_generators.hpp>
#include <boost\filesystem\operations.hpp>
#include <boost\filesystem\path.hpp>
#include <boost\property_tree\ptree.hpp>
#include <boost\property_tree\json_parser.hpp>
#include <boost/dll.hpp>
#include "ConfigSync.hpp"
#include <fstream>
#ifdef DEBUG
#define DEBUG_MODE 1
#else
#define DEBUG_MODE 0
#endif
#define SETTINGS_ID 1
#define VERSION "v2.9.0"
volatile sig_atomic_t interrupt = 0;
int verbose = 0;
const std::string uName = std::getenv("USERNAME");
const int log_limit_extern = 60;
std::string pLocat;
std::string pLocatFull;
std::string root;
const std::string taskName = "ConfigSyncTask";
std::string archiveDir;
std::string savesFile;
std::string backupDir;
std::string settingsPath;
std::string CS::Logs::logDir;
std::string CS::Logs::logPath;
std::ofstream CS::Logs::logf;
inline void CS::Logs::init(){
logDir = pLocat + "\\logs";
logPath = logDir + "\\" + timestamp() + ".log";
logf = create_log(logPath);
}
class Defaultsetting{
public:
const int savelimit = 60;
const int preRestoreLimit = 60;
const bool task = false;
const std::string taskfrequency = "daily,1";
const std::string editor = "vscode";
void putDefault(boost::property_tree::ptree& pt){
pt.put("settingsID", SETTINGS_ID); // Settings identifier for updates
pt.put("savelimit", savelimit);
pt.put("pre-restore-limit", preRestoreLimit);
pt.put("task", task);
pt.put("taskfrequency", taskfrequency);
pt.put("editor", editor);
}
void savelimitDefault(boost::property_tree::ptree& pt){
pt.put("saveimit", savelimit);
}
void preRestoreLimitDefault(boost::property_tree::ptree& pt){
pt.put("pre-restore-limit", preRestoreLimit);
}
void taskDefault(boost::property_tree::ptree& pt){
pt.put("task", task);
}
void taskfreqDefault(boost::property_tree::ptree& pt){
pt.put("taskfrequency", taskfrequency);
}
void editorDefault(boost::property_tree::ptree& pt){
pt.put("editor", editor);
}
};
void enableColors(){
DWORD consoleMode;
HANDLE outputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
if (GetConsoleMode(outputHandle, &consoleMode))
{
SetConsoleMode(outputHandle, consoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
}
}
void exitSignalHandler(int signum){
if(interrupt == 0){
interrupt = 1;
std::cout << "Warning: Ctrl+C was pressed. Please remain calm and wait for the program to finish.\n";
std::signal(SIGINT, exitSignalHandler); // Reset SIGINT flag
}
else{
static int count = 2;
std::cout << ANSI_COLOR_226 << "WARNING: Prematurely killing this process can corrupt the targeted programs!" << ANSI_COLOR_RESET << std::endl;
if(count < 9){
std::signal(SIGINT, exitSignalHandler); // Reset SIGINT flag
}
else{
std::exit(signum);
}
}
}
inline void checkDynamicPath(CS::Saves& S, const std::string& prog, const uint64_t date){
if(S.cmp_root(prog, date, root) != 1){
S.replace_root(prog, date, root);
}
if(S.cmp_uname(prog, date, uName) != 1){
S.replace_uname(prog, date, uName);
}
}
inline void handleSyncOption(char* argv[], int argc, boost::property_tree::ptree& pt){
CS::Programs::Mgm mgm;
// std::cout << uName << std::endl;
if(argv[2] == NULL){
std::cerr << "Fatal: Missing argument or value." << std::endl;
}
else if(mgm.checkName(std::string(argv[2])) == 1){ // single program
std::string msg;
const char* cmp[] = {"--message", "-m"};
size_t cmpSize = sizeof(cmp) / sizeof(cmp[0]);
int msgFlag= 0;
if(CS::Args::argfcmp(msg, argv, argc, cmp, cmpSize) == 1){
if(msg == "Pre-Restore-Backup"){
std::cerr << "Fatal: Message is not allowed. Name is reserved." << std::endl;
std::exit(EXIT_FAILURE);
}
msgFlag = 1;
}
const std::string canName = mgm.get_canonical(std::string(argv[2]));
// Check if the program is installed.
bool installed = false;
for(const auto& path : mgm.programs().at(canName).paths){
if(std::filesystem::exists(path)){
// std::cout << "Installed: " << path << std::endl;
installed = true;
break;
}
// else {std::cout << "NOT installed: " << path << std::endl;}
}
if(!installed){
std::cout << ANSI_COLOR_YELLOW << canName << " is not installed, skipping." << ANSI_COLOR_RESET << std::endl;
return;
}
int forceFlag = 0;
if(CS::Args::argcmp(argv, argc, "--force") || CS::Args::argcmp(argv, argc, "-f")){
std::cout << ANSI_COLOR_161 << "Synchronizing " << canName << " - Forced:" << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_222 << "Starting synchronization..." << ANSI_COLOR_RESET << std::endl;
for(const auto& proc : mgm.programs().at(canName).procNames){
if(CS::Process::killProcess(proc.c_str()) == 1){
std::cout << ANSI_COLOR_222 << "Killed " << proc << ANSI_COLOR_RESET << std::endl;
}
}
forceFlag = 1;
}
else{
std::cout << ANSI_COLOR_161 << "Synchronizing " << canName << ":" << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_222 << "Fetching files..." << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_222 << "Starting synchronization..." << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_222 << "Computing hashes..." << ANSI_COLOR_RESET << std::endl;
}
CS::Saves S(savesFile);
S.load();
unsigned loadFlag = 1;
if(S.load() != 1){
loadFlag = 0;
}
if(forceFlag == 0){
int equal = 1;
if(loadFlag != 0){
if(S.exists(canName)){
const auto& lastSave = S.get_lastsave(canName);
if(lastSave.pathVec.empty()){
equal = 0; // Treat an empty save as "out of sync"
} else {
for(const auto& pair : S.get_lastsave(canName).pathVec){
if(!std::filesystem::exists(pair.first) || !std::filesystem::exists(pair.second)){
equal = 0;
break;
}
try{
if(CS::Utility::get_sha256hash_cpp(pair.first) != CS::Utility::get_sha256hash_cpp(pair.second)){
equal = 0;
break;
}
}
catch(const std::runtime_error& err){
equal = 0;
CS::Logs::msg("ERROR: Sync - runtime_error: " + std::string(err.what()) + " file: " + pair.first);
break;
}
}
}
}
else{
equal = 0;
}
}
else{
equal = 0;
}
if(equal == 1){
std::cout << ANSI_COLOR_66 << canName + " config is up to date." << ANSI_COLOR_RESET << std::endl;
CS::Logs::msg("Hashes identical: " + canName);
return;
}
std::cout << ANSI_COLOR_138 << canName << " config is out of sync! Synchronizing..." << ANSI_COLOR_RESET << std::endl;
CS::Logs::msg("Hashes differ: " + canName);
}
uint64_t tst = CS::Utility::timestamp();
const std::string dayDir = archiveDir + "\\" + canName + "\\" + CS::Utility::ymd_date();
const std::string tstDir = dayDir + "\\" + std::to_string(tst);
if(!std::filesystem::exists(tstDir)){
std::filesystem::create_directories(tstDir);
}
else{
CS::Filesystem::recurse_remove(tstDir);
std::filesystem::create_directories(tstDir);
}
std::vector<std::pair<std::string,std::string>> pathvec;
std::optional<std::reference_wrapper<std::vector<std::pair<std::string,std::string>>>> pvecRef = std::ref(pathvec);
unsigned id = 1;
for(const auto& el : mgm.programs().at(canName).paths){
const std::string dest = tstDir + "\\" + std::to_string(id);
if(!std::filesystem::exists(el)){
std::cerr << ANSI_COLOR_YELLOW << "Warning: Program path not found. [" << el << "]" << ANSI_COLOR_RESET << std::endl;
CS::Logs::msg("Warning: Program path not found. [" + el + "]");
id++;
continue;
}
try{
CS::Filesystem::recurse_copy(el, dest, std::nullopt, pvecRef);
}
catch(std::runtime_error& err){
CS::Logs::msg("ERROR: Filesystem error: " + std::string(err.what()) + " path: " + el);
}
id++;
}
if (pathvec.empty()) {
std::cerr << ANSI_COLOR_YELLOW << "Warning: No files were backed up for " << canName << ". Save skipped." << ANSI_COLOR_RESET << std::endl;
CS::Logs::msg("No files backed up for " + canName + ". Save skipped.");
}
else {
if(msgFlag == 1){
S.add(canName, uName, root, msg, pathvec, tst);
}
else{
S.add(canName, uName, root, "", pathvec, tst);
}
}
if(S.saves().at(canName).size() > pt.get<int>("savelimit")){
uint64_t oldestTst = S.get_oldest_tst(canName);
CS::Filesystem::recurse_remove(dayDir + "\\" + std::to_string(oldestTst));
S.erase_save(canName, oldestTst);
}
std::cout << ANSI_COLOR_GREEN << "Synchronization of " << canName << " finished." << ANSI_COLOR_RESET << std::endl;
S.save();
}
else if(std::string(argv[2]) == "--all" || std::string(argv[2]) == "-a"){ //multi program
int forceFlag = 0;
if(CS::Args::argcmp(argv, argc, "--force") || CS::Args::argcmp(argv, argc, "-f")){
std::cout << ANSI_COLOR_161 << "Synchronizing all programs - Forced:" << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_222 << "Starting synchronization..." << ANSI_COLOR_RESET << std::endl;
forceFlag = 1;
}
else{
std::cout << ANSI_COLOR_161 << "Synchronizing all programs:" << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_222 << "Fetching programs..." << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_222 << "Starting synchronization..." << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_222 << "Computing hashes..." << ANSI_COLOR_RESET << std::endl;
}
std::string msg;
const char* cmp[] = {"--message", "-m"};
size_t cmpSize = sizeof(cmp) / sizeof(cmp[0]);
int msgFlag= 0;
if(CS::Args::argfcmp(msg, argv, argc, cmp, cmpSize == 1)){
msgFlag = 1;
}
CS::Saves S(savesFile);
unsigned loadFlag = 1;
if(S.load() != 1){
loadFlag = 0;
}
std::vector<std::string> synced;
for(const auto& prog : mgm.get_supported()){
// Check if the program is installed.
bool installed = false;
for(const auto& path : mgm.programs().at(prog).paths){
if(std::filesystem::exists(path)){
installed = true;
break;
}
}
if(!installed){
std::cout << ANSI_COLOR_YELLOW << prog << " is not installed, skipping." << ANSI_COLOR_RESET << std::endl;
continue;
}
if(forceFlag == 0){
unsigned equal = 1;
if(loadFlag != 0){
if(S.exists(prog)){
const auto& lastSave = S.get_lastsave(prog);
if(lastSave.pathVec.empty()){
equal = 0; // Treat an empty save as "out of sync"
} else {
for(const auto& pair : S.get_lastsave(prog).pathVec){
if(!std::filesystem::exists(pair.first) || !std::filesystem::exists(pair.second)){
equal = 0;
CS::Logs::msg("FILE-EXISTS-CHECK:" + pair.first + " 2: " + pair.second);
break;
}
try{
if(CS::Utility::get_sha256hash_cpp(pair.first) != CS::Utility::get_sha256hash_cpp(pair.second)){
equal = 0;
CS::Logs::msg("HASHES:" + pair.first + " 2: " + pair.second);
break;
}
}
catch(std::runtime_error& err){
equal = 0;
CS::Logs::msg("RUNTIME ERROR:" + pair.first + " 2: " + pair.second);
break;
}
}
}
}
else{
equal = 0;
}
}
else{
equal = 0;
}
if(equal == 1){
std::cout << ANSI_COLOR_66 << prog + " config is up to date." << ANSI_COLOR_RESET << std::endl;
CS::Logs::msg("Hashes identical: " + prog);
continue;
}
std::cout << ANSI_COLOR_138 << prog << " config is out of sync! Synchronizing..." << ANSI_COLOR_RESET << std::endl;
CS::Logs::msg("Hashes differ: " + prog);
}
else{
for(const auto& proc : mgm.programs().at(prog).procNames){
if(CS::Process::killProcess(proc.c_str()) == 1){
std::cout << ANSI_COLOR_222 << "Killed" << proc << ANSI_COLOR_RESET << std::endl;
}
}
}
uint64_t tst = CS::Utility::timestamp();
const std::string dayDir = archiveDir + "\\" + prog + "\\" + CS::Utility::ymd_date();
const std::string tstDir = dayDir + "\\" + std::to_string(tst);
if(!std::filesystem::exists(tstDir)){
std::filesystem::create_directories(tstDir);
}
else{
CS::Filesystem::recurse_remove(tstDir);
std::filesystem::create_directories(tstDir);
}
std::vector<std::pair<std::string,std::string>> pathvec;
std::optional<std::reference_wrapper<std::vector<std::pair<std::string,std::string>>>> pvecRef = std::ref(pathvec);
unsigned id = 1;
for(const auto& el : mgm.programs()[prog].paths){
const std::string dest = tstDir + "\\" + std::to_string(id);
if(!std::filesystem::exists(el)){
std::cerr << ANSI_COLOR_YELLOW << "Warning: Program path not found. [" << el << "]" << ANSI_COLOR_RESET << std::endl;
CS::Logs::msg("Warning: Program path not found. " + el);
id++;
continue;
}
try{
CS::Filesystem::recurse_copy(el, dest, std::nullopt, pvecRef);
}
catch(std::runtime_error& err){
CS::Logs::msg("ERROR: Filesystem error: " + std::string(err.what()) + " path: " + el);
}
id++;
}
if (pathvec.empty()) {
std::cerr << ANSI_COLOR_YELLOW << "Warning: No files were backed up for " << prog << ". Save skipped." << ANSI_COLOR_RESET << std::endl;
CS::Logs::msg("No files backed up for " + prog + ". Save skipped.");
continue;
}
else {
// // for(const auto& pair : pathvec){
// std::cout << "pathvec: " << pair.first << std::endl;
if(msgFlag == 1){
S.add(prog, uName, root, msg, pathvec, tst);
}
else{
S.add(prog, uName, root, "", pathvec, tst);
}
}
if(S.saves().at(prog).size() > pt.get<int>("savelimit")){
uint64_t oldestTst = S.get_oldest_tst(prog);
std::string dayTst = CS::Utility::timestamp_to_str_ymd(oldestTst);
std::string dayTstDir = archiveDir + "\\" + prog + "\\" + dayTst;
CS::Filesystem::recurse_remove(dayTstDir + "\\" + std::to_string(oldestTst));
S.erase_save(prog, oldestTst);
}
synced.push_back(prog);
}
std::cout << ANSI_COLOR_222 << "Preparing summary..." << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_161 << "Synced programs:" << ANSI_COLOR_RESET << std::endl;
unsigned i = 1;
for(const auto& elem : synced){
std::cout << ANSI_COLOR_GREEN << i << "." << elem << ANSI_COLOR_RESET << std::endl;
i++;
}
std::cout << ANSI_COLOR_GREEN << "Synchronization finished!" << ANSI_COLOR_RESET << std::endl;
S.save();
}
else{
std::cerr << "Fatal: '" << argv[2] << "' is not a ConfigSync command. See 'configsync --help'." << std::endl;
}
}
inline void handleCheckOption(char* argv[], int argc, boost::property_tree::ptree& pt){
if(argv[2] == NULL){
std::cerr << "Fatal: Missing argument or value." << std::endl;
}
else if(std::string(argv[2]) == "--task" || std::string(argv[2]) == "-t"){
/* Ensure that task setting reflects reality. */
if(pt.get<bool>("task") == true && !CS::Task::exists(taskName)){ // Task setting is true and task doesnt exist
if(CS::Task::create(taskName, pLocatFull, pt.get<std::string>("taskfrequency")) != 1){std::cerr << "Errror: failed to create task." << std::endl; exit(EXIT_FAILURE);}
}
else if(pt.get<bool>("task") == false && CS::Task::exists(taskName)){ // Task setting is false and task exists
if(CS::Task::remove(taskName) != 1){std::cerr << "Errror: failed to remove task." << std::endl; exit(EXIT_FAILURE);} // Remove existing task
std::exit(EXIT_FAILURE);
}
else if(pt.get<bool>("task") == false && !CS::Task::exists(taskName)){
// Do nothing.
}
else if(pt.get<bool>("task") == true && CS::Task::exists(taskName)){
// Do nothing.
}
else{
std::cerr << "File: [" << __FILE__ << "] Line: [" << __LINE__ << "] This should never happen!\n";
}
}
else{
std::cerr << "Fatal: '" << argv[2] << "' is not a ConfigSync command. See 'configsync --help'." << std::endl;
}
}
inline uint64_t getNearestDate(CS::Saves& S, const std::string& prog, const uint64_t date){
if(!S.exists(prog)){
return 0;
}
if(S.find_save(prog, date) == 1){
return date;
}
auto it = S.saves().at(prog).upper_bound(date);
if(it == S.saves().at(prog).end()){
return 0;
}
return it--->first;
}
inline uint64_t getNearestResDate(CS::Saves& S, const std::string& prog, const uint64_t date){
if(!S.exists(prog)){
return 0;
}
if(S.find_save(prog, date) == 1){
if(S.saves().at(prog).at(date).message == "Pre-Restore-Backup"){
return date;
}
}
auto it = S.saves().at(prog).upper_bound(date);
while(true){
if(it == S.saves().at(prog).end()){
return 0;
}
if(it--->second.message != "Pre-Restore-Backup"){
it--;
}
else{
break;
}
}
return it->first;
}
inline uint64_t getNearestSaveDate(CS::Saves& S, const std::string& prog, const uint64_t date){
if(!S.exists(prog)){
return 0;
}
if(S.find_save(prog, date) == 1){
if(S.saves().at(prog).at(date).message != "Pre-Restore-Backup"){
return date;
}
}
auto it = S.saves().at(prog).upper_bound(date);
while(true){
if(it == S.saves().at(prog).end()){
return 0;
}
if(it--->second.message == "Pre-Restore-Backup"){
it--;
}
else{
break;
}
}
return it->first;
}
inline int createSave(CS::Programs::Mgm& mgm, CS::Saves& S, const std::string& prog, const std::string& destDayDir, const uint64_t tst, const std::string& message){
const std::string tstDir = destDayDir + "\\" + std::to_string(tst);
std::vector<std::pair<std::string,std::string>> pathvec;
std::optional<std::reference_wrapper<std::vector<std::pair<std::string,std::string>>>> pvecRef = std::ref(pathvec);
unsigned id = 1;
for(const auto& el : mgm.programs()[prog].paths){
const std::string dest = tstDir + "\\" + std::to_string(id);
if(!std::filesystem::exists(el)){
CS::Logs::msg("Warning: Program path not found. " + el);
id++;
continue;
}
try{
CS::Filesystem::recurse_copy(el, dest, std::nullopt, pvecRef);
}
catch(std::runtime_error& err){
CS::Logs::msg("ERROR: Filesystem error: " + std::string(err.what()) + " path: " + el);
}
id++;
}
S.add(prog, uName, root, message, pathvec, tst);
return 1;
}
inline int preRestoreBackup(CS::Programs::Mgm& mgm, CS::Saves& S, const std::string& prog){
uint64_t tst = CS::Utility::timestamp();
const std::string backupDayDir = backupDir + "\\" + prog + "\\" + CS::Utility::ymd_date();
if(createSave(mgm, S, prog, backupDayDir, tst, "Pre-Restore-Backup") != 1){
return 0;
}
return 1;
}
inline int restoreSave(CS::Saves& S, const std::string& prog, const uint64_t date){
checkDynamicPath(S, prog, date);
for(const auto& pair : S.saves().at(prog).at(date).pathVec){
if(std::filesystem::exists(pair.first)){
try{
if(std::filesystem::is_directory(pair.first)){
CS::Filesystem::recurse_remove(pair.first);
}
else{
std::filesystem::permissions(pair.first, std::filesystem::perms::all);
std::filesystem::remove(pair.first);
}
}
catch(const std::filesystem::filesystem_error& err){
CS::Logs::msg("ERROR: 'restoreSave': " + std::string(err.what()));
std::cerr << "Try again with '--force'. But save your open applications beforehand.";
throw err;
std::exit(EXIT_FAILURE);
}
}
else{
std::cout << "Warning: Restore target program path not found, creating directories...." << pair.first << std::endl;
CS::Logs::msg("Warning: Restore target program path not found, creating directories...." + pair.first);
std::filesystem::create_directories(std::filesystem::path(pair.first).parent_path());
}
std::filesystem::permissions(pair.second, std::filesystem::perms::all);
if(std::filesystem::copy_file(pair.second, pair.first, std::filesystem::copy_options::overwrite_existing | std::filesystem::copy_options::update_existing) != 1){
return 0;
}
}
return 1;
}
inline int removeSave(CS::Saves& S, const std::string& prog, const uint64_t date){
checkDynamicPath(S, prog, date);
for(const auto& [first, second] : S.saves().at(prog).at(date).pathVec){
if(std::filesystem::exists(second)){
if(std::filesystem::is_directory(second)){
CS::Filesystem::recurse_remove(second);
}
else{
std::filesystem::permissions(second, std::filesystem::perms::all);
std::filesystem::remove(second);
}
}
}
if(!S.erase_save(prog, date)){
return 0;
}
return 1;
}
inline void handleRestoreOption(char* argv[], int argc){
CS::Programs::Mgm mgm;
if(argv[2] == NULL){
std::cerr << "Fatal: Missing argument or value." << std::endl;
}
else if(mgm.checkName(std::string(argv[2])) == 1){
const std::string canName = mgm.get_canonical(std::string(argv[2]));
CS::Saves S(savesFile);
if(S.load() != 1){
std::cerr << "Fatal: No previous saves to restore." << std::endl;
return;
}
const char* cmp[] = {"--date", "-d"};
const size_t cmpSize = sizeof(cmp) / sizeof(cmp[0]);
std::string dateVal;
if(CS::Args::argfcmp(dateVal, argv, argc, cmp, cmpSize) == 1){
if(CS::Args::argcmp(argv, argc, "--force") == 1 || CS::Args::argcmp(argv, argc, "-f") == 1){
std::cout << ANSI_COLOR_161 << "Restoring config of " << canName << " from " << dateVal << " - Forced:" << std::endl;
for(const auto& proc : mgm.programs().at(canName).procNames){
if(CS::Process::killProcess(proc.c_str()) == 1){
std::cout << ANSI_COLOR_222 << "Killed " << proc << ANSI_COLOR_RESET << std::endl;
}
}
}
else{
std::cout << ANSI_COLOR_161 << "Restoring config of " << canName << " from " << dateVal << ":" << std::endl;
}
const uint64_t dateTst = CS::Utility::str_to_timestamp<uint64_t>(dateVal);
const uint64_t date = getNearestDate(S, canName, dateTst);
if(date == 0 || S.find_save(canName, date) == 0){
std::cout << "Fatal: No save of " << canName << " found." << std::endl;
return;
}
if(preRestoreBackup(mgm, S, canName) != 1){
std::cerr << ANSI_COLOR_RED << "Error: Failed to create Pre-Restore-Backup, terminating..." << ANSI_COLOR_RESET << std::endl;
std::exit(EXIT_FAILURE);
}
else{
std::cout << ANSI_COLOR_222 << "Created Pre-Restore-Backup" << ANSI_COLOR_RESET << std::endl;
}
if(restoreSave(S, canName, date) != 1){
std::cerr << ANSI_COLOR_RED << "Failed to restore save." << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_222 << "Restoring from Pre-Restore-Backup..." << ANSI_COLOR_RESET << std::endl;
if(restoreSave(S, canName, S.get_last_tst(canName)) != 1){
std::cerr << ANSI_COLOR_RED << "Error: Failed to restore from Pre-Restore-Backup" << ANSI_COLOR_RESET << std::endl;
std::cerr << "Please ensure the target program is functioning normally." << std::endl;
std::exit(EXIT_FAILURE);
}
}
std::cout << ANSI_COLOR_GREEN << "Restore successfull." << ANSI_COLOR_RESET << std::endl;
S.save();
}
else{
if(S.exists(canName) != 1){
std::cerr << "Fatal: No save of " << canName << " found." << std::endl;
return;
}
const uint64_t date = S.get_last_tst(canName);
if(CS::Args::argcmp(argv, argc, "--force") == 1 || CS::Args::argcmp(argv, argc, "-f") == 1){
for(const auto& proc : mgm.programs().at(canName).procNames){
if(CS::Process::killProcess(proc.c_str()) == 1){
std::cout << ANSI_COLOR_222 << "Killed " << proc << ANSI_COLOR_RESET << std::endl;
}
}
std::cout << ANSI_COLOR_161 << "Restoring config of " << canName << " - Forced:" << std::endl;
}
else{
std::cout << ANSI_COLOR_161 << "Restoring config of " << canName << ":" << std::endl;
}
if(preRestoreBackup(mgm, S, canName) != 1){
std::cerr << ANSI_COLOR_RED << "Error: Failed to create Pre-Restore-Backup, terminating..." << ANSI_COLOR_RESET << std::endl;
std::exit(EXIT_FAILURE);
}
else{
std::cout << ANSI_COLOR_222 << "Created Pre-Restore-Backup" << ANSI_COLOR_RESET << std::endl;
}
if(restoreSave(S, canName, date) != 1){
std::cerr << ANSI_COLOR_RED << "Failed to restore save." << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_222 << "Restoring from Pre-Restore-Backup..." << ANSI_COLOR_RESET << std::endl;
if(restoreSave(S, canName, S.get_last_tst(canName)) != 1){
std::cerr << ANSI_COLOR_RED << "Error: Failed to restore from Pre-Restore-Backup" << ANSI_COLOR_RESET << std::endl;
std::cerr << "Please ensure the target program is functioning normally." << std::endl;
std::exit(EXIT_FAILURE);
}
}
std::cout << ANSI_COLOR_GREEN << "Restore successfull." << ANSI_COLOR_RESET << std::endl;
S.save();
}
}
else if(CS::Args::argcmp(argv, argc, "--all") == 1 || CS::Args::argcmp(argv, argc, "-a") == 1){
const char* cmp[] = {"--date", "-d"};
const size_t cmpSize = sizeof(cmp) / sizeof(cmp[0]);
std::string dateVal;
if(CS::Args::argfcmp(dateVal, argv, argc, cmp, cmpSize) == 1){
const uint64_t dateTst = CS::Utility::str_to_timestamp<uint64_t>(dateVal);
CS::Saves S(savesFile);
if(S.load() != 1){
std::cerr << "Fatal: No previous saves to restore." << std::endl;
return;
}
int forceFlag = 0;
if(CS::Args::argcmp(argv, argc, "--force") == 1 || CS::Args::argcmp(argv, argc, "-f") == 1){
std::cout << ANSI_COLOR_161 << "Restoring config of all programs from " << dateVal << " - Forced:" << std::endl;
forceFlag = 1;
}
else{
std::cout << ANSI_COLOR_161 << "Restoring config of all programs from " << dateVal << ":" << ANSI_COLOR_RESET << std::endl;
}
std::vector<std::string> success;
std::vector<std::string> failed;
for(const auto& prog : mgm.get_supported()){
const uint64_t date = getNearestDate(S, prog, dateTst);
if(date == 0 || S.find_save(prog, date) == 0){
std::cout << ANSI_COLOR_YELLOW << "No save of " << prog << " found, skipping..." << ANSI_COLOR_RESET << std::endl;
failed.emplace_back(prog);
continue;
}
if(forceFlag == 1){
for(const auto& proc : mgm.programs().at(prog).procNames){
if(CS::Process::killProcess(proc.c_str()) == 1){
std::cout << ANSI_COLOR_222 << "Killed " << proc << ANSI_COLOR_RESET << std::endl;
}
}
}
if(preRestoreBackup(mgm, S, prog) != 1){
std::cerr << ANSI_COLOR_RED << "Error: Failed to create Pre-Restore-Backup, terminating..." << ANSI_COLOR_RESET << std::endl;
std::exit(EXIT_FAILURE);
}
else{
std::cout << ANSI_COLOR_222 << "Created Pre-Restore-Backup" << ANSI_COLOR_RESET << std::endl;
}
if(restoreSave(S, prog, date) != 1){
failed.push_back(prog);
std::cerr << ANSI_COLOR_RED << "Failed to restore save." << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_222 << "Restoring from Pre-Restore-Backup..." << ANSI_COLOR_RESET << std::endl;
if(restoreSave(S, prog, S.get_last_tst(prog)) != 1){
std::cerr << ANSI_COLOR_RED << "Error: Failed to restore from Pre-Restore-Backup" << ANSI_COLOR_RESET << std::endl;
std::cerr << "Please ensure the target program is functioning normally." << std::endl;
S.save();
std::exit(EXIT_FAILURE);
}
}
else{
success.push_back(prog);
}
}
std::cout << ANSI_COLOR_222 << "Restore complete." << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_222 << "Preparing summary..." << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_161 << "Restored programs:" << ANSI_COLOR_RESET << std::endl;
unsigned i = 0;
for(const auto& el : success){
std::cout << ANSI_COLOR_GREEN << i << ". " << el << ANSI_COLOR_RESET << std::endl;
i++;
}
if(!failed.empty()){
std::cout << ANSI_COLOR_RED << "Failed to restore:" << ANSI_COLOR_RESET << std::endl;
unsigned ii = 0;
for(const auto& el : failed){
std::cout << ANSI_COLOR_RED << ii << ". " << el << ANSI_COLOR_RESET << std::endl;
i++;
}
}
S.save();
}
else{
CS::Saves S(savesFile);
if(S.load() != 1){
std::cerr << "Fatal: No previous saves to restore." << std::endl;
return;
}
int forceFlag = 0;
if(CS::Args::argcmp(argv, argc, "--force") == 1 || CS::Args::argcmp(argv, argc, "-f") == 1){
std::cout << ANSI_COLOR_161 << "Restoring config of all programs from latest save - Forced:" << std::endl;
forceFlag = 1;
}
else{
std::cout << ANSI_COLOR_161 << "Restoring config of all programs from latest save:" << ANSI_COLOR_RESET << std::endl;
}
std::vector<std::string> success;
std::vector<std::string> failed;
for(const auto& prog : mgm.get_supported()){
const uint64_t date = S.get_last_tst(prog);
if(date == 0 || S.find_save(prog, date) == 0){
std::cout << ANSI_COLOR_YELLOW << "No save of " << prog << " found, skipping..." << ANSI_COLOR_RESET << std::endl;
failed.emplace_back(prog);
continue;
}
if(forceFlag == 1){
for(const auto& proc : mgm.programs().at(prog).procNames){
if(CS::Process::killProcess(proc.c_str()) == 1){
std::cout << ANSI_COLOR_222 << "Killed " << proc << ANSI_COLOR_RESET << std::endl;
}
}
}
if(preRestoreBackup(mgm, S, prog) != 1){
std::cerr << ANSI_COLOR_RED << "Error: Failed to create Pre-Restore-Backup, terminating..." << ANSI_COLOR_RESET << std::endl;
std::exit(EXIT_FAILURE);
}
else{
std::cout << ANSI_COLOR_222 << "Created Pre-Restore-Backup" << ANSI_COLOR_RESET << std::endl;
}
if(restoreSave(S, prog, date) != 1){
failed.push_back(prog);
std::cerr << ANSI_COLOR_RED << "Failed to restore save." << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_222 << "Restoring from Pre-Restore-Backup..." << ANSI_COLOR_RESET << std::endl;
if(restoreSave(S, prog, S.get_last_tst(prog)) != 1){
std::cerr << ANSI_COLOR_RED << "Error: Failed to restore from Pre-Restore-Backup" << ANSI_COLOR_RESET << std::endl;
std::cerr << "Please ensure the target program is functioning normally." << std::endl;
S.save();
std::exit(EXIT_FAILURE);
}
}
else{
success.push_back(prog);
}
}
std::cout << ANSI_COLOR_222 << "Restore complete." << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_222 << "Preparing summary..." << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_161 << "Restored programs:" << ANSI_COLOR_RESET << std::endl;
unsigned i = 0;
for(const auto& el : success){
std::cout << ANSI_COLOR_GREEN << i << ". " << el << ANSI_COLOR_RESET << std::endl;
i++;
}
if(!failed.empty()){
std::cout << ANSI_COLOR_RED << "Failed to restore:" << ANSI_COLOR_RESET << std::endl;
unsigned ii = 0;
for(const auto& el : failed){
std::cout << ANSI_COLOR_RED << ii << ". " << el << ANSI_COLOR_RESET << std::endl;
i++;
}
}
S.save();
}
}
else{
std::cerr << "Fatal: Missing argument or value. See 'configsync --help'" << std::endl;
}
}
inline void handleShowOption(char* argv[], int argc){
CS::Programs::Mgm mgm;
if(argv[2] == NULL){
std::cerr << "Fatal: Missing argument or value." << std::endl;
}
else if(mgm.checkName(std::string(argv[2])) == 1){
const std::string canName = mgm.get_canonical(std::string(argv[2]));
CS::Saves S(savesFile);
if(S.load() == 0){
std::cerr << "Fatal: No previous saves found. See 'configsync --help'." << std::endl;
return;
}
std::cout << ANSI_COLOR_222 << canName << ":" << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_166 << "Saves:" << ANSI_COLOR_RESET << std::endl;
std::vector<uint64_t> resVec;
unsigned sCount = 0;
unsigned pCount = 0;
unsigned i = 1;
for(const auto& save : S.saves().at(canName)){
if(save.second.message != "Pre-Restore-Backup"){
if(!save.second.message.empty()){
std::cout << ANSI_COLOR_146 << i << ". " << CS::Utility::timestamp_to_str(save.first) << ANSI_COLOR_RESET << " - " << save.second.message << std::endl;
}
else{
std::cout << ANSI_COLOR_146 << i << ". " << CS::Utility::timestamp_to_str(save.first) << ANSI_COLOR_RESET << std::endl;
}
sCount++;
}
else{
resVec.emplace_back(save.first);
pCount++;
}
i++;
}
std::cout << std::endl;
if(!resVec.empty()){
std::cout << ANSI_COLOR_166 << "Pre-Restore-Backups:" << ANSI_COLOR_RESET << std::endl;
unsigned ii = 1;
for(const auto& tst : resVec){
std::cout << ANSI_COLOR_151 << ii << ". " << CS::Utility::timestamp_to_str(tst) << ANSI_COLOR_RESET << std::endl;
ii++;
}
if(sCount == 1){
std::cout << "Found " << ANSI_COLOR_GREEN << sCount << ANSI_COLOR_RESET << " save and ";
}
else{
std::cout << "Found " << ANSI_COLOR_GREEN << sCount << ANSI_COLOR_RESET << " saves and ";
}
if(pCount == 1){
std::cout << ANSI_COLOR_GREEN << pCount << ANSI_COLOR_RESET << " Pre-Restore-Backup." << std::endl;
}
else{
std::cout << ANSI_COLOR_GREEN << pCount << ANSI_COLOR_RESET << " Pre-Restore-Backups." << std::endl;
}
}
else{
if(sCount == 1){
std::cout << "Found " << ANSI_COLOR_GREEN << sCount << ANSI_COLOR_RESET << " save." << std::endl;
}
else{
std::cout << "Found " << ANSI_COLOR_GREEN << sCount << ANSI_COLOR_RESET << " saves." << std::endl;
}
}
}
else if(CS::Args::argcmp(argv, argc, "--explorer") == 1 || CS::Args::argcmp(argv, argc, "-e") == 1){
const std::string com = "cmd /c \"explorer \"" + archiveDir + "\"\"";
const std::string pwshcom = "pwsh /c explorer \"" + archiveDir + "\"";
if(std::system(com.c_str()) != 1){ // Explorer exit code is 1 for success
std::cerr << ANSI_COLOR_RED << "Failed to call windows command processor." << ANSI_COLOR_RESET << std::endl;
std::cout << "Trying to call powershell..." << std::endl;
if(std::system(pwshcom.c_str()) != 1){
std::cerr << ANSI_COLOR_RED << "Failed to call powershell, terminating..." << ANSI_COLOR_RESET << std::endl;
}
std::exit(EXIT_FAILURE);
}
return;
}
else{
std::cerr << "Fatal: Missing argument or value. See 'configsync --help'" << std::endl;
}
}
inline void handleStatusOption(char* argv[], int argc){
CS::Programs::Mgm mgm;
if(argv[2] == NULL){
std::cout << ANSI_COLOR_166 << "Status:" << ANSI_COLOR_RESET << std::endl;
CS::Saves S(savesFile);
if(!S.load()){
CS::Logs::msg("WARNING!!! Failed to load Saves file!!!");
std::cout << ANSI_COLOR_RED << "Never synced:" << ANSI_COLOR_RESET << std::endl;
for(const auto& prog : mgm.get_supported()){
std::cout << prog << std::endl;
}
return;
}
std::vector<std::string> insync;
std::vector<std::string> outsync;
std::vector<std::string> nsync;
std::vector<std::string> notInstalled;
for(const auto& prog : mgm.get_supported()){
CS::Logs::msg("Status - Prog: " + prog);
// First, check if the program is installed (i.e. any path exists)
bool installed = false;
for(const auto& path : mgm.programs().at(prog).paths){
if(std::filesystem::exists(path)){
installed = true;