Fully reset update state during ResetStatus() call

List of dynamic partitions is cached in memory, need to clear it during
ResetStatus(), as the next OTA update might have a different set of
dynamic partitions.

Test: OTA TP1A.220310.002-to-TP1A.220311.001,
    ResetStatus(), then OTA
    TP1A.220310.002-to-TP1A.220314.001
Bug: 224648567
Change-Id: Ie39fda2279fbcc48afb18a565389a32cfd19adc1
diff --git a/aosp/dynamic_partition_control_android.cc b/aosp/dynamic_partition_control_android.cc
index 6d33a09..e39e7bc 100644
--- a/aosp/dynamic_partition_control_android.cc
+++ b/aosp/dynamic_partition_control_android.cc
@@ -465,6 +465,9 @@
   if (!SetTargetBuildVars(manifest)) {
     return false;
   }
+  for (auto& list : dynamic_partition_list_) {
+    list.clear();
+  }
 
   // Although the current build supports dynamic partitions, the given payload
   // doesn't use it for target partitions. This could happen when applying a
@@ -1280,6 +1283,9 @@
   if (!GetVirtualAbFeatureFlag().IsEnabled()) {
     return true;
   }
+  for (auto& list : dynamic_partition_list_) {
+    list.clear();
+  }
 
   LOG(INFO) << __func__ << " resetting update state and deleting snapshots.";
   TEST_AND_RETURN_FALSE(prefs != nullptr);
diff --git a/aosp/dynamic_partition_control_android.h b/aosp/dynamic_partition_control_android.h
index cebca07..457ef18 100644
--- a/aosp/dynamic_partition_control_android.h
+++ b/aosp/dynamic_partition_control_android.h
@@ -21,7 +21,7 @@
 #include <set>
 #include <string>
 #include <string_view>
-#include <vector>
+#include <array>
 
 #include <base/files/file_util.h>
 #include <libsnapshot/auto_device.h>
@@ -348,7 +348,9 @@
 
   uint32_t source_slot_ = UINT32_MAX;
   uint32_t target_slot_ = UINT32_MAX;
-  std::vector<std::vector<std::string>> dynamic_partition_list_{2UL};
+  // We assume that there's only 2 slots, A and B. This assumption is unlikely
+  // to change in the future. And certaintly won't change at runtime.
+  std::array<std::vector<std::string>, 2> dynamic_partition_list_{};
 
   DISALLOW_COPY_AND_ASSIGN(DynamicPartitionControlAndroid);
 };
diff --git a/aosp/update_attempter_android.cc b/aosp/update_attempter_android.cc
index 4e609d4..a3485ea 100644
--- a/aosp/update_attempter_android.cc
+++ b/aosp/update_attempter_android.cc
@@ -400,6 +400,10 @@
 bool UpdateAttempterAndroid::ResetStatus(brillo::ErrorPtr* error) {
   LOG(INFO) << "Attempting to reset state from "
             << UpdateStatusToString(status_) << " to UpdateStatus::IDLE";
+  if (processor_->IsRunning()) {
+    return LogAndSetError(
+        error, FROM_HERE, "Already processing an update, cancel it first.");
+  }
 
   if (apex_handler_android_ != nullptr) {
     LOG(INFO) << "Cleaning up reserved space for compressed APEX (if any)";
@@ -416,12 +420,12 @@
                           "ClearUpdateCompletedMarker() failed");
   }
 
+  if (!boot_control_->GetDynamicPartitionControl()->ResetUpdate(prefs_)) {
+    LOG(WARNING) << "Failed to reset snapshots. UpdateStatus is IDLE but"
+                  << "space might not be freed.";
+  }
   switch (status_) {
     case UpdateStatus::IDLE: {
-      if (!boot_control_->GetDynamicPartitionControl()->ResetUpdate(prefs_)) {
-        LOG(WARNING) << "Failed to reset snapshots. UpdateStatus is IDLE but"
-                     << "space might not be freed.";
-      }
       return true;
     }