libartpalette: add PaletteCreateOdrefreshStagingDirectory am: 4894a472dc

Original change: https://android-review.googlesource.com/c/platform/system/libartpalette/+/1518042

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Ie64cbacd2362dc7203802d1cfc4624186624bf29
diff --git a/Android.bp b/Android.bp
index 0451c2b..5091fc6 100644
--- a/Android.bp
+++ b/Android.bp
@@ -19,6 +19,7 @@
 // ART which now ships as an APEX and not part of the system.
 cc_library {
     name: "libartpalette-system",
+    cpp_std: "experimental",
     compile_multilib: "both",
     header_libs: ["libartpalette-headers"],
     host_supported: true,
@@ -32,7 +33,9 @@
                 "liblog",
                 "libprocessgroup",
                 "libtombstoned_client",
+                "libselinux",
             ],
+            static_libs: ["libc++fs"],
         },
         host: {
             header_libs: ["libbase_headers"],
diff --git a/libartpalette.map.txt b/libartpalette.map.txt
index 7f29fde..caf955f 100644
--- a/libartpalette.map.txt
+++ b/libartpalette.map.txt
@@ -28,6 +28,7 @@
     PaletteAshmemCreateRegion; # apex
     PaletteAshmemSetProtRegion; # apex
     PaletteGetHooks; # apex
+    PaletteCreateOdrefreshStagingDirectory; # apex
 
   local:
     *;
diff --git a/palette_android.cc b/palette_android.cc
index dbd0305..ac9a968 100644
--- a/palette_android.cc
+++ b/palette_android.cc
@@ -23,6 +23,7 @@
 #include <sys/time.h>
 #include <unistd.h>
 
+#include <filesystem>
 #include <mutex>
 
 #include <android-base/file.h>
@@ -32,6 +33,7 @@
 #include <cutils/trace.h>
 #include <processgroup/processgroup.h>
 #include <processgroup/sched_policy.h>
+#include <selinux/selinux.h>
 #include <tombstoned/tombstoned.h>
 #include <utils/Thread.h>
 
@@ -235,6 +237,33 @@
 }
 
 palette_status_t PaletteGetHooks(PaletteHooks** hooks) {
-  *hooks = nullptr;
-  return PALETTE_STATUS_NOT_SUPPORTED;
+    *hooks = nullptr;
+    return PALETTE_STATUS_NOT_SUPPORTED;
+}
+
+palette_status_t PaletteCreateOdrefreshStagingDirectory(const char** staging_dir) {
+    static constexpr const char* kStagingDirectory = "/data/misc/apexdata/com.android.art/staging";
+
+    std::error_code ec;
+    if (std::filesystem::exists(kStagingDirectory, ec)) {
+        if (!std::filesystem::remove_all(kStagingDirectory, ec)) {
+            LOG(ERROR) << ec.message()
+                       << "Could not remove existing staging directory: " << kStagingDirectory;
+            DCHECK_EQ(ec.value(), errno);
+            return PALETTE_STATUS_CHECK_ERRNO;
+        }
+    }
+
+    if (mkdir(kStagingDirectory, S_IRWXU) != 0) {
+        PLOG(ERROR) << "Could not set permissions on staging directory: " << kStagingDirectory;
+        return PALETTE_STATUS_CHECK_ERRNO;
+    }
+
+    if (setfilecon(kStagingDirectory, "u:object_r:apex_art_staging_data_file:s0") != 0) {
+        PLOG(ERROR) << "Could not set label on staging directory: " << kStagingDirectory;
+        return PALETTE_STATUS_CHECK_ERRNO;
+    }
+
+    *staging_dir = kStagingDirectory;
+    return PALETTE_STATUS_OK;
 }
diff --git a/palette_fake.cc b/palette_fake.cc
index 1bd5744..b373c6c 100644
--- a/palette_fake.cc
+++ b/palette_fake.cc
@@ -72,12 +72,12 @@
 }
 
 palette_status_t PaletteTraceIntegerValue(const char* name ATTRIBUTE_UNUSED,
-                                            int32_t value ATTRIBUTE_UNUSED) {
+                                          int32_t value ATTRIBUTE_UNUSED) {
     return PALETTE_STATUS_OK;
 }
 
 palette_status_t PaletteAshmemCreateRegion(const char* name ATTRIBUTE_UNUSED,
-                                             size_t size ATTRIBUTE_UNUSED, int* fd) {
+                                           size_t size ATTRIBUTE_UNUSED, int* fd) {
     *fd = -1;
     return PALETTE_STATUS_NOT_SUPPORTED;
 }
@@ -87,6 +87,11 @@
 }
 
 palette_status_t PaletteGetHooks(PaletteHooks** hooks) {
-  *hooks = nullptr;
-  return PALETTE_STATUS_NOT_SUPPORTED;
+    *hooks = nullptr;
+    return PALETTE_STATUS_NOT_SUPPORTED;
+}
+
+palette_status_t PaletteCreateOdrefreshStagingDirectory(const char** staging_dir) {
+    *staging_dir = nullptr;
+    return PALETTE_STATUS_NOT_SUPPORTED;
 }