|
|
|
@ -147,33 +147,6 @@ static bool readFileToString(const std::string& filename, std::string* result) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool writeStringToFile(const std::string& payload, const std::string& 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool readRandomBytesOrLog(size_t count, std::string* out) {
|
|
|
|
|
auto status = ReadRandomBytes(count, *out);
|
|
|
|
|
if (status != OK) {
|
|
|
|
|