Chromium Code Reviews| Index: base/files/file_util_win.cc |
| diff --git a/base/files/file_util_win.cc b/base/files/file_util_win.cc |
| index b58619192378d50bab86a791dcdce665cf7a0545..f8583c38967a7620fe8019f7f171a687771c8b00 100644 |
| --- a/base/files/file_util_win.cc |
| +++ b/base/files/file_util_win.cc |
| @@ -644,7 +644,7 @@ int WriteFile(const FilePath& filename, const char* data, int size) { |
| return -1; |
| } |
| -int AppendToFile(const FilePath& filename, const char* data, int size) { |
| +bool AppendToFile(const FilePath& filename, const char* data, int size) { |
| ThreadRestrictions::AssertIOAllowed(); |
| base::win::ScopedHandle file(CreateFile(filename.value().c_str(), |
| FILE_APPEND_DATA, |
| @@ -653,27 +653,29 @@ int AppendToFile(const FilePath& filename, const char* data, int size) { |
| OPEN_EXISTING, |
| 0, |
| NULL)); |
| - if (!file.IsValid()) { |
| - DPLOG(WARNING) << "CreateFile failed for path " |
| - << UTF16ToUTF8(filename.value()); |
| - return -1; |
| - } |
| + if (!file.IsValid()) |
| + return false; |
| + DWORD saved_error = 0; |
| DWORD written; |
| - BOOL result = ::WriteFile(file.Get(), data, size, &written, NULL); |
| - if (result && static_cast<int>(written) == size) |
| - return written; |
| - if (!result) { |
| - // WriteFile failed. |
| - DPLOG(WARNING) << "writing file " << UTF16ToUTF8(filename.value()) |
| - << " failed"; |
| - } else { |
| - // Didn't write all the bytes. |
| - DLOG(WARNING) << "wrote" << written << " bytes to " |
| - << UTF16ToUTF8(filename.value()) << " expected " << size; |
| + // According to MSFT documentation the only time a partial write can occur is |
| + // if we are writing to a pipe, which we aren't currently doing so if this |
|
rvargas (doing something else)
2014/10/01 20:01:14
I don't think this code has enough context to know
|
| + // call succeeds we can assume that all the data was written out to the file. |
| + if (!::WriteFile(file.Get(), data, size, &written, NULL)) |
| + saved_error = ::GetLastError(); |
| + |
| + // Close the file now so that we can restore the write error, if any. |
| + file.Close(); |
| + |
| + // Restore any error that occurred while writing the data. This takes |
| + // precedence over any errors that happened while closing the file. |
| + if (saved_error != 0) { |
| + ::SetLastError(saved_error); |
| + return false; |
| } |
| - return -1; |
| + |
| + return true; |
| } |
| // Gets the current working directory for the process. |