Retry path removal and log errors in case of failure

Bug: 161687281
Test: art_runtime_tests
Change-Id: I1d68b1dd6382b6873c33aa2c09bb25c3b6fe5c30
diff --git a/runtime/hidden_api_test.cc b/runtime/hidden_api_test.cc
index e933c01..c40041b 100644
--- a/runtime/hidden_api_test.cc
+++ b/runtime/hidden_api_test.cc
@@ -19,6 +19,7 @@
 #include <fstream>
 #include <sstream>
 
+#include "android-base/stringprintf.h"
 #include "base/file_utils.h"
 #include "base/sdk_version.h"
 #include "base/stl_util.h"
@@ -29,6 +30,7 @@
 
 namespace art {
 
+using android::base::StringPrintf;
 using hiddenapi::detail::MemberSignature;
 using hiddenapi::detail::ShouldDenyAccessToMemberImpl;
 
@@ -534,6 +536,14 @@
   return true;
 }
 
+static bool Remove(const std::string& path, /*out*/ std::string* error_msg) {
+  if (TEMP_FAILURE_RETRY(remove(path.c_str())) == 0) {
+    return true;
+  }
+  *error_msg = StringPrintf("Unable to remove(\"%s\"): %s", path.c_str(), strerror(errno));
+  return false;
+}
+
 static bool CheckAllDexFilesInDomain(ObjPtr<mirror::ClassLoader> loader,
                                      const std::vector<std::unique_ptr<const DexFile>>& dex_files,
                                      hiddenapi::Domain expected_domain,
@@ -582,7 +592,7 @@
                                        &error_msg)) << error_msg;
 
   dex_files.clear();
-  ASSERT_EQ(0, remove(data_location_path.c_str()));
+  ASSERT_TRUE(Remove(data_location_path, &error_msg)) << error_msg;
 }
 
 TEST_F(HiddenApiTest, DexDomain_SystemDir) {
@@ -605,7 +615,7 @@
                                        &error_msg)) << error_msg;
 
   dex_files.clear();
-  ASSERT_EQ(0, remove(system_location_path.c_str()));
+  ASSERT_TRUE(Remove(system_location_path, &error_msg)) << error_msg;
 }
 
 TEST_F(HiddenApiTest, DexDomain_SystemExtDir) {
@@ -628,7 +638,7 @@
                                        &error_msg)) << error_msg;
 
   dex_files.clear();
-  ASSERT_EQ(0, remove(system_ext_location_path.c_str()));
+  ASSERT_TRUE(Remove(system_ext_location_path, &error_msg)) << error_msg;
 }
 
 TEST_F(HiddenApiTest, DexDomain_SystemFrameworkDir) {
@@ -656,7 +666,7 @@
                                        &error_msg)) << error_msg;
 
   dex_files.clear();
-  ASSERT_EQ(0, remove(system_framework_location_path.c_str()));
+  ASSERT_TRUE(Remove(system_framework_location_path, &error_msg)) << error_msg;
 }
 
 TEST_F(HiddenApiTest, DexDomain_SystemExtFrameworkDir) {
@@ -683,7 +693,7 @@
                                        &error_msg)) << error_msg;
 
   dex_files.clear();
-  ASSERT_EQ(0, remove(system_ext_framework_location_path.c_str()));
+  ASSERT_TRUE(Remove(system_ext_framework_location_path, &error_msg)) << error_msg;
 }
 
 TEST_F(HiddenApiTest, DexDomain_DataDir_MultiDex) {
@@ -707,7 +717,7 @@
                                        &error_msg)) << error_msg;
 
   dex_files.clear();
-  ASSERT_EQ(0, remove(data_multi_location_path.c_str()));
+  ASSERT_TRUE(Remove(data_multi_location_path, &error_msg)) << error_msg;
 }
 
 TEST_F(HiddenApiTest, DexDomain_SystemDir_MultiDex) {
@@ -732,7 +742,7 @@
                                        &error_msg)) << error_msg;
 
   dex_files.clear();
-  ASSERT_EQ(0, remove(system_multi_location_path.c_str()));
+  ASSERT_TRUE(Remove(system_multi_location_path, &error_msg)) << error_msg;
 }
 
 TEST_F(HiddenApiTest, DexDomain_SystemExtDir_MultiDex) {
@@ -757,7 +767,7 @@
                                        &error_msg)) << error_msg;
 
   dex_files.clear();
-  ASSERT_EQ(0, remove(system_ext_multi_location_path.c_str()));
+  ASSERT_TRUE(Remove(system_ext_multi_location_path, &error_msg)) << error_msg;
 }
 
 TEST_F(HiddenApiTest, DexDomain_SystemFrameworkDir_MultiDex) {
@@ -786,7 +796,7 @@
                                        &error_msg)) << error_msg;
 
   dex_files.clear();
-  ASSERT_EQ(0, remove(system_framework_multi_location_path.c_str()));
+  ASSERT_TRUE(Remove(system_framework_multi_location_path, &error_msg)) << error_msg;
 }
 
 TEST_F(HiddenApiTest, DexDomain_SystemExtFrameworkDir_MultiDex) {
@@ -815,7 +825,7 @@
                                        &error_msg)) << error_msg;
 
   dex_files.clear();
-  ASSERT_EQ(0, remove(system_ext_framework_multi_location_path.c_str()));
+  ASSERT_TRUE(Remove(system_ext_framework_multi_location_path, &error_msg)) << error_msg;
 }
 
 }  // namespace art