GCC Code Coverage Report


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