-
Notifications
You must be signed in to change notification settings - Fork 51
/
COptions.h
408 lines (370 loc) · 14.5 KB
/
COptions.h
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
/*
Copyright (c) 2011 Wangdera Corporation ([email protected])
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "CHoboCopyException.h"
#include "CParseOptionsException.h"
#include "Utilities.h"
#include "OutputWriter.h"
using namespace std;
class COptions
{
private:
bool _acceptAll;
VSS_BACKUP_TYPE _backupType;
bool _clearDestination;
bool _debug;
CString _destination;
vector<CString> _filespecs;
std::wregex* _ignorePattern;
bool _recursive;
bool _simulate;
bool _skipDenied;
CString _source;
CString _stateFile;
int _verbosityLevel;
public:
bool get_AcceptAll()
{
return _acceptAll;
}
VSS_BACKUP_TYPE get_BackupType()
{
return _backupType;
}
bool get_ClearDestination()
{
return _clearDestination;
}
bool get_Debug(void)
{
return _debug;
}
LPCTSTR get_Destination(void)
{
return _destination.GetString();
}
vector<CString>& get_Filespecs(void)
{
return _filespecs;
}
wregex* get_IgnorePattern(void)
{
return _ignorePattern;
}
bool get_Recursive(void)
{
return _recursive;
}
bool get_Simulate(void)
{
return _simulate;
}
bool get_SkipDenied(void)
{
return _skipDenied;
}
LPCTSTR get_StateFile()
{
return NullIfEmpty(_stateFile);
}
LPCTSTR get_Source(void)
{
return _source.GetString();
}
static LPCTSTR get_Usage(void)
{
return TEXT("Usage:\n\n")
TEXT("hobocopy [/statefile=FILE] [/verbosity=LEVEL]\n")
TEXT(" [ /full | /incremental ] [ /clear ] [ /skipdenied ] [ /y ]\n")
TEXT(" [ /simulate ] [/recursive]\n")
TEXT(" <src> <dest> [<file> [<file> [ ... ] ]\n")
TEXT("\n")
TEXT("Recursively copies a directory tree from <src> to <dest>.\n")
TEXT("\n")
TEXT("/statefile - Specifies a file where information about the copy will\n")
TEXT(" be written. This argument is required when /incremental\n")
TEXT(" is specified, as the date and time of the last copy is\n")
TEXT(" read from this file to determine which files should be\n")
TEXT(" copied.\n")
TEXT("\n")
TEXT("/verbosity - Specifies how much information HoboCopy will emit\n")
TEXT(" during copy. Legal values are: 0 - almost no\n")
TEXT(" information will be emitted. 1 - Only error information\n")
TEXT(" will be emitted. 2 - Errors and warnings will be\n")
TEXT(" emitted. 3 - Errors, warnings, and some status\n")
TEXT(" information will be emitted. 4 - Lots of diagnostic\n")
TEXT(" information will be emitted. The default level is 2.\n")
TEXT("\n")
TEXT("/full - Perform a full copy. All files will be copied\n")
TEXT(" regardless of modification date.\n")
TEXT("\n")
TEXT("/incremental - Perform an incremental copy. Only files that have\n")
TEXT(" changed since the last full copy will be copied.\n")
TEXT(" Specifying this switch requires the /statefile switch\n")
TEXT(" to be specified, as that's where the date of the last\n")
TEXT(" full copy is read from.\n")
TEXT("\n")
TEXT("/clear - Recursively delete the destination directory before\n")
TEXT(" copying. HoboCopy will ask for confirmation before\n")
TEXT(" deleting unless the /y switch is also specified.\n")
TEXT("\n")
TEXT("/skipdenied - By default, if HoboCopy does not have sufficient\n")
TEXT(" privilege to copy a file, the copy will fail with an\n")
TEXT(" error. When the /skipdenied switch is specified,\n")
TEXT(" permission errors trying to copy a source file result\n")
TEXT(" in the file being skipped and the copy continuing.\n")
TEXT("\n")
TEXT("/y - Instructs HoboCopy to proceed as if user answered yes\n")
TEXT(" to any confirmation prompts. Use with caution - in\n")
TEXT(" combination with the /clear switch, this switch will\n")
TEXT(" cause the destination directory to be deleted without\n")
TEXT(" confirmation.\n")
TEXT("\n")
TEXT("/simulate - Simulates copy only - no snapshot is taken and no copy\n")
TEXT(" is performed.\n")
TEXT("\n")
TEXT("/recursive - Copies subdirectories (including empty ones). Shortcut: /r\n")
TEXT("\n")
TEXT("<src> - The directory to copy (the source directory).\n")
TEXT("<dest> - The directory to copy to (the destination directory).\n")
TEXT("<file> - A file (e.g. foo.txt) or filespec (e.g. *.txt) to copy.\n")
TEXT(" Defaults to *.*.\n");
//TEXT("\n")
//TEXT("\n")
;
}
int get_VerbosityLevel(void)
{
return _verbosityLevel;
}
static COptions Parse(int argc, _TCHAR* argv[])
{
COptions options;
options._backupType = VSS_BT_FULL;
options._clearDestination = false;
options._verbosityLevel = VERBOSITY_LEVEL_NORMAL;
options._acceptAll = false;
options._skipDenied = false;
options._debug = false;
options._simulate = false;
options._recursive = false;
options._ignorePattern = NULL;
if (argc < 3)
{
throw new CParseOptionsException(TEXT("Wrong number of arguments."));
}
for (int i = 1; i < argc; ++i)
{
CString arg(argv[i]);
arg.MakeLower();
if (Utilities::StartsWith(arg, TEXT("/")) || Utilities::StartsWith(arg, TEXT("-")))
{
arg = arg.Mid(1);
if (arg.Compare(TEXT("full")) == 0)
{
options._backupType = VSS_BT_FULL;
}
else if (arg.Compare(TEXT("incremental")) == 0)
{
options._backupType = VSS_BT_INCREMENTAL;
}
else if (arg.Compare(TEXT("clear")) == 0)
{
options._clearDestination = true;
}
else if (Utilities::StartsWith(arg, TEXT("statefile=")))
{
options._stateFile = GetArgValue(arg);
}
else if (Utilities::StartsWith(arg, TEXT("verbosity=")))
{
options._verbosityLevel = _ttoi(GetArgValue(arg));
}
else if (Utilities::StartsWith(arg, TEXT("ignorepattern=")))
{
throw new CParseOptionsException(CString("The ignorepattern switch is no longer supported."));
}
else if (arg.Compare(TEXT("y")) == 0)
{
options._acceptAll = true;
}
else if (arg.Compare(TEXT("skipdenied")) == 0)
{
options._skipDenied = true;
}
else if (arg.Compare(TEXT("debug")) == 0)
{
options._debug = true;
}
else if (arg.Compare(TEXT("simulate")) == 0)
{
options._simulate = true;
}
else if (arg.Compare(TEXT("recursive")) == 0 || arg.Compare(TEXT("r")) == 0)
{
options._recursive = true;
}
else
{
CString message("Unrecognized switch: ");
message.Append(arg);
throw new CParseOptionsException(message);
}
}
else
{
if (options._source.IsEmpty())
{
options._source = argv[i];
}
else if (options._destination.IsEmpty())
{
options._destination = argv[i];
}
else
{
options._filespecs.push_back(argv[i]);
}
}
}
// Normalize paths to full paths
options._source = NormalizePath(options._source);
options._destination = NormalizePath(options._destination);
if (!options._stateFile.IsEmpty())
{
options._stateFile = NormalizePath(options._stateFile);
}
return options;
}
private:
static CString GetArgValue(const CString& arg)
{
int index = arg.Find(L'=');
if (index == -1)
{
CString message;
message.Format(TEXT("Couldn't parse the option value from %s"),
arg.GetString());
throw new CParseOptionsException(message.GetString());
}
return arg.Mid(index + 1);
}
static CString NormalizePath(LPCTSTR path)
{
int length = MAX_PATH;
while (true)
{
_TCHAR* normalizedPath = new _TCHAR[length];
LPTSTR filePart;
DWORD result = ::GetFullPathName(path, MAX_PATH, normalizedPath, &filePart);
if (result == 0)
{
DWORD error = ::GetLastError();
CString errorMessage;
Utilities::FormatErrorMessage(error, errorMessage);
CString message;
message.AppendFormat(TEXT("Error calling GetFullPathName: %s"), errorMessage);
throw new CHoboCopyException(message.GetString());
}
else if (result > MAX_PATH)
{
delete normalizedPath;
length = result;
}
else
{
return normalizedPath;
}
}
}
static LPCTSTR NullIfEmpty(CString& string)
{
if (string.GetLength() == 0)
{
return NULL;
}
else
{
return string.GetString();
}
}
static wregex* ParseRegex(const CString& userInput)
{
try
{
return new wregex(userInput.GetString());
}
catch (regex_error err)
{
switch (err.code())
{
case regex_constants::error_badbrace:
ThrowRegexParseException(TEXT("The expression contained an invlid count in a { } expression"));
break;
case regex_constants::error_badrepeat:
ThrowRegexParseException(TEXT("A repeat expression (one of '*', '?', '+', '{' in most contexts) was not preceded by an expression"));
break;
case regex_constants::error_brace:
ThrowRegexParseException(TEXT("The expression contained an unmatched '{' or '}'"));
break;
case regex_constants::error_brack:
ThrowRegexParseException(TEXT("The expression contained an unmatched '[' or ']'"));
break;
case regex_constants::error_collate:
ThrowRegexParseException(TEXT("The expression contained an invalid collating element name"));
break;
case regex_constants::error_complexity:
ThrowRegexParseException(TEXT("An attempted match failed because it was too complex"));
break;
case regex_constants::error_ctype:
ThrowRegexParseException(TEXT("The expression contained an invalid character class name"));
break;
case regex_constants::error_escape:
ThrowRegexParseException(TEXT("The expression contained an invalid escape sequence"));
break;
case regex_constants::error_paren:
ThrowRegexParseException(TEXT("The expression contained an unmatched '(' or ')'"));
break;
case regex_constants::error_range:
ThrowRegexParseException(TEXT("The expression contained an invalid character range specifier"));
break;
case regex_constants::error_space:
ThrowRegexParseException(TEXT("Parsing a regular expression failed because there were not enough resources available"));
break;
case regex_constants::error_stack:
ThrowRegexParseException(TEXT("An attempted match failed because there was not enough memory available"));
break;
case regex_constants::error_backref:
ThrowRegexParseException(TEXT("The expression contained an invalid back reference"));
break;
default:
ThrowRegexParseException(TEXT("Parse failed"));
break;
}
}
ThrowRegexParseException(TEXT("Parse failed"));
return NULL;
}
static void ThrowRegexParseException(const TCHAR* pszDetails)
{
CString message;
message.Format(TEXT("Couldn't parse regular expression supplied to /ignorepattern option (%s)"), pszDetails);
throw new CParseOptionsException(message);
}
};