1 //
2 // Copyright (C) 2020 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 #include <stdio.h>
17 #include <unistd.h>
18 
19 #include <iostream>
20 #include <string>
21 
22 #include <android-base/logging.h>
23 #include <android-base/unique_fd.h>
24 #include <libsnapshot/cow_reader.h>
25 
26 namespace android {
27 namespace snapshot {
28 
MyLogger(android::base::LogId,android::base::LogSeverity severity,const char *,const char *,unsigned int,const char * message)29 void MyLogger(android::base::LogId, android::base::LogSeverity severity, const char*, const char*,
30               unsigned int, const char* message) {
31     if (severity == android::base::ERROR) {
32         fprintf(stderr, "%s\n", message);
33     } else {
34         fprintf(stdout, "%s\n", message);
35     }
36 }
37 
usage(void)38 static void usage(void) {
39     LOG(ERROR) << "Usage: inspect_cow [-sd] <COW_FILE>";
40     LOG(ERROR) << "\t -s Run Silent";
41     LOG(ERROR) << "\t -d Attempt to decompress";
42     LOG(ERROR) << "\t -b Show data for failed decompress\n";
43 }
44 
45 // Sink that always appends to the end of a string.
46 class StringSink : public IByteSink {
47   public:
GetBuffer(size_t requested,size_t * actual)48     void* GetBuffer(size_t requested, size_t* actual) override {
49         size_t old_size = stream_.size();
50         stream_.resize(old_size + requested, '\0');
51         *actual = requested;
52         return stream_.data() + old_size;
53     }
ReturnData(void *,size_t)54     bool ReturnData(void*, size_t) override { return true; }
Reset()55     void Reset() { stream_.clear(); }
56 
stream()57     std::string& stream() { return stream_; }
58 
59   private:
60     std::string stream_;
61 };
62 
ShowBad(CowReader & reader,const struct CowOperation & op)63 static void ShowBad(CowReader& reader, const struct CowOperation& op) {
64     size_t count;
65     auto buffer = std::make_unique<uint8_t[]>(op.data_length);
66 
67     if (!reader.GetRawBytes(op.source, buffer.get(), op.data_length, &count)) {
68         std::cerr << "Failed to read at all!\n";
69     } else {
70         std::cout << "The Block data is:\n";
71         for (int i = 0; i < op.data_length; i++) {
72             std::cout << std::hex << (int)buffer[i];
73         }
74         std::cout << std::dec << "\n\n";
75         if (op.data_length >= sizeof(CowOperation)) {
76             std::cout << "The start, as an op, would be " << *(CowOperation*)buffer.get() << "\n";
77         }
78     }
79 }
80 
Inspect(const std::string & path,bool silent,bool decompress,bool show_bad)81 static bool Inspect(const std::string& path, bool silent, bool decompress, bool show_bad) {
82     android::base::unique_fd fd(open(path.c_str(), O_RDONLY));
83     if (fd < 0) {
84         PLOG(ERROR) << "open failed: " << path;
85         return false;
86     }
87 
88     CowReader reader;
89     if (!reader.Parse(fd)) {
90         LOG(ERROR) << "parse failed: " << path;
91         return false;
92     }
93 
94     CowHeader header;
95     if (!reader.GetHeader(&header)) {
96         LOG(ERROR) << "could not get header: " << path;
97         return false;
98     }
99     CowFooter footer;
100     bool has_footer = false;
101     if (reader.GetFooter(&footer)) has_footer = true;
102 
103     if (!silent) {
104         std::cout << "Major version: " << header.major_version << "\n";
105         std::cout << "Minor version: " << header.minor_version << "\n";
106         std::cout << "Header size: " << header.header_size << "\n";
107         std::cout << "Footer size: " << header.footer_size << "\n";
108         std::cout << "Block size: " << header.block_size << "\n";
109         std::cout << "\n";
110         if (has_footer) {
111             std::cout << "Total Ops size: " << footer.op.ops_size << "\n";
112             std::cout << "Number of Ops: " << footer.op.num_ops << "\n";
113             std::cout << "\n";
114         }
115     }
116 
117     auto iter = reader.GetOpIter();
118     StringSink sink;
119     bool success = true;
120     while (!iter->Done()) {
121         const CowOperation& op = iter->Get();
122 
123         if (!silent) std::cout << op << "\n";
124 
125         if (decompress && op.type == kCowReplaceOp && op.compression != kCowCompressNone) {
126             if (!reader.ReadData(op, &sink)) {
127                 std::cerr << "Failed to decompress for :" << op << "\n";
128                 success = false;
129                 if (show_bad) ShowBad(reader, op);
130             }
131             sink.Reset();
132         }
133 
134         iter->Next();
135     }
136 
137     return success;
138 }
139 
140 }  // namespace snapshot
141 }  // namespace android
142 
main(int argc,char ** argv)143 int main(int argc, char** argv) {
144     int ch;
145     bool silent = false;
146     bool decompress = false;
147     bool show_bad = false;
148     while ((ch = getopt(argc, argv, "sdb")) != -1) {
149         switch (ch) {
150             case 's':
151                 silent = true;
152                 break;
153             case 'd':
154                 decompress = true;
155                 break;
156             case 'b':
157                 show_bad = true;
158                 break;
159             default:
160                 android::snapshot::usage();
161         }
162     }
163     android::base::InitLogging(argv, android::snapshot::MyLogger);
164 
165     if (argc < optind + 1) {
166         android::snapshot::usage();
167         return 1;
168     }
169 
170     if (!android::snapshot::Inspect(argv[optind], silent, decompress, show_bad)) {
171         return 1;
172     }
173     return 0;
174 }
175