Commit 8d8d449c authored by rvargas@google.com's avatar rvargas@google.com

ImportantFileWriter: Flush the data before closing the

temporary file so that the subsequent rename always operates
with data in disk.

BUG=89356
TEST=none
Review URL: http://codereview.chromium.org/7695014

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@98704 0039d316-1c4b-4281-b951-d872f2087c98
parent 5314e65f
...@@ -36,22 +36,33 @@ class WriteToDiskTask : public Task { ...@@ -36,22 +36,33 @@ class WriteToDiskTask : public Task {
// as target file, so it can be moved in one step, and that the temp file // as target file, so it can be moved in one step, and that the temp file
// is securely created. // is securely created.
FilePath tmp_file_path; FilePath tmp_file_path;
FILE* tmp_file = file_util::CreateAndOpenTemporaryFileInDir( if (!file_util::CreateTemporaryFileInDir(path_.DirName(), &tmp_file_path)) {
path_.DirName(), &tmp_file_path);
if (!tmp_file) {
LogFailure("could not create temporary file"); LogFailure("could not create temporary file");
return; return;
} }
size_t bytes_written = fwrite(data_.data(), 1, data_.length(), tmp_file); int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE;
if (!file_util::CloseFile(tmp_file)) { base::PlatformFile tmp_file =
base::CreatePlatformFile(tmp_file_path, flags, NULL, NULL);
if (tmp_file == base::kInvalidPlatformFileValue) {
LogFailure("could not open temporary file");
return;
}
CHECK_LE(data_.length(), static_cast<size_t>(kint32max));
int bytes_written = base::WritePlatformFile(
tmp_file, 0, data_.data(), static_cast<int>(data_.length()));
base::FlushPlatformFile(tmp_file); // Ignore return value.
if (!base::ClosePlatformFile(tmp_file)) {
LogFailure("failed to close temporary file"); LogFailure("failed to close temporary file");
file_util::Delete(tmp_file_path, false); file_util::Delete(tmp_file_path, false);
return; return;
} }
if (bytes_written < data_.length()) {
if (bytes_written < static_cast<int>(data_.length())) {
LogFailure("error writing, bytes_written=" + LogFailure("error writing, bytes_written=" +
base::Uint64ToString(bytes_written)); base::IntToString(bytes_written));
file_util::Delete(tmp_file_path, false); file_util::Delete(tmp_file_path, false);
return; return;
} }
...@@ -102,6 +113,10 @@ bool ImportantFileWriter::HasPendingWrite() const { ...@@ -102,6 +113,10 @@ bool ImportantFileWriter::HasPendingWrite() const {
void ImportantFileWriter::WriteNow(const std::string& data) { void ImportantFileWriter::WriteNow(const std::string& data) {
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
if (data.length() > static_cast<size_t>(kint32max)) {
NOTREACHED();
return;
}
if (HasPendingWrite()) if (HasPendingWrite())
timer_.Stop(); timer_.Stop();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment