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 int cache_state = CACHE_STATE_PRESENT; | 815 CacheEntry new_cache_entry(md5, CACHE_STATE_NONE); |
816 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_TMP; | 816 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_TMP; |
817 | 817 |
818 scoped_ptr<CacheEntry> cache_entry = GetCacheEntry(resource_id, md5); | 818 scoped_ptr<CacheEntry> cache_entry = GetCacheEntry(resource_id, md5); |
819 | 819 |
820 // If file was previously pinned, store it in persistent dir and create | 820 // If file was previously pinned, store it in persistent dir and create |
821 // symlink in pinned dir. | 821 // symlink in pinned dir. |
822 if (cache_entry.get()) { // File exists in cache. | 822 if (cache_entry.get()) { // File exists in cache. |
823 // If file is dirty or mounted, return error. | 823 // If file is dirty or mounted, return error. |
824 if (cache_entry->IsDirty() || cache_entry->IsMounted()) { | 824 if (cache_entry->IsDirty() || cache_entry->IsMounted()) { |
825 LOG(WARNING) << "Can't store a file to replace a " | 825 LOG(WARNING) << "Can't store a file to replace a " |
826 << (cache_entry->IsDirty() ? "dirty" : "mounted") | 826 << (cache_entry->IsDirty() ? "dirty" : "mounted") |
827 << " file: res_id=" << resource_id | 827 << " file: res_id=" << resource_id |
828 << ", md5=" << md5; | 828 << ", md5=" << md5; |
829 *error = base::PLATFORM_FILE_ERROR_IN_USE; | 829 *error = base::PLATFORM_FILE_ERROR_IN_USE; |
830 return; | 830 return; |
831 } | 831 } |
832 | 832 |
833 cache_state |= cache_entry->cache_state; | 833 new_cache_entry.cache_state = cache_entry->cache_state; |
834 | 834 |
835 // If file is pinned, determines destination path. | 835 // If file is pinned, determines destination path. |
836 if (cache_entry->IsPinned()) { | 836 if (cache_entry->IsPinned()) { |
837 sub_dir_type = CACHE_TYPE_PERSISTENT; | 837 sub_dir_type = CACHE_TYPE_PERSISTENT; |
838 dest_path = GetCacheFilePath(resource_id, md5, sub_dir_type, | 838 dest_path = GetCacheFilePath(resource_id, md5, sub_dir_type, |
839 CACHED_FILE_FROM_SERVER); | 839 CACHED_FILE_FROM_SERVER); |
840 symlink_path = GetCacheFilePath( | 840 symlink_path = GetCacheFilePath( |
841 resource_id, std::string(), CACHE_TYPE_PINNED, | 841 resource_id, std::string(), CACHE_TYPE_PINNED, |
842 CACHED_FILE_FROM_SERVER); | 842 CACHED_FILE_FROM_SERVER); |
843 } | 843 } |
(...skipping 29 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 UpdateCacheWithSubDirectoryType(resource_id, | 884 UpdateCacheWithSubDirectoryType(resource_id, |
884 sub_dir_type, | 885 sub_dir_type, |
885 CacheEntry(md5, cache_state)); | 886 new_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 int cache_state = CACHE_STATE_PINNED; | 901 CacheEntry new_cache_entry(md5, CACHE_STATE_NONE); |
901 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_PERSISTENT; | 902 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_PERSISTENT; |
902 | 903 |
903 scoped_ptr<CacheEntry> cache_entry = GetCacheEntry(resource_id, md5); | 904 scoped_ptr<CacheEntry> cache_entry = GetCacheEntry(resource_id, md5); |
904 | 905 |
905 if (!cache_entry.get()) { // Entry does not exist in cache. | 906 if (!cache_entry.get()) { // Entry does not exist in cache. |
906 // Set both |dest_path| and |source_path| to /dev/null, so that: | 907 // 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| | 908 // 1) ModifyCacheState won't move files when |source_path| and |dest_path| |
908 // are the same. | 909 // are the same. |
909 // 2) symlinks to /dev/null will be picked up by GDataSyncClient to download | 910 // 2) symlinks to /dev/null will be picked up by GDataSyncClient to download |
910 // pinned files that don't exist in cache. | 911 // pinned files that don't exist in cache. |
911 dest_path = FilePath::FromUTF8Unsafe(util::kSymLinkToDevNull); | 912 dest_path = FilePath::FromUTF8Unsafe(util::kSymLinkToDevNull); |
912 source_path = dest_path; | 913 source_path = dest_path; |
913 | 914 |
914 // Set sub_dir_type to TMP. The file will be first downloaded in 'tmp', | 915 // Set sub_dir_type to TMP. The file will be first downloaded in 'tmp', |
915 // then moved to 'persistent'. | 916 // then moved to 'persistent'. |
916 sub_dir_type = CACHE_TYPE_TMP; | 917 sub_dir_type = CACHE_TYPE_TMP; |
917 } else { // File exists in cache, determines destination path. | 918 } else { // File exists in cache, determines destination path. |
918 cache_state |= cache_entry->cache_state; | 919 new_cache_entry.cache_state = cache_entry->cache_state; |
919 | 920 |
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, |
(...skipping 33 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. |
973 new_cache_entry.SetPinned(true); | |
972 UpdateCacheWithSubDirectoryType(resource_id, | 974 UpdateCacheWithSubDirectoryType(resource_id, |
973 sub_dir_type, | 975 sub_dir_type, |
974 CacheEntry(md5, cache_state)); | 976 new_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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1042 | 1044 |
1043 *error = ModifyCacheState( | 1045 *error = ModifyCacheState( |
1044 source_path, | 1046 source_path, |
1045 dest_path, | 1047 dest_path, |
1046 file_operation_type, | 1048 file_operation_type, |
1047 symlink_path, // This will be deleted if it exists. | 1049 symlink_path, // This will be deleted if it exists. |
1048 false /* don't create symlink*/); | 1050 false /* don't create symlink*/); |
1049 | 1051 |
1050 if (*error == base::PLATFORM_FILE_OK) { | 1052 if (*error == base::PLATFORM_FILE_OK) { |
1051 // Now that file operations have completed, update cache map. | 1053 // Now that file operations have completed, update cache map. |
1052 int cache_state = ClearCachePinned(cache_entry->cache_state); | 1054 CacheEntry new_cache_entry(md5, cache_entry->cache_state); |
1055 new_cache_entry.SetPinned(false); | |
1053 UpdateCacheWithSubDirectoryType(resource_id, | 1056 UpdateCacheWithSubDirectoryType(resource_id, |
1054 sub_dir_type, | 1057 sub_dir_type, |
1055 CacheEntry(md5, cache_state)); | 1058 new_cache_entry); |
1056 } | 1059 } |
1057 } | 1060 } |
1058 | 1061 |
1059 void GDataCache::SetMountedState(const FilePath& file_path, | 1062 void GDataCache::SetMountedState(const FilePath& file_path, |
1060 bool to_mount, | 1063 bool to_mount, |
1061 base::PlatformFileError *error, | 1064 base::PlatformFileError *error, |
1062 FilePath* cache_file_path) { | 1065 FilePath* cache_file_path) { |
1063 AssertOnSequencedWorkerPool(); | 1066 AssertOnSequencedWorkerPool(); |
1064 DCHECK(error); | 1067 DCHECK(error); |
1065 DCHECK(cache_file_path); | 1068 DCHECK(cache_file_path); |
(...skipping 25 matching lines...) Expand all Loading... | |
1091 resource_id, md5, unmounted_subdir, CACHED_FILE_FROM_SERVER); | 1094 resource_id, md5, unmounted_subdir, CACHED_FILE_FROM_SERVER); |
1092 | 1095 |
1093 // Get the subdir type and path for the mounted state. | 1096 // Get the subdir type and path for the mounted state. |
1094 CacheSubDirectoryType mounted_subdir = CACHE_TYPE_PERSISTENT; | 1097 CacheSubDirectoryType mounted_subdir = CACHE_TYPE_PERSISTENT; |
1095 FilePath mounted_path = GetCacheFilePath( | 1098 FilePath mounted_path = GetCacheFilePath( |
1096 resource_id, md5, mounted_subdir, CACHED_FILE_MOUNTED); | 1099 resource_id, md5, mounted_subdir, CACHED_FILE_MOUNTED); |
1097 | 1100 |
1098 // Determine the source and destination paths for moving the cache blob. | 1101 // Determine the source and destination paths for moving the cache blob. |
1099 FilePath source_path; | 1102 FilePath source_path; |
1100 CacheSubDirectoryType dest_subdir; | 1103 CacheSubDirectoryType dest_subdir; |
1101 int cache_state = cache_entry->cache_state; | 1104 CacheEntry new_cache_entry(md5, cache_entry->cache_state); |
1102 if (to_mount) { | 1105 if (to_mount) { |
1103 source_path = unmounted_path; | 1106 source_path = unmounted_path; |
1104 *cache_file_path = mounted_path; | 1107 *cache_file_path = mounted_path; |
1105 dest_subdir = mounted_subdir; | 1108 dest_subdir = mounted_subdir; |
1106 cache_state = SetCacheMounted(cache_state); | 1109 new_cache_entry.SetMounted(true); |
1107 } else { | 1110 } else { |
1108 source_path = mounted_path; | 1111 source_path = mounted_path; |
1109 *cache_file_path = unmounted_path; | 1112 *cache_file_path = unmounted_path; |
1110 dest_subdir = unmounted_subdir; | 1113 dest_subdir = unmounted_subdir; |
1111 cache_state = ClearCacheMounted(cache_state); | 1114 new_cache_entry.SetMounted(false); |
1112 } | 1115 } |
1113 | 1116 |
1114 // Move cache blob from source path to destination path. | 1117 // Move cache blob from source path to destination path. |
1115 *error = ModifyCacheState(source_path, *cache_file_path, | 1118 *error = ModifyCacheState(source_path, *cache_file_path, |
1116 FILE_OPERATION_MOVE, FilePath(), false); | 1119 FILE_OPERATION_MOVE, FilePath(), false); |
1117 if (*error == base::PLATFORM_FILE_OK) { | 1120 if (*error == base::PLATFORM_FILE_OK) { |
1118 // Now that cache operation is complete, update cache map | 1121 // Now that cache operation is complete, update cache map |
1119 UpdateCacheWithSubDirectoryType(resource_id, | 1122 UpdateCacheWithSubDirectoryType(resource_id, |
1120 dest_subdir, | 1123 dest_subdir, |
1121 CacheEntry(md5, cache_state)); | 1124 new_cache_entry); |
1122 } | 1125 } |
1123 } | 1126 } |
1124 | 1127 |
1125 void GDataCache::MarkDirty(const std::string& resource_id, | 1128 void GDataCache::MarkDirty(const std::string& resource_id, |
1126 const std::string& md5, | 1129 const std::string& md5, |
1127 FileOperationType file_operation_type, | 1130 FileOperationType file_operation_type, |
1128 base::PlatformFileError* error, | 1131 base::PlatformFileError* error, |
1129 FilePath* cache_file_path) { | 1132 FilePath* cache_file_path) { |
1130 AssertOnSequencedWorkerPool(); | 1133 AssertOnSequencedWorkerPool(); |
1131 DCHECK(error); | 1134 DCHECK(error); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1213 | 1216 |
1214 *error = ModifyCacheState( | 1217 *error = ModifyCacheState( |
1215 source_path, | 1218 source_path, |
1216 *cache_file_path, | 1219 *cache_file_path, |
1217 file_operation_type, | 1220 file_operation_type, |
1218 symlink_path, | 1221 symlink_path, |
1219 !symlink_path.empty() /* create symlink */); | 1222 !symlink_path.empty() /* create symlink */); |
1220 | 1223 |
1221 if (*error == base::PLATFORM_FILE_OK) { | 1224 if (*error == base::PLATFORM_FILE_OK) { |
1222 // Now that file operations have completed, update cache map. | 1225 // Now that file operations have completed, update cache map. |
1223 int cache_state = SetCacheDirty(cache_entry->cache_state); | 1226 CacheEntry new_cache_entry(md5, cache_entry->cache_state); |
1227 new_cache_entry.SetDirty(true); | |
1224 UpdateCacheWithSubDirectoryType(resource_id, | 1228 UpdateCacheWithSubDirectoryType(resource_id, |
1225 sub_dir_type, | 1229 sub_dir_type, |
1226 CacheEntry(md5, cache_state)); | 1230 new_cache_entry); |
1227 } | 1231 } |
1228 } | 1232 } |
1229 | 1233 |
1230 void GDataCache::CommitDirty(const std::string& resource_id, | 1234 void GDataCache::CommitDirty(const std::string& resource_id, |
1231 const std::string& md5, | 1235 const std::string& md5, |
1232 FileOperationType file_operation_type, | 1236 FileOperationType file_operation_type, |
1233 base::PlatformFileError* error) { | 1237 base::PlatformFileError* error) { |
1234 AssertOnSequencedWorkerPool(); | 1238 AssertOnSequencedWorkerPool(); |
1235 DCHECK(error); | 1239 DCHECK(error); |
1236 | 1240 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1361 // if source and destination are different. | 1365 // if source and destination are different. |
1362 *error = ModifyCacheState(dest_path, // source path | 1366 *error = ModifyCacheState(dest_path, // source path |
1363 dest_path, // destination path | 1367 dest_path, // destination path |
1364 file_operation_type, | 1368 file_operation_type, |
1365 symlink_path, | 1369 symlink_path, |
1366 true /* create symlink */); | 1370 true /* create symlink */); |
1367 } | 1371 } |
1368 | 1372 |
1369 if (*error == base::PLATFORM_FILE_OK) { | 1373 if (*error == base::PLATFORM_FILE_OK) { |
1370 // Now that file operations have completed, update cache map. | 1374 // Now that file operations have completed, update cache map. |
1371 int cache_state = ClearCacheDirty(cache_entry->cache_state); | 1375 CacheEntry new_cache_entry(md5, cache_entry->cache_state); |
1372 UpdateCacheWithSubDirectoryType(resource_id, | 1376 new_cache_entry.SetDirty(false); |
1377 UpdateCacheWithSubDirectoryType(resource_id, | |
1373 sub_dir_type, | 1378 sub_dir_type, |
1374 CacheEntry(md5, cache_state)); | 1379 new_cache_entry); |
1375 } | 1380 } |
1376 } | 1381 } |
1377 | 1382 |
1378 void GDataCache::Remove(const std::string& resource_id, | 1383 void GDataCache::Remove(const std::string& resource_id, |
1379 base::PlatformFileError* error) { | 1384 base::PlatformFileError* error) { |
1380 AssertOnSequencedWorkerPool(); | 1385 AssertOnSequencedWorkerPool(); |
1381 DCHECK(error); | 1386 DCHECK(error); |
1382 | 1387 |
1383 // MD5 is not passed into RemoveFromCache and hence | 1388 // MD5 is not passed into RemoveFromCache and hence |
1384 // RemoveFromCacheOnBlockingPool, because we would delete all cache files | 1389 // RemoveFromCacheOnBlockingPool, because we would delete all cache files |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1508 | 1513 |
1509 void GDataCache::UpdateCacheWithSubDirectoryType( | 1514 void GDataCache::UpdateCacheWithSubDirectoryType( |
1510 const std::string& resource_id, | 1515 const std::string& resource_id, |
1511 CacheSubDirectoryType sub_dir_type, | 1516 CacheSubDirectoryType sub_dir_type, |
1512 const CacheEntry& in_cache_entry) { | 1517 const CacheEntry& in_cache_entry) { |
1513 DCHECK(sub_dir_type == CACHE_TYPE_PERSISTENT || | 1518 DCHECK(sub_dir_type == CACHE_TYPE_PERSISTENT || |
1514 sub_dir_type == CACHE_TYPE_TMP); | 1519 sub_dir_type == CACHE_TYPE_TMP); |
1515 | 1520 |
1516 CacheEntry cache_entry = in_cache_entry; | 1521 CacheEntry cache_entry = in_cache_entry; |
1517 if (sub_dir_type == CACHE_TYPE_PERSISTENT) | 1522 if (sub_dir_type == CACHE_TYPE_PERSISTENT) |
1518 cache_entry.cache_state = SetCachePersistent(cache_entry.cache_state); | 1523 cache_entry.SetPersistent(true); |
hashimoto
2012/07/11 11:08:04
nit: SetPersistent(sub_dir_type == CACHE_TYPE_PERS
satorux1
2012/07/11 19:54:42
Nice. Done.
| |
1519 else | 1524 else |
1520 cache_entry.cache_state = ClearCachePersistent(cache_entry.cache_state); | 1525 cache_entry.SetPersistent(false); |
1521 | 1526 |
1522 metadata_->UpdateCache(resource_id, cache_entry); | 1527 metadata_->UpdateCache(resource_id, cache_entry); |
1523 } | 1528 } |
1524 | 1529 |
1525 // static | 1530 // static |
1526 FilePath GDataCache::GetCacheRootPath(Profile* profile) { | 1531 FilePath GDataCache::GetCacheRootPath(Profile* profile) { |
1527 FilePath cache_base_path; | 1532 FilePath cache_base_path; |
1528 chrome::GetUserCacheDirectory(profile->GetPath(), &cache_base_path); | 1533 chrome::GetUserCacheDirectory(profile->GetPath(), &cache_base_path); |
1529 FilePath cache_root_path = | 1534 FilePath cache_root_path = |
1530 cache_base_path.Append(chrome::kGDataCacheDirname); | 1535 cache_base_path.Append(chrome::kGDataCacheDirname); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1565 } | 1570 } |
1566 return success; | 1571 return success; |
1567 } | 1572 } |
1568 | 1573 |
1569 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { | 1574 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { |
1570 delete global_free_disk_getter_for_testing; // Safe to delete NULL; | 1575 delete global_free_disk_getter_for_testing; // Safe to delete NULL; |
1571 global_free_disk_getter_for_testing = getter; | 1576 global_free_disk_getter_for_testing = getter; |
1572 } | 1577 } |
1573 | 1578 |
1574 } // namespace gdata | 1579 } // namespace gdata |
OLD | NEW |