odrefresh: Stop refreshing artifacts on new Android versions. am: 39f6d00b73

Original change: https://android-review.googlesource.com/c/platform/art/+/1980107

Change-Id: I4b0b14bbd98a1b474cf6ce23bbd58f9928fea450
diff --git a/odrefresh/Android.bp b/odrefresh/Android.bp
index 04d65b9..8fee83a 100644
--- a/odrefresh/Android.bp
+++ b/odrefresh/Android.bp
@@ -182,6 +182,7 @@
     header_libs: ["odrefresh_headers"],
     srcs: [
         "odr_artifacts_test.cc",
+        "odr_common_test.cc",
         "odr_compilation_log_test.cc",
         "odr_fs_utils_test.cc",
         "odr_metrics_test.cc",
diff --git a/odrefresh/odr_common.cc b/odrefresh/odr_common.cc
index 8f4e263..c3c410e 100644
--- a/odrefresh/odr_common.cc
+++ b/odrefresh/odr_common.cc
@@ -21,6 +21,9 @@
 #include <string>
 #include <string_view>
 
+#include "android-base/logging.h"
+#include "android-base/parseint.h"
+
 namespace art {
 namespace odrefresh {
 
@@ -36,5 +39,14 @@
   return Concatenate({"'", path, "'"});
 }
 
+bool ShouldDisableRefresh(const std::string& sdk_version_str) {
+  int sdk_version = 0;
+  if (!android::base::ParseInt(sdk_version_str, &sdk_version)) {
+    LOG(ERROR) << "Invalid SDK version string \"" << sdk_version_str << "\"";
+    return false;
+  }
+  return sdk_version >= 32;
+}
+
 }  // namespace odrefresh
 }  // namespace art
diff --git a/odrefresh/odr_common.h b/odrefresh/odr_common.h
index 7aad354..c7680e2 100644
--- a/odrefresh/odr_common.h
+++ b/odrefresh/odr_common.h
@@ -30,6 +30,11 @@
 // Quotes a path with single quotes (').
 std::string QuotePath(std::string_view path);
 
+// Returns true if there is no need to load existing artifacts that are already up-to-date and write
+// them back. See `OnDeviceRefresh::RefreshExistingArtifacts` for more details. Takes a string from
+// `ro.build.version.sdk`, which represents the SDK version.
+bool ShouldDisableRefresh(const std::string& sdk_version_str);
+
 }  // namespace odrefresh
 }  // namespace art
 
diff --git a/odrefresh/odr_common_test.cc b/odrefresh/odr_common_test.cc
new file mode 100644
index 0000000..976b3f0
--- /dev/null
+++ b/odrefresh/odr_common_test.cc
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "odr_common.h"
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace art {
+namespace odrefresh {
+
+TEST(OdrCommonTest, ShouldDisableRefresh) {
+  EXPECT_TRUE(ShouldDisableRefresh("32"));
+  EXPECT_TRUE(ShouldDisableRefresh("33"));
+  EXPECT_FALSE(ShouldDisableRefresh("31"));
+  EXPECT_FALSE(ShouldDisableRefresh(""));
+  EXPECT_FALSE(ShouldDisableRefresh("invalid"));
+}
+
+}  // namespace odrefresh
+}  // namespace art
diff --git a/odrefresh/odrefresh.h b/odrefresh/odrefresh.h
index 4558344..58da57a 100644
--- a/odrefresh/odrefresh.h
+++ b/odrefresh/odrefresh.h
@@ -114,9 +114,9 @@
   android::base::Result<void> CleanupArtifactDirectory(
       const std::vector<std::string>& artifacts_to_keep) const;
 
-  // Loads artifacts to memory and writes them back. This essentially removes the existing artifacts
-  // from fs-verity so that odsign will not encounter "file exists" error when it adds the existing
-  // artifacts to fs-verity.
+  // Loads artifacts to memory and writes them back. This is a workaround for old versions of
+  // odsign, which encounters "file exists" error when it adds existing artifacts to fs-verity. This
+  // function essentially removes existing artifacts from fs-verity to avoid the error.
   android::base::Result<void> RefreshExistingArtifacts() const;
 
   // Checks whether all boot classpath artifacts are present. Returns true if all are present, false
diff --git a/odrefresh/odrefresh_main.cc b/odrefresh/odrefresh_main.cc
index 6dbe372..c2298f0 100644
--- a/odrefresh/odrefresh_main.cc
+++ b/odrefresh/odrefresh_main.cc
@@ -41,6 +41,7 @@
 using ::art::odrefresh::OdrMetrics;
 using ::art::odrefresh::OnDeviceRefresh;
 using ::art::odrefresh::QuotePath;
+using ::art::odrefresh::ShouldDisableRefresh;
 using ::art::odrefresh::ZygoteKind;
 
 void UsageMsgV(const char* fmt, va_list ap) {
@@ -168,6 +169,11 @@
     config->SetSystemServerCompilerFilter(filter);
   }
 
+  if (ShouldDisableRefresh(
+          android::base::GetProperty("ro.build.version.sdk", /*default_value=*/""))) {
+    config->SetRefresh(false);
+  }
+
   return n;
 }