/Users/deen/code/yugabyte-db/src/yb/tools/fs_list-tool.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | // Tool to list local files and directories |
33 | | |
34 | | #include "yb/tools/fs_tool.h" |
35 | | |
36 | | #include <iostream> |
37 | | #include <vector> |
38 | | |
39 | | #include <gflags/gflags.h> |
40 | | #include <glog/logging.h> |
41 | | |
42 | | #include "yb/util/flags.h" |
43 | | #include "yb/util/logging.h" |
44 | | #include "yb/util/status.h" |
45 | | #include "yb/util/status_log.h" |
46 | | |
47 | | DEFINE_bool(verbose, false, |
48 | | "Print additional information (e.g., log segment headers)"); |
49 | | |
50 | | namespace yb { |
51 | | namespace tools { |
52 | | |
53 | | using std::string; |
54 | | using std::vector; |
55 | | |
56 | | namespace { |
57 | | |
58 | | enum CommandType { |
59 | | FS_TREE = 1, |
60 | | LIST_LOGS = 2, |
61 | | LIST_TABLETS = 3, |
62 | | }; |
63 | | |
64 | | // TODO: extract and generalized the "verb" handling code with other |
65 | | // tools such that it can be shared with other tools. |
66 | | |
67 | | struct CommandHandler { |
68 | | CommandType type_; |
69 | | string name_; |
70 | | string desc_; |
71 | | |
72 | | CommandHandler(CommandType type, string name, string desc) |
73 | 0 | : type_(type), name_(std::move(name)), desc_(std::move(desc)) {} |
74 | | }; |
75 | | |
76 | | const vector<CommandHandler> kCommandHandlers = { |
77 | | CommandHandler(FS_TREE, "tree", "Print out a file system tree." ), |
78 | | CommandHandler(LIST_LOGS, "list_logs", |
79 | | "List file system logs (optionally accepts a tablet id)."), |
80 | | CommandHandler(LIST_TABLETS, "list_tablets", "List tablets." ) }; |
81 | | |
82 | 0 | void PrintUsageToStream(const string& prog_name, std::ostream* out) { |
83 | 0 | *out << "Usage: " << prog_name << " [-verbose] " |
84 | 0 | << "-fs_wal_dirs <dirs> -fs_data_dirs <dirs> <command> [option] " |
85 | 0 | << std::endl << std::endl |
86 | 0 | << "Commands: " << std::endl; |
87 | 0 | for (const CommandHandler& handler : kCommandHandlers) { |
88 | 0 | *out << handler.name_ << ": " << handler.desc_ << std::endl; |
89 | 0 | } |
90 | 0 | } |
91 | | |
92 | 0 | void Usage(const string& prog_name, const string& msg) { |
93 | 0 | std::cerr << "Error " << prog_name << ": " << msg << std::endl |
94 | 0 | << std::endl; |
95 | 0 | PrintUsageToStream(prog_name, &std::cerr); |
96 | 0 | } |
97 | | |
98 | 0 | bool ValidateCommand(int argc, char** argv, CommandType* out) { |
99 | 0 | if (argc < 2) { |
100 | 0 | Usage(argv[0], "At least one command must be specified!"); |
101 | 0 | return false; |
102 | 0 | } |
103 | 0 | for (const CommandHandler& handler : kCommandHandlers) { |
104 | 0 | if (argv[1] == handler.name_) { |
105 | 0 | *out = handler.type_; |
106 | 0 | return true; |
107 | 0 | } |
108 | 0 | } |
109 | 0 | Usage("Invalid command specified ", argv[1]); |
110 | 0 | return false; |
111 | 0 | } |
112 | | |
113 | | } // anonymous namespace |
114 | | |
115 | 0 | static int FsListToolMain(int argc, char** argv) { |
116 | 0 | FLAGS_logtostderr = 1; |
117 | 0 | std::stringstream usage_str; |
118 | 0 | PrintUsageToStream(argv[0], &usage_str); |
119 | 0 | google::SetUsageMessage(usage_str.str()); |
120 | 0 | ParseCommandLineFlags(&argc, &argv, true); |
121 | 0 | InitGoogleLoggingSafe(argv[0]); |
122 | |
|
123 | 0 | CommandType cmd; |
124 | 0 | if (!ValidateCommand(argc, argv, &cmd)) { |
125 | 0 | return 2; |
126 | 0 | } |
127 | | |
128 | 0 | FsTool fs_tool(FLAGS_verbose ? FsTool::HEADERS_ONLY : FsTool::MINIMUM); |
129 | 0 | CHECK_OK_PREPEND(fs_tool.Init(), "Error initializing file system tool"); |
130 | |
|
131 | 0 | switch (cmd) { |
132 | 0 | case FS_TREE: { |
133 | 0 | CHECK_OK(fs_tool.FsTree()); |
134 | 0 | break; |
135 | 0 | } |
136 | 0 | case LIST_LOGS: { |
137 | 0 | if (argc > 2) { |
138 | 0 | CHECK_OK(fs_tool.ListLogSegmentsForTablet(argv[2])); |
139 | 0 | } else { |
140 | 0 | CHECK_OK(fs_tool.ListAllLogSegments()); |
141 | 0 | } |
142 | 0 | break; |
143 | 0 | } |
144 | 0 | case LIST_TABLETS: { |
145 | 0 | CHECK_OK(fs_tool.ListAllTablets()); |
146 | 0 | break; |
147 | 0 | } |
148 | 0 | } |
149 | | |
150 | 0 | return 0; |
151 | 0 | } |
152 | | |
153 | | } // namespace tools |
154 | | } // namespace yb |
155 | | |
156 | 13.2k | int main(int argc, char** argv) { |
157 | 13.2k | return yb::tools::FsListToolMain(argc, argv); |
158 | 13.2k | } |