Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: net/disk_cache/simple/simple_index.cc

Issue 13165005: Move FileEnumerator to its own file, do some refactoring. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge, fixes Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/disk_cache/cache_util_posix.cc ('k') | net/tools/dump_cache/dump_files.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/simple/simple_index.h" 5 #include "net/disk_cache/simple/simple_index.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/files/file_enumerator.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 #include "base/message_loop.h" 14 #include "base/message_loop.h"
14 #include "base/metrics/histogram.h" 15 #include "base/metrics/histogram.h"
15 #include "base/pickle.h" 16 #include "base/pickle.h"
16 #include "base/task_runner.h" 17 #include "base/task_runner.h"
17 #include "base/threading/thread_restrictions.h" 18 #include "base/threading/thread_restrictions.h"
18 #include "base/threading/worker_pool.h" 19 #include "base/threading/worker_pool.h"
19 #include "base/time.h" 20 #include "base/time.h"
20 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
21 #include "net/disk_cache/simple/simple_entry_format.h" 22 #include "net/disk_cache/simple/simple_entry_format.h"
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 441
441 io_thread->PostTask(FROM_HERE, 442 io_thread->PostTask(FROM_HERE,
442 base::Bind(completion_callback, 443 base::Bind(completion_callback,
443 base::Passed(&index_file_entries), 444 base::Passed(&index_file_entries),
444 force_index_flush)); 445 force_index_flush));
445 } 446 }
446 447
447 // static 448 // static
448 scoped_ptr<SimpleIndex::EntrySet> SimpleIndex::RestoreFromDisk( 449 scoped_ptr<SimpleIndex::EntrySet> SimpleIndex::RestoreFromDisk(
449 const base::FilePath& index_filename) { 450 const base::FilePath& index_filename) {
450 using file_util::FileEnumerator;
451 LOG(INFO) << "Simple Cache Index is being restored from disk."; 451 LOG(INFO) << "Simple Cache Index is being restored from disk.";
452 452
453 file_util::Delete(index_filename, /* recursive = */ false); 453 file_util::Delete(index_filename, /* recursive = */ false);
454 scoped_ptr<EntrySet> index_file_entries(new EntrySet()); 454 scoped_ptr<EntrySet> index_file_entries(new EntrySet());
455 455
456 // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format. 456 // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format.
457 COMPILE_ASSERT(kSimpleEntryFileCount == 3, 457 COMPILE_ASSERT(kSimpleEntryFileCount == 3,
458 file_pattern_must_match_file_count); 458 file_pattern_must_match_file_count);
459 459
460 const int kFileSuffixLenght = std::string("_0").size(); 460 const int kFileSuffixLenght = std::string("_0").size();
461 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]"); 461 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]");
462 FileEnumerator enumerator(index_filename.DirName(), 462 base::FileEnumerator enumerator(index_filename.DirName(),
463 false /* recursive */, 463 false /* recursive */,
464 FileEnumerator::FILES, 464 base::FileEnumerator::FILES,
465 file_pattern); 465 file_pattern);
466 for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); 466 for (base::FilePath file_path = enumerator.Next(); !file_path.empty();
467 file_path = enumerator.Next()) { 467 file_path = enumerator.Next()) {
468 const base::FilePath::StringType base_name = file_path.BaseName().value(); 468 const base::FilePath::StringType base_name = file_path.BaseName().value();
469 // Converting to std::string is OK since we never use UTF8 wide chars in our 469 // Converting to std::string is OK since we never use UTF8 wide chars in our
470 // file names. 470 // file names.
471 const std::string hash_name(base_name.begin(), base_name.end()); 471 const std::string hash_name(base_name.begin(), base_name.end());
472 const std::string hash_key_string = 472 const std::string hash_key_string =
473 hash_name.substr(0, hash_name.size() - kFileSuffixLenght); 473 hash_name.substr(0, hash_name.size() - kFileSuffixLenght);
474 uint64 hash_key = 0; 474 uint64 hash_key = 0;
475 if (!simple_util::GetEntryHashKeyFromHexString( 475 if (!simple_util::GetEntryHashKeyFromHexString(
476 hash_key_string, &hash_key)) { 476 hash_key_string, &hash_key)) {
477 LOG(WARNING) << "Invalid Entry Hash Key filename while restoring " 477 LOG(WARNING) << "Invalid Entry Hash Key filename while restoring "
478 << "Simple Index from disk: " << hash_name; 478 << "Simple Index from disk: " << hash_name;
479 // TODO(felipeg): Should we delete the invalid file here ? 479 // TODO(felipeg): Should we delete the invalid file here ?
480 continue; 480 continue;
481 } 481 }
482 482
483 FileEnumerator::FindInfo find_info = {}; 483 base::FileEnumerator::FileInfo find_info = enumerator.GetInfo();
484 enumerator.GetFindInfo(&find_info);
485 base::Time last_used_time; 484 base::Time last_used_time;
486 #if defined(OS_POSIX) 485 #if defined(OS_POSIX)
487 // For POSIX systems, a last access time is available. However, it's not 486 // For POSIX systems, a last access time is available. However, it's not
488 // guaranteed to be more accurate than mtime. It is no worse though. 487 // guaranteed to be more accurate than mtime. It is no worse though.
489 last_used_time = base::Time::FromTimeT(find_info.stat.st_atime); 488 last_used_time = base::Time::FromTimeT(find_info.stat().st_atime);
490 #endif 489 #endif
491 if (last_used_time.is_null()) 490 if (last_used_time.is_null())
492 last_used_time = FileEnumerator::GetLastModifiedTime(find_info); 491 last_used_time = find_info.GetLastModifiedTime();
493 492
494 int64 file_size = FileEnumerator::GetFilesize(find_info); 493 int64 file_size = find_info.GetSize();
495 EntrySet::iterator it = index_file_entries->find(hash_key); 494 EntrySet::iterator it = index_file_entries->find(hash_key);
496 if (it == index_file_entries->end()) { 495 if (it == index_file_entries->end()) {
497 InsertInEntrySet(EntryMetadata(hash_key, last_used_time, file_size), 496 InsertInEntrySet(EntryMetadata(hash_key, last_used_time, file_size),
498 index_file_entries.get()); 497 index_file_entries.get());
499 } else { 498 } else {
500 // Summing up the total size of the entry through all the *_[0-2] files 499 // Summing up the total size of the entry through all the *_[0-2] files
501 it->second.SetEntrySize(it->second.GetEntrySize() + file_size); 500 it->second.SetEntrySize(it->second.GetEntrySize() + file_size);
502 } 501 }
503 } 502 }
504 return index_file_entries.Pass(); 503 return index_file_entries.Pass();
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata, 589 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata,
591 entries_set_); 590 entries_set_);
592 cache_thread_->PostTask(FROM_HERE, base::Bind( 591 cache_thread_->PostTask(FROM_HERE, base::Bind(
593 &SimpleIndex::WriteToDiskInternal, 592 &SimpleIndex::WriteToDiskInternal,
594 index_filename_, 593 index_filename_,
595 base::Passed(&pickle), 594 base::Passed(&pickle),
596 start)); 595 start));
597 } 596 }
598 597
599 } // namespace disk_cache 598 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/cache_util_posix.cc ('k') | net/tools/dump_cache/dump_files.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698