Checkpoint update progress before pausing

Save update progress before attempting to pause or cancel an update, so
that next call to applyPayload can resume from exactly where it was left
off last time.

Test: th
Bug: 250637970
Change-Id: If941e3013511e491fdb5f7e783822b82d56df7dd
diff --git a/payload_consumer/delta_performer.cc b/payload_consumer/delta_performer.cc
index 776eb2d..04a158f 100644
--- a/payload_consumer/delta_performer.cc
+++ b/payload_consumer/delta_performer.cc
@@ -200,6 +200,9 @@
 }
 
 int DeltaPerformer::Close() {
+  // Checkpoint update progress before canceling, so that subsequent attempts
+  // can resume from exactly where update_engine left last time.
+  CheckpointUpdateProgress(true);
   int err = -CloseCurrentPartition();
   LOG_IF(ERROR,
          !payload_hash_calculator_.Finalize() ||
@@ -1246,43 +1249,66 @@
                                      const string& update_check_response_hash) {
   int64_t next_operation = kUpdateStateOperationInvalid;
   if (!(prefs->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) &&
-        next_operation != kUpdateStateOperationInvalid && next_operation > 0))
+        next_operation != kUpdateStateOperationInvalid && next_operation > 0)) {
+    LOG(WARNING) << "Failed to resume update " << kPrefsUpdateStateNextOperation
+                 << " invalid: " << next_operation;
     return false;
+  }
 
   string interrupted_hash;
   if (!(prefs->GetString(kPrefsUpdateCheckResponseHash, &interrupted_hash) &&
         !interrupted_hash.empty() &&
-        interrupted_hash == update_check_response_hash))
+        interrupted_hash == update_check_response_hash)) {
+    LOG(WARNING) << "Failed to resume update " << kPrefsUpdateCheckResponseHash
+                 << " mismatch, last hash: " << interrupted_hash
+                 << ", current hash: " << update_check_response_hash << "";
     return false;
+  }
 
   int64_t resumed_update_failures{};
   // Note that storing this value is optional, but if it is there it should
   // not be more than the limit.
   if (prefs->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures) &&
-      resumed_update_failures > kMaxResumedUpdateFailures)
+      resumed_update_failures > kMaxResumedUpdateFailures) {
+    LOG(WARNING) << "Failed to resume update " << kPrefsResumedUpdateFailures
+                 << " invalid: " << resumed_update_failures;
     return false;
+  }
 
   // Validation check the rest.
   int64_t next_data_offset = -1;
   if (!(prefs->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset) &&
-        next_data_offset >= 0))
+        next_data_offset >= 0)) {
+    LOG(WARNING) << "Failed to resume update "
+                 << kPrefsUpdateStateNextDataOffset
+                 << " invalid: " << next_data_offset;
     return false;
+  }
 
   string sha256_context;
   if (!(prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) &&
-        !sha256_context.empty()))
+        !sha256_context.empty())) {
+    LOG(WARNING) << "Failed to resume update " << kPrefsUpdateStateSHA256Context
+                 << " is empty.";
     return false;
+  }
 
   int64_t manifest_metadata_size = 0;
   if (!(prefs->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size) &&
-        manifest_metadata_size > 0))
+        manifest_metadata_size > 0)) {
+    LOG(WARNING) << "Failed to resume update " << kPrefsManifestMetadataSize
+                 << " invalid: " << manifest_metadata_size;
     return false;
+  }
 
   int64_t manifest_signature_size = 0;
   if (!(prefs->GetInt64(kPrefsManifestSignatureSize,
                         &manifest_signature_size) &&
-        manifest_signature_size >= 0))
+        manifest_signature_size >= 0)) {
+    LOG(WARNING) << "Failed to resume update " << kPrefsManifestSignatureSize
+                 << " invalid: " << manifest_signature_size;
     return false;
+  }
 
   return true;
 }