Commit 29b51210 authored by rvargas@google.com's avatar rvargas@google.com

Base: WritePlatformFile now retries the operation if

the OS writes less than expected (for POSIX).

BUG=94161
TEST=none
Review URL: http://codereview.chromium.org/7745008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@98497 0039d316-1c4b-4281-b951-d872f2087c98
parent 5e36e7f0
...@@ -123,7 +123,8 @@ BASE_EXPORT int ReadPlatformFile(PlatformFile file, int64 offset, ...@@ -123,7 +123,8 @@ BASE_EXPORT int ReadPlatformFile(PlatformFile file, int64 offset,
// Writes the given buffer into the file at the given offset, overwritting any // Writes the given buffer into the file at the given offset, overwritting any
// data that was previously there. Returns the number of bytes written, or -1 // data that was previously there. Returns the number of bytes written, or -1
// on error. // on error. Note that this function makes a best effort to write all data on
// all platforms.
BASE_EXPORT int WritePlatformFile(PlatformFile file, int64 offset, BASE_EXPORT int WritePlatformFile(PlatformFile file, int64 offset,
const char* data, int size); const char* data, int size);
......
...@@ -155,10 +155,21 @@ int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) { ...@@ -155,10 +155,21 @@ int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) {
int WritePlatformFile(PlatformFile file, int64 offset, int WritePlatformFile(PlatformFile file, int64 offset,
const char* data, int size) { const char* data, int size) {
if (file < 0) if (file < 0 || size < 0)
return -1; return -1;
return HANDLE_EINTR(pwrite(file, data, size, offset)); int bytes_written = 0;
int rv;
do {
rv = HANDLE_EINTR(pwrite(file, data + bytes_written,
size - bytes_written, offset + bytes_written));
if (rv <= 0)
break;
bytes_written += rv;
} while (bytes_written < size);
return bytes_written ? bytes_written : rv;
} }
bool TruncatePlatformFile(PlatformFile file, int64 length) { bool TruncatePlatformFile(PlatformFile file, int64 length) {
......
...@@ -37,23 +37,7 @@ int ReadFully(base::PlatformFile file, int64 offset, char* data, int size) { ...@@ -37,23 +37,7 @@ int ReadFully(base::PlatformFile file, int64 offset, char* data, int size) {
// Returns the number of bytes written. // Returns the number of bytes written.
int WriteFully(base::PlatformFile file, int64 offset, int WriteFully(base::PlatformFile file, int64 offset,
const char* data, int size) { const char* data, int size) {
int total_bytes_written = 0; return base::WritePlatformFile(file, offset, data, size);
int bytes_written;
while (total_bytes_written < size) {
bytes_written = base::WritePlatformFile(
file, offset + total_bytes_written, &data[total_bytes_written],
size - total_bytes_written);
if ((bytes_written == 0) && (size == 0))
return 0;
if ((bytes_written <= 0) || (bytes_written > size - total_bytes_written))
return -1;
total_bytes_written += bytes_written;
}
return total_bytes_written;
} }
} // namespace } // namespace
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment