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 "components/drive/file_cache.h" | 5 #include "components/drive/file_cache.h" |
6 | 6 |
| 7 #include <unistd.h> |
| 8 |
7 #include <queue> | 9 #include <queue> |
8 #include <vector> | 10 #include <vector> |
9 | 11 |
10 #include "base/bind.h" | 12 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
12 #include "base/callback_helpers.h" | 14 #include "base/callback_helpers.h" |
13 #include "base/files/file_enumerator.h" | 15 #include "base/files/file_enumerator.h" |
14 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
15 #include "base/location.h" | 17 #include "base/location.h" |
16 #include "base/logging.h" | 18 #include "base/logging.h" |
(...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 const std::string& id = GetIdFromPath(new_path); | 676 const std::string& id = GetIdFromPath(new_path); |
675 new_path = GetCacheFilePath(util::CanonicalizeResourceId(id)); | 677 new_path = GetCacheFilePath(util::CanonicalizeResourceId(id)); |
676 if (new_path != current && !base::Move(current, new_path)) | 678 if (new_path != current && !base::Move(current, new_path)) |
677 return false; | 679 return false; |
678 } | 680 } |
679 return true; | 681 return true; |
680 } | 682 } |
681 | 683 |
682 // static | 684 // static |
683 bool FileCache::MigrateCacheFiles(const base::FilePath& from, | 685 bool FileCache::MigrateCacheFiles(const base::FilePath& from, |
684 const base::FilePath& to, | 686 const base::FilePath& to_files, |
| 687 const base::FilePath& to_links, |
685 ResourceMetadataStorage* metadata_storage) { | 688 ResourceMetadataStorage* metadata_storage) { |
686 scoped_ptr<ResourceMetadataStorage::Iterator> it = | 689 scoped_ptr<ResourceMetadataStorage::Iterator> it = |
687 metadata_storage->GetIterator(); | 690 metadata_storage->GetIterator(); |
688 for (; !it->IsAtEnd(); it->Advance()) { | 691 for (; !it->IsAtEnd(); it->Advance()) { |
689 const ResourceEntry& entry = it->GetValue(); | 692 const ResourceEntry& entry = it->GetValue(); |
690 if (!entry.file_specific_info().cache_state().is_present()) { | 693 if (!entry.file_specific_info().cache_state().is_present()) { |
691 continue; | 694 continue; |
692 } | 695 } |
693 | 696 |
| 697 // Ignore missing cache file case since it never succeeds. |
| 698 // TODO(yawano): handle this case at metadata validation in FileCache. |
694 const base::FilePath move_from = GetPathForId(from, entry.local_id()); | 699 const base::FilePath move_from = GetPathForId(from, entry.local_id()); |
695 if (!base::PathExists(move_from)) { | 700 if (!base::PathExists(move_from)) { |
696 continue; | 701 continue; |
697 } | 702 } |
698 | 703 |
699 const base::FilePath move_to = GetPathForId(to, entry.local_id()); | 704 // Create hard link to cache file if it's pinned or dirty. cryptohome will |
| 705 // not delete a cache file if there is a hard link to it. |
| 706 const FileCacheEntry& file_cache_entry = |
| 707 entry.file_specific_info().cache_state(); |
| 708 if (file_cache_entry.is_pinned() || file_cache_entry.is_dirty()) { |
| 709 const base::FilePath link_path = GetPathForId(to_links, entry.local_id()); |
| 710 int link_result = link(move_from.AsUTF8Unsafe().c_str(), |
| 711 link_path.AsUTF8Unsafe().c_str()); |
| 712 if (link_result != 0 && errno != EEXIST) { |
| 713 return false; |
| 714 } |
| 715 } |
| 716 |
| 717 // Move cache file. |
| 718 const base::FilePath move_to = GetPathForId(to_files, entry.local_id()); |
700 if (!base::Move(move_from, move_to)) { | 719 if (!base::Move(move_from, move_to)) { |
701 return false; | 720 return false; |
702 } | 721 } |
703 | |
704 // TODO(yawano): create hard link if entry is marked as pinned or dirty. | |
705 } | 722 } |
706 | 723 |
707 return true; | 724 return true; |
708 } | 725 } |
709 | 726 |
710 void FileCache::CloseForWrite(const std::string& id) { | 727 void FileCache::CloseForWrite(const std::string& id) { |
711 AssertOnSequencedWorkerPool(); | 728 AssertOnSequencedWorkerPool(); |
712 | 729 |
713 std::map<std::string, int>::iterator it = write_opened_files_.find(id); | 730 std::map<std::string, int>::iterator it = write_opened_files_.find(id); |
714 if (it == write_opened_files_.end()) | 731 if (it == write_opened_files_.end()) |
(...skipping 23 matching lines...) Expand all Loading... |
738 | 755 |
739 bool FileCache::IsEvictable(const std::string& id, const ResourceEntry& entry) { | 756 bool FileCache::IsEvictable(const std::string& id, const ResourceEntry& entry) { |
740 return entry.file_specific_info().has_cache_state() && | 757 return entry.file_specific_info().has_cache_state() && |
741 !entry.file_specific_info().cache_state().is_pinned() && | 758 !entry.file_specific_info().cache_state().is_pinned() && |
742 !entry.file_specific_info().cache_state().is_dirty() && | 759 !entry.file_specific_info().cache_state().is_dirty() && |
743 !mounted_files_.count(id); | 760 !mounted_files_.count(id); |
744 } | 761 } |
745 | 762 |
746 } // namespace internal | 763 } // namespace internal |
747 } // namespace drive | 764 } // namespace drive |
OLD | NEW |