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

Commit cf0b935a authored by Daniel Rosenberg's avatar Daniel Rosenberg Committed by Automerger Merge Worker
Browse files

Merge "libsnapshot: Add decompression check to Inspect_Cow" am: ff5f4cc7

Original change: https://android-review.googlesource.com/c/platform/system/core/+/1533827

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I2b971727bf8b5cca77b42f1c8e646f62a88006ba
parents 588f732b ff5f4cc7
Loading
Loading
Loading
Loading
+39 −5
Original line number Diff line number Diff line
@@ -36,10 +36,30 @@ void MyLogger(android::base::LogId, android::base::LogSeverity severity, const c
}

static void usage(void) {
    LOG(ERROR) << "Usage: inspect_cow [-s] <COW_FILE>";
    LOG(ERROR) << "Usage: inspect_cow [-sd] <COW_FILE>";
    LOG(ERROR) << "\t -s Run Silent";
    LOG(ERROR) << "\t -d Attempt to decompress\n";
}

static bool Inspect(const std::string& path, bool silent) {
// Sink that always appends to the end of a string.
class StringSink : public IByteSink {
  public:
    void* GetBuffer(size_t requested, size_t* actual) override {
        size_t old_size = stream_.size();
        stream_.resize(old_size + requested, '\0');
        *actual = requested;
        return stream_.data() + old_size;
    }
    bool ReturnData(void*, size_t) override { return true; }
    void Reset() { stream_.clear(); }

    std::string& stream() { return stream_; }

  private:
    std::string stream_;
};

static bool Inspect(const std::string& path, bool silent, bool decompress) {
    android::base::unique_fd fd(open(path.c_str(), O_RDONLY));
    if (fd < 0) {
        PLOG(ERROR) << "open failed: " << path;
@@ -76,15 +96,25 @@ static bool Inspect(const std::string& path, bool silent) {
    }

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

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

        if (decompress && op.type == kCowReplaceOp && op.compression != kCowCompressNone) {
            if (!reader.ReadData(op, &sink)) {
                std::cerr << "Failed to decompress for :" << op << "\n";
                success = false;
            }
            sink.Reset();
        }

        iter->Next();
    }

    return true;
    return success;
}

}  // namespace snapshot
@@ -93,11 +123,15 @@ static bool Inspect(const std::string& path, bool silent) {
int main(int argc, char** argv) {
    int ch;
    bool silent = false;
    while ((ch = getopt(argc, argv, "s")) != -1) {
    bool decompress = false;
    while ((ch = getopt(argc, argv, "sd")) != -1) {
        switch (ch) {
            case 's':
                silent = true;
                break;
            case 'd':
                decompress = true;
                break;
            default:
                android::snapshot::usage();
        }
@@ -109,7 +143,7 @@ int main(int argc, char** argv) {
        return 1;
    }

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