• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2019 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include <sysexits.h>
18 
19 #include <chrono>
20 #include <iostream>
21 #include <map>
22 #include <sstream>
23 
24 #include <android-base/file.h>
25 #include <android-base/logging.h>
26 #include <android-base/unique_fd.h>
27 
28 #include <libsnapshot/snapshot.h>
29 
30 using namespace std::string_literals;
31 
Usage()32 int Usage() {
33     std::cerr << "snapshotctl: Control snapshots.\n"
34                  "Usage: snapshotctl [action] [flags]\n"
35                  "Actions:\n"
36                  "  dump\n"
37                  "    Print snapshot states.\n"
38                  "  merge\n"
39                  "    Deprecated.\n";
40     return EX_USAGE;
41 }
42 
43 namespace android {
44 namespace snapshot {
45 
DumpCmdHandler(int,char ** argv)46 bool DumpCmdHandler(int /*argc*/, char** argv) {
47     android::base::InitLogging(argv, &android::base::StderrLogger);
48     return SnapshotManager::New()->Dump(std::cout);
49 }
50 
MergeCmdHandler(int,char ** argv)51 bool MergeCmdHandler(int /*argc*/, char** argv) {
52     android::base::InitLogging(argv, &android::base::StderrLogger);
53     LOG(WARNING) << "Deprecated. Call update_engine_client --merge instead.";
54     return false;
55 }
56 
57 static std::map<std::string, std::function<bool(int, char**)>> kCmdMap = {
58         // clang-format off
59         {"dump", DumpCmdHandler},
60         {"merge", MergeCmdHandler},
61         // clang-format on
62 };
63 
64 }  // namespace snapshot
65 }  // namespace android
66 
main(int argc,char ** argv)67 int main(int argc, char** argv) {
68     using namespace android::snapshot;
69     if (argc < 2) {
70         return Usage();
71     }
72 
73     for (const auto& cmd : kCmdMap) {
74         if (cmd.first == argv[1]) {
75             return cmd.second(argc, argv) ? EX_OK : EX_SOFTWARE;
76         }
77     }
78 
79     return Usage();
80 }
81