GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/swissknife_main.cc
Date: 2026-03-22 02:40:38
Exec Total Coverage
Lines: 0 114 0.0%
Branches: 0 54 0.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include <cassert>
6 #include <string>
7
8 #include "statistics_database.h"
9 #include "swissknife.h"
10 #include "swissknife_check.h"
11 #include "swissknife_filestats.h"
12 #include "swissknife_gc.h"
13 #include "swissknife_graft.h"
14 #include "swissknife_history.h"
15 #include "swissknife_info.h"
16 #include "swissknife_ingest.h"
17 #include "swissknife_ingestsql.h"
18 #include "swissknife_lease.h"
19 #include "swissknife_letter.h"
20 #include "swissknife_list_reflog.h"
21 #include "swissknife_lsrepo.h"
22 #include "swissknife_migrate.h"
23 #include "swissknife_notify.h"
24 #include "swissknife_overlay.h"
25 #include "swissknife_pull.h"
26 #include "swissknife_reflog.h"
27 #include "swissknife_rotate_statsdb.h"
28 #include "swissknife_scrub.h"
29 #include "swissknife_sign.h"
30 #include "swissknife_sync.h"
31 #include "swissknife_zpipe.h"
32 #include "util/logging.h"
33 #include "util/posix.h"
34 #include "util/string.h"
35
36 using namespace std; // NOLINT
37
38 typedef vector<swissknife::Command *> Commands;
39 Commands command_list;
40
41 void Usage() {
42 LogCvmfs(kLogCvmfs, kLogStdout,
43 "CernVM-FS repository storage management commands\n"
44 "Usage (normally called from cvmfs_server):\n"
45 " cvmfs_swissknife <command> [options]\n");
46
47 for (unsigned i = 0; i < command_list.size(); ++i) {
48 LogCvmfs(kLogCvmfs, kLogStdout | kLogNoLinebreak,
49 "\n"
50 "Command %s\n"
51 "--",
52 command_list[i]->GetName().c_str());
53 LogCvmfs(kLogCvmfs, kLogStdout | kLogNoLinebreak, "\n");
54 LogCvmfs(kLogCvmfs, kLogStdout, "%s",
55 command_list[i]->GetDescription().c_str());
56 swissknife::ParameterList params = command_list[i]->GetParams();
57 if (!params.empty()) {
58 LogCvmfs(kLogCvmfs, kLogStdout, "Options:");
59 for (unsigned j = 0; j < params.size(); ++j) {
60 LogCvmfs(kLogCvmfs, kLogStdout | kLogNoLinebreak, " -%c %s",
61 params[j].key(), params[j].description().c_str());
62 if (params[j].optional())
63 LogCvmfs(kLogCvmfs, kLogStdout | kLogNoLinebreak, " (optional)");
64 LogCvmfs(kLogCvmfs, kLogStdout | kLogNoLinebreak, "\n");
65 }
66 } // Parameter list
67 } // Command list
68
69 LogCvmfs(kLogCvmfs, kLogStdout | kLogNoLinebreak, "\n");
70 }
71
72
73 int main(int argc, char **argv) {
74 // Set default logging facilities
75 DefaultLogging::Set(kLogStdout, kLogStderr);
76
77 command_list.push_back(new swissknife::CommandCreate());
78 command_list.push_back(new swissknife::CommandUpload());
79 command_list.push_back(new swissknife::CommandRemove());
80 command_list.push_back(new swissknife::CommandPeek());
81 command_list.push_back(new swissknife::CommandSync());
82 command_list.push_back(new swissknife::CommandApplyDirtab());
83 command_list.push_back(new swissknife::CommandEditTag());
84 command_list.push_back(new swissknife::CommandListTags());
85 command_list.push_back(new swissknife::CommandInfoTag());
86 command_list.push_back(new swissknife::CommandRollbackTag());
87 command_list.push_back(new swissknife::CommandEmptyRecycleBin());
88 command_list.push_back(new swissknife::CommandSign());
89 command_list.push_back(new swissknife::CommandLetter());
90 command_list.push_back(new swissknife::CommandCheck());
91 command_list.push_back(new swissknife::CommandListCatalogs());
92 command_list.push_back(new swissknife::CommandPull());
93 command_list.push_back(new swissknife::CommandZpipe());
94 command_list.push_back(new swissknife::CommandGraft());
95 command_list.push_back(new swissknife::CommandInfo());
96 command_list.push_back(new swissknife::CommandVersion());
97 command_list.push_back(new swissknife::CommandMigrate());
98 command_list.push_back(new swissknife::CommandScrub());
99 command_list.push_back(new swissknife::CommandGc());
100 command_list.push_back(new swissknife::CommandListReflog());
101 command_list.push_back(new swissknife::CommandReconstructReflog());
102 command_list.push_back(new swissknife::CommandLease());
103 command_list.push_back(new swissknife::Ingest());
104 command_list.push_back(new swissknife::IngestSQL());
105 command_list.push_back(new swissknife::CommandNotify());
106 command_list.push_back(new swissknife::CommandOverlay());
107 command_list.push_back(new swissknife::CommandFileStats());
108 command_list.push_back(new swissknife::CommandRotateStatsDB());
109
110 if (argc < 2) {
111 Usage();
112 return 1;
113 }
114 if ((string(argv[1]) == "--help")) {
115 Usage();
116 return 0;
117 }
118 if ((string(argv[1]) == "--version")) {
119 swissknife::CommandVersion().Main(swissknife::ArgumentList());
120 return 0;
121 }
122
123 // find the command to be run
124 swissknife::Command *command = NULL;
125 for (unsigned i = 0; i < command_list.size(); ++i) {
126 if (command_list[i]->GetName() == string(argv[1])) {
127 command = command_list[i];
128 break;
129 }
130 }
131
132 if (NULL == command) {
133 Usage();
134 return 1;
135 }
136
137 bool display_statistics = false;
138
139 // parse the command line arguments for the Command
140 swissknife::ArgumentList args;
141 optind = 1;
142 string option_string = "";
143 swissknife::ParameterList params = command->GetParams();
144 for (unsigned j = 0; j < params.size(); ++j) {
145 option_string.push_back(params[j].key());
146 if (!params[j].switch_only())
147 option_string.push_back(':');
148 }
149 // Now adding the generic -+ extra option command
150 option_string.push_back(swissknife::Command::kGenericParam);
151 option_string.push_back(':');
152 int c;
153 while ((c = getopt(argc, argv, option_string.c_str())) != -1) {
154 bool valid_option = false;
155 for (unsigned j = 0; j < params.size(); ++j) {
156 if (c == params[j].key()) {
157 assert(c != swissknife::Command::kGenericParam);
158 valid_option = true;
159 args[c].Reset();
160 if (!params[j].switch_only()) {
161 args[c].Reset(new string(optarg));
162 }
163 break;
164 }
165 }
166 if (c == swissknife::Command::kGenericParam) {
167 valid_option = true;
168 vector<string> flags = SplitString(
169 optarg, swissknife::Command::kGenericParamSeparator);
170 for (unsigned i = 0; i < flags.size(); ++i) {
171 if (flags[i] == "stats") {
172 display_statistics = true;
173 }
174 }
175 }
176 if (!valid_option) {
177 Usage();
178 return 1;
179 }
180 }
181 for (unsigned j = 0; j < params.size(); ++j) {
182 if (!params[j].optional()) {
183 if (args.find(params[j].key()) == args.end()) {
184 LogCvmfs(kLogCvmfs, kLogStderr, "parameter -%c missing",
185 params[j].key());
186 return 1;
187 }
188 }
189 }
190
191 // run the command
192 const string start_time = GetGMTimestamp();
193 const int retval = command->Main(args);
194 const string finish_time = GetGMTimestamp();
195
196 if (display_statistics) {
197 LogCvmfs(kLogCvmfs, kLogStdout, "Command statistics");
198 LogCvmfs(kLogCvmfs, kLogStdout, "%s",
199 command->statistics()
200 ->PrintList(perf::Statistics::kPrintHeader)
201 .c_str());
202 }
203
204 // delete the command list
205 Commands::const_iterator i = command_list.begin();
206 const Commands::const_iterator iend = command_list.end();
207 for (; i != iend; ++i) {
208 delete *i;
209 }
210 command_list.clear();
211
212 return retval;
213 }
214