[idmap] Cache target apks as they are often reused

idmap2d is being often called for the same targets, e.g. system
apps. Caching those apks makes idmap verification and creation
much faster e.g. for user switching

Test: build + boot + UTs
Bug: 271904589
Change-Id: Ib6f7af385c2389b50d5c74aa08b4bab290580809
diff --git a/cmds/idmap2/idmap2d/Idmap2Service.cpp b/cmds/idmap2/idmap2d/Idmap2Service.cpp
index 10947dc..409ab84 100644
--- a/cmds/idmap2/idmap2d/Idmap2Service.cpp
+++ b/cmds/idmap2/idmap2d/Idmap2Service.cpp
@@ -59,7 +59,7 @@
 
 namespace {
 
-constexpr const char* kFrameworkPath = "/system/framework/framework-res.apk";
+constexpr std::string_view kFrameworkPath = "/system/framework/framework-res.apk";
 
 Status ok() {
   return Status::ok();
@@ -207,23 +207,47 @@
 
 idmap2::Result<Idmap2Service::TargetResourceContainerPtr> Idmap2Service::GetTargetContainer(
     const std::string& target_path) {
-  if (target_path == kFrameworkPath) {
-    if (framework_apk_cache_ == nullptr) {
-      // Initialize the framework APK cache.
-      auto target = TargetResourceContainer::FromPath(target_path);
-      if (!target) {
-        return target.GetError();
+  const bool is_framework = target_path == kFrameworkPath;
+  bool use_cache;
+  struct stat st = {};
+  if (is_framework || !::stat(target_path.c_str(), &st)) {
+    use_cache = true;
+  } else {
+    LOG(WARNING) << "failed to stat target path '" << target_path << "' for the cache";
+    use_cache = false;
+  }
+
+  if (use_cache) {
+    std::lock_guard lock(container_cache_mutex_);
+    if (auto cache_it = container_cache_.find(target_path); cache_it != container_cache_.end()) {
+      const auto& item = cache_it->second;
+      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()};
       }
-      framework_apk_cache_ = std::move(*target);
+      container_cache_.erase(cache_it);
     }
-    return {framework_apk_cache_.get()};
   }
 
   auto target = TargetResourceContainer::FromPath(target_path);
   if (!target) {
     return target.GetError();
   }
-  return {std::move(*target)};
+  if (!use_cache) {
+    return {std::move(*target)};
+  }
+
+  const auto res = target->get();
+  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)
+  });
+  return {res};
 }
 
 Status Idmap2Service::createFabricatedOverlay(