ResetStatus deletes snapshots for VAB.

When ResetStatus() is called, delete snapshots to free up space on
VAB devices.

On regular A/B, there is no need to cancel the update completely so that
the update may be resumed next time. However, on VAB devices, a
cancelled update should not occupy previously allocated space.

Test: manually call update_engine_client --cancel when an update is in progress, then
      call update_engine_client --reset_status, then
      call snapshotctl dump to check that snapshots are deleted.
      Then, re-apply the update.

Test: manually kill update_engine when an update is in progress, then
      call update_engine_client --reset_status, then
      call snapshotctl dump to check that snapshots are deleted.
      Then, re-apply the update.

Test: manually apply an update until UPDATED_NEED_REBOOT, then
      call update_engine_client --reset_status, then
      call snapshotctl dump to check that snapshots are deleted.
      Then, re-apply the update.

Test: manually apply an update until UPDATED_NEED_REBOOT, then
      kill update_engine, then
      call update_engine_client --reset_status, then
      call snapshotctl dump to check that snapshots are deleted.
      Then, re-apply the update.

Bug: 147696014

Change-Id: Ic7049772091a34e0e666fd7ae361ef474d5a28aa
Merged-In: Ic7049772091a34e0e666fd7ae361ef474d5a28aa
diff --git a/dynamic_partition_control_android.cc b/dynamic_partition_control_android.cc
index d75a8fc..81d0d77 100644
--- a/dynamic_partition_control_android.cc
+++ b/dynamic_partition_control_android.cc
@@ -39,6 +39,7 @@
 #include "update_engine/common/boot_control_interface.h"
 #include "update_engine/common/utils.h"
 #include "update_engine/dynamic_partition_utils.h"
+#include "update_engine/payload_consumer/delta_performer.h"
 
 using android::base::GetBoolProperty;
 using android::base::Join;
@@ -786,4 +787,24 @@
       prefs, boot_control, snapshot_.get(), delegate);
 }
 
+bool DynamicPartitionControlAndroid::ResetUpdate(PrefsInterface* prefs) {
+  if (!GetVirtualAbFeatureFlag().IsEnabled()) {
+    return true;
+  }
+
+  LOG(INFO) << __func__ << " resetting update state and deleting snapshots.";
+  TEST_AND_RETURN_FALSE(prefs != nullptr);
+
+  // If the device has already booted into the target slot,
+  // ResetUpdateProgress may pass but CancelUpdate fails.
+  // This is expected. A scheduled CleanupPreviousUpdateAction should free
+  // space when it is done.
+  TEST_AND_RETURN_FALSE(DeltaPerformer::ResetUpdateProgress(
+      prefs, false /* quick */, false /* skip dynamic partitions metadata */));
+
+  TEST_AND_RETURN_FALSE(snapshot_->CancelUpdate());
+
+  return true;
+}
+
 }  // namespace chromeos_update_engine