diff options
author | sky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-09 15:49:20 +0000 |
---|---|---|
committer | sky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-09 15:49:20 +0000 |
commit | 0643a49e311c0ddb17f292f525ebd11f51f158a1 (patch) | |
tree | de8f50c0070fd6ff52eced3bdd96c747f1e9ea45 | |
parent | f3e8965444b0b615ed8aabfcacc5840f6684c0c5 (diff) | |
download | chromium_src-0643a49e311c0ddb17f292f525ebd11f51f158a1.zip chromium_src-0643a49e311c0ddb17f292f525ebd11f51f158a1.tar.gz chromium_src-0643a49e311c0ddb17f292f525ebd11f51f158a1.tar.bz2 |
Adds truncate to FileStream.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/39301
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@11245 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | net/base/file_stream.h | 5 | ||||
-rw-r--r-- | net/base/file_stream_posix.cc | 17 | ||||
-rw-r--r-- | net/base/file_stream_unittest.cc | 27 | ||||
-rw-r--r-- | net/base/file_stream_win.cc | 24 |
4 files changed, 73 insertions, 0 deletions
diff --git a/net/base/file_stream.h b/net/base/file_stream.h index 28b40e7..9447c07 100644 --- a/net/base/file_stream.h +++ b/net/base/file_stream.h @@ -109,6 +109,11 @@ class FileStream { // You can pass NULL as the callback for synchronous I/O. int Write(const char* buf, int buf_len, CompletionCallback* callback); + // Truncates the file to be |bytes| length. This is only valid for writable + // files. After truncation the file stream is positioned at |bytes|. The new + // position is retured, or a value < 0 on error. + int64 Truncate(int64 bytes); + private: class AsyncContext; friend class AsyncContext; diff --git a/net/base/file_stream_posix.cc b/net/base/file_stream_posix.cc index 34f7889..d7f4811 100644 --- a/net/base/file_stream_posix.cc +++ b/net/base/file_stream_posix.cc @@ -201,4 +201,21 @@ int FileStream::Write( return total_bytes_written; } +int64 FileStream::Truncate(int64 bytes) { + if (!IsOpen()) + return ERR_UNEXPECTED; + + // We better be open for reading. + DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); + + // Seek to the position to truncate from. + int64 seek_position = Seek(FROM_BEGIN, bytes); + if (seek_position != bytes) + return ERR_UNEXPECTED; + + // And truncate the file. + int result = ftruncate(file_, bytes); + return result == 0 ? seek_position : MapErrorCode(errno); +} + } // namespace net diff --git a/net/base/file_stream_unittest.cc b/net/base/file_stream_unittest.cc index 318eb93..966b9ff 100644 --- a/net/base/file_stream_unittest.cc +++ b/net/base/file_stream_unittest.cc @@ -406,4 +406,31 @@ TEST_F(FileStreamTest, BasicReadWrite) { EXPECT_EQ(kTestDataSize * 2, file_size); } +// Tests truncating a file. +TEST_F(FileStreamTest, Truncate) { + int flags = base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE; + + net::FileStream write_stream; + ASSERT_EQ(net::OK, write_stream.Open(temp_file_path(), flags)); + + // Write some data to the file. + const char test_data[] = "0123456789"; + write_stream.Write(test_data, arraysize(test_data), NULL); + + // Truncate the file. + ASSERT_EQ(4, write_stream.Truncate(4)); + + // Write again. + write_stream.Write(test_data, 4, NULL); + + // Close the stream. + write_stream.Close(); + + // Read in the contents and make sure we get back what we expected. + std::string read_contents; + file_util::ReadFileToString(temp_file_path(), &read_contents); + + ASSERT_TRUE(read_contents == "01230123"); +} + // TODO(erikkay): more READ_WRITE tests? diff --git a/net/base/file_stream_win.cc b/net/base/file_stream_win.cc index 0a6c201..93a3fd9 100644 --- a/net/base/file_stream_win.cc +++ b/net/base/file_stream_win.cc @@ -287,4 +287,28 @@ int FileStream::Write( return rv; } +int64 FileStream::Truncate(int64 bytes) { + if (!IsOpen()) + return ERR_UNEXPECTED; + + // We better be open for reading. + DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); + + // Seek to the position to truncate from. + int64 seek_position = Seek(FROM_BEGIN, bytes); + if (seek_position != bytes) + return ERR_UNEXPECTED; + + // And truncate the file. + BOOL result = SetEndOfFile(file_); + if (!result) { + DWORD error = GetLastError(); + LOG(WARNING) << "SetEndOfFile failed: " << error; + return MapErrorCode(error); + } + + // Success. + return seek_position; +} + } // namespace net |