| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/drive/file_cache.h" | 5 #include "chrome/browser/chromeos/drive/file_cache.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_enumerator.h" | 10 #include "base/files/file_enumerator.h" |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 FROM_HERE, | 386 FROM_HERE, |
| 387 base::Bind(&FileCache::DestroyOnBlockingPool, base::Unretained(this))); | 387 base::Bind(&FileCache::DestroyOnBlockingPool, base::Unretained(this))); |
| 388 } | 388 } |
| 389 | 389 |
| 390 void FileCache::DestroyOnBlockingPool() { | 390 void FileCache::DestroyOnBlockingPool() { |
| 391 AssertOnSequencedWorkerPool(); | 391 AssertOnSequencedWorkerPool(); |
| 392 delete this; | 392 delete this; |
| 393 } | 393 } |
| 394 | 394 |
| 395 bool FileCache::RecoverFilesFromCacheDirectory( | 395 bool FileCache::RecoverFilesFromCacheDirectory( |
| 396 const base::FilePath& dest_directory) { | 396 const base::FilePath& dest_directory, |
| 397 const std::map<std::string, FileCacheEntry>& recovered_cache_entries) { |
| 397 int file_number = 1; | 398 int file_number = 1; |
| 398 | 399 |
| 399 base::FileEnumerator enumerator(cache_file_directory_, | 400 base::FileEnumerator enumerator(cache_file_directory_, |
| 400 false, // not recursive | 401 false, // not recursive |
| 401 base::FileEnumerator::FILES); | 402 base::FileEnumerator::FILES); |
| 402 for (base::FilePath current = enumerator.Next(); !current.empty(); | 403 for (base::FilePath current = enumerator.Next(); !current.empty(); |
| 403 current = enumerator.Next()) { | 404 current = enumerator.Next()) { |
| 404 const std::string& id = GetIdFromPath(current); | 405 const std::string& id = GetIdFromPath(current); |
| 405 FileCacheEntry entry; | 406 FileCacheEntry entry; |
| 406 if (storage_->GetCacheEntry(id, &entry)) { | 407 if (storage_->GetCacheEntry(id, &entry)) { |
| 407 // This file is managed by FileCache, no need to recover it. | 408 // This file is managed by FileCache, no need to recover it. |
| 408 continue; | 409 continue; |
| 409 } | 410 } |
| 410 | 411 |
| 412 // If a cache entry which is non-dirty and has matching MD5 is found in |
| 413 // |recovered_cache_entries|, it means the current file is already uploaded |
| 414 // to the server. Just delete it instead of recovering it. |
| 415 std::map<std::string, FileCacheEntry>::const_iterator it = |
| 416 recovered_cache_entries.find(id); |
| 417 if (it != recovered_cache_entries.end()) { |
| 418 const FileCacheEntry& recovered_entry = it->second; |
| 419 // Due to the DB corruption, |recovered_entry| might be recovered from old |
| 420 // revision. Perform MD5 check even when is_dirty() is false just in case. |
| 421 if (!recovered_entry.is_dirty() && |
| 422 recovered_entry.md5() == util::GetMd5Digest(current)) { |
| 423 base::DeleteFile(current, false /* recursive */); |
| 424 continue; |
| 425 } |
| 426 } |
| 427 |
| 411 // Read file contents to sniff mime type. | 428 // Read file contents to sniff mime type. |
| 412 std::vector<char> content(net::kMaxBytesToSniff); | 429 std::vector<char> content(net::kMaxBytesToSniff); |
| 413 const int read_result = | 430 const int read_result = |
| 414 file_util::ReadFile(current, &content[0], content.size()); | 431 file_util::ReadFile(current, &content[0], content.size()); |
| 415 if (read_result < 0) { | 432 if (read_result < 0) { |
| 416 LOG(WARNING) << "Cannot read: " << current.value(); | 433 LOG(WARNING) << "Cannot read: " << current.value(); |
| 417 return false; | 434 return false; |
| 418 } | 435 } |
| 419 if (read_result == 0) // Skip empty files. | 436 if (read_result == 0) // Skip empty files. |
| 420 continue; | 437 continue; |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 const std::string& id = GetIdFromPath(new_path); | 578 const std::string& id = GetIdFromPath(new_path); |
| 562 new_path = GetCacheFilePath(util::CanonicalizeResourceId(id)); | 579 new_path = GetCacheFilePath(util::CanonicalizeResourceId(id)); |
| 563 if (new_path != current && !base::Move(current, new_path)) | 580 if (new_path != current && !base::Move(current, new_path)) |
| 564 return false; | 581 return false; |
| 565 } | 582 } |
| 566 return true; | 583 return true; |
| 567 } | 584 } |
| 568 | 585 |
| 569 } // namespace internal | 586 } // namespace internal |
| 570 } // namespace drive | 587 } // namespace drive |
| OLD | NEW |