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 <iomanip>
20 #include <iostream>
21 #include <string>
22 #include <vector>
23 
24 #include <android-base/logging.h>
25 #include <android-base/unique_fd.h>
26 #include <libsnapshot/cow_reader.h>
27 
28 namespace android {
29 namespace snapshot {
30 
MyLogger(android::base::LogId,android::base::LogSeverity severity,const char *,const char *,unsigned int,const char * message)31 void MyLogger(android::base::LogId, android::base::LogSeverity severity, const char*, const char*,
32               unsigned int, const char* message) {
33     if (severity == android::base::ERROR) {
34         fprintf(stderr, "%s\n", message);
35     } else {
36         fprintf(stdout, "%s\n", message);
37     }
38 }
39 
usage(void)40 static void usage(void) {
41     LOG(ERROR) << "Usage: inspect_cow [-sd] <COW_FILE>";
42     LOG(ERROR) << "\t -s Run Silent";
43     LOG(ERROR) << "\t -d Attempt to decompress";
44     LOG(ERROR) << "\t -b Show data for failed decompress";
45     LOG(ERROR) << "\t -l Show ops";
46     LOG(ERROR) << "\t -m Show ops in reverse merge order";
47     LOG(ERROR) << "\t -n Show ops in merge order";
48     LOG(ERROR) << "\t -a Include merged ops in any merge order listing";
49     LOG(ERROR) << "\t -o Shows sequence op block order";
50     LOG(ERROR) << "\t -v Verifies merge order has no conflicts\n";
51 }
52 
53 enum OpIter { Normal, RevMerge, Merge };
54 
55 struct Options {
56     bool silent;
57     bool decompress;
58     bool show_ops;
59     bool show_bad;
60     bool show_seq;
61     bool verify_sequence;
62     OpIter iter_type;
63     bool include_merged;
64 };
65 
66 // Sink that always appends to the end of a string.
67 class StringSink : public IByteSink {
68   public:
GetBuffer(size_t requested,size_t * actual)69     void* GetBuffer(size_t requested, size_t* actual) override {
70         size_t old_size = stream_.size();
71         stream_.resize(old_size + requested, '\0');
72         *actual = requested;
73         return stream_.data() + old_size;
74     }
ReturnData(void *,size_t)75     bool ReturnData(void*, size_t) override { return true; }
Reset()76     void Reset() { stream_.clear(); }
77 
stream()78     std::string& stream() { return stream_; }
79 
80   private:
81     std::string stream_;
82 };
83 
ShowBad(CowReader & reader,const struct CowOperation & op)84 static void ShowBad(CowReader& reader, const struct CowOperation& op) {
85     size_t count;
86     auto buffer = std::make_unique<uint8_t[]>(op.data_length);
87 
88     if (!reader.GetRawBytes(op.source, buffer.get(), op.data_length, &count)) {
89         std::cerr << "Failed to read at all!\n";
90     } else {
91         std::cout << "The Block data is:\n";
92         for (int i = 0; i < op.data_length; i++) {
93             std::cout << std::hex << (int)buffer[i];
94         }
95         std::cout << std::dec << "\n\n";
96         if (op.data_length >= sizeof(CowOperation)) {
97             std::cout << "The start, as an op, would be " << *(CowOperation*)buffer.get() << "\n";
98         }
99     }
100 }
101 
Inspect(const std::string & path,Options opt)102 static bool Inspect(const std::string& path, Options opt) {
103     android::base::unique_fd fd(open(path.c_str(), O_RDONLY));
104     if (fd < 0) {
105         PLOG(ERROR) << "open failed: " << path;
106         return false;
107     }
108 
109     CowReader reader;
110     if (!reader.Parse(fd)) {
111         LOG(ERROR) << "parse failed: " << path;
112         return false;
113     }
114 
115     CowHeader header;
116     if (!reader.GetHeader(&header)) {
117         LOG(ERROR) << "could not get header: " << path;
118         return false;
119     }
120     CowFooter footer;
121     bool has_footer = false;
122     if (reader.GetFooter(&footer)) has_footer = true;
123 
124     if (!opt.silent) {
125         std::cout << "Major version: " << header.major_version << "\n";
126         std::cout << "Minor version: " << header.minor_version << "\n";
127         std::cout << "Header size: " << header.header_size << "\n";
128         std::cout << "Footer size: " << header.footer_size << "\n";
129         std::cout << "Block size: " << header.block_size << "\n";
130         std::cout << "Num merge ops: " << header.num_merge_ops << "\n";
131         std::cout << "RA buffer size: " << header.buffer_size << "\n";
132         std::cout << "\n";
133         if (has_footer) {
134             std::cout << "Total Ops size: " << footer.op.ops_size << "\n";
135             std::cout << "Number of Ops: " << footer.op.num_ops << "\n";
136             std::cout << "\n";
137         }
138     }
139 
140     if (opt.verify_sequence) {
141         if (reader.VerifyMergeOps()) {
142             std::cout << "\nMerge sequence is consistent.\n";
143         } else {
144             std::cout << "\nMerge sequence is inconsistent!\n";
145         }
146     }
147 
148     std::unique_ptr<ICowOpIter> iter;
149     if (opt.iter_type == Normal) {
150         iter = reader.GetOpIter();
151     } else if (opt.iter_type == RevMerge) {
152         iter = reader.GetRevMergeOpIter(opt.include_merged);
153     } else if (opt.iter_type == Merge) {
154         iter = reader.GetMergeOpIter(opt.include_merged);
155     }
156     StringSink sink;
157     bool success = true;
158     uint64_t xor_ops = 0, copy_ops = 0, replace_ops = 0, zero_ops = 0;
159     while (!iter->Done()) {
160         const CowOperation& op = iter->Get();
161 
162         if (!opt.silent && opt.show_ops) std::cout << op << "\n";
163 
164         if (opt.decompress && op.type == kCowReplaceOp && op.compression != kCowCompressNone) {
165             if (!reader.ReadData(op, &sink)) {
166                 std::cerr << "Failed to decompress for :" << op << "\n";
167                 success = false;
168                 if (opt.show_bad) ShowBad(reader, op);
169             }
170             sink.Reset();
171         }
172 
173         if (op.type == kCowSequenceOp && opt.show_seq) {
174             size_t read;
175             std::vector<uint32_t> merge_op_blocks;
176             size_t seq_len = op.data_length / sizeof(uint32_t);
177             merge_op_blocks.resize(seq_len);
178             if (!reader.GetRawBytes(op.source, merge_op_blocks.data(), op.data_length, &read)) {
179                 PLOG(ERROR) << "Failed to read sequence op!";
180                 return false;
181             }
182             if (!opt.silent) {
183                 std::cout << "Sequence for " << op << " is :\n";
184                 for (size_t i = 0; i < seq_len; i++) {
185                     std::cout << std::setfill('0') << std::setw(6) << merge_op_blocks[i] << ", ";
186                     if ((i + 1) % 10 == 0 || i + 1 == seq_len) std::cout << "\n";
187                 }
188             }
189         }
190 
191         if (op.type == kCowCopyOp) {
192             copy_ops++;
193         } else if (op.type == kCowReplaceOp) {
194             replace_ops++;
195         } else if (op.type == kCowZeroOp) {
196             zero_ops++;
197         } else if (op.type == kCowXorOp) {
198             xor_ops++;
199         }
200 
201         iter->Next();
202     }
203 
204     if (!opt.silent) {
205         auto total_ops = replace_ops + zero_ops + copy_ops + xor_ops;
206         std::cout << "Total-data-ops: " << total_ops << "Replace-ops: " << replace_ops
207                   << " Zero-ops: " << zero_ops << " Copy-ops: " << copy_ops
208                   << " Xor_ops: " << xor_ops << std::endl;
209     }
210 
211     return success;
212 }
213 
214 }  // namespace snapshot
215 }  // namespace android
216 
main(int argc,char ** argv)217 int main(int argc, char** argv) {
218     int ch;
219     struct android::snapshot::Options opt;
220     opt.silent = false;
221     opt.decompress = false;
222     opt.show_bad = false;
223     opt.iter_type = android::snapshot::Normal;
224     opt.verify_sequence = false;
225     opt.include_merged = false;
226     while ((ch = getopt(argc, argv, "sdbmnolva")) != -1) {
227         switch (ch) {
228             case 's':
229                 opt.silent = true;
230                 break;
231             case 'd':
232                 opt.decompress = true;
233                 break;
234             case 'b':
235                 opt.show_bad = true;
236                 break;
237             case 'm':
238                 opt.iter_type = android::snapshot::RevMerge;
239                 break;
240             case 'n':
241                 opt.iter_type = android::snapshot::Merge;
242                 break;
243             case 'o':
244                 opt.show_seq = true;
245                 break;
246             case 'l':
247                 opt.show_ops = true;
248                 break;
249             case 'v':
250                 opt.verify_sequence = true;
251                 break;
252             case 'a':
253                 opt.include_merged = true;
254                 break;
255             default:
256                 android::snapshot::usage();
257         }
258     }
259     android::base::InitLogging(argv, android::snapshot::MyLogger);
260 
261     if (argc < optind + 1) {
262         android::snapshot::usage();
263         return 1;
264     }
265 
266     if (!android::snapshot::Inspect(argv[optind], opt)) {
267         return 1;
268     }
269     return 0;
270 }
271