update_engine: fix checkpointing

If update_engine is interrupted during checkpointing, we could be left
with some stale prefs in prefs_tmp.

update_engine should NEVER read from prefs_tmp. When GetFilenameForkey()
is invoked, we return the path to prefs_tmp if it exists. This is
because during checkpointing, we make all our writes to prefs_tmp.
However since this function is also used during reads, we need to fix
any stale pref states during init.

If prefs exists, we simply delete prefs_tmp. If prefs doesn't exist,
this means that an interrupt happened before our rename finished, so we
just continue with the rename.

Bug: 358042228
Test: ota on CVD with random injections of echo c > proc/sysrq-trigger
Change-Id: I7d2b7f734ae5309b7ae4a60d6557c6010ccb478b
diff --git a/common/prefs.cc b/common/prefs.cc
index af4d318..79d622f 100644
--- a/common/prefs.cc
+++ b/common/prefs.cc
@@ -240,6 +240,15 @@
     }
   }
 
+  if (std::filesystem::exists(GetTemporaryDir())) {
+    LOG(INFO)
+        << "Deleting temporary prefs, checkpoint transaction was interrupted";
+    if (!utils::DeleteDirectory(GetTemporaryDir().c_str())) {
+      LOG(ERROR) << "Failed to delete temporary prefs";
+      return false;
+    }
+  }
+
   // Delete empty directories. Ignore errors when deleting empty directories.
   DeleteEmptyDirectories(prefs_dir_);
   return true;