Skip to content

Commit

Permalink
Vold: Add fsync in writeStringToFile() am: 701d05d
Browse files Browse the repository at this point in the history
am: ee8170f

Change-Id: I2ce9e9a6ea1969e1bf41ef6886dc4d27ee19f58a
  • Loading branch information
weivincewang authored and android-build-merger committed Nov 8, 2017
2 parents cbcb292 + ee8170f commit a087947
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion KeyStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/unique_fd.h>

#include <cutils/properties.h>

Expand Down Expand Up @@ -152,10 +153,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;
}

Expand Down

0 comments on commit a087947

Please sign in to comment.