-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpts.cpp
281 lines (247 loc) · 8.88 KB
/
Opts.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
#include "Opts.h"
#include "Utils.h"
#include "Logging.h"
#include <grp.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <filesystem>
using namespace std;
namespace fs = filesystem;
// global options declaration
Opts O;
// default version - overriden in Makefile from git release tag
#ifndef VERSION_MAJOR
#define VERSION_MAJOR 0
#endif
#ifndef VERSION_MINOR
#define VERSION_MINOR 5
#endif
#define MAXARGVALLEN 1000 // maximum length of individual command line arguments
static void PrintHelp(int rval=0) {
fprintf (stderr, "%s\n", R"(
Usage: PhatBak <Operation> [Options] Repo[::Archive] [file/dir list]
use "man PhatBak" for details
)");
exit (rval);
}
static void ArgError (const char *arg, const char *nxtarg = NULL) {
const char *a = nxtarg ? nxtarg : "";
fprintf (stderr, "ERROR: Illegal Argument: %s %s\n", arg, a);
PrintHelp(1);
}
void PrintOpHelp () {
fprintf (stderr, "ERROR: Exactly one flag of type {c -c x -x t -t} must be given\n");
PrintHelp(1);
}
static void PrintVersion() {
printf ("Version: %d.%d\n", VERSION_MAJOR, VERSION_MINOR);
exit (0);
}
// maybe use this for sizes someday
#if 0
static int ParseSize (const char *SizeStr) {
const char *Save = SizeStr;
int Num = 0;
// handle number
while (*SizeStr) {
if (*SizeStr >= '0' && *SizeStr <= '9') {
Num *= 10;
Num += *SizeStr - '0';
} else {
break;
}
SizeStr++;
}
// handle units
if (!strcmp (SizeStr, "KiB"))
Num *= 1024;
else if (!strcmp (SizeStr, "MiB"))
Num *= 1024 * 1024;
else if (!strcmp (SizeStr, "GiB"))
Num *= 1024 * 1024 * 1024;
else if (!strcmp (SizeStr, "KB"))
Num *= 1000 ;
else if (!strcmp (SizeStr, "MB"))
Num *= 1000 * 1000;
else if (!strcmp (SizeStr, "GB"))
Num *= 1000 * 1000 * 1000;
else if (*SizeStr)
ArgError (Save);
if (!Num)
ArgError (Save);
return Num;
}
#endif
// macro to skip to the next argument
#define NXTARG { \
argidx++; \
if (argidx >= argc) \
ArgError (arg); \
arg = argv[argidx]; \
if (strlen (arg) >= (MAXARGVALLEN-2)) \
ArgError (arg); \
}
// parse command line options
#define TESTOP {if (Operation != DoUndef) PrintOpHelp();}
void Opts::ParseCmdLine (const int argc, const char *argv[]) {
// apply defaults
VersionMajor = VERSION_MAJOR;
VersionMinor = VERSION_MINOR;
Operation = DoUndef;
ShowFiles = 0;
NumThreads = 100;
CompType = CompType_ZTSD;
CompLevel = 2;
ChunkSize = 1 << 18;
HashType = HashType_MD5;
ExtractTarget = "PhatBakExtract";
DebugPrint = 0;
BlockNumModulus = 100;
Rebase = false;
CWD = fs::canonical(fs::current_path()); // resolves symlinks
// save command line
int argidx;
for (argidx=0; argidx < argc; argidx++) {
if (argidx)
CmdLine += " ";
CmdLine += argv[argidx];
}
// first arg is operation
// allow abbreviations
if (argc < 2 || argv[1][0] == '\0')
ERROR ("No operation given\n");
const char *arg = argv[1];
vecstr MatchNames;
for (OpEnum i = (OpEnum)((int)DoUndef+1); i < DoVoid; i = (OpEnum)((int)i + 1))
if (OpText(i).find (arg) == 0)
MatchNames.push_back(OpText(i));
if (MatchNames.size() == 0)
ERROR ("Operation (%s) not recognized\n", arg);
if (MatchNames.size() > 1)
ERROR ("Operation (%s) is ambiguous, use one of %s\n", arg, Utils::JoinStrs(MatchNames, ",").c_str());
if (OpText (DoInit ) == MatchNames[0]) Operation = DoInit ;
else if (OpText (DoCreate ) == MatchNames[0]) Operation = DoCreate ;
else if (OpText (DoExtract ) == MatchNames[0]) Operation = DoExtract ;
else if (OpText (DoTest ) == MatchNames[0]) Operation = DoTest ;
else if (OpText (DoCompare ) == MatchNames[0]) Operation = DoCompare ;
else if (OpText (DoList ) == MatchNames[0]) Operation = DoList ;
else if (OpText (DoShowLatest) == MatchNames[0]) Operation = DoShowLatest;
else if (OpText (DoVersion ) == MatchNames[0]) Operation = DoVersion ;
// basic operation must be set
if (Operation == DoUndef)
THROW_PBEXCEPTION ("Unrecognized OpText: %s", MatchNames[0]);
// process version here
if (Operation == DoVersion)
PrintVersion();
// step through all "-" and "--" args
for (argidx = 2; argidx < argc; argidx++) {
const char *arg = argv[argidx];
// test non-minus arg
if (arg[0] != '-') {
// it's the end of minus args
break;
}
// begin generic argument parsing
#define PARSE_MinusVal(Name, ValFmt, ValPtr, Action) { \
if (!strcmp (arg, Name)) { \
NXTARG \
if (sscanf (arg, ValFmt, ValPtr) != 1) \
ArgError (arg, arg); \
Action \
continue; \
} \
}
#define PARSE_MinusStr(Name, ValNam, Action) { \
if (!strcmp (arg, Name)) { \
NXTARG \
ValNam = arg; \
Action \
continue; \
} \
}
#define PARSE_MinusFlg(Name, Test, ValNam, Val, Action) { \
if (!strcmp (arg, Name)) { \
Test \
ValNam = Val; \
Action \
continue; \
} \
}
//int TmpInt = 0;
PARSE_MinusFlg ("-v" ,, ShowFiles , 1,)
PARSE_MinusFlg ("-D" ,, ShowFiles=ArchDiag, 1, )
PARSE_MinusVal ("-T" ,"%d", &NumThreads,)
PARSE_MinusStr ("--CompType" , arg, CompType = Comp::CompNameToEnum(arg);)
PARSE_MinusVal ("--CompLevel" ,"%d", &CompLevel,)
PARSE_MinusStr ("--HashType" , arg, HashType = HashNameToEnum(arg);)
PARSE_MinusVal ("--ChunkSize" ,"%d", &ChunkSize,)
PARSE_MinusVal ("--BlockNumModulus" ,"%d", &BlockNumModulus,)
PARSE_MinusFlg ("--rebase" ,, Rebase , 1,)
PARSE_MinusStr ("--BaseArchive" , BaseArchive, )
PARSE_MinusFlg ("-h" ,, arg , arg, PrintHelp();)
PARSE_MinusFlg ("-help" ,, arg , arg, PrintHelp();)
PARSE_MinusFlg ("--help" ,, arg , arg, PrintHelp();)
PARSE_MinusFlg ("--debug" ,, DebugPrint, 1, ;)
ArgError(arg);
}
// first remaining arg is the repo/archive name
if (argidx >= argc) {
printf ("No Repo::Archive argument given\n");
PrintHelp(1);
}
string RepoArchName = argv[argidx++];
vector <string> Parts = Utils::SplitStr (RepoArchName, "::");
if (Parts.size() > 2)
PrintHelp(1);
RepoDirName = Parts[0];
if (Parts.size() >= 2)
ArchDirName = Parts[1];
// remaining args are file/dir names for create or extract
for (; argidx < argc; argidx++) {
string FileName = argv[argidx];
// strip trailing /
if (!FileName.size())
continue;
if (FileName[FileName.size()-1] == '/')
FileName.resize(FileName.size()-1);
if (!FileName.size())
FileName = "/";
FileArgs.push_back (FileName);
}
// default to cwd for create
if (!FileArgs.size() && Operation == DoCreate)
FileArgs.push_back (".");
}
Opts::Opts () {
StartTime = Utils::TimeNowNs ();
StartTimeTxt = Utils::NsToText (StartTime);
}
void Opts::Print (fstream &F) {
F << OptsString ();
}
void Opts::Print (ostream &F) {
F << OptsString ();
}
string Opts::OptsString () {
stringstream F;
F << "Options:\n";
F << " CmdLine = " << CmdLine << endl;
F << " Operation = " << OpText() << endl;
F << " FileArgs = " << Utils::JoinStrs (FileArgs, " ") << endl;
F << " CWD = " << CWD << endl;
F << " RepoDirName = " << RepoDirName << endl;
F << " ArchDirName = " << ArchDirName << endl;
F << " BaseArchive = " << BaseArchive << endl;
F << " BlockNumModulus = " << BlockNumModulus << endl;
F << " ChunkSize = " << ChunkSize << endl;
F << " HashType = " << HashNames[HashType] << endl;
F << " CompType = " << CompNames[CompType] << endl;
F << " CompLevel = " << CompLevel << endl;
F << " ShowFiles = " << ShowFiles << endl;
F << " DebugPrint = " << DebugPrint << endl;
return F.str();
}