diff options
author | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-01 23:04:07 +0000 |
---|---|---|
committer | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-01 23:04:07 +0000 |
commit | a37b56a06d42ed1e726ea86f529b295b2e6ea22a (patch) | |
tree | 9f9d8448c47f1dbf6f3c94399103f75e6f43e126 /base | |
parent | 2513e5dd5b5b2c0bfa512edf47ef0d6e07dd30cb (diff) | |
download | chromium_src-a37b56a06d42ed1e726ea86f529b295b2e6ea22a.zip chromium_src-a37b56a06d42ed1e726ea86f529b295b2e6ea22a.tar.gz chromium_src-a37b56a06d42ed1e726ea86f529b295b2e6ea22a.tar.bz2 |
Base: Change ReadPlatformFile to perform a best
effort read of the requested data, and add another
function (ReadPlatformFileNoBestEffort) that just
forwards the call to the OS.
Now ReadPlatformFile and WritePlatformFile behave
the same way.
BUG=none
TEST=base_unittests
Review URL: http://codereview.chromium.org/7821013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@99264 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/platform_file.h | 11 | ||||
-rw-r--r-- | base/platform_file_posix.cc | 19 | ||||
-rw-r--r-- | base/platform_file_unittest.cc | 26 | ||||
-rw-r--r-- | base/platform_file_win.cc | 5 |
4 files changed, 42 insertions, 19 deletions
diff --git a/base/platform_file.h b/base/platform_file.h index cf3716c..2bfd181 100644 --- a/base/platform_file.h +++ b/base/platform_file.h @@ -117,10 +117,19 @@ BASE_EXPORT PlatformFile CreatePlatformFile(const FilePath& name, BASE_EXPORT bool ClosePlatformFile(PlatformFile file); // Reads the given number of bytes (or until EOF is reached) starting with the -// given offset. Returns the number of bytes read, or -1 on error. +// given offset. Returns the number of bytes read, or -1 on error. Note that +// this function makes a best effort to read all data on all platforms, so it is +// not intended for stream oriented files but instead for cases when the normal +// expectation is that actually |size| bytes are read unless there is an error. BASE_EXPORT int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size); +// Reads the given number of bytes (or until EOF is reached) starting with the +// given offset, but does not make any effort to read all data on all platforms. +// Returns the number of bytes read, or -1 on error. +BASE_EXPORT int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, + char* data, int size); + // 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 // on error. Note that this function makes a best effort to write all data on diff --git a/base/platform_file_posix.cc b/base/platform_file_posix.cc index b058edc..d4ee768 100644 --- a/base/platform_file_posix.cc +++ b/base/platform_file_posix.cc @@ -147,6 +147,25 @@ bool ClosePlatformFile(PlatformFile file) { } int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) { + if (file < 0 || size < 0) + return -1; + + int bytes_read = 0; + int rv; + do { + rv = HANDLE_EINTR(pread(file, data + bytes_read, + size - bytes_read, offset + bytes_read)); + if (rv <= 0) + break; + + bytes_read += rv; + } while (bytes_read < size); + + return bytes_read ? bytes_read : rv; +} + +int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, + char* data, int size) { if (file < 0) return -1; diff --git a/base/platform_file_unittest.cc b/base/platform_file_unittest.cc index 7eb1af9..1acbaa6 100644 --- a/base/platform_file_unittest.cc +++ b/base/platform_file_unittest.cc @@ -13,24 +13,7 @@ namespace { // Reads from a file the given number of bytes, or until EOF is reached. // Returns the number of bytes read. int ReadFully(base::PlatformFile file, int64 offset, char* data, int size) { - int total_bytes_read = 0; - int bytes_read; - while (total_bytes_read < size) { - bytes_read = base::ReadPlatformFile( - file, offset + total_bytes_read, &data[total_bytes_read], - size - total_bytes_read); - - // If we reached EOF, bytes_read will be 0. - if (bytes_read == 0) - return total_bytes_read; - - if ((bytes_read < 0) || (bytes_read > size - total_bytes_read)) - return -1; - - total_bytes_read += bytes_read; - } - - return total_bytes_read; + return base::ReadPlatformFile(file, offset, data, size); } // Writes the given number of bytes to a file. @@ -192,6 +175,13 @@ TEST(PlatformFile, ReadWritePlatformFile) { for (int i = 0; i < bytes_read; i++) EXPECT_EQ(data_to_write[i], data_read_1[i]); + // Read again, but using the trivial native wrapper. + bytes_read = base::ReadPlatformFileNoBestEffort(file, 0, data_read_1, + kTestDataSize); + EXPECT_LE(bytes_read, kTestDataSize); + for (int i = 0; i < bytes_read; i++) + EXPECT_EQ(data_to_write[i], data_read_1[i]); + // Write past the end of the file. const int kOffsetBeyondEndOfFile = 10; const int kPartialWriteLength = 2; diff --git a/base/platform_file_win.cc b/base/platform_file_win.cc index 8374042..072c24a 100644 --- a/base/platform_file_win.cc +++ b/base/platform_file_win.cc @@ -134,6 +134,11 @@ int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) { return -1; } +int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, + char* data, int size) { + return ReadPlatformFile(file, offset, data, size); +} + int WritePlatformFile(PlatformFile file, int64 offset, const char* data, int size) { base::ThreadRestrictions::AssertIOAllowed(); |