Merge "Extract AppFuse as a util"
diff --git a/Checkpoint.cpp b/Checkpoint.cpp
index 94a78eb..e238acf 100644
--- a/Checkpoint.cpp
+++ b/Checkpoint.cpp
@@ -82,6 +82,7 @@
 }
 
 Status cp_commitChanges() {
+    if (!cp_needsCheckpoint()) return Status::ok();
     // Must take action for list of mounted checkpointed things here
     // To do this, we walk the list of mounted file systems.
     // But we also need to get the matching fstab entries to see
@@ -104,14 +105,17 @@
 
         if (fs_mgr_is_checkpoint_fs(fstab_rec)) {
             if (!strcmp(fstab_rec->fs_type, "f2fs")) {
-                mount(mount_rec->blk_device, mount_rec->mount_point, "none",
-                      MS_REMOUNT | fstab_rec->flags, "checkpoint=enable");
+                if (mount(mount_rec->blk_device, mount_rec->mount_point, "none",
+                          MS_REMOUNT | fstab_rec->flags, "checkpoint=enable")) {
+                    return Status::fromExceptionCode(EINVAL, "Failed to remount");
+                }
             }
         } else if (fs_mgr_is_checkpoint_blk(fstab_rec)) {
-            setBowState(mount_rec->blk_device, "2");
+            if (!setBowState(mount_rec->blk_device, "2"))
+                return Status::fromExceptionCode(EINVAL, "Failed to set bow state");
         }
     }
-    if (android::base::RemoveFileIfExists(kMetadataCPFile, &err_str))
+    if (!android::base::RemoveFileIfExists(kMetadataCPFile, &err_str))
         return Status::fromExceptionCode(errno, err_str.c_str());
     return Status::ok();
 }
diff --git a/FsCrypt.cpp b/FsCrypt.cpp
index 087b916..cf179c4 100644
--- a/FsCrypt.cpp
+++ b/FsCrypt.cpp
@@ -57,6 +57,7 @@
 #include <android-base/logging.h>
 #include <android-base/properties.h>
 #include <android-base/stringprintf.h>
+#include <android-base/unique_fd.h>
 
 using android::base::StringPrintf;
 using android::base::WriteStringToFile;
@@ -172,8 +173,25 @@
     auto const current_path = get_ce_key_current_path(directory_path);
     if (to_fix != current_path) {
         LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
+        android::base::unique_fd fd(TEMP_FAILURE_RETRY(
+            open(to_fix.c_str(), O_RDONLY | O_CLOEXEC)));
+        if (fd == -1) {
+            PLOG(ERROR) << "Failed to open " << to_fix;
+            return;
+        }
+        if (fsync(fd) == -1) {
+            if (errno == EROFS || errno == EINVAL) {
+                PLOG(WARNING) << "Skip fsync " << to_fix
+                              << " on a file system does not support synchronization";
+            } else {
+                PLOG(ERROR) << "Failed to fsync " << to_fix;
+                unlink(to_fix.c_str());
+                return;
+            }
+        }
         if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
             PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
+            return;
         }
     }
 }
diff --git a/Utils.cpp b/Utils.cpp
index 04c3956..ce1f777 100644
--- a/Utils.cpp
+++ b/Utils.cpp
@@ -766,7 +766,7 @@
 }
 
 status_t UnmountTree(const std::string& prefix) {
-    FILE* fp = setmntent("/proc/mounts", "r");
+    FILE* fp = setmntent("/proc/mounts", "re");
     if (fp == NULL) {
         PLOG(ERROR) << "Failed to open /proc/mounts";
         return -errno;
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index e08319b..45dd591 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -590,7 +590,7 @@
 
     // Worst case we might have some stale mounts lurking around, so
     // force unmount those just to be safe.
-    FILE* fp = setmntent("/proc/mounts", "r");
+    FILE* fp = setmntent("/proc/mounts", "re");
     if (fp == NULL) {
         PLOG(ERROR) << "Failed to open /proc/mounts";
         return -errno;