Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(472)

Unified Diff: base/files/file_util_posix.cc

Issue 614893004: Refactor AppendToFile and WriteFileDescriptor (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address nits Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/files/file_util.h ('k') | base/files/file_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..561f5c7e6615ac45fcd7e44ea35ab9a4343bc4a8 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,33 @@ 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();
+ bool ret = true;
int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND));
- if (fd < 0)
- return -1;
+ if (fd < 0) {
+ VPLOG(1) << "Unable to create file " << filename.value();
+ 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)) {
+ VPLOG(1) << "Error while writing to file " << filename.value();
+ ret = false;
+ }
+
+ if (IGNORE_EINTR(close(fd)) < 0) {
+ VPLOG(1) << "Error while closing file " << filename.value();
+ return false;
+ }
+
+ return ret;
}
// Gets the current working directory for the process.
« no previous file with comments | « base/files/file_util.h ('k') | base/files/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698