diff options
author | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-23 01:36:31 +0000 |
---|---|---|
committer | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-23 01:36:31 +0000 |
commit | 5acb2cb8eed3ef2f6c244e13e2a0e39e607ab63f (patch) | |
tree | 609ed7d87013488724369da8c29f5a794d94e7e9 /net | |
parent | 147479d5e94f4c091b62c69a76bdfe6b6e15f39a (diff) | |
download | chromium_src-5acb2cb8eed3ef2f6c244e13e2a0e39e607ab63f.zip chromium_src-5acb2cb8eed3ef2f6c244e13e2a0e39e607ab63f.tar.gz chromium_src-5acb2cb8eed3ef2f6c244e13e2a0e39e607ab63f.tar.bz2 |
Disk cache: Make sure we don't overwrite external files when creating new ones.
TEST=unit test
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1270 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/disk_cache/backend_impl.cc | 4 | ||||
-rw-r--r-- | net/disk_cache/backend_unittest.cc | 23 | ||||
-rw-r--r-- | net/disk_cache/entry_impl.cc | 2 |
3 files changed, 27 insertions, 2 deletions
diff --git a/net/disk_cache/backend_impl.cc b/net/disk_cache/backend_impl.cc index 9b15d5e..6abcd94c 100644 --- a/net/disk_cache/backend_impl.cc +++ b/net/disk_cache/backend_impl.cc @@ -576,7 +576,7 @@ bool BackendImpl::CreateExternalFile(Addr* address) { int file_number = data_->header.last_file + 1; Addr file_address(0); bool success = false; - for (int i = 0; (i < 0x0fffffff) && !success; i++) { + for (int i = 0; (i < 0x0fffffff) && !success; i++, file_number++) { if (!file_address.SetFileNumber(file_number)) { file_number = 1; continue; @@ -584,7 +584,7 @@ bool BackendImpl::CreateExternalFile(Addr* address) { std::wstring name = GetFileName(file_address); scoped_refptr<disk_cache::File> file( new disk_cache::File(CreateOSFile(name.c_str(), OS_FILE_READ | - OS_FILE_WRITE |OS_FILE_SHARE_READ | OS_FILE_CREATE_ALWAYS, NULL))); + OS_FILE_WRITE |OS_FILE_SHARE_READ | OS_FILE_CREATE, NULL))); if (!file->IsValid()) continue; diff --git a/net/disk_cache/backend_unittest.cc b/net/disk_cache/backend_unittest.cc index cbdc450..661e52f 100644 --- a/net/disk_cache/backend_unittest.cc +++ b/net/disk_cache/backend_unittest.cc @@ -223,6 +223,29 @@ TEST_F(DiskCacheBackendTest, MemoryOnlyKeying) { BackendKeying(); } +TEST_F(DiskCacheBackendTest, ExternalFiles) { + InitCache(); + // First, lets create a file on the folder. + std::wstring filename = GetCachePath(); + file_util::AppendToPath(&filename, L"f_000001"); + + const int kDataSize = 50; + char data[kDataSize]; + CacheTestFillBuffer(data, kDataSize, false); + ASSERT_EQ(kDataSize, file_util::WriteFile(filename, data, kDataSize)); + + // Now let's create a file with the cache. + disk_cache::Entry* entry; + ASSERT_TRUE(cache_->CreateEntry("key", &entry)); + ASSERT_EQ(0, entry->WriteData(0, 20000, data, 0, NULL, false)); + entry->Close(); + + // And verify that the first file is still there. + char buffer[kDataSize]; + ASSERT_EQ(kDataSize, file_util::ReadFile(filename, buffer, kDataSize)); + EXPECT_EQ(0, memcmp(data, buffer, kDataSize)); +} + void DiskCacheBackendTest::BackendSetSize() { SetDirectMode(); const int cache_size = 0x10000; // 64 kB diff --git a/net/disk_cache/entry_impl.cc b/net/disk_cache/entry_impl.cc index 19dcae8..25c6236 100644 --- a/net/disk_cache/entry_impl.cc +++ b/net/disk_cache/entry_impl.cc @@ -331,6 +331,8 @@ int EntryImpl::WriteData(int index, int offset, const char* buf, int buf_len, unreported_size_[index] += offset + buf_len - entry_size; entry_.Data()->data_size[index] = offset + buf_len; entry_.set_modified(); + if (!buf_len) + truncate = true; // Force file extension. } else if (truncate) { // If the size was modified inside PrepareTarget, we should not do // anything here. |