CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swissknife_main.cc
Go to the documentation of this file.
1 
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;
37 
38 void Usage() {
40  "CernVM-FS repository storage management commands\n"
41  "Version %s\n"
42  "Usage (normally called from cvmfs_server):\n"
43  " cvmfs_swissknife <command> [options]\n",
44  CVMFS_VERSION);
45 
46  for (unsigned i = 0; i < command_list.size(); ++i) {
48  "\n"
49  "Command %s\n"
50  "--------",
51  command_list[i]->GetName().c_str());
52  for (unsigned j = 0; j < command_list[i]->GetName().length(); ++j) {
54  }
57  command_list[i]->GetDescription().c_str());
58  swissknife::ParameterList params = command_list[i]->GetParams();
59  if (!params.empty()) {
60  LogCvmfs(kLogCvmfs, kLogStdout, "Options:");
61  for (unsigned j = 0; j < params.size(); ++j) {
63  params[j].key(), params[j].description().c_str());
64  if (params[j].optional())
65  LogCvmfs(kLogCvmfs, kLogStdout | kLogNoLinebreak, " (optional)");
67  }
68  } // Parameter list
69  } // Command list
70 
72 }
73 
74 
75 int main(int argc, char **argv) {
76  // Set default logging facilities
78 
79  command_list.push_back(new swissknife::CommandCreate());
80  command_list.push_back(new swissknife::CommandUpload());
81  command_list.push_back(new swissknife::CommandRemove());
82  command_list.push_back(new swissknife::CommandPeek());
83  command_list.push_back(new swissknife::CommandSync());
90  command_list.push_back(new swissknife::CommandSign());
91  command_list.push_back(new swissknife::CommandLetter());
92  command_list.push_back(new swissknife::CommandCheck());
94  command_list.push_back(new swissknife::CommandPull());
95  command_list.push_back(new swissknife::CommandZpipe());
96  command_list.push_back(new swissknife::CommandGraft());
97  command_list.push_back(new swissknife::CommandInfo());
100  command_list.push_back(new swissknife::CommandScrub());
101  command_list.push_back(new swissknife::CommandGc());
104  command_list.push_back(new swissknife::CommandLease());
105  command_list.push_back(new swissknife::Ingest());
106  command_list.push_back(new swissknife::CommandNotify());
108 
109  if (argc < 2) {
110  Usage();
111  return 1;
112  }
113  if ((string(argv[1]) == "--help")) {
114  Usage();
115  return 0;
116  }
117  if ((string(argv[1]) == "--version")) {
119  return 0;
120  }
121 
122  // find the command to be run
123  swissknife::Command *command = NULL;
124  for (unsigned i = 0; i < command_list.size(); ++i) {
125  if (command_list[i]->GetName() == string(argv[1])) {
126  command = command_list[i];
127  break;
128  }
129  }
130 
131  if (NULL == command) {
132  Usage();
133  return 1;
134  }
135 
136  bool display_statistics = false;
137 
138  // parse the command line arguments for the Command
140  optind = 1;
141  string option_string = "";
142  swissknife::ParameterList params = command->GetParams();
143  for (unsigned j = 0; j < params.size(); ++j) {
144  option_string.push_back(params[j].key());
145  if (!params[j].switch_only())
146  option_string.push_back(':');
147  }
148  // Now adding the generic -+ extra option command
149  option_string.push_back(swissknife::Command::kGenericParam);
150  option_string.push_back(':');
151  int c;
152  while ((c = getopt(argc, argv, option_string.c_str())) != -1) {
153  bool valid_option = false;
154  for (unsigned j = 0; j < params.size(); ++j) {
155  if (c == params[j].key()) {
157  valid_option = true;
158  args[c].Reset();
159  if (!params[j].switch_only()) {
160  args[c].Reset(new string(optarg));
161  }
162  break;
163  }
164  }
166  valid_option = true;
167  vector<string> flags = SplitString(
169  for (unsigned i = 0; i < flags.size(); ++i) {
170  if (flags[i] == "stats") {
171  display_statistics = true;
172  }
173  }
174  }
175  if (!valid_option) {
176  Usage();
177  return 1;
178  }
179  }
180  for (unsigned j = 0; j < params.size(); ++j) {
181  if (!params[j].optional()) {
182  if (args.find(params[j].key()) == args.end()) {
183  LogCvmfs(kLogCvmfs, kLogStderr, "parameter -%c missing",
184  params[j].key());
185  return 1;
186  }
187  }
188  }
189 
190  // run the command
191  string start_time = GetGMTimestamp();
192  const int retval = command->Main(args);
193  string finish_time = GetGMTimestamp();
194 
195  if (display_statistics) {
196  LogCvmfs(kLogCvmfs, kLogStdout, "Command statistics");
198  command->statistics()
200  .c_str());
201  }
202 
203  // delete the command list
204  Commands::const_iterator i = command_list.begin();
205  const Commands::const_iterator iend = command_list.end();
206  for (; i != iend; ++i) {
207  delete *i;
208  }
209  command_list.clear();
210 
211  return retval;
212 }
static const char kGenericParamSeparator
Definition: swissknife.h:78
virtual ParameterList GetParams() const =0
std::string GetGMTimestamp(const std::string &format)
Definition: string.cc:654
int Main(const ArgumentList &args)
std::vector< Parameter > ParameterList
Definition: swissknife.h:71
static void Set(LogFacilities info, LogFacilities error)
Definition: logging.cc:47
virtual int Main(const ArgumentList &args)=0
assert((mem||(size==0))&&"Out Of Memory")
Commands command_list
int main()
Definition: helper_allow.cc:16
vector< string > SplitString(const string &str, char delim)
Definition: string.cc:306
perf::Statistics * statistics()
Definition: server_tool.h:47
void Usage()
static const char kGenericParam
Definition: swissknife.h:77
std::map< char, SharedPtr< std::string > > ArgumentList
Definition: swissknife.h:72
std::string PrintList(const PrintOptions print_options)
Definition: statistics.cc:80
vector< swissknife::Command * > Commands
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:545