Merge "Add checkpointing support for A/B updates"
diff --git a/Checkpoint.cpp b/Checkpoint.cpp
index 5d1ec1f..1833733 100644
--- a/Checkpoint.cpp
+++ b/Checkpoint.cpp
@@ -19,6 +19,7 @@
 
 #include <fstream>
 #include <list>
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -82,19 +83,19 @@
     // To do this, we walk the list of mounted file systems.
     // But we also need to get the matching fstab entries to see
     // the original flags
-    struct fstab* fstab = fs_mgr_read_fstab_default();
-    if (!fstab) return false;
+    auto fstab_default = std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)>{
+        fs_mgr_read_fstab_default(), fs_mgr_free_fstab};
+    if (!fstab_default) return false;
 
-    struct fstab* mounts = fs_mgr_read_fstab("/proc/mounts");
-    if (mounts == NULL) {
-        fs_mgr_free_fstab(fstab);
-        return false;
-    }
+    auto mounts = std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)>{
+        fs_mgr_read_fstab("/proc/mounts"), fs_mgr_free_fstab};
+    if (!mounts) return false;
 
     // Walk mounted file systems
     for (int i = 0; i < mounts->num_entries; ++i) {
         const fstab_rec* mount_rec = &mounts->recs[i];
-        const fstab_rec* fstab_rec = fs_mgr_get_entry_for_mount_point(fstab, mount_rec->mount_point);
+        const fstab_rec* fstab_rec =
+            fs_mgr_get_entry_for_mount_point(fstab_default.get(), mount_rec->mount_point);
         if (!fstab_rec) continue;
 
         if (fs_mgr_is_checkpoint_fs(fstab_rec)) {
@@ -106,8 +107,6 @@
             setBowState(mount_rec->blk_device, "2");
         }
     }
-    fs_mgr_free_fstab(mounts);
-    fs_mgr_free_fstab(fstab);
     return android::base::RemoveFileIfExists(kMetadataCPFile);
 }
 
@@ -150,18 +149,18 @@
 }
 
 bool cp_prepareDriveForCheckpoint(const std::string&) {
-    struct fstab* fstab = fs_mgr_read_fstab_default();
-    if (!fstab) return false;
+    auto fstab_default = std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)>{
+        fs_mgr_read_fstab_default(), fs_mgr_free_fstab};
+    if (!fstab_default) return false;
 
-    struct fstab* mounts = fs_mgr_read_fstab("/proc/mounts");
-    if (mounts == NULL) {
-        fs_mgr_free_fstab(fstab);
-        return false;
-    }
+    auto mounts = std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)>{
+        fs_mgr_read_fstab("/proc/mounts"), fs_mgr_free_fstab};
+    if (!mounts) return false;
 
     for (int i = 0; i < mounts->num_entries; ++i) {
         const fstab_rec* mount_rec = &mounts->recs[i];
-        const fstab_rec* fstab_rec = fs_mgr_get_entry_for_mount_point(fstab, mount_rec->mount_point);
+        const fstab_rec* fstab_rec =
+            fs_mgr_get_entry_for_mount_point(fstab_default.get(), mount_rec->mount_point);
         if (!fstab_rec) continue;
 
         if (fs_mgr_is_checkpoint_blk(fstab_rec)) {
@@ -182,8 +181,6 @@
             setBowState(mount_rec->blk_device, "1");
         }
     }
-    fs_mgr_free_fstab(mounts);
-    fs_mgr_free_fstab(fstab);
     return true;
 }