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/gdata/gdata_cache.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_cache.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/chromeos/chromeos_version.h" | 9 #include "base/chromeos/chromeos_version.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 794 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
805 void GDataCache::Store(const std::string& resource_id, | 805 void GDataCache::Store(const std::string& resource_id, |
806 const std::string& md5, | 806 const std::string& md5, |
807 const FilePath& source_path, | 807 const FilePath& source_path, |
808 FileOperationType file_operation_type, | 808 FileOperationType file_operation_type, |
809 base::PlatformFileError* error) { | 809 base::PlatformFileError* error) { |
810 AssertOnSequencedWorkerPool(); | 810 AssertOnSequencedWorkerPool(); |
811 DCHECK(error); | 811 DCHECK(error); |
812 | 812 |
813 FilePath dest_path; | 813 FilePath dest_path; |
814 FilePath symlink_path; | 814 FilePath symlink_path; |
815 GDataCacheEntry new_cache_entry(md5, CACHE_STATE_NONE); | |
816 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_TMP; | 815 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_TMP; |
817 | 816 |
818 scoped_ptr<GDataCacheEntry> cache_entry = GetCacheEntry(resource_id, md5); | 817 scoped_ptr<GDataCacheEntry> cache_entry = GetCacheEntry(resource_id, md5); |
819 | 818 |
820 // If file was previously pinned, store it in persistent dir and create | 819 // If file was previously pinned, store it in persistent dir and create |
821 // symlink in pinned dir. | 820 // symlink in pinned dir. |
822 if (cache_entry.get()) { // File exists in cache. | 821 if (cache_entry.get()) { // File exists in cache. |
823 // If file is dirty or mounted, return error. | 822 // If file is dirty or mounted, return error. |
824 if (cache_entry->IsDirty() || cache_entry->IsMounted()) { | 823 if (cache_entry->IsDirty() || cache_entry->IsMounted()) { |
825 LOG(WARNING) << "Can't store a file to replace a " | 824 LOG(WARNING) << "Can't store a file to replace a " |
826 << (cache_entry->IsDirty() ? "dirty" : "mounted") | 825 << (cache_entry->IsDirty() ? "dirty" : "mounted") |
827 << " file: res_id=" << resource_id | 826 << " file: res_id=" << resource_id |
828 << ", md5=" << md5; | 827 << ", md5=" << md5; |
829 *error = base::PLATFORM_FILE_ERROR_IN_USE; | 828 *error = base::PLATFORM_FILE_ERROR_IN_USE; |
830 return; | 829 return; |
831 } | 830 } |
832 | 831 |
833 new_cache_entry.set_cache_state(cache_entry->cache_state()); | |
834 | |
835 // If file is pinned, determines destination path. | 832 // If file is pinned, determines destination path. |
836 if (cache_entry->IsPinned()) { | 833 if (cache_entry->IsPinned()) { |
837 sub_dir_type = CACHE_TYPE_PERSISTENT; | 834 sub_dir_type = CACHE_TYPE_PERSISTENT; |
838 dest_path = GetCacheFilePath(resource_id, md5, sub_dir_type, | 835 dest_path = GetCacheFilePath(resource_id, md5, sub_dir_type, |
839 CACHED_FILE_FROM_SERVER); | 836 CACHED_FILE_FROM_SERVER); |
840 symlink_path = GetCacheFilePath( | 837 symlink_path = GetCacheFilePath( |
841 resource_id, std::string(), CACHE_TYPE_PINNED, | 838 resource_id, std::string(), CACHE_TYPE_PINNED, |
842 CACHED_FILE_FROM_SERVER); | 839 CACHED_FILE_FROM_SERVER); |
843 } | 840 } |
| 841 } else { |
| 842 // The file does not exist in the cache. Create a new entry. |
| 843 cache_entry.reset(new GDataCacheEntry); |
844 } | 844 } |
845 | 845 |
846 // File wasn't pinned or doesn't exist in cache, store in tmp dir. | 846 // File wasn't pinned or doesn't exist in cache, store in tmp dir. |
847 if (dest_path.empty()) { | 847 if (dest_path.empty()) { |
848 DCHECK_EQ(CACHE_TYPE_TMP, sub_dir_type); | 848 DCHECK_EQ(CACHE_TYPE_TMP, sub_dir_type); |
849 dest_path = GetCacheFilePath(resource_id, md5, sub_dir_type, | 849 dest_path = GetCacheFilePath(resource_id, md5, sub_dir_type, |
850 CACHED_FILE_FROM_SERVER); | 850 CACHED_FILE_FROM_SERVER); |
851 } | 851 } |
852 | 852 |
853 *error = ModifyCacheState( | 853 *error = ModifyCacheState( |
(...skipping 19 matching lines...) Expand all Loading... |
873 // Note that ReplaceExtension automatically prefixes the extension with the | 873 // Note that ReplaceExtension automatically prefixes the extension with the |
874 // extension separator '.'. | 874 // extension separator '.'. |
875 stale_filenames_pattern = dest_path.ReplaceExtension(util::kWildCard); | 875 stale_filenames_pattern = dest_path.ReplaceExtension(util::kWildCard); |
876 } | 876 } |
877 | 877 |
878 // Delete files that match |stale_filenames_pattern| except for |dest_path|. | 878 // Delete files that match |stale_filenames_pattern| except for |dest_path|. |
879 DeleteFilesSelectively(stale_filenames_pattern, dest_path); | 879 DeleteFilesSelectively(stale_filenames_pattern, dest_path); |
880 | 880 |
881 if (*error == base::PLATFORM_FILE_OK) { | 881 if (*error == base::PLATFORM_FILE_OK) { |
882 // Now that file operations have completed, update cache map. | 882 // Now that file operations have completed, update cache map. |
883 new_cache_entry.SetPresent(true); | 883 cache_entry->set_md5(md5); |
884 new_cache_entry.SetPersistent(sub_dir_type == CACHE_TYPE_PERSISTENT); | 884 cache_entry->SetPresent(true); |
885 metadata_->AddOrUpdateCacheEntry(resource_id, new_cache_entry); | 885 cache_entry->SetPersistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
| 886 metadata_->AddOrUpdateCacheEntry(resource_id, *cache_entry); |
886 } | 887 } |
887 } | 888 } |
888 | 889 |
889 void GDataCache::Pin(const std::string& resource_id, | 890 void GDataCache::Pin(const std::string& resource_id, |
890 const std::string& md5, | 891 const std::string& md5, |
891 FileOperationType file_operation_type, | 892 FileOperationType file_operation_type, |
892 base::PlatformFileError* error) { | 893 base::PlatformFileError* error) { |
893 AssertOnSequencedWorkerPool(); | 894 AssertOnSequencedWorkerPool(); |
894 DCHECK(error); | 895 DCHECK(error); |
895 | 896 |
896 FilePath source_path; | 897 FilePath source_path; |
897 FilePath dest_path; | 898 FilePath dest_path; |
898 FilePath symlink_path; | 899 FilePath symlink_path; |
899 bool create_symlink = true; | 900 bool create_symlink = true; |
900 GDataCacheEntry new_cache_entry(md5, CACHE_STATE_NONE); | |
901 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_PERSISTENT; | 901 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_PERSISTENT; |
902 | 902 |
903 scoped_ptr<GDataCacheEntry> cache_entry = GetCacheEntry(resource_id, md5); | 903 scoped_ptr<GDataCacheEntry> cache_entry = GetCacheEntry(resource_id, md5); |
904 | 904 |
905 if (!cache_entry.get()) { // Entry does not exist in cache. | 905 if (!cache_entry.get()) { // Entry does not exist in cache. |
906 // Set both |dest_path| and |source_path| to /dev/null, so that: | 906 // Set both |dest_path| and |source_path| to /dev/null, so that: |
907 // 1) ModifyCacheState won't move files when |source_path| and |dest_path| | 907 // 1) ModifyCacheState won't move files when |source_path| and |dest_path| |
908 // are the same. | 908 // are the same. |
909 // 2) symlinks to /dev/null will be picked up by GDataSyncClient to download | 909 // 2) symlinks to /dev/null will be picked up by GDataSyncClient to download |
910 // pinned files that don't exist in cache. | 910 // pinned files that don't exist in cache. |
911 dest_path = FilePath::FromUTF8Unsafe(util::kSymLinkToDevNull); | 911 dest_path = FilePath::FromUTF8Unsafe(util::kSymLinkToDevNull); |
912 source_path = dest_path; | 912 source_path = dest_path; |
913 | 913 |
914 // Set sub_dir_type to TMP. The file will be first downloaded in 'tmp', | 914 // Set sub_dir_type to TMP. The file will be first downloaded in 'tmp', |
915 // then moved to 'persistent'. | 915 // then moved to 'persistent'. |
916 sub_dir_type = CACHE_TYPE_TMP; | 916 sub_dir_type = CACHE_TYPE_TMP; |
| 917 |
| 918 // We'll add a new cache entry. |
| 919 cache_entry.reset(new GDataCacheEntry); |
917 } else { // File exists in cache, determines destination path. | 920 } else { // File exists in cache, determines destination path. |
918 new_cache_entry.set_cache_state(cache_entry->cache_state()); | |
919 | |
920 // Determine source and destination paths. | 921 // Determine source and destination paths. |
921 | 922 |
922 // If file is dirty or mounted, don't move it, so determine |dest_path| and | 923 // If file is dirty or mounted, don't move it, so determine |dest_path| and |
923 // set |source_path| the same, because ModifyCacheState only moves files if | 924 // set |source_path| the same, because ModifyCacheState only moves files if |
924 // source and destination are different. | 925 // source and destination are different. |
925 if (cache_entry->IsDirty() || cache_entry->IsMounted()) { | 926 if (cache_entry->IsDirty() || cache_entry->IsMounted()) { |
926 DCHECK(cache_entry->IsPersistent()); | 927 DCHECK(cache_entry->IsPersistent()); |
927 dest_path = GetCacheFilePath(resource_id, | 928 dest_path = GetCacheFilePath(resource_id, |
928 md5, | 929 md5, |
929 GetSubDirectoryType(*cache_entry), | 930 GetSubDirectoryType(*cache_entry), |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
962 } | 963 } |
963 | 964 |
964 *error = ModifyCacheState(source_path, | 965 *error = ModifyCacheState(source_path, |
965 dest_path, | 966 dest_path, |
966 file_operation_type, | 967 file_operation_type, |
967 symlink_path, | 968 symlink_path, |
968 create_symlink); | 969 create_symlink); |
969 | 970 |
970 if (*error == base::PLATFORM_FILE_OK) { | 971 if (*error == base::PLATFORM_FILE_OK) { |
971 // Now that file operations have completed, update cache map. | 972 // Now that file operations have completed, update cache map. |
972 new_cache_entry.SetPinned(true); | 973 cache_entry->set_md5(md5); |
973 new_cache_entry.SetPersistent(sub_dir_type == CACHE_TYPE_PERSISTENT); | 974 cache_entry->SetPinned(true); |
974 metadata_->AddOrUpdateCacheEntry(resource_id, new_cache_entry); | 975 cache_entry->SetPersistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
| 976 metadata_->AddOrUpdateCacheEntry(resource_id, *cache_entry); |
975 } | 977 } |
976 } | 978 } |
977 | 979 |
978 void GDataCache::Unpin(const std::string& resource_id, | 980 void GDataCache::Unpin(const std::string& resource_id, |
979 const std::string& md5, | 981 const std::string& md5, |
980 FileOperationType file_operation_type, | 982 FileOperationType file_operation_type, |
981 base::PlatformFileError* error) { | 983 base::PlatformFileError* error) { |
982 AssertOnSequencedWorkerPool(); | 984 AssertOnSequencedWorkerPool(); |
983 DCHECK(error); | 985 DCHECK(error); |
984 | 986 |
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1558 const GDataCacheEntry& cache_entry) { | 1560 const GDataCacheEntry& cache_entry) { |
1559 return cache_entry.IsPersistent() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; | 1561 return cache_entry.IsPersistent() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; |
1560 } | 1562 } |
1561 | 1563 |
1562 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { | 1564 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { |
1563 delete global_free_disk_getter_for_testing; // Safe to delete NULL; | 1565 delete global_free_disk_getter_for_testing; // Safe to delete NULL; |
1564 global_free_disk_getter_for_testing = getter; | 1566 global_free_disk_getter_for_testing = getter; |
1565 } | 1567 } |
1566 | 1568 |
1567 } // namespace gdata | 1569 } // namespace gdata |
OLD | NEW |