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

Commit d97386c3 authored by Daniel Rosenberg's avatar Daniel Rosenberg
Browse files

libsnapshot: Add silent option to inspect_cow

This adds the -s option to not print out Cow information, providing a
simple benchmark for reading and iterating through a cow file one time.

Bug: 172026020
Test: inspect cow -s [file]
Change-Id: I6e8b80a80c71155364b8467acd556d00f1dd2a42
parent c62fdd91
Loading
Loading
Loading
Loading
+38 −12
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
// limitations under the License.
//
#include <stdio.h>
#include <unistd.h>

#include <iostream>
#include <string>
@@ -34,7 +35,11 @@ void MyLogger(android::base::LogId, android::base::LogSeverity severity, const c
    }
}

static bool Inspect(const std::string& path) {
static void usage(void) {
    LOG(ERROR) << "Usage: inspect_cow [-s] <COW_FILE>";
}

static bool Inspect(const std::string& path, bool silent) {
    android::base::unique_fd fd(open(path.c_str(), O_RDONLY));
    if (fd < 0) {
        PLOG(ERROR) << "open failed: " << path;
@@ -52,19 +57,29 @@ static bool Inspect(const std::string& path) {
        LOG(ERROR) << "could not get header: " << path;
        return false;
    }
    CowFooter footer;
    bool has_footer = false;
    if (reader.GetFooter(&footer)) has_footer = true;

    if (!silent) {
        std::cout << "Major version: " << header.major_version << "\n";
        std::cout << "Minor version: " << header.minor_version << "\n";
        std::cout << "Header size: " << header.header_size << "\n";
        std::cout << "Footer size: " << header.footer_size << "\n";
        std::cout << "Block size: " << header.block_size << "\n";
        std::cout << "\n";
        if (has_footer) {
            std::cout << "Total Ops size: " << footer.op.ops_size << "\n";
            std::cout << "Number of Ops: " << footer.op.num_ops << "\n";
            std::cout << "\n";
        }
    }

    auto iter = reader.GetOpIter();
    while (!iter->Done()) {
        const CowOperation& op = iter->Get();

        std::cout << op << "\n";
        if (!silent) std::cout << op << "\n";

        iter->Next();
    }
@@ -76,14 +91,25 @@ static bool Inspect(const std::string& path) {
}  // namespace android

int main(int argc, char** argv) {
    int ch;
    bool silent = false;
    while ((ch = getopt(argc, argv, "s")) != -1) {
        switch (ch) {
            case 's':
                silent = true;
                break;
            default:
                android::snapshot::usage();
        }
    }
    android::base::InitLogging(argv, android::snapshot::MyLogger);

    if (argc < 2) {
        LOG(ERROR) << "Usage: inspect_cow <COW_FILE>";
    if (argc < optind + 1) {
        android::snapshot::usage();
        return 1;
    }

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