Snap for 12229688 from 35c947f68dfbad2f87b0c155108f24729a90e38a to 24Q4-release

Change-Id: Ic908f745bc8eb0721a51b08dcb4b1e8aa1ef3052
diff --git a/common/prefs.cc b/common/prefs.cc
index af4d318..77078cf 100644
--- a/common/prefs.cc
+++ b/common/prefs.cc
@@ -200,7 +200,12 @@
     return false;
   }
   // Copy the directory.
-  std::filesystem::copy(source_directory, destination_directory);
+  std::error_code e;
+  std::filesystem::copy(source_directory, destination_directory, e);
+  if (e) {
+    LOG(ERROR) << "failed to copy prefs to prefs_tmp: " << e.message();
+    return false;
+  }
 
   return true;
 }
@@ -209,7 +214,12 @@
   std::filesystem::path destination_directory(GetTemporaryDir());
 
   if (std::filesystem::exists(destination_directory)) {
-    return std::filesystem::remove_all(destination_directory);
+    std::error_code e;
+    std::filesystem::remove_all(destination_directory, e);
+    if (e) {
+      LOG(ERROR) << "failed to remove directory: " << e.message();
+      return false;
+    }
   }
   return true;
 }
@@ -240,6 +250,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;