diff options
author | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-29 21:29:17 +0000 |
---|---|---|
committer | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-29 21:29:17 +0000 |
commit | 8d8d449c4276cf520aae147976b075b62f7e8d62 (patch) | |
tree | 70b1ac55cbc29b4e294c18de41f5e16176e6cdab /chrome/common | |
parent | 5314e65f22a606008ed767f8216fc28a1d44d609 (diff) | |
download | chromium_src-8d8d449c4276cf520aae147976b075b62f7e8d62.zip chromium_src-8d8d449c4276cf520aae147976b075b62f7e8d62.tar.gz chromium_src-8d8d449c4276cf520aae147976b075b62f7e8d62.tar.bz2 |
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
Diffstat (limited to 'chrome/common')
-rw-r--r-- | chrome/common/important_file_writer.cc | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/chrome/common/important_file_writer.cc b/chrome/common/important_file_writer.cc index e770678..73381d1 100644 --- a/chrome/common/important_file_writer.cc +++ b/chrome/common/important_file_writer.cc @@ -36,22 +36,33 @@ class WriteToDiskTask : public Task { // as target file, so it can be moved in one step, and that the temp file // is securely created. FilePath tmp_file_path; - FILE* tmp_file = file_util::CreateAndOpenTemporaryFileInDir( - path_.DirName(), &tmp_file_path); - if (!tmp_file) { + if (!file_util::CreateTemporaryFileInDir(path_.DirName(), &tmp_file_path)) { LogFailure("could not create temporary file"); return; } - size_t bytes_written = fwrite(data_.data(), 1, data_.length(), tmp_file); - if (!file_util::CloseFile(tmp_file)) { + int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE; + 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"); file_util::Delete(tmp_file_path, false); return; } - if (bytes_written < data_.length()) { + + if (bytes_written < static_cast<int>(data_.length())) { LogFailure("error writing, bytes_written=" + - base::Uint64ToString(bytes_written)); + base::IntToString(bytes_written)); file_util::Delete(tmp_file_path, false); return; } @@ -102,6 +113,10 @@ bool ImportantFileWriter::HasPendingWrite() const { void ImportantFileWriter::WriteNow(const std::string& data) { DCHECK(CalledOnValidThread()); + if (data.length() > static_cast<size_t>(kint32max)) { + NOTREACHED(); + return; + } if (HasPendingWrite()) timer_.Stop(); |