-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSassySync_v006.cpp
More file actions
518 lines (443 loc) · 15.5 KB
/
Copy pathSassySync_v006.cpp
File metadata and controls
518 lines (443 loc) · 15.5 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
// Cleaner rewrite of INI parser with less code and easier expandability
// Sas van Gulik, 6-9-2022
#include <iostream>
#include <filesystem>
#include <string>
#include <string_view>
#include <fstream>
#include <chrono>
#include <thread>
#include <ctime>
#include <stdexcept>
#include <vector>
#include <windows.h>
#include <optional>
#include <array>
#include <bits/stl_pair.h>
#include <unordered_map>
#include "headers/sas_threadpool.hpp"
void rem_trail_lead_spaces(std::string_view& string)
{
size_t pos_start = 0;
size_t pos_end = string.length()-1;
while(string.at(pos_start)==' ')
{
pos_start++;
}
while(string.at(pos_end)==' ')
{
pos_end--;
}
string = string.substr(pos_start,pos_end-pos_start+1);
}
std::string_view rem_trail_lead_spaces(std::string_view const& string)
{
size_t pos_start = 0;
size_t pos_end = string.length()-1;
while(string.at(pos_start)==' ')
{
pos_start++;
}
while(string.at(pos_end)==' ')
{
pos_end--;
}
return string.substr(pos_start,pos_end-pos_start+1);
}
std::string normalize_path(std::string_view const& path)
{
std::string spath = std::string(rem_trail_lead_spaces(path));
size_t pos = spath.find('\\');
while(pos!=std::string::npos)
{
spath.replace(pos,1,"/");
pos = spath.find('\\');
}
return spath;
}
std::optional<bool> bool_from_string(std::string_view const& boolstring)
{
std::array checkTrue =
std::to_array((std::string_view[]){"1","true","True","TRUE"});
std::array checkFalse =
std::to_array((std::string_view[]){"0","false","False","FALSE"});
for(auto const& item : checkTrue)
{
if(boolstring.find(item)!=std::string_view::npos) return true;
}
for(auto const& item : checkFalse)
{
if(boolstring.find(item)!=std::string_view::npos) return false;
}
return {};
}
std::optional<std::pair<std::string_view,std::string_view>> parse_line(std::string_view const& line)
{
if(line.find('[',0)==std::string_view::npos || line.find(']',0)==std::string_view::npos) return {} ;
size_t open_bracket = line.find_first_of('[',0);
size_t close_bracket = line.find_first_of(']',0);
std::string_view key =
line.substr(open_bracket+1,close_bracket-open_bracket-1);
rem_trail_lead_spaces(key);
size_t colon = line.find(':',0);
if(colon==std::string_view::npos) return std::make_pair(key,"");
std::string_view value = line.substr(colon+1);
rem_trail_lead_spaces(value);
return std::make_pair(key,value);
};
float string_to_float(std::string const& float_str)
{
try
{
return std::stof(float_str);
}
catch(std::invalid_argument const& except)
{
return -1;
}
catch(std::out_of_range const& except)
{
return -1;
}
}
//Struct to parse and gather all services
struct SettingsList;
//data to pass to service.
struct INIstruct
{
std::filesystem::path folder_in; //folder to get files FROM
std::filesystem::path folder_to; //DESTINATION of files
std::chrono::seconds wait_time = std::chrono::seconds(600);
std::string name;
INIstruct(std::string_view const& _name) : name(_name){};
//buncha checks
bool setup_ok = false;
bool del_to = false;
bool run = true;
bool found_from = false;
bool found_to = false;
bool found_timer = false;
bool safety = true;
static constexpr std::string_view safety_path = "Y:/";
};
bool verify_INI(INIstruct& I)
{
if(!I.found_from || !I.found_to)
{
std::cout << "Service: " << I.name << " didn't initialize properly.\n";
if(!I.found_from) printf("[FROM FOLDER] is missing, doesn't exist, or is not recognised in settings.ini\n");
if(!I.found_to) printf("[DESTINATION FOLDER] is missing, doesn't exist, or is not recognised in settings.ini\n");
if(!I.found_timer) printf("[DELAY] is missing or not recognised in settings.ini\n");
printf("Running this service will be halted.\n\n");
}
else
{
printf("Service: %s\n==--==--==--==--==--==--==--==--==--==--==--==--\n",std::string(I.name).c_str());
printf("Source folder: %s\n",I.folder_in.string().c_str());
printf("Destination folder: %s\n",I.folder_to.string().c_str());
printf("Running once every %f minutes\n",float(I.wait_time.count())/60);
if(I.del_to) printf("Delete non-matching is turned on. \nFiles in the destination folder will be deleted if they don't match the source.\n");
if(I.safety && I.folder_in.string().find(I.safety_path)==0)
{
printf("Drive Safety is on! Hard syncing with destination in %s will be disabled.\n",std::string(I.safety_path).c_str());
I.del_to = false;
}
if(I.run)
printf("Service: %s is set up and is scheduled to run.\n\n",std::string(I.name).c_str());
else
printf("Service: %s is set up and is scheduled to NOT run.\n\n",std::string(I.name).c_str());
I.setup_ok = true;
}
return (I.run&&I.setup_ok);
}
//the function signature is a pointer to the settings, and the gathered value.
using func_t = std::function<void(SettingsList*, std::string_view const&)>;
using func_map_t = std::unordered_map<std::string_view,func_t>;
struct SettingsList
{
static func_map_t const parse_functions;
INIstruct* INI_cur = nullptr;
std::vector<INIstruct> services = {};
bool run = true;
bool hide = false;
SettingsList()
{
std::fstream file("settings.ini");
std::string line;
while(getline(file,line))
{
if(line.find("#")!=std::string_view::npos || line.length() == 0) continue; //comment or empty line
auto const parse = parse_line(line);
if(!parse.has_value()) continue; //nothing found, continue;
auto const& key = parse.value().first;
auto const& value = parse.value().second;
//run key through map and run function stored within.
try
{
auto func = parse_functions.at(key);
func(this, value);
}
catch(std::out_of_range const& except)
{
std::cout<< "keyword "<< key << " not present\n";
}
catch(std::runtime_error const& except)
{
std::cout << except.what();
std::cout << "Quitting...\n";
run = false;
break;
}
}
//don't run if there's nothing to run.
if(services.empty()) run = false;
};
};
//set up map
func_map_t const SettingsList::parse_functions =
{
{"SERVICE",
[](SettingsList* Settings, std::string_view const& val)
{
if(Settings->INI_cur!=nullptr)
{
throw std::runtime_error("[ERROR] : SERVICE not properly initialised.\n");
}
INIstruct* new_ini = new INIstruct(val);
Settings->INI_cur = new_ini;
}
},
{"FROM FOLDER",
[](SettingsList* Settings, std::string_view const& val)
{
auto INI = Settings->INI_cur;
auto path_value = normalize_path(val);
INI->folder_in = std::filesystem::path(path_value);
INI->found_from = std::filesystem::exists(INI->folder_in);
}
},
{"DESTINATION FOLDER",
[](SettingsList* Settings, std::string_view const& val)
{
auto INI = Settings->INI_cur;
auto path_value = normalize_path(val);
INI->folder_to = std::filesystem::path(path_value);
INI->found_to = std::filesystem::exists(INI->folder_to);
}
},
{"DELAY",
[](SettingsList* Settings, std::string_view const& val)
{
auto INI = Settings->INI_cur;
int val_i = int(string_to_float(std::string(val)))*60;
if(val_i!=-1 && val_i > 0)
{
INI->wait_time = std::chrono::seconds(val_i);
INI->found_timer = true;
}
else
{
INI->wait_time = std::chrono::seconds(600);
INI->found_timer = true;
}
}
},
{"DELAY S",
[](SettingsList* Settings, std::string_view const& val)
{
auto INI = Settings->INI_cur;
int val_i = int(string_to_float(std::string(val)));
if(val_i!=-1 && val_i > 0)
{
INI->wait_time = std::chrono::seconds(val_i);
INI->found_timer = true;
}
else
{
INI->wait_time = std::chrono::seconds(600);
INI->found_timer = true;
}
}
},
{"DELETE NON-MATCHING",
[](SettingsList* Settings, std::string_view const& val)
{
auto INI = Settings->INI_cur;
auto opt_bool = bool_from_string(val);
(opt_bool.has_value())?
INI->del_to = opt_bool.value() : INI->del_to = false;
}
},
{"SAFETY",
[](SettingsList* Settings, std::string_view const& val)
{
auto INI = Settings->INI_cur;
auto opt_bool = bool_from_string(val);
(opt_bool.has_value())?
INI->safety = opt_bool.value() : INI->safety = true;
}
},
{"RUN",
[](SettingsList* Settings, std::string_view const& val)
{
auto INI = Settings->INI_cur;
auto opt_bool = bool_from_string(val);
(opt_bool.has_value())?
INI->run = opt_bool.value() : INI->run = false;
}
},
{"GLOBAL RUN",
[](SettingsList* Settings, std::string_view const& val)
{
auto opt_bool = bool_from_string(val);
(opt_bool.has_value())?
Settings->run = opt_bool.value() : Settings->run = true;
}
},
{"GLOBAL HIDE",
[](SettingsList* Settings, std::string_view const& val)
{
auto opt_bool = bool_from_string(val);
(opt_bool.has_value())?
Settings->hide = opt_bool.value() : Settings->hide = false;
}
},
{"SERVICE END",
[](SettingsList* Settings, std::string_view const& val)
{
if(Settings->INI_cur==nullptr)
{
throw std::runtime_error("[ERROR] : SERVICE END not properly initialised.\n");
}
auto INI = *Settings->INI_cur;
std::cout << "Verifying: " << INI.name << "...\n\n";
if(verify_INI(INI))
{
Settings->services.push_back(INI);
Settings->INI_cur = nullptr;
}
else
{
//free memory if setting is not going to run.
delete Settings->INI_cur;
Settings->INI_cur = nullptr;
std::cout << "Deleted service from memory. \n\n";
}
}
},
};
//SERVICE COMPONENT
//run job according to settings stored in INIstruct
void job_sync(INIstruct const& settings)
{
//setting some options for updating files when nessecary
std::filesystem::copy_options cpy_options = std::filesystem::copy_options::overwrite_existing;
for(std::filesystem::directory_entry dir
: std::filesystem::recursive_directory_iterator(settings.folder_in))
{
std::string cur_path = dir.path().string();
normalize_path(cur_path);
//figure out equivaluent path on the other side.
std::string rel_to_path = settings.folder_to.string()
+ cur_path.substr(settings.folder_in.string().length());
//if it doesn't exist, let's make it.
if(!std::filesystem::exists(rel_to_path))
{
if(std::filesystem::is_directory(cur_path))
{
std::filesystem::create_directory(rel_to_path);
}
else if (std::filesystem::is_regular_file(cur_path))
{
std::filesystem::copy(cur_path,rel_to_path,cpy_options);
}
}
else
{
if (std::filesystem::is_regular_file(cur_path))
{
auto time_in_file = std::filesystem::last_write_time(cur_path);
auto time_to_file = std::filesystem::last_write_time(rel_to_path);
if(time_in_file>time_to_file)
{
std::filesystem::remove(rel_to_path); // safeguard
try {
std::filesystem::copy(cur_path,rel_to_path,cpy_options);
} catch (std::exception& e) {
std::cout << "Exception occurred during update of file " << rel_to_path <<":\n" << e.what() << "\n\nMoving on...";
}
}
}
}
}
if(settings.del_to)
{
for(std::filesystem::directory_entry dir
: std::filesystem::recursive_directory_iterator(settings.folder_to))
{
std::string to_path = dir.path().string();
normalize_path(to_path);
std::string rel_in_path = settings.folder_in.string()
+ to_path.substr(settings.folder_to.string().length());
if(!std::filesystem::exists(rel_in_path))
{
std::filesystem::remove_all(to_path);
}
}
}
}
void service_routine(INIstruct const& settings,std::mutex& mut)
{
while(true)
{
auto now = std::chrono::system_clock::now();
auto nowt = std::chrono::system_clock::to_time_t(now);
auto time = std::chrono::high_resolution_clock::now();
{
std::lock_guard<std::mutex> lk(mut);
//print start
now = std::chrono::system_clock::now();
nowt = std::chrono::system_clock::to_time_t(now);
std::cout << "[SERVICE JOB START] - Sync service: "<< settings.name <<" started at: " << std::ctime(&nowt) << "\n";
time = std::chrono::high_resolution_clock::now();
}
//do work
job_sync(settings);
{
std::lock_guard<std::mutex> lk(mut);
//define variables.
auto time2 = std::chrono::high_resolution_clock::now();
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(time2-time);
//print end
auto now2 = std::chrono::system_clock::now();
auto nowt2 = std::chrono::system_clock::to_time_t(now2);
std::cout << "[SERVICE JOB END] - Sync service " << settings.name << " ended at: " << std::ctime(&nowt2)
<< "---------------------------------------------------------------------------------\n"
<< "- Service " << settings.name <<" took: " << seconds.count() <<" seconds\n"
<< "- Waiting " << (float(settings.wait_time.count())/60) << " minutes for next run of " << settings.name<<".\n\n";
}
std::this_thread::sleep_for(settings.wait_time);
}
}
int main()
{
auto Settings = SettingsList();
if(!Settings.run) return 0;
if(Settings.hide)
{
HWND hWnd = GetConsoleWindow();
ShowWindow( hWnd, SW_HIDE );
}
std::mutex service_mutex;
//create a pool of needed size.
threadPool pool(Settings.services.size());
pool.Run();
std::cout << "SassySync v0.6 made by Sas van Gulik.\n\n";
//setup services to run.
for (auto const& ini : Settings.services)
{
pool.addJob_bind(service_routine,std::ref(ini),std::ref(service_mutex));
}
pool.waitForAll();
return 0;
}