Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 691b78a0 authored by David Anderson's avatar David Anderson
Browse files

inspect_cow: Switch to gflags.

gflags is a lot cleaner to use than getopt, and we don't really need any
advanced argument parsing.

Test: inspect_cow
Bug: N/A
Change-Id: I19007a787a2e5cd3a67486c7949b34ba76f762e4
parent 76622d44
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -417,6 +417,7 @@ cc_binary {
        "libbrotli",
        "libcrypto_static",
        "liblog",
        "libgflags",
        "libsnapshot_cow",
        "libz",
    ],
+34 −86
Original line number Diff line number Diff line
@@ -24,8 +24,19 @@

#include <android-base/logging.h>
#include <android-base/unique_fd.h>
#include <gflags/gflags.h>
#include <libsnapshot/cow_reader.h>

DEFINE_bool(silent, false, "Run silently");
DEFINE_bool(decompress, false, "Attempt to decompress data ops");
DEFINE_bool(show_bad_data, false, "If an op fails to decompress, show its daw data");
DEFINE_bool(show_ops, false, "Print all opcode information");
DEFINE_string(order, "", "If show_ops is true, change the order (either merge or reverse-merge)");
DEFINE_bool(show_merged, false,
            "If show_ops is true, and order is merge or reverse-merge, include merged ops");
DEFINE_bool(verify_merge_sequence, false, "Verify merge order sequencing");
DEFINE_bool(show_merge_sequence, false, "Show merge order sequence");

namespace android {
namespace snapshot {

@@ -38,32 +49,6 @@ void MyLogger(android::base::LogId, android::base::LogSeverity severity, const c
    }
}

static void usage(void) {
    std::cerr << "Usage: inspect_cow [-sd] <COW_FILE>\n";
    std::cerr << "\t -s Run Silent\n";
    std::cerr << "\t -d Attempt to decompress\n";
    std::cerr << "\t -b Show data for failed decompress\n";
    std::cerr << "\t -l Show ops\n";
    std::cerr << "\t -m Show ops in reverse merge order\n";
    std::cerr << "\t -n Show ops in merge order\n";
    std::cerr << "\t -a Include merged ops in any merge order listing\n";
    std::cerr << "\t -o Shows sequence op block order\n";
    std::cerr << "\t -v Verifies merge order has no conflicts\n";
}

enum OpIter { Normal, RevMerge, Merge };

struct Options {
    bool silent;
    bool decompress;
    bool show_ops;
    bool show_bad;
    bool show_seq;
    bool verify_sequence;
    OpIter iter_type;
    bool include_merged;
};

static void ShowBad(CowReader& reader, const struct CowOperation* op) {
    size_t count;
    auto buffer = std::make_unique<uint8_t[]>(op->data_length);
@@ -82,7 +67,7 @@ static void ShowBad(CowReader& reader, const struct CowOperation* op) {
    }
}

static bool Inspect(const std::string& path, Options opt) {
static bool Inspect(const std::string& path) {
    android::base::unique_fd fd(open(path.c_str(), O_RDONLY));
    if (fd < 0) {
        PLOG(ERROR) << "open failed: " << path;
@@ -103,7 +88,7 @@ static bool Inspect(const std::string& path, Options opt) {
    bool has_footer = false;
    if (reader.GetFooter(&footer)) has_footer = true;

    if (!opt.silent) {
    if (!FLAGS_silent) {
        std::cout << "Version: " << header.prefix.major_version << "."
                  << header.prefix.minor_version << "\n";
        std::cout << "Header size: " << header.prefix.header_size << "\n";
@@ -119,11 +104,11 @@ static bool Inspect(const std::string& path, Options opt) {
        }
    }

    if (!opt.silent) {
    if (!FLAGS_silent) {
        std::cout << "Parse time: " << (parse_time.count() * 1000) << "ms\n";
    }

    if (opt.verify_sequence) {
    if (FLAGS_verify_merge_sequence) {
        std::cout << "\n";
        if (reader.VerifyMergeOps()) {
            std::cout << "\nMerge sequence is consistent.\n";
@@ -133,12 +118,12 @@ static bool Inspect(const std::string& path, Options opt) {
    }

    std::unique_ptr<ICowOpIter> iter;
    if (opt.iter_type == Normal) {
    if (FLAGS_order.empty()) {
        iter = reader.GetOpIter();
    } else if (opt.iter_type == RevMerge) {
        iter = reader.GetRevMergeOpIter(opt.include_merged);
    } else if (opt.iter_type == Merge) {
        iter = reader.GetMergeOpIter(opt.include_merged);
    } else if (FLAGS_order == "reverse-merge") {
        iter = reader.GetRevMergeOpIter(FLAGS_show_merged);
    } else if (FLAGS_order == "merge") {
        iter = reader.GetMergeOpIter(FLAGS_show_merged);
    }

    std::string buffer(header.block_size, '\0');
@@ -148,17 +133,17 @@ static bool Inspect(const std::string& path, Options opt) {
    while (!iter->AtEnd()) {
        const CowOperation* op = iter->Get();

        if (!opt.silent && opt.show_ops) std::cout << *op << "\n";
        if (!FLAGS_silent && FLAGS_show_ops) std::cout << *op << "\n";

        if (opt.decompress && op->type == kCowReplaceOp && op->compression != kCowCompressNone) {
        if (FLAGS_decompress && op->type == kCowReplaceOp && op->compression != kCowCompressNone) {
            if (reader.ReadData(op, buffer.data(), buffer.size()) < 0) {
                std::cerr << "Failed to decompress for :" << *op << "\n";
                success = false;
                if (opt.show_bad) ShowBad(reader, op);
                if (FLAGS_show_bad_data) ShowBad(reader, op);
            }
        }

        if (op->type == kCowSequenceOp && opt.show_seq) {
        if (op->type == kCowSequenceOp && FLAGS_show_merge_sequence) {
            size_t read;
            std::vector<uint32_t> merge_op_blocks;
            size_t seq_len = op->data_length / sizeof(uint32_t);
@@ -167,7 +152,7 @@ static bool Inspect(const std::string& path, Options opt) {
                PLOG(ERROR) << "Failed to read sequence op!";
                return false;
            }
            if (!opt.silent) {
            if (!FLAGS_silent) {
                std::cout << "Sequence for " << *op << " is :\n";
                for (size_t i = 0; i < seq_len; i++) {
                    std::cout << std::setfill('0') << std::setw(6) << merge_op_blocks[i] << ", ";
@@ -189,7 +174,7 @@ static bool Inspect(const std::string& path, Options opt) {
        iter->Next();
    }

    if (!opt.silent) {
    if (!FLAGS_silent) {
        auto total_ops = replace_ops + zero_ops + copy_ops + xor_ops;
        std::cout << "Data ops: " << total_ops << "\n";
        std::cout << "Replace ops: " << replace_ops << "\n";
@@ -205,57 +190,20 @@ static bool Inspect(const std::string& path, Options opt) {
}  // namespace android

int main(int argc, char** argv) {
    int ch;
    struct android::snapshot::Options opt;
    opt.silent = false;
    opt.decompress = false;
    opt.show_bad = false;
    opt.iter_type = android::snapshot::Normal;
    opt.verify_sequence = false;
    opt.include_merged = false;
    while ((ch = getopt(argc, argv, "sdbmnolva")) != -1) {
        switch (ch) {
            case 's':
                opt.silent = true;
                break;
            case 'd':
                opt.decompress = true;
                break;
            case 'b':
                opt.show_bad = true;
                break;
            case 'm':
                opt.iter_type = android::snapshot::RevMerge;
                break;
            case 'n':
                opt.iter_type = android::snapshot::Merge;
                break;
            case 'o':
                opt.show_seq = true;
                break;
            case 'l':
                opt.show_ops = true;
                break;
            case 'v':
                opt.verify_sequence = true;
                break;
            case 'a':
                opt.include_merged = true;
                break;
            default:
                android::snapshot::usage();
    gflags::ParseCommandLineFlags(&argc, &argv, true);

    if (argc < 2) {
        gflags::ShowUsageWithFlags(argv[0]);
        return 1;
    }
    }

    if (argc < optind + 1) {
        android::snapshot::usage();
    if (FLAGS_order != "" && FLAGS_order != "merge" && FLAGS_order != "reverse-merge") {
        std::cerr << "Order must either be \"merge\" or \"reverse-merge\".\n";
        return 1;
    }

    android::base::InitLogging(argv, android::snapshot::MyLogger);

    if (!android::snapshot::Inspect(argv[optind], opt)) {
    if (!android::snapshot::Inspect(argv[1])) {
        return 1;
    }
    return 0;