summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWei Wang <wvw@google.com>2017-11-07 09:44:16 -0800
committerandroid-build-team Robot <android-build-team-robot@google.com>2017-11-09 05:43:19 +0000
commit8c4438e22c640311b16baa53e3935066848cc84b (patch)
tree4fe3a5eb9a71694d79a4756c1dd85f66542a3d52
parenta416f721d911ce84c6a32f39ceb231f250edf4ef (diff)
downloadandroid_system_vold-8c4438e22c640311b16baa53e3935066848cc84b.tar.gz
android_system_vold-8c4438e22c640311b16baa53e3935066848cc84b.tar.bz2
android_system_vold-8c4438e22c640311b16baa53e3935066848cc84b.zip
Vold: Add fsync in writeStringToFile()
Test: Build, test with ag/3180275 Bug: 68901441 Change-Id: Ieca9e5227025e00184a67508d5e8fbbddd12f21e (cherry picked from commit 701d05d32c8b415c6fbb2e8c9439185333870046)
-rw-r--r--KeyStorage.cpp22
1 files changed, 21 insertions, 1 deletions
diff --git a/KeyStorage.cpp b/KeyStorage.cpp
index 9d61555..20b2391 100644
--- a/KeyStorage.cpp
+++ b/KeyStorage.cpp
@@ -35,6 +35,7 @@
#include <android-base/file.h>
#include <android-base/logging.h>
+#include <android-base/unique_fd.h>
#include <cutils/properties.h>
@@ -153,10 +154,29 @@ static bool readFileToString(const std::string& filename, std::string* result) {
}
static bool writeStringToFile(const std::string& payload, const std::string& filename) {
- if (!android::base::WriteStringToFile(payload, filename)) {
+ android::base::unique_fd fd(TEMP_FAILURE_RETRY(
+ open(filename.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0666)));
+ if (fd == -1) {
+ PLOG(ERROR) << "Failed to open " << filename;
+ return false;
+ }
+ if (!android::base::WriteStringToFd(payload, fd)) {
PLOG(ERROR) << "Failed to write to " << filename;
+ unlink(filename.c_str());
return false;
}
+ // fsync as close won't guarantee flush data
+ // see close(2), fsync(2) and b/68901441
+ if (fsync(fd) == -1) {
+ if (errno == EROFS || errno == EINVAL) {
+ PLOG(WARNING) << "Skip fsync " << filename
+ << " on a file system does not support synchronization";
+ } else {
+ PLOG(ERROR) << "Failed to fsync " << filename;
+ unlink(filename.c_str());
+ return false;
+ }
+ }
return true;
}