Chromium Code Reviews| Index: chrome/common/important_file_writer.cc |
| =================================================================== |
| --- chrome/common/important_file_writer.cc (revision 97170) |
| +++ chrome/common/important_file_writer.cc (working copy) |
| @@ -36,22 +36,33 @@ |
| // 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); |
|
Scott Hess - ex-Googler
2011/08/23 00:54:38
I would prefer if you could not introduce a race c
rvargas (doing something else)
2011/08/23 01:41:52
I was writing the code with some posix specific ve
Scott Hess - ex-Googler
2011/08/23 19:23:57
The rename case is a very good point! I won't dig
|
| + 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())); |
|
Scott Hess - ex-Googler
2011/08/23 19:27:17
BTW ... AFAICT the POSIX version of this function
rvargas (doing something else)
2011/08/23 23:36:58
In that case, line 63 should detect the error and
|
| + base::FlushPlatformFile(tmp_file); // Ignore return value. |
|
Scott Hess - ex-Googler
2011/08/23 00:54:38
Would it be reasonable to wire up something like f
rvargas (doing something else)
2011/08/23 01:41:52
Not sure. I can make fflush do the right thing on
Scott Hess - ex-Googler
2011/08/23 19:23:57
OK, that's all I wanted to hear - I hate having mu
|
| + |
| + 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 @@ |
| void ImportantFileWriter::WriteNow(const std::string& data) { |
| DCHECK(CalledOnValidThread()); |
| + if (data.length() > static_cast<size_t>(kint32max)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| if (HasPendingWrite()) |
| timer_.Stop(); |