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 " map\n"
41 " Map all partitions at /dev/block/mapper\n";
42 return EX_USAGE;
43 }
44
45 namespace android {
46 namespace snapshot {
47
DumpCmdHandler(int,char ** argv)48 bool DumpCmdHandler(int /*argc*/, char** argv) {
49 android::base::InitLogging(argv, &android::base::StderrLogger);
50 return SnapshotManager::New()->Dump(std::cout);
51 }
52
MapCmdHandler(int,char ** argv)53 bool MapCmdHandler(int, char** argv) {
54 android::base::InitLogging(argv, &android::base::StderrLogger);
55 using namespace std::chrono_literals;
56 return SnapshotManager::New()->MapAllSnapshots(5000ms);
57 }
58
UnmapCmdHandler(int,char ** argv)59 bool UnmapCmdHandler(int, char** argv) {
60 android::base::InitLogging(argv, &android::base::StderrLogger);
61 return SnapshotManager::New()->UnmapAllSnapshots();
62 }
63
MergeCmdHandler(int,char ** argv)64 bool MergeCmdHandler(int /*argc*/, char** argv) {
65 android::base::InitLogging(argv, &android::base::StderrLogger);
66 LOG(WARNING) << "Deprecated. Call update_engine_client --merge instead.";
67 return false;
68 }
69
70 static std::map<std::string, std::function<bool(int, char**)>> kCmdMap = {
71 // clang-format off
72 {"dump", DumpCmdHandler},
73 {"merge", MergeCmdHandler},
74 {"map", MapCmdHandler},
75 {"unmap", UnmapCmdHandler},
76 // clang-format on
77 };
78
79 } // namespace snapshot
80 } // namespace android
81
main(int argc,char ** argv)82 int main(int argc, char** argv) {
83 using namespace android::snapshot;
84 if (argc < 2) {
85 return Usage();
86 }
87
88 for (const auto& cmd : kCmdMap) {
89 if (cmd.first == argv[1]) {
90 return cmd.second(argc, argv) ? EX_OK : EX_SOFTWARE;
91 }
92 }
93
94 return Usage();
95 }
96