diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2017-11-13 19:52:36 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2017-11-13 19:52:36 +0000 |
commit | e4772ac91ff2304aefbf3d3bbbddeb44a88395ad (patch) | |
tree | 4fe3a5eb9a71694d79a4756c1dd85f66542a3d52 | |
parent | a416f721d911ce84c6a32f39ceb231f250edf4ef (diff) | |
parent | 7ce9f66723ff5f2fd2704ee2fd37e56b72e19bea (diff) | |
download | vold-oreo-m4-s1-release.tar.gz |
Snap for 4448085 from 7ce9f66723ff5f2fd2704ee2fd37e56b72e19bea to oc-m4-releaseandroid-8.1.0_r23oreo-m4-s1-release
Change-Id: I9d9ec9a8d00b85fed7efd999ccede1230cf25734
-rw-r--r-- | KeyStorage.cpp | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/KeyStorage.cpp b/KeyStorage.cpp index 9d615550..20b23915 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; } |