| OLD | NEW |
| 1 // Copyright (c) 2009-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009-2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/disk_cache/sparse_control.h" | 5 #include "net/disk_cache/sparse_control.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 if (CHILD_ENTRY & entry_->GetEntryFlags()) | 312 if (CHILD_ENTRY & entry_->GetEntryFlags()) |
| 313 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 313 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 314 | 314 |
| 315 memset(&sparse_header_, 0, sizeof(sparse_header_)); | 315 memset(&sparse_header_, 0, sizeof(sparse_header_)); |
| 316 sparse_header_.signature = Time::Now().ToInternalValue(); | 316 sparse_header_.signature = Time::Now().ToInternalValue(); |
| 317 sparse_header_.magic = kIndexMagic; | 317 sparse_header_.magic = kIndexMagic; |
| 318 sparse_header_.parent_key_len = entry_->GetKey().size(); | 318 sparse_header_.parent_key_len = entry_->GetKey().size(); |
| 319 children_map_.Resize(kNumSparseBits, true); | 319 children_map_.Resize(kNumSparseBits, true); |
| 320 | 320 |
| 321 // Save the header. The bitmap is saved in the destructor. | 321 // Save the header. The bitmap is saved in the destructor. |
| 322 scoped_refptr<net::IOBuffer> buf = | 322 scoped_refptr<net::IOBuffer> buf( |
| 323 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_)); | 323 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_))); |
| 324 | 324 |
| 325 int rv = entry_->WriteData(kSparseIndex, 0, buf, sizeof(sparse_header_), NULL, | 325 int rv = entry_->WriteData(kSparseIndex, 0, buf, sizeof(sparse_header_), NULL, |
| 326 false); | 326 false); |
| 327 if (rv != sizeof(sparse_header_)) { | 327 if (rv != sizeof(sparse_header_)) { |
| 328 DLOG(ERROR) << "Unable to save sparse_header_"; | 328 DLOG(ERROR) << "Unable to save sparse_header_"; |
| 329 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 329 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 330 } | 330 } |
| 331 | 331 |
| 332 entry_->SetEntryFlags(PARENT_ENTRY); | 332 entry_->SetEntryFlags(PARENT_ENTRY); |
| 333 return net::OK; | 333 return net::OK; |
| 334 } | 334 } |
| 335 | 335 |
| 336 // We are opening an entry from disk. Make sure that our control data is there. | 336 // We are opening an entry from disk. Make sure that our control data is there. |
| 337 int SparseControl::OpenSparseEntry(int data_len) { | 337 int SparseControl::OpenSparseEntry(int data_len) { |
| 338 if (data_len < static_cast<int>(sizeof(SparseData))) | 338 if (data_len < static_cast<int>(sizeof(SparseData))) |
| 339 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 339 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 340 | 340 |
| 341 if (entry_->GetDataSize(kSparseData)) | 341 if (entry_->GetDataSize(kSparseData)) |
| 342 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 342 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 343 | 343 |
| 344 if (!(PARENT_ENTRY & entry_->GetEntryFlags())) | 344 if (!(PARENT_ENTRY & entry_->GetEntryFlags())) |
| 345 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 345 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 346 | 346 |
| 347 // Dont't go over board with the bitmap. 8 KB gives us offsets up to 64 GB. | 347 // Dont't go over board with the bitmap. 8 KB gives us offsets up to 64 GB. |
| 348 int map_len = data_len - sizeof(sparse_header_); | 348 int map_len = data_len - sizeof(sparse_header_); |
| 349 if (map_len > kMaxMapSize || map_len % 4) | 349 if (map_len > kMaxMapSize || map_len % 4) |
| 350 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 350 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 351 | 351 |
| 352 scoped_refptr<net::IOBuffer> buf = | 352 scoped_refptr<net::IOBuffer> buf( |
| 353 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_)); | 353 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_))); |
| 354 | 354 |
| 355 // Read header. | 355 // Read header. |
| 356 int rv = entry_->ReadData(kSparseIndex, 0, buf, sizeof(sparse_header_), NULL); | 356 int rv = entry_->ReadData(kSparseIndex, 0, buf, sizeof(sparse_header_), NULL); |
| 357 if (rv != static_cast<int>(sizeof(sparse_header_))) | 357 if (rv != static_cast<int>(sizeof(sparse_header_))) |
| 358 return net::ERR_CACHE_READ_FAILURE; | 358 return net::ERR_CACHE_READ_FAILURE; |
| 359 | 359 |
| 360 // The real validation should be performed by the caller. This is just to | 360 // The real validation should be performed by the caller. This is just to |
| 361 // double check. | 361 // double check. |
| 362 if (sparse_header_.magic != kIndexMagic || | 362 if (sparse_header_.magic != kIndexMagic || |
| 363 sparse_header_.parent_key_len != | 363 sparse_header_.parent_key_len != |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 child_ = entry_->backend_->OpenEntryImpl(key); | 395 child_ = entry_->backend_->OpenEntryImpl(key); |
| 396 if (!child_) | 396 if (!child_) |
| 397 return ContinueWithoutChild(key); | 397 return ContinueWithoutChild(key); |
| 398 | 398 |
| 399 EntryImpl* child = static_cast<EntryImpl*>(child_); | 399 EntryImpl* child = static_cast<EntryImpl*>(child_); |
| 400 if (!(CHILD_ENTRY & child->GetEntryFlags()) || | 400 if (!(CHILD_ENTRY & child->GetEntryFlags()) || |
| 401 child->GetDataSize(kSparseIndex) < | 401 child->GetDataSize(kSparseIndex) < |
| 402 static_cast<int>(sizeof(child_data_))) | 402 static_cast<int>(sizeof(child_data_))) |
| 403 return KillChildAndContinue(key, false); | 403 return KillChildAndContinue(key, false); |
| 404 | 404 |
| 405 scoped_refptr<net::WrappedIOBuffer> buf = | 405 scoped_refptr<net::WrappedIOBuffer> buf( |
| 406 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_)); | 406 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); |
| 407 | 407 |
| 408 // Read signature. | 408 // Read signature. |
| 409 int rv = child_->ReadData(kSparseIndex, 0, buf, sizeof(child_data_), NULL); | 409 int rv = child_->ReadData(kSparseIndex, 0, buf, sizeof(child_data_), NULL); |
| 410 if (rv != sizeof(child_data_)) | 410 if (rv != sizeof(child_data_)) |
| 411 return KillChildAndContinue(key, true); // This is a fatal failure. | 411 return KillChildAndContinue(key, true); // This is a fatal failure. |
| 412 | 412 |
| 413 if (child_data_.header.signature != sparse_header_.signature || | 413 if (child_data_.header.signature != sparse_header_.signature || |
| 414 child_data_.header.magic != kIndexMagic) | 414 child_data_.header.magic != kIndexMagic) |
| 415 return KillChildAndContinue(key, false); | 415 return KillChildAndContinue(key, false); |
| 416 | 416 |
| 417 if (child_data_.header.last_block_len < 0 || | 417 if (child_data_.header.last_block_len < 0 || |
| 418 child_data_.header.last_block_len > kBlockSize) { | 418 child_data_.header.last_block_len > kBlockSize) { |
| 419 // Make sure these values are always within range. | 419 // Make sure these values are always within range. |
| 420 child_data_.header.last_block_len = 0; | 420 child_data_.header.last_block_len = 0; |
| 421 child_data_.header.last_block = -1; | 421 child_data_.header.last_block = -1; |
| 422 } | 422 } |
| 423 | 423 |
| 424 return true; | 424 return true; |
| 425 } | 425 } |
| 426 | 426 |
| 427 void SparseControl::CloseChild() { | 427 void SparseControl::CloseChild() { |
| 428 scoped_refptr<net::WrappedIOBuffer> buf = | 428 scoped_refptr<net::WrappedIOBuffer> buf( |
| 429 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_)); | 429 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); |
| 430 | 430 |
| 431 // Save the allocation bitmap before closing the child entry. | 431 // Save the allocation bitmap before closing the child entry. |
| 432 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), | 432 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), |
| 433 NULL, false); | 433 NULL, false); |
| 434 if (rv != sizeof(child_data_)) | 434 if (rv != sizeof(child_data_)) |
| 435 DLOG(ERROR) << "Failed to save child data"; | 435 DLOG(ERROR) << "Failed to save child data"; |
| 436 child_->Release(); | 436 child_->Release(); |
| 437 child_ = NULL; | 437 child_ = NULL; |
| 438 } | 438 } |
| 439 | 439 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 int child_bit = static_cast<int>(offset_ >> 20); | 485 int child_bit = static_cast<int>(offset_ >> 20); |
| 486 | 486 |
| 487 // We may have to increase the bitmap of child entries. | 487 // We may have to increase the bitmap of child entries. |
| 488 if (children_map_.Size() <= child_bit) | 488 if (children_map_.Size() <= child_bit) |
| 489 children_map_.Resize(Bitmap::RequiredArraySize(child_bit + 1) * 32, true); | 489 children_map_.Resize(Bitmap::RequiredArraySize(child_bit + 1) * 32, true); |
| 490 | 490 |
| 491 children_map_.Set(child_bit, value); | 491 children_map_.Set(child_bit, value); |
| 492 } | 492 } |
| 493 | 493 |
| 494 void SparseControl::WriteSparseData() { | 494 void SparseControl::WriteSparseData() { |
| 495 scoped_refptr<net::IOBuffer> buf = new net::WrappedIOBuffer( | 495 scoped_refptr<net::IOBuffer> buf(new net::WrappedIOBuffer( |
| 496 reinterpret_cast<const char*>(children_map_.GetMap())); | 496 reinterpret_cast<const char*>(children_map_.GetMap()))); |
| 497 | 497 |
| 498 int len = children_map_.ArraySize() * 4; | 498 int len = children_map_.ArraySize() * 4; |
| 499 int rv = entry_->WriteData(kSparseIndex, sizeof(sparse_header_), buf, len, | 499 int rv = entry_->WriteData(kSparseIndex, sizeof(sparse_header_), buf, len, |
| 500 NULL, false); | 500 NULL, false); |
| 501 if (rv != len) { | 501 if (rv != len) { |
| 502 DLOG(ERROR) << "Unable to save sparse map"; | 502 DLOG(ERROR) << "Unable to save sparse map"; |
| 503 } | 503 } |
| 504 } | 504 } |
| 505 | 505 |
| 506 bool SparseControl::VerifyRange() { | 506 bool SparseControl::VerifyRange() { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 590 } | 590 } |
| 591 | 591 |
| 592 void SparseControl::InitChildData() { | 592 void SparseControl::InitChildData() { |
| 593 // We know the real type of child_. | 593 // We know the real type of child_. |
| 594 EntryImpl* child = static_cast<EntryImpl*>(child_); | 594 EntryImpl* child = static_cast<EntryImpl*>(child_); |
| 595 child->SetEntryFlags(CHILD_ENTRY); | 595 child->SetEntryFlags(CHILD_ENTRY); |
| 596 | 596 |
| 597 memset(&child_data_, 0, sizeof(child_data_)); | 597 memset(&child_data_, 0, sizeof(child_data_)); |
| 598 child_data_.header = sparse_header_; | 598 child_data_.header = sparse_header_; |
| 599 | 599 |
| 600 scoped_refptr<net::WrappedIOBuffer> buf = | 600 scoped_refptr<net::WrappedIOBuffer> buf( |
| 601 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_)); | 601 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); |
| 602 | 602 |
| 603 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), | 603 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), |
| 604 NULL, false); | 604 NULL, false); |
| 605 if (rv != sizeof(child_data_)) | 605 if (rv != sizeof(child_data_)) |
| 606 DLOG(ERROR) << "Failed to save child data"; | 606 DLOG(ERROR) << "Failed to save child data"; |
| 607 SetChildBit(true); | 607 SetChildBit(true); |
| 608 } | 608 } |
| 609 | 609 |
| 610 void SparseControl::DoChildrenIO() { | 610 void SparseControl::DoChildrenIO() { |
| 611 while (DoChildIO()) continue; | 611 while (DoChildIO()) continue; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 net::CompletionCallback* c = abort_callbacks_[i]; | 764 net::CompletionCallback* c = abort_callbacks_[i]; |
| 765 if (i == abort_callbacks_.size() - 1) | 765 if (i == abort_callbacks_.size() - 1) |
| 766 abort_callbacks_.clear(); | 766 abort_callbacks_.clear(); |
| 767 | 767 |
| 768 entry_->Release(); // Don't touch object after this line. | 768 entry_->Release(); // Don't touch object after this line. |
| 769 c->Run(net::OK); | 769 c->Run(net::OK); |
| 770 } | 770 } |
| 771 } | 771 } |
| 772 | 772 |
| 773 } // namespace disk_cache | 773 } // namespace disk_cache |
| OLD | NEW |