-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
222 lines (174 loc) · 4.97 KB
/
main.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
#include <iostream>
#include "OneWaySync.h"
#include "FolderPair.h"
using namespace std;
/*
TODO :
regex
*/
/*
Test Command Line
-s "C:\TEST_SOURCE" -t "C:\TEST_TARGET" -m fast -f ".*f[1-2]" ".*d1.*" -e 3 5 -v 3
*/
void printHelp()
{
wcout << "OneWaySync" << endl
<< " -h - help" << endl
<< " Display this help" << endl
<< endl
<< " -s -source 'Path'" << endl
<< " Path of the left folder" << endl
<< endl
<< " -t - target 'Path'" << endl
<< " Path of the right folder" << endl
<< endl
<< " -m -mode fast/accurate" << endl
<< " fast : Compare the name, last modification date, and size of the files" << endl
<< " accurate : Compare the md8 hash of the files" << endl
<< endl
<< " -f -filter \"Regex1\" \"Regex2\" \"RegexN\"" << endl
<< " Filter out every file and folder matching the regexes" << endl
<< endl
<< " -e -errorfix n t" << endl
<< " Retry every action that failed after t seconds n times" << endl
<< endl
<< " -v -verbose 1/2/3" << endl
<< " 1 : Errors only" << endl
<< " 2 : Output every IO" << endl
<< " 3 : Debug output" << endl
<< endl;
}
int wmain(int argc, wchar_t* argv[])
{
String source = L"";
String target = L"";
int errorNumberOfRetries = 0;
int errorTimeout = 0;
enum CompareMode mode = CompareMode::Fast;
Filter filter = Filter();
if (argc < 5)
{
wcout << L"Error: not enough arguments." << endl << endl;
printHelp();
return 0;
}
int i = 1;
while (i+1 < argc)
{
if (wcscmp(argv[i], L"-h") == 0 || wcscmp(argv[i], L"-help") == 0)
{
printHelp();
}
else if (wcscmp(argv[i], L"-s") == 0 || wcscmp(argv[i], L"-source") == 0)
{
++i;
source = String(argv[i]);
wcout << endl << L"Source Path: " << source;
++i;
}
else if (wcscmp(argv[i], L"-t") == 0 || wcscmp(argv[i], L"-target") == 0)
{
++i;
target = String(argv[i]);
wcout << endl << L"Target Path: " << target;
++i;
}
else if (wcscmp(argv[i], L"-m") == 0 || wcscmp(argv[i], L"-mode") == 0)
{
++i;
if (wcscmp(argv[i], L"fast") == 0)
{
mode = CompareMode::Fast;
wcout << endl << L"Mode: fast";
}
else if (wcscmp(argv[i], L"accurate") == 0)
{
mode = CompareMode::Accurate;
wcout << endl << L"Mode: accurate";
}
else
{
wcout << endl << L"Error: unknown mode \"" << argv[i] << "\".";
}
++i;
}
else if (wcscmp(argv[i], L"-f") == 0 || wcscmp(argv[i], L"-filter") == 0)
{
++i;
wcout << endl << L"Filter(s): ";
while (i < argc && argv[i][0] != '-')
{
filter.AddCondition(argv[i]);
wcout << endl << L" " << argv[i];
++i;
}
}
else if (wcscmp(argv[i], L"-e") == 0 || wcscmp(argv[i], L"-errorfix") == 0)
{
++i;
errorNumberOfRetries = wcstol(argv[i], NULL, 10);
++i;
if (i < argc)
{
errorTimeout = wcstol(argv[i], NULL, 10)*1000;
++i;
}
wcout << endl << L"Fix Error: " << errorNumberOfRetries << L" tries with " << (errorTimeout/1000) << L"s between each try.";
}
else if (wcscmp(argv[i], L"-v") == 0 || wcscmp(argv[i], L"-verbose") == 0)
{
++i;
auto verbose = wcstol(argv[i], NULL, 10);
ErrorsHandler::GetInstance().VerboseLevel = verbose;
wcout << endl << L"Verbose level: " << verbose;
++i;
}
else
{
wcout << endl << L"Error: unknown argument \"" << argv[i] << L"\".";
++i;
}
}
if (source.empty() || target.empty())
{
wcout << endl << L"Error: source and target are not optional." << endl << endl;
printHelp();
return 0;
}
wcout << endl << endl << L"----------" << endl << L"Sync in progress ..." << endl;
// Do the actual syncing
FolderPair syncPair = FolderPair(&source, &target, L"", filter, mode);
syncPair.Sync();
wcout << endl << L"Syncing is over." << endl << L"----------" << endl;
// Error retrying
for (int i = 0; i < errorNumberOfRetries; i++)
{
wcout << endl << L"Waiting " << (errorTimeout / 1000) << L"s" << endl;
Sleep(errorTimeout);
wcout << endl << L"Attempt " << i << L" out of " << errorNumberOfRetries << L"." << endl;
if (ErrorsHandler::GetInstance().FixAllErrors() == 0)
break;
}
/*
filter.AddCondition(L".*f[1-3]");
FolderPair Test1 = FolderPair(new String(L"C:\\TEST_SOURCE"), new String(L"C:\\TEST_TARGET"), L"", filter, CompareMode::Fast);
FolderPair Test2 = FolderPair(new String(L"C:\\TEST_SOURCE"), new String(L"C:\\TEST_TARGET"), L"", filter, CompareMode::Fast);
Test1.Sync();
wcout << endl << endl << endl;
Sleep(1000);
Test2.Sync();
wcout << endl << endl << endl;
Sleep(1000);
Test1.Sync();
Sleep(1000);
FolderPair Test3 = FolderPair(new String(L"C:\\Users\\Miow\\AppData"), new String(L"H:\\BACKUP\\AppData"), L"", filter, CompareMode::Fast);
//Test3.Sync();
wcout << endl << endl << L"###################" << endl << endl;
Sleep(5000);
ErrorsHandler::GetInstance().VerboseLevel = 2;
ErrorsHandler::GetInstance().FixAllErrors();
Sleep(10000);
*/
wcout << endl << endl << L"All done !" << endl;
return 1;
}