1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "common/debug.h"
16 #include "common/expected.h"
17 #include "inode2filename/search_directories.h"
18
19 using namespace iorap::inode2filename; // NOLINT
20
21 #if defined(IORAP_INODE2FILENAME_MAIN)
22
Usage(char ** argv)23 void Usage(char** argv) {
24 std::cerr << "Usage: " << argv[0] << " <options> <<inode_syntax>> [inode_syntax1 inode_syntax2 ...]" << std::endl;
25 std::cerr << "" << std::endl;
26 std::cerr << " Block until all inodes have been read in, then begin searching for filenames for those inodes." << std::endl;
27 std::cerr << " Results are written immediately as they are available, and once all inodes are found, " << std::endl;
28 std::cerr << " the program will terminate." << std::endl;
29 std::cerr << "" << std::endl;
30 std::cerr << " Inode syntax: ('dev_t@inode' | 'major:minor:inode')" << std::endl;
31 std::cerr << " --help,-h Print this Usage." << std::endl;
32 std::cerr << " --root,-r Add root directory (default '.'). Repeatable." << std::endl;
33 std::cerr << " --verbose,-v Set verbosity (default off)." << std::endl;
34 std::cerr << " --wait,-w Wait for key stroke before continuing (default off)." << std::endl;
35 exit(1);
36 }
37
GetSearchDirectoriesComponent()38 static fruit::Component<SearchDirectories> GetSearchDirectoriesComponent() {
39 return fruit::createComponent().bind<SystemCall, SystemCallImpl>();
40 }
41
main(int argc,char ** argv)42 int main(int argc, char** argv) {
43 android::base::InitLogging(argv);
44 android::base::SetLogger(android::base::StderrLogger);
45
46 bool wait_for_keystroke = false;
47 bool enable_verbose = false;
48 std::vector<std::string> root_directories;
49 std::vector<Inode> inode_list;
50
51 if (argc == 1) {
52 Usage(argv);
53 }
54
55 for (int arg = 1; arg < argc; ++arg) {
56 std::string argstr = argv[arg];
57 bool has_arg_next = (arg+1)<argc;
58 std::string arg_next = has_arg_next ? argv[arg+1] : "";
59
60 if (argstr == "--help" || argstr == "-h") {
61 Usage(argv);
62 } else if (argstr == "--root" || argstr == "-r") {
63 if (!has_arg_next) {
64 std::cerr << "Missing --root <value>" << std::endl;
65 return 1;
66 }
67 root_directories.push_back(arg_next);
68 ++arg;
69 } else if (argstr == "--verbose" || argstr == "-v") {
70 enable_verbose = true;
71 } else if (argstr == "--wait" || argstr == "-w") {
72 wait_for_keystroke = true;
73 } else {
74 Inode maybe_inode{};
75
76 std::string error_msg;
77 if (Inode::Parse(argstr, /*out*/&maybe_inode, /*out*/&error_msg)) {
78 inode_list.push_back(maybe_inode);
79 } else {
80 if (argstr.size() >= 1) {
81 if (argstr[0] == '-') {
82 std::cerr << "Unrecognized flag: " << argstr << std::endl;
83 return 1;
84 }
85 }
86
87 std::cerr << "Failed to parse inode (" << argstr << ") because: " << error_msg << std::endl;
88 return 1;
89 }
90 }
91 }
92
93 if (root_directories.size() == 0) {
94 root_directories.push_back(".");
95 }
96
97 if (inode_list.size() == 0) {
98 DCHECK_EQ(true, false);
99 std::cerr << "Provide at least one inode." << std::endl;
100 return 1;
101 }
102
103 if (enable_verbose) {
104 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
105
106 LOG(VERBOSE) << "Verbose check";
107 LOG(VERBOSE) << "Debug check: " << ::iorap::kIsDebugBuild;
108
109 for (auto& inode_num : inode_list) {
110 LOG(VERBOSE) << "Searching for inode " << inode_num;
111 }
112 }
113
114 // Useful to attach a debugger...
115 // 1) $> inode2filename -w <args>
116 // 2) $> gdbclient <pid>
117 if (wait_for_keystroke) {
118 LOG(INFO) << "Self pid: " << getpid();
119 LOG(INFO) << "Press any key to continue...";
120 std::cin >> wait_for_keystroke;
121 }
122
123 fruit::Injector<SearchDirectories> injector(GetSearchDirectoriesComponent);
124 SearchDirectories* search_directories = injector.get<SearchDirectories*>();
125
126 auto/*observable[2]*/ [inode_results, connectable] =
127 search_directories->FindFilenamesFromInodesPair(
128 std::move(root_directories),
129 std::move(inode_list),
130 SearchMode::kInProcessDirect);
131
132 int return_code = 1;
133 inode_results.subscribe([&return_code](const InodeResult& result) {
134 if (result) {
135 LOG(DEBUG) << "Inode match: " << result.inode << ", " << result.data.value();
136 std::cout << "Inode match: " << result.inode << ", " << result.data.value() << std::endl;
137 return_code = 0;
138 } else {
139 LOG(WARNING) << "Failed to match inode: " << result.inode;
140 }
141 });
142
143 // Normally #subscribe would start emitting items immediately, but this does nothing yet
144 // because one of the nodes in the flow graph was published. Published streams make the entire
145 // downstream inert until #connect is called.
146 connectable->connect();
147
148 // 0 -> found at least a single match, 1 -> could not find any matches.
149 return return_code;
150 }
151
152 #endif
153