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: Alessio Balsini <balsini@google.com>
This commit is contained in:
Alessio Balsini 2020-02-20 01:06:57 +00:00
parent e53f8b8f9d
commit 91da9dfeef
2 changed files with 29 additions and 17 deletions

View file

@ -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 (!report.SerializeToString(&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)) {
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::Start() {
report_.set_resume_count(0);
report_.set_state(UpdateState::None);
WriteState();
}
void SnapshotMergeStats::Resume() {
std::string contents;
if (!android::base::ReadFileToString(parent_.GetMergeStateFilePath(), &contents)) {
PLOG(INFO) << "Read merge statistics file failed";
if (!ReadState()) {
return;
}
if (!report_.ParseFromString(contents)) {
LOG(ERROR) << "Unable to parse merge statistics file as SnapshotMergeReport";
return;
}
report_.set_resume_count(report_.resume_count() + 1);
WriteState();
}
void SnapshotMergeStats::set_state(android::snapshot::UpdateState state) {

View file

@ -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_;