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
|
/*
* Copyright (C) 2010 Vyatta, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <libgen.h>
#include <cli_cstore.h>
#include <cstore/unionfs/cstore-unionfs.hpp>
static int op_idx = -1;
static const char *op_bin_name[] = {
"my_set",
"my_delete",
"my_activate",
"my_deactivate",
"my_rename",
"my_copy",
"my_comment",
"my_discard",
"my_move",
NULL
};
static const char *op_Str[] = {
"Set",
"Delete",
"Activate",
"Deactivate",
"Rename",
"Copy",
"Comment",
"Discard",
"Move",
NULL
};
static const char *op_str[] = {
"set",
"delete",
"activate",
"deactivate",
"rename",
"copy",
"comment",
"discard",
"move",
NULL
};
static const bool op_need_cfg_node_args[] = {
true,
true,
true,
true,
true,
true,
true,
false,
true,
false // dummy
};
#define OP_Str op_Str[op_idx]
#define OP_str op_str[op_idx]
#define OP_need_cfg_node_args op_need_cfg_node_args[op_idx]
static void
doSet(Cstore& cstore, const vector<string>& path_comps)
{
if (!cstore.validateSetPath(path_comps)) {
bye("invalid set path\n");
}
if (!cstore.setCfgPath(path_comps)) {
bye("set cfg path failed\n");
}
}
static void
doDelete(Cstore& cstore, const vector<string>& path_comps)
{
vtw_def def;
if (!cstore.validateDeletePath(path_comps, def)) {
bye("invalid delete path");
}
if (!cstore.deleteCfgPath(path_comps, def)) {
bye("delete failed\n");
}
}
static void
doActivate(Cstore& cstore, const vector<string>& path_comps)
{
if (!cstore.validateActivatePath(path_comps)) {
bye("%s validate failed", OP_str);
}
if (!cstore.unmarkCfgPathDeactivated(path_comps)) {
bye("%s failed", OP_str);
}
}
static void
doDeactivate(Cstore& cstore, const vector<string>& path_comps)
{
if (!cstore.validateDeactivatePath(path_comps)) {
bye("%s validate failed", OP_str);
}
if (!cstore.markCfgPathDeactivated(path_comps)) {
bye("%s failed", OP_str);
}
}
static void
doRename(Cstore& cstore, const vector<string>& path_comps)
{
if (!cstore.validateRenameArgs(path_comps)) {
bye("invalid rename args\n");
}
if (!cstore.renameCfgPath(path_comps)) {
bye("rename cfg path failed\n");
}
}
static void
doCopy(Cstore& cstore, const vector<string>& path_comps)
{
if (!cstore.validateCopyArgs(path_comps)) {
bye("invalid copy args\n");
}
if (!cstore.copyCfgPath(path_comps)) {
bye("copy cfg path failed\n");
}
}
static void
doComment(Cstore& cstore, const vector<string>& path_comps)
{
vtw_def def;
if (!cstore.validateCommentArgs(path_comps, def)) {
bye("invalid comment args\n");
}
if (!cstore.commentCfgPath(path_comps, def)) {
bye("comment cfg path failed\n");
}
}
static void
doDiscard(Cstore& cstore, const vector<string>& args)
{
if (args.size() > 0) {
OUTPUT_USER("Invalid discard command\n");
bye("invalid discard command\n");
}
if (!cstore.discardChanges()) {
bye("discard failed\n");
}
}
static void
doMove(Cstore& cstore, const vector<string>& path_comps)
{
if (!cstore.validateMoveArgs(path_comps)) {
bye("invalid move args\n");
}
if (!cstore.moveCfgPath(path_comps)) {
bye("move cfg path failed\n");
}
}
typedef void (*OpFuncT)(Cstore& cstore,
const vector<string>& path_comps);
OpFuncT OpFunc[] = {
&doSet,
&doDelete,
&doActivate,
&doDeactivate,
&doRename,
&doCopy,
&doComment,
&doDiscard,
&doMove,
NULL
};
int
main(int argc, char **argv)
{
int i = 0;
while (op_bin_name[i]) {
if (strcmp(basename(argv[0]), op_bin_name[i]) == 0) {
op_idx = i;
break;
}
++i;
}
if (op_idx == -1) {
printf("Invalid operation\n");
exit(-1);
}
if (initialize_output(OP_Str) == -1) {
bye("can't initialize output\n");
}
if (OP_need_cfg_node_args && argc < 2) {
fprintf(out_stream, "Need to specify the config node to %s\n", OP_str);
bye("nothing to %s\n", OP_str);
}
// actual CLI operations use the edit levels from environment, so pass true.
UnionfsCstore cstore(true);
vector<string> path_comps;
for (int i = 1; i < argc; i++) {
path_comps.push_back(argv[i]);
}
// call the op function
OpFunc[op_idx](cstore, path_comps);
exit(0);
}
|