| Index: base/files/file_util_posix.cc
|
| diff --git a/base/files/file_util_posix.cc b/base/files/file_util_posix.cc
|
| index 07c21d1430f94527d4c3faf87078913bb265ab2e..86eada5a4b9344c21f2acf64c8ca01ddb7c89fea 100644
|
| --- a/base/files/file_util_posix.cc
|
| +++ b/base/files/file_util_posix.cc
|
| @@ -682,13 +682,13 @@ int WriteFile(const FilePath& filename, const char* data, int size) {
|
| if (fd < 0)
|
| return -1;
|
|
|
| - int bytes_written = WriteFileDescriptor(fd, data, size);
|
| + int bytes_written = WriteFileDescriptor(fd, data, size) ? size : -1;
|
| if (IGNORE_EINTR(close(fd)) < 0)
|
| return -1;
|
| return bytes_written;
|
| }
|
|
|
| -int WriteFileDescriptor(const int fd, const char* data, int size) {
|
| +bool WriteFileDescriptor(const int fd, const char* data, int size) {
|
| // Allow for partial writes.
|
| ssize_t bytes_written_total = 0;
|
| for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
|
| @@ -697,22 +697,37 @@ int WriteFileDescriptor(const int fd, const char* data, int size) {
|
| HANDLE_EINTR(write(fd, data + bytes_written_total,
|
| size - bytes_written_total));
|
| if (bytes_written_partial < 0)
|
| - return -1;
|
| + return false;
|
| }
|
|
|
| - return bytes_written_total;
|
| + return true;
|
| }
|
|
|
| -int AppendToFile(const FilePath& filename, const char* data, int size) {
|
| +bool AppendToFile(const FilePath& filename, const char* data, int size) {
|
| ThreadRestrictions::AssertIOAllowed();
|
| + int saved_errno = 0;
|
| int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND));
|
| if (fd < 0)
|
| - return -1;
|
| + return false;
|
|
|
| - int bytes_written = WriteFileDescriptor(fd, data, size);
|
| - if (IGNORE_EINTR(close(fd)) < 0)
|
| - return -1;
|
| - return bytes_written;
|
| + // This call will either write all of the data or return false.
|
| + if (!WriteFileDescriptor(fd, data, size))
|
| + saved_errno = errno;
|
| +
|
| + if (IGNORE_EINTR(close(fd)) < 0 && saved_errno == 0) {
|
| + // An error occurred while closing the file but the data was written
|
| + // successfully so it's ok to return here.
|
| + return false;
|
| + }
|
| +
|
| + // Restore any error that occurred while writing the data. This takes
|
| + // precedence over any error that occurred while closing the file.
|
| + if (saved_errno != 0) {
|
| + errno = saved_errno;
|
| + return false;
|
| + }
|
| +
|
| + return true;
|
| }
|
|
|
| // Gets the current working directory for the process.
|
|
|