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

Commit 91da9dfe authored by Alessio Balsini's avatar Alessio Balsini
Browse files

libsnapshot: fix stats write in Resume()



The Resume() operation is supposed to increment the resume counter, but
the updated counter value was not written to the device.
Fix by adding the write operation and refactoring the code.

Bug: none
Test: m
Change-Id: I3fffd61cc779c59e2780900809f0ce0b84258e78
Signed-off-by: default avatarAlessio Balsini <balsini@google.com>
parent e53f8b8f
Loading
Loading
Loading
Loading
+25 −16
Original line number Diff line number Diff line
@@ -36,36 +36,45 @@ SnapshotMergeStats::~SnapshotMergeStats() {
    }
}

void SnapshotMergeStats::Start() {
    SnapshotMergeReport report;
    report.set_resume_count(0);
    report.set_state(UpdateState::None);
bool SnapshotMergeStats::ReadState() {
    std::string contents;
    if (!android::base::ReadFileToString(parent_.GetMergeStateFilePath(), &contents)) {
        PLOG(INFO) << "Read merge statistics file failed";
        return false;
    }
    if (!report_.ParseFromString(contents)) {
        LOG(ERROR) << "Unable to parse merge statistics file as SnapshotMergeReport";
        return false;
    }
    return true;
}

bool SnapshotMergeStats::WriteState() {
    std::string contents;
    if (!report.SerializeToString(&contents)) {
    if (!report_.SerializeToString(&contents)) {
        LOG(ERROR) << "Unable to serialize SnapshotMergeStats.";
        return;
        return false;
    }
    auto file_path = parent_.GetMergeStateFilePath();
    if (!WriteStringToFileAtomic(contents, file_path)) {
        PLOG(ERROR) << "Could not write to merge statistics file";
        return;
        return false;
    }
    return true;
}

void SnapshotMergeStats::Resume() {
    std::string contents;
    if (!android::base::ReadFileToString(parent_.GetMergeStateFilePath(), &contents)) {
        PLOG(INFO) << "Read merge statistics file failed";
        return;
void SnapshotMergeStats::Start() {
    report_.set_resume_count(0);
    report_.set_state(UpdateState::None);
    WriteState();
}

    if (!report_.ParseFromString(contents)) {
        LOG(ERROR) << "Unable to parse merge statistics file as SnapshotMergeReport";
void SnapshotMergeStats::Resume() {
    if (!ReadState()) {
        return;
    }

    report_.set_resume_count(report_.resume_count() + 1);
    WriteState();
}

void SnapshotMergeStats::set_state(android::snapshot::UpdateState state) {
+3 −0
Original line number Diff line number Diff line
@@ -32,6 +32,9 @@ class SnapshotMergeStats {
    SnapshotMergeReport GetReport();

  private:
    bool ReadState();
    bool WriteState();

    const SnapshotManager& parent_;
    SnapshotMergeReport report_;
    std::chrono::time_point<std::chrono::steady_clock> init_time_;