vold: fsync both file and directory after write keys

Use vold version of writeStringToFile which fsync files, and
manually fsync directories after initialize global DE

(cherry picked from commit a98464f688d6e16ca7558251306ece98058b55ce)

Bug: 71810347
Test: Build pass and reboot stress test.
      Original boot failure symptom is NOT reproducible.
Change-Id: I1ca8f8cf0ccfd01075a9c33f79042e58d99aea26
Merged-In: I1ca8f8cf0ccfd01075a9c33f79042e58d99aea26
diff --git a/FsCrypt.cpp b/FsCrypt.cpp
index e810d58..ea8bb73 100644
--- a/FsCrypt.cpp
+++ b/FsCrypt.cpp
@@ -60,10 +60,10 @@
 #include <android-base/unique_fd.h>
 
 using android::base::StringPrintf;
-using android::base::WriteStringToFile;
 using android::fs_mgr::GetEntryForMountPoint;
 using android::vold::kEmptyAuthentication;
 using android::vold::KeyBuffer;
+using android::vold::writeStringToFile;
 
 namespace {
 
@@ -351,18 +351,14 @@
 
     std::string modestring = device_ref.contents_mode + ":" + device_ref.filenames_mode;
     std::string mode_filename = std::string("/data") + fscrypt_key_mode;
-    if (!android::base::WriteStringToFile(modestring, mode_filename)) {
-        PLOG(ERROR) << "Cannot save type";
-        return false;
-    }
+    if (!android::vold::writeStringToFile(modestring, mode_filename)) return false;
 
     std::string ref_filename = std::string("/data") + fscrypt_key_ref;
-    if (!android::base::WriteStringToFile(device_ref.key_raw_ref, ref_filename)) {
-        PLOG(ERROR) << "Cannot save key reference to:" << ref_filename;
-        return false;
-    }
+    if (!android::vold::writeStringToFile(device_ref.key_raw_ref, ref_filename)) return false;
+
     LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
 
+    if (!android::vold::FsyncDirectory(device_key_dir)) return false;
     s_global_de_initialized = true;
     return true;
 }
@@ -419,7 +415,7 @@
     // Clean any dirty pages (otherwise they won't be dropped).
     sync();
     // Drop inode and page caches.
-    if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
+    if (!writeStringToFile("3", "/proc/sys/vm/drop_caches")) {
         PLOG(ERROR) << "Failed to drop caches during key eviction";
     }
 }