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

Unified Diff: base/file_util_posix.cc

Issue 184563006: Move WriteFile and WriteFileDescriptor from file_util to base namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 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/file_util.h ('k') | base/file_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/file_util_posix.cc
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc
index f17b34161ca94312eaa153da27df038af23b0701..39b29b3e26bf7dcdb1aae8c389c0d437e41a2f5a 100644
--- a/base/file_util_posix.cc
+++ b/base/file_util_posix.cc
@@ -712,6 +712,33 @@ int ReadFile(const FilePath& filename, char* data, int size) {
return bytes_read;
}
+int WriteFile(const FilePath& filename, const char* data, int size) {
+ ThreadRestrictions::AssertIOAllowed();
+ int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666));
+ if (fd < 0)
+ return -1;
+
+ int bytes_written = WriteFileDescriptor(fd, data, size);
+ if (int ret = IGNORE_EINTR(close(fd)) < 0)
+ return ret;
+ return bytes_written;
+}
+
+int 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;
+ bytes_written_total += bytes_written_partial) {
+ bytes_written_partial =
+ HANDLE_EINTR(write(fd, data + bytes_written_total,
+ size - bytes_written_total));
+ if (bytes_written_partial < 0)
+ return -1;
+ }
+
+ return bytes_written_total;
+}
+
} // namespace base
// -----------------------------------------------------------------------------
@@ -750,40 +777,13 @@ FILE* OpenFile(const std::string& filename, const char* mode) {
return OpenFile(FilePath(filename), mode);
}
-int WriteFile(const FilePath& filename, const char* data, int size) {
- base::ThreadRestrictions::AssertIOAllowed();
- int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666));
- if (fd < 0)
- return -1;
-
- int bytes_written = WriteFileDescriptor(fd, data, size);
- if (int ret = IGNORE_EINTR(close(fd)) < 0)
- return ret;
- return bytes_written;
-}
-
-int 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;
- bytes_written_total += bytes_written_partial) {
- bytes_written_partial =
- HANDLE_EINTR(write(fd, data + bytes_written_total,
- size - bytes_written_total));
- if (bytes_written_partial < 0)
- return -1;
- }
-
- return bytes_written_total;
-}
-
int AppendToFile(const FilePath& filename, const char* data, int size) {
base::ThreadRestrictions::AssertIOAllowed();
int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND));
if (fd < 0)
return -1;
- int bytes_written = WriteFileDescriptor(fd, data, size);
+ int bytes_written = base::WriteFileDescriptor(fd, data, size);
if (int ret = IGNORE_EINTR(close(fd)) < 0)
return ret;
return bytes_written;
« no previous file with comments | « base/file_util.h ('k') | base/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698