[res] Make sure cached container is retained off cache

The object in the cache may get removed by a different thread,
so getting it from the cache needs to also take (shared)
ownership instead of relying just on the cache itself.

This CL makes the cache hols shared_ptr<> and getting it
increments the ref counter, so the object won't go away
anymore

+ fix a few small issues in Idmap - const return types
  and bad formatting

Bug: 332234677
Flag: EXEMPT bugfix
Test: build + boot
Change-Id: I8e666e380a58b45142ddbd196dd684e5874fd2a6
diff --git a/cmds/idmap2/idmap2d/Idmap2Service.cpp b/cmds/idmap2/idmap2d/Idmap2Service.cpp
index f264125..6902d6d 100644
--- a/cmds/idmap2/idmap2d/Idmap2Service.cpp
+++ b/cmds/idmap2/idmap2d/Idmap2Service.cpp
@@ -78,6 +78,11 @@
 
 namespace android::os {
 
+template <typename T>
+const T* Idmap2Service::GetPointer(const OwningPtr<T>& ptr) {
+    return std::visit([](auto&& ptr) { return ptr.get(); }, ptr);
+}
+
 Status Idmap2Service::getIdmapPath(const std::string& overlay_path,
                                    int32_t user_id ATTRIBUTE_UNUSED, std::string* _aidl_return) {
   assert(_aidl_return);
@@ -224,7 +229,7 @@
       if (is_framework ||
         (item.dev == st.st_dev && item.inode == st.st_ino && item.size == st.st_size
           && item.mtime.tv_sec == st.st_mtim.tv_sec && item.mtime.tv_nsec == st.st_mtim.tv_nsec)) {
-        return {item.apk.get()};
+        return {item.apk};
       }
       container_cache_.erase(cache_it);
     }
@@ -238,14 +243,14 @@
     return {std::move(*target)};
   }
 
-  const auto res = target->get();
+  auto res = std::shared_ptr(std::move(*target));
   std::lock_guard lock(container_cache_mutex_);
   container_cache_.emplace(target_path, CachedContainer {
     .dev = dev_t(st.st_dev),
     .inode = ino_t(st.st_ino),
     .size = st.st_size,
     .mtime = st.st_mtim,
-    .apk = std::move(*target)
+    .apk = res
   });
   return {res};
 }