Refactor GsiService/GsiInstaller to use ImageManager.

Since ImageManager was mostly lifted from gsi_installer.cpp, it is
straightforward to remove this code and transition entirely to
the new libfiemap.

Bug: 134536978
Test: gsi_tool install, enable/disable, status, wipe
Change-Id: Ic5e19906cfce9018fd5f9029e1e4de3852dbc5a9
diff --git a/gsi_service.cpp b/gsi_service.cpp
index dc0f0a4..89b3a74 100644
--- a/gsi_service.cpp
+++ b/gsi_service.cpp
@@ -34,6 +34,7 @@
 #include <android-base/strings.h>
 #include <android/gsi/BnImageManager.h>
 #include <android/gsi/IGsiService.h>
+#include <ext4_utils/ext4_utils.h>
 #include <fs_mgr.h>
 #include <libfiemap/image_manager.h>
 #include <private/android_filesystem_config.h>
@@ -325,17 +326,10 @@
         }
         *_aidl_return = size;
     } else {
-        // Stat the size of the userdata file.
-        auto userdata_gsi = GetInstalledImagePath("userdata_gsi");
-        struct stat s;
-        if (stat(userdata_gsi.c_str(), &s)) {
-            if (errno != ENOENT) {
-                PLOG(ERROR) << "open " << userdata_gsi;
-                return binder::Status::ok();
+        if (auto manager = ImageManager::Open(kDsuMetadataDir, GetInstalledImageDir())) {
+            if (auto device = MappedDevice::Open(manager.get(), 10s, "userdata_gsi")) {
+                *_aidl_return = get_block_device_size(device->fd());
             }
-            *_aidl_return = 0;
-        } else {
-            *_aidl_return = s.st_size;
         }
     }
     return binder::Status::ok();
@@ -531,8 +525,7 @@
         PLOG(ERROR) << "realpath failed: " << origInstallDir;
         return INSTALL_ERROR_GENERIC;
     }
-    // Ensure the path ends in / for consistency. Even though GetImagePath()
-    // does this already, we want it to appear this way in install_dir.
+    // Ensure the path ends in / for consistency.
     if (!android::base::EndsWith(params->installDir, "/")) {
         params->installDir += "/";
     }
@@ -570,14 +563,6 @@
     return INSTALL_OK;
 }
 
-std::string GsiService::GetImagePath(const std::string& image_dir, const std::string& name) {
-    std::string dir = image_dir;
-    if (!android::base::EndsWith(dir, "/")) {
-        dir += "/";
-    }
-    return dir + name + ".img";
-}
-
 std::string GsiService::GetInstalledImageDir() {
     // If there's no install left, just return /data/gsi since that's where
     // installs go by default.
@@ -588,10 +573,6 @@
     return kDefaultDsuImageFolder;
 }
 
-std::string GsiService::GetInstalledImagePath(const std::string& name) {
-    return GetImagePath(GetInstalledImageDir(), name);
-}
-
 int GsiService::ReenableGsi(bool one_shot) {
     if (!android::gsi::IsGsiInstalled()) {
         LOG(ERROR) << "no gsi installed - cannot re-enable";
@@ -614,24 +595,20 @@
 
 bool GsiService::RemoveGsiFiles(const std::string& install_dir, bool wipeUserdata) {
     bool ok = true;
-    std::string message;
-    if (!SplitFiemap::RemoveSplitFiles(GetImagePath(install_dir, "system_gsi"), &message)) {
-        LOG(ERROR) << message;
-        ok = false;
-    }
-    if (wipeUserdata &&
-        !SplitFiemap::RemoveSplitFiles(GetImagePath(install_dir, "userdata_gsi"), &message)) {
-        LOG(ERROR) << message;
-        ok = false;
+    if (auto manager = ImageManager::Open(kDsuMetadataDir, install_dir)) {
+        ok &= manager->DeleteBackingImage("system_gsi");
+        if (wipeUserdata) {
+            ok &= manager->DeleteBackingImage("userdata_gsi");
+        }
     }
 
     std::vector<std::string> files{
             kDsuInstallStatusFile,
-            kDsuLpMetadataFile,
             kDsuOneShotBootFile,
             kDsuInstallDirFile,
     };
     for (const auto& file : files) {
+        std::string message;
         if (!android::base::RemoveFileIfExists(file, &message)) {
             LOG(ERROR) << message;
             ok = false;