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 19 matching lines...) Expand all Loading... |
30 const FilePath::CharType kGDataCacheOutgoingDir[] = | 30 const FilePath::CharType kGDataCacheOutgoingDir[] = |
31 FILE_PATH_LITERAL("outgoing"); | 31 FILE_PATH_LITERAL("outgoing"); |
32 const FilePath::CharType kGDataCachePersistentDir[] = | 32 const FilePath::CharType kGDataCachePersistentDir[] = |
33 FILE_PATH_LITERAL("persistent"); | 33 FILE_PATH_LITERAL("persistent"); |
34 const FilePath::CharType kGDataCacheTmpDir[] = FILE_PATH_LITERAL("tmp"); | 34 const FilePath::CharType kGDataCacheTmpDir[] = FILE_PATH_LITERAL("tmp"); |
35 const FilePath::CharType kGDataCacheTmpDownloadsDir[] = | 35 const FilePath::CharType kGDataCacheTmpDownloadsDir[] = |
36 FILE_PATH_LITERAL("tmp/downloads"); | 36 FILE_PATH_LITERAL("tmp/downloads"); |
37 const FilePath::CharType kGDataCacheTmpDocumentsDir[] = | 37 const FilePath::CharType kGDataCacheTmpDocumentsDir[] = |
38 FILE_PATH_LITERAL("tmp/documents"); | 38 FILE_PATH_LITERAL("tmp/documents"); |
39 | 39 |
40 std::string CacheSubDirectoryTypeToString( | |
41 GDataCache::CacheSubDirectoryType subdir) { | |
42 switch (subdir) { | |
43 case GDataCache::CACHE_TYPE_META: | |
44 return "meta"; | |
45 case GDataCache::CACHE_TYPE_PINNED: | |
46 return "pinned"; | |
47 case GDataCache::CACHE_TYPE_OUTGOING: | |
48 return "outgoing"; | |
49 case GDataCache::CACHE_TYPE_PERSISTENT: | |
50 return "persistent"; | |
51 case GDataCache::CACHE_TYPE_TMP: | |
52 return "tmp"; | |
53 case GDataCache::CACHE_TYPE_TMP_DOWNLOADS: | |
54 return "tmp_downloads"; | |
55 case GDataCache::CACHE_TYPE_TMP_DOCUMENTS: | |
56 return "tmp_documents"; | |
57 case GDataCache::NUM_CACHE_TYPES: | |
58 NOTREACHED(); | |
59 } | |
60 NOTREACHED(); | |
61 return "unknown subdir"; | |
62 } | |
63 | |
64 // Returns the home directory path, or an empty string if the home directory | 40 // Returns the home directory path, or an empty string if the home directory |
65 // is not found. | 41 // is not found. |
66 // Copied from webkit/chromeos/cros_mount_point_provider.h. | 42 // Copied from webkit/chromeos/cros_mount_point_provider.h. |
67 // TODO(satorux): Share the code. | 43 // TODO(satorux): Share the code. |
68 std::string GetHomeDirectory() { | 44 std::string GetHomeDirectory() { |
69 if (base::chromeos::IsRunningOnChromeOS()) | 45 if (base::chromeos::IsRunningOnChromeOS()) |
70 return "/home/chronos/user"; | 46 return "/home/chronos/user"; |
71 | 47 |
72 const char* home = getenv("HOME"); | 48 const char* home = getenv("HOME"); |
73 if (home) | 49 if (home) |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 } // namespace | 336 } // namespace |
361 | 337 |
362 std::string GDataCache::CacheEntry::ToString() const { | 338 std::string GDataCache::CacheEntry::ToString() const { |
363 std::vector<std::string> cache_states; | 339 std::vector<std::string> cache_states; |
364 if (GDataCache::IsCachePresent(cache_state)) | 340 if (GDataCache::IsCachePresent(cache_state)) |
365 cache_states.push_back("present"); | 341 cache_states.push_back("present"); |
366 if (GDataCache::IsCachePinned(cache_state)) | 342 if (GDataCache::IsCachePinned(cache_state)) |
367 cache_states.push_back("pinned"); | 343 cache_states.push_back("pinned"); |
368 if (GDataCache::IsCacheDirty(cache_state)) | 344 if (GDataCache::IsCacheDirty(cache_state)) |
369 cache_states.push_back("dirty"); | 345 cache_states.push_back("dirty"); |
| 346 if (GDataCache::IsCachePersistent(cache_state)) |
| 347 cache_states.push_back("persistent"); |
370 | 348 |
371 return base::StringPrintf("md5=%s, subdir=%s, cache_state=%s", | 349 return base::StringPrintf("md5=%s, cache_state=%s", |
372 md5.c_str(), | 350 md5.c_str(), |
373 CacheSubDirectoryTypeToString(sub_dir_type).c_str(), | |
374 JoinString(cache_states, ',').c_str()); | 351 JoinString(cache_states, ',').c_str()); |
375 } | 352 } |
376 | 353 |
377 GDataCache::GDataCache( | 354 GDataCache::GDataCache( |
378 const FilePath& cache_root_path, | 355 const FilePath& cache_root_path, |
379 base::SequencedWorkerPool* pool, | 356 base::SequencedWorkerPool* pool, |
380 const base::SequencedWorkerPool::SequenceToken& sequence_token) | 357 const base::SequencedWorkerPool::SequenceToken& sequence_token) |
381 : cache_root_path_(cache_root_path), | 358 : cache_root_path_(cache_root_path), |
382 cache_paths_(GetCachePaths(cache_root_path_)), | 359 cache_paths_(GetCachePaths(cache_root_path_)), |
383 pool_(pool), | 360 pool_(pool), |
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
810 if (cache_entry->IsMounted()) { | 787 if (cache_entry->IsMounted()) { |
811 file_origin = CACHED_FILE_MOUNTED; | 788 file_origin = CACHED_FILE_MOUNTED; |
812 } else if (cache_entry->IsDirty()) { | 789 } else if (cache_entry->IsDirty()) { |
813 file_origin = CACHED_FILE_LOCALLY_MODIFIED; | 790 file_origin = CACHED_FILE_LOCALLY_MODIFIED; |
814 } else { | 791 } else { |
815 file_origin = CACHED_FILE_FROM_SERVER; | 792 file_origin = CACHED_FILE_FROM_SERVER; |
816 } | 793 } |
817 *cache_file_path = GetCacheFilePath( | 794 *cache_file_path = GetCacheFilePath( |
818 resource_id, | 795 resource_id, |
819 md5, | 796 md5, |
820 cache_entry->sub_dir_type, | 797 cache_entry->GetSubDirectoryType(), |
821 file_origin); | 798 file_origin); |
822 *error = base::PLATFORM_FILE_OK; | 799 *error = base::PLATFORM_FILE_OK; |
823 } else { | 800 } else { |
824 *error = base::PLATFORM_FILE_ERROR_NOT_FOUND; | 801 *error = base::PLATFORM_FILE_ERROR_NOT_FOUND; |
825 } | 802 } |
826 } | 803 } |
827 | 804 |
828 void GDataCache::Store(const std::string& resource_id, | 805 void GDataCache::Store(const std::string& resource_id, |
829 const std::string& md5, | 806 const std::string& md5, |
830 const FilePath& source_path, | 807 const FilePath& source_path, |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
896 // Note that ReplaceExtension automatically prefixes the extension with the | 873 // Note that ReplaceExtension automatically prefixes the extension with the |
897 // extension separator '.'. | 874 // extension separator '.'. |
898 stale_filenames_pattern = dest_path.ReplaceExtension(util::kWildCard); | 875 stale_filenames_pattern = dest_path.ReplaceExtension(util::kWildCard); |
899 } | 876 } |
900 | 877 |
901 // Delete files that match |stale_filenames_pattern| except for |dest_path|. | 878 // Delete files that match |stale_filenames_pattern| except for |dest_path|. |
902 DeleteFilesSelectively(stale_filenames_pattern, dest_path); | 879 DeleteFilesSelectively(stale_filenames_pattern, dest_path); |
903 | 880 |
904 if (*error == base::PLATFORM_FILE_OK) { | 881 if (*error == base::PLATFORM_FILE_OK) { |
905 // Now that file operations have completed, update cache map. | 882 // Now that file operations have completed, update cache map. |
906 metadata_->UpdateCache(resource_id, md5, sub_dir_type, cache_state); | 883 UpdateCacheWithSubDirectoryType(resource_id, |
| 884 md5, |
| 885 sub_dir_type, |
| 886 cache_state); |
907 } | 887 } |
908 } | 888 } |
909 | 889 |
910 void GDataCache::Pin(const std::string& resource_id, | 890 void GDataCache::Pin(const std::string& resource_id, |
911 const std::string& md5, | 891 const std::string& md5, |
912 FileOperationType file_operation_type, | 892 FileOperationType file_operation_type, |
913 base::PlatformFileError* error) { | 893 base::PlatformFileError* error) { |
914 AssertOnSequencedWorkerPool(); | 894 AssertOnSequencedWorkerPool(); |
915 DCHECK(error); | 895 DCHECK(error); |
916 | 896 |
(...skipping 20 matching lines...) Expand all Loading... |
937 sub_dir_type = CACHE_TYPE_TMP; | 917 sub_dir_type = CACHE_TYPE_TMP; |
938 } else { // File exists in cache, determines destination path. | 918 } else { // File exists in cache, determines destination path. |
939 cache_state |= cache_entry->cache_state; | 919 cache_state |= cache_entry->cache_state; |
940 | 920 |
941 // Determine source and destination paths. | 921 // Determine source and destination paths. |
942 | 922 |
943 // 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 |
944 // set |source_path| the same, because ModifyCacheState only moves files if | 924 // set |source_path| the same, because ModifyCacheState only moves files if |
945 // source and destination are different. | 925 // source and destination are different. |
946 if (cache_entry->IsDirty() || cache_entry->IsMounted()) { | 926 if (cache_entry->IsDirty() || cache_entry->IsMounted()) { |
947 DCHECK_EQ(CACHE_TYPE_PERSISTENT, cache_entry->sub_dir_type); | 927 DCHECK(cache_entry->IsPersistent()); |
948 dest_path = GetCacheFilePath(resource_id, | 928 dest_path = GetCacheFilePath(resource_id, |
949 md5, | 929 md5, |
950 cache_entry->sub_dir_type, | 930 cache_entry->GetSubDirectoryType(), |
951 CACHED_FILE_LOCALLY_MODIFIED); | 931 CACHED_FILE_LOCALLY_MODIFIED); |
952 source_path = dest_path; | 932 source_path = dest_path; |
953 } else { | 933 } else { |
954 // Gets the current path of the file in cache. | 934 // Gets the current path of the file in cache. |
955 source_path = GetCacheFilePath(resource_id, | 935 source_path = GetCacheFilePath(resource_id, |
956 md5, | 936 md5, |
957 cache_entry->sub_dir_type, | 937 cache_entry->GetSubDirectoryType(), |
958 CACHED_FILE_FROM_SERVER); | 938 CACHED_FILE_FROM_SERVER); |
959 | 939 |
960 // If file was pinned before but actual file blob doesn't exist in cache: | 940 // If file was pinned before but actual file blob doesn't exist in cache: |
961 // - don't need to move the file, so set |dest_path| to |source_path|, | 941 // - don't need to move the file, so set |dest_path| to |source_path|, |
962 // because ModifyCacheState only moves files if source and destination | 942 // because ModifyCacheState only moves files if source and destination |
963 // are different | 943 // are different |
964 // - don't create symlink since it already exists. | 944 // - don't create symlink since it already exists. |
965 if (!cache_entry->IsPresent()) { | 945 if (!cache_entry->IsPresent()) { |
966 dest_path = source_path; | 946 dest_path = source_path; |
967 create_symlink = false; | 947 create_symlink = false; |
(...skipping 15 matching lines...) Expand all Loading... |
983 } | 963 } |
984 | 964 |
985 *error = ModifyCacheState(source_path, | 965 *error = ModifyCacheState(source_path, |
986 dest_path, | 966 dest_path, |
987 file_operation_type, | 967 file_operation_type, |
988 symlink_path, | 968 symlink_path, |
989 create_symlink); | 969 create_symlink); |
990 | 970 |
991 if (*error == base::PLATFORM_FILE_OK) { | 971 if (*error == base::PLATFORM_FILE_OK) { |
992 // Now that file operations have completed, update cache map. | 972 // Now that file operations have completed, update cache map. |
993 metadata_->UpdateCache(resource_id, md5, sub_dir_type, cache_state); | 973 UpdateCacheWithSubDirectoryType(resource_id, |
| 974 md5, |
| 975 sub_dir_type, |
| 976 cache_state); |
994 } | 977 } |
995 } | 978 } |
996 | 979 |
997 void GDataCache::Unpin(const std::string& resource_id, | 980 void GDataCache::Unpin(const std::string& resource_id, |
998 const std::string& md5, | 981 const std::string& md5, |
999 FileOperationType file_operation_type, | 982 FileOperationType file_operation_type, |
1000 base::PlatformFileError* error) { | 983 base::PlatformFileError* error) { |
1001 AssertOnSequencedWorkerPool(); | 984 AssertOnSequencedWorkerPool(); |
1002 DCHECK(error); | 985 DCHECK(error); |
1003 | 986 |
(...skipping 12 matching lines...) Expand all Loading... |
1016 | 999 |
1017 FilePath source_path; | 1000 FilePath source_path; |
1018 FilePath dest_path; | 1001 FilePath dest_path; |
1019 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_TMP; | 1002 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_TMP; |
1020 | 1003 |
1021 // If file is dirty or mounted, don't move it, so determine |dest_path| and | 1004 // If file is dirty or mounted, don't move it, so determine |dest_path| and |
1022 // set |source_path| the same, because ModifyCacheState moves files if source | 1005 // set |source_path| the same, because ModifyCacheState moves files if source |
1023 // and destination are different. | 1006 // and destination are different. |
1024 if (cache_entry->IsDirty() || cache_entry->IsMounted()) { | 1007 if (cache_entry->IsDirty() || cache_entry->IsMounted()) { |
1025 sub_dir_type = CACHE_TYPE_PERSISTENT; | 1008 sub_dir_type = CACHE_TYPE_PERSISTENT; |
1026 DCHECK_EQ(sub_dir_type, cache_entry->sub_dir_type); | 1009 DCHECK(cache_entry->IsPersistent()); |
1027 dest_path = GetCacheFilePath(resource_id, | 1010 dest_path = GetCacheFilePath(resource_id, |
1028 md5, | 1011 md5, |
1029 cache_entry->sub_dir_type, | 1012 cache_entry->GetSubDirectoryType(), |
1030 CACHED_FILE_LOCALLY_MODIFIED); | 1013 CACHED_FILE_LOCALLY_MODIFIED); |
1031 source_path = dest_path; | 1014 source_path = dest_path; |
1032 } else { | 1015 } else { |
1033 // Gets the current path of the file in cache. | 1016 // Gets the current path of the file in cache. |
1034 source_path = GetCacheFilePath(resource_id, | 1017 source_path = GetCacheFilePath(resource_id, |
1035 md5, | 1018 md5, |
1036 cache_entry->sub_dir_type, | 1019 cache_entry->GetSubDirectoryType(), |
1037 CACHED_FILE_FROM_SERVER); | 1020 CACHED_FILE_FROM_SERVER); |
1038 | 1021 |
1039 // If file was pinned but actual file blob still doesn't exist in cache, | 1022 // If file was pinned but actual file blob still doesn't exist in cache, |
1040 // don't need to move the file, so set |dest_path| to |source_path|, because | 1023 // don't need to move the file, so set |dest_path| to |source_path|, because |
1041 // ModifyCacheState only moves files if source and destination are | 1024 // ModifyCacheState only moves files if source and destination are |
1042 // different. | 1025 // different. |
1043 if (!cache_entry->IsPresent()) { | 1026 if (!cache_entry->IsPresent()) { |
1044 dest_path = source_path; | 1027 dest_path = source_path; |
1045 } else { // File exists, move it to tmp dir. | 1028 } else { // File exists, move it to tmp dir. |
1046 dest_path = GetCacheFilePath(resource_id, md5, | 1029 dest_path = GetCacheFilePath(resource_id, md5, |
(...skipping 15 matching lines...) Expand all Loading... |
1062 *error = ModifyCacheState( | 1045 *error = ModifyCacheState( |
1063 source_path, | 1046 source_path, |
1064 dest_path, | 1047 dest_path, |
1065 file_operation_type, | 1048 file_operation_type, |
1066 symlink_path, // This will be deleted if it exists. | 1049 symlink_path, // This will be deleted if it exists. |
1067 false /* don't create symlink*/); | 1050 false /* don't create symlink*/); |
1068 | 1051 |
1069 if (*error == base::PLATFORM_FILE_OK) { | 1052 if (*error == base::PLATFORM_FILE_OK) { |
1070 // Now that file operations have completed, update cache map. | 1053 // Now that file operations have completed, update cache map. |
1071 int cache_state = ClearCachePinned(cache_entry->cache_state); | 1054 int cache_state = ClearCachePinned(cache_entry->cache_state); |
1072 metadata_->UpdateCache(resource_id, md5, sub_dir_type, cache_state); | 1055 UpdateCacheWithSubDirectoryType(resource_id, |
| 1056 md5, |
| 1057 sub_dir_type, |
| 1058 cache_state); |
1073 } | 1059 } |
1074 } | 1060 } |
1075 | 1061 |
1076 void GDataCache::SetMountedState(const FilePath& file_path, | 1062 void GDataCache::SetMountedState(const FilePath& file_path, |
1077 bool to_mount, | 1063 bool to_mount, |
1078 base::PlatformFileError *error, | 1064 base::PlatformFileError *error, |
1079 FilePath* cache_file_path) { | 1065 FilePath* cache_file_path) { |
1080 AssertOnSequencedWorkerPool(); | 1066 AssertOnSequencedWorkerPool(); |
1081 DCHECK(error); | 1067 DCHECK(error); |
1082 DCHECK(cache_file_path); | 1068 DCHECK(cache_file_path); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1126 *cache_file_path = unmounted_path; | 1112 *cache_file_path = unmounted_path; |
1127 dest_subdir = unmounted_subdir; | 1113 dest_subdir = unmounted_subdir; |
1128 cache_state = ClearCacheMounted(cache_state); | 1114 cache_state = ClearCacheMounted(cache_state); |
1129 } | 1115 } |
1130 | 1116 |
1131 // Move cache blob from source path to destination path. | 1117 // Move cache blob from source path to destination path. |
1132 *error = ModifyCacheState(source_path, *cache_file_path, | 1118 *error = ModifyCacheState(source_path, *cache_file_path, |
1133 FILE_OPERATION_MOVE, FilePath(), false); | 1119 FILE_OPERATION_MOVE, FilePath(), false); |
1134 if (*error == base::PLATFORM_FILE_OK) { | 1120 if (*error == base::PLATFORM_FILE_OK) { |
1135 // Now that cache operation is complete, update cache map | 1121 // Now that cache operation is complete, update cache map |
1136 metadata_->UpdateCache(resource_id, md5, dest_subdir, cache_state); | 1122 UpdateCacheWithSubDirectoryType(resource_id, |
| 1123 md5, |
| 1124 dest_subdir, |
| 1125 cache_state); |
1137 } | 1126 } |
1138 } | 1127 } |
1139 | 1128 |
1140 void GDataCache::MarkDirty(const std::string& resource_id, | 1129 void GDataCache::MarkDirty(const std::string& resource_id, |
1141 const std::string& md5, | 1130 const std::string& md5, |
1142 FileOperationType file_operation_type, | 1131 FileOperationType file_operation_type, |
1143 base::PlatformFileError* error, | 1132 base::PlatformFileError* error, |
1144 FilePath* cache_file_path) { | 1133 FilePath* cache_file_path) { |
1145 AssertOnSequencedWorkerPool(); | 1134 AssertOnSequencedWorkerPool(); |
1146 DCHECK(error); | 1135 DCHECK(error); |
(...skipping 17 matching lines...) Expand all Loading... |
1164 } | 1153 } |
1165 | 1154 |
1166 // If a file is already dirty (i.e. MarkDirtyInCache was called before), | 1155 // If a file is already dirty (i.e. MarkDirtyInCache was called before), |
1167 // delete outgoing symlink if it exists. | 1156 // delete outgoing symlink if it exists. |
1168 // TODO(benchan): We should only delete outgoing symlink if file is currently | 1157 // TODO(benchan): We should only delete outgoing symlink if file is currently |
1169 // not being uploaded. However, for now, cache doesn't know if uploading of a | 1158 // not being uploaded. However, for now, cache doesn't know if uploading of a |
1170 // file is in progress. Per zel, the upload process should be canceled before | 1159 // file is in progress. Per zel, the upload process should be canceled before |
1171 // MarkDirtyInCache is called again. | 1160 // MarkDirtyInCache is called again. |
1172 if (cache_entry->IsDirty()) { | 1161 if (cache_entry->IsDirty()) { |
1173 // The file must be in persistent dir. | 1162 // The file must be in persistent dir. |
1174 DCHECK_EQ(CACHE_TYPE_PERSISTENT, cache_entry->sub_dir_type); | 1163 DCHECK(cache_entry->IsPersistent()); |
1175 | 1164 |
1176 // Determine symlink path in outgoing dir, so as to remove it. | 1165 // Determine symlink path in outgoing dir, so as to remove it. |
1177 FilePath symlink_path = GetCacheFilePath( | 1166 FilePath symlink_path = GetCacheFilePath( |
1178 resource_id, | 1167 resource_id, |
1179 std::string(), | 1168 std::string(), |
1180 CACHE_TYPE_OUTGOING, | 1169 CACHE_TYPE_OUTGOING, |
1181 CACHED_FILE_FROM_SERVER); | 1170 CACHED_FILE_FROM_SERVER); |
1182 | 1171 |
1183 // We're not moving files here, so simply use empty FilePath for both | 1172 // We're not moving files here, so simply use empty FilePath for both |
1184 // |source_path| and |dest_path| because ModifyCacheState only move files | 1173 // |source_path| and |dest_path| because ModifyCacheState only move files |
(...skipping 15 matching lines...) Expand all Loading... |
1200 } | 1189 } |
1201 return; | 1190 return; |
1202 } | 1191 } |
1203 | 1192 |
1204 // Move file to persistent dir with new .local extension. | 1193 // Move file to persistent dir with new .local extension. |
1205 | 1194 |
1206 // Get the current path of the file in cache. | 1195 // Get the current path of the file in cache. |
1207 FilePath source_path = GetCacheFilePath( | 1196 FilePath source_path = GetCacheFilePath( |
1208 resource_id, | 1197 resource_id, |
1209 md5, | 1198 md5, |
1210 cache_entry->sub_dir_type, | 1199 cache_entry->GetSubDirectoryType(), |
1211 CACHED_FILE_FROM_SERVER); | 1200 CACHED_FILE_FROM_SERVER); |
1212 | 1201 |
1213 // Determine destination path. | 1202 // Determine destination path. |
1214 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_PERSISTENT; | 1203 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_PERSISTENT; |
1215 *cache_file_path = GetCacheFilePath(resource_id, | 1204 *cache_file_path = GetCacheFilePath(resource_id, |
1216 md5, | 1205 md5, |
1217 sub_dir_type, | 1206 sub_dir_type, |
1218 CACHED_FILE_LOCALLY_MODIFIED); | 1207 CACHED_FILE_LOCALLY_MODIFIED); |
1219 | 1208 |
1220 // If file is pinned, update symlink in pinned dir. | 1209 // If file is pinned, update symlink in pinned dir. |
1221 FilePath symlink_path; | 1210 FilePath symlink_path; |
1222 if (cache_entry->IsPinned()) { | 1211 if (cache_entry->IsPinned()) { |
1223 symlink_path = GetCacheFilePath(resource_id, | 1212 symlink_path = GetCacheFilePath(resource_id, |
1224 std::string(), | 1213 std::string(), |
1225 CACHE_TYPE_PINNED, | 1214 CACHE_TYPE_PINNED, |
1226 CACHED_FILE_FROM_SERVER); | 1215 CACHED_FILE_FROM_SERVER); |
1227 } | 1216 } |
1228 | 1217 |
1229 *error = ModifyCacheState( | 1218 *error = ModifyCacheState( |
1230 source_path, | 1219 source_path, |
1231 *cache_file_path, | 1220 *cache_file_path, |
1232 file_operation_type, | 1221 file_operation_type, |
1233 symlink_path, | 1222 symlink_path, |
1234 !symlink_path.empty() /* create symlink */); | 1223 !symlink_path.empty() /* create symlink */); |
1235 | 1224 |
1236 if (*error == base::PLATFORM_FILE_OK) { | 1225 if (*error == base::PLATFORM_FILE_OK) { |
1237 // Now that file operations have completed, update cache map. | 1226 // Now that file operations have completed, update cache map. |
1238 int cache_state = SetCacheDirty(cache_entry->cache_state); | 1227 int cache_state = SetCacheDirty(cache_entry->cache_state); |
1239 metadata_->UpdateCache(resource_id, md5, sub_dir_type, cache_state); | 1228 UpdateCacheWithSubDirectoryType(resource_id, |
| 1229 md5, |
| 1230 sub_dir_type, |
| 1231 cache_state); |
1240 } | 1232 } |
1241 } | 1233 } |
1242 | 1234 |
1243 void GDataCache::CommitDirty(const std::string& resource_id, | 1235 void GDataCache::CommitDirty(const std::string& resource_id, |
1244 const std::string& md5, | 1236 const std::string& md5, |
1245 FileOperationType file_operation_type, | 1237 FileOperationType file_operation_type, |
1246 base::PlatformFileError* error) { | 1238 base::PlatformFileError* error) { |
1247 AssertOnSequencedWorkerPool(); | 1239 AssertOnSequencedWorkerPool(); |
1248 DCHECK(error); | 1240 DCHECK(error); |
1249 | 1241 |
(...skipping 18 matching lines...) Expand all Loading... |
1268 // MarkDirtyInCache), committing it dirty is an invalid operation. | 1260 // MarkDirtyInCache), committing it dirty is an invalid operation. |
1269 if (!cache_entry->IsDirty()) { | 1261 if (!cache_entry->IsDirty()) { |
1270 LOG(WARNING) << "Can't commit a non-dirty file: res_id=" | 1262 LOG(WARNING) << "Can't commit a non-dirty file: res_id=" |
1271 << resource_id | 1263 << resource_id |
1272 << ", md5=" << md5; | 1264 << ", md5=" << md5; |
1273 *error = base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 1265 *error = base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
1274 return; | 1266 return; |
1275 } | 1267 } |
1276 | 1268 |
1277 // Dirty files must be in persistent dir. | 1269 // Dirty files must be in persistent dir. |
1278 DCHECK_EQ(CACHE_TYPE_PERSISTENT, cache_entry->sub_dir_type); | 1270 DCHECK(cache_entry->IsPersistent()); |
1279 | 1271 |
1280 // Create symlink in outgoing dir. | 1272 // Create symlink in outgoing dir. |
1281 FilePath symlink_path = GetCacheFilePath(resource_id, | 1273 FilePath symlink_path = GetCacheFilePath(resource_id, |
1282 std::string(), | 1274 std::string(), |
1283 CACHE_TYPE_OUTGOING, | 1275 CACHE_TYPE_OUTGOING, |
1284 CACHED_FILE_FROM_SERVER); | 1276 CACHED_FILE_FROM_SERVER); |
1285 | 1277 |
1286 // Get target path of symlink i.e. current path of the file in cache. | 1278 // Get target path of symlink i.e. current path of the file in cache. |
1287 FilePath target_path = GetCacheFilePath(resource_id, | 1279 FilePath target_path = GetCacheFilePath(resource_id, |
1288 md5, | 1280 md5, |
1289 cache_entry->sub_dir_type, | 1281 cache_entry->GetSubDirectoryType(), |
1290 CACHED_FILE_LOCALLY_MODIFIED); | 1282 CACHED_FILE_LOCALLY_MODIFIED); |
1291 | 1283 |
1292 // Since there's no need to move files, use |target_path| for both | 1284 // Since there's no need to move files, use |target_path| for both |
1293 // |source_path| and |dest_path|, because ModifyCacheState only moves files | 1285 // |source_path| and |dest_path|, because ModifyCacheState only moves files |
1294 // if source and destination are different. | 1286 // if source and destination are different. |
1295 *error = ModifyCacheState(target_path, // source | 1287 *error = ModifyCacheState(target_path, // source |
1296 target_path, // destination | 1288 target_path, // destination |
1297 file_operation_type, | 1289 file_operation_type, |
1298 symlink_path, | 1290 symlink_path, |
1299 true /* create symlink */); | 1291 true /* create symlink */); |
(...skipping 25 matching lines...) Expand all Loading... |
1325 // MarkDirtyInCache), clearing its dirty state is an invalid operation. | 1317 // MarkDirtyInCache), clearing its dirty state is an invalid operation. |
1326 if (!cache_entry->IsDirty()) { | 1318 if (!cache_entry->IsDirty()) { |
1327 LOG(WARNING) << "Can't clear dirty state of a non-dirty file: res_id=" | 1319 LOG(WARNING) << "Can't clear dirty state of a non-dirty file: res_id=" |
1328 << resource_id | 1320 << resource_id |
1329 << ", md5=" << md5; | 1321 << ", md5=" << md5; |
1330 *error = base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 1322 *error = base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
1331 return; | 1323 return; |
1332 } | 1324 } |
1333 | 1325 |
1334 // File must be dirty and hence in persistent dir. | 1326 // File must be dirty and hence in persistent dir. |
1335 DCHECK_EQ(CACHE_TYPE_PERSISTENT, cache_entry->sub_dir_type); | 1327 DCHECK(cache_entry->IsPersistent()); |
1336 | 1328 |
1337 // Get the current path of the file in cache. | 1329 // Get the current path of the file in cache. |
1338 FilePath source_path = GetCacheFilePath(resource_id, | 1330 FilePath source_path = GetCacheFilePath(resource_id, |
1339 md5, | 1331 md5, |
1340 cache_entry->sub_dir_type, | 1332 cache_entry->GetSubDirectoryType(), |
1341 CACHED_FILE_LOCALLY_MODIFIED); | 1333 CACHED_FILE_LOCALLY_MODIFIED); |
1342 | 1334 |
1343 // Determine destination path. | 1335 // Determine destination path. |
1344 // If file is pinned, move it to persistent dir with .md5 extension; | 1336 // If file is pinned, move it to persistent dir with .md5 extension; |
1345 // otherwise, move it to tmp dir with .md5 extension. | 1337 // otherwise, move it to tmp dir with .md5 extension. |
1346 CacheSubDirectoryType sub_dir_type = | 1338 CacheSubDirectoryType sub_dir_type = |
1347 cache_entry->IsPinned() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; | 1339 cache_entry->IsPinned() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; |
1348 FilePath dest_path = GetCacheFilePath(resource_id, | 1340 FilePath dest_path = GetCacheFilePath(resource_id, |
1349 md5, | 1341 md5, |
1350 sub_dir_type, | 1342 sub_dir_type, |
(...skipping 24 matching lines...) Expand all Loading... |
1375 *error = ModifyCacheState(dest_path, // source path | 1367 *error = ModifyCacheState(dest_path, // source path |
1376 dest_path, // destination path | 1368 dest_path, // destination path |
1377 file_operation_type, | 1369 file_operation_type, |
1378 symlink_path, | 1370 symlink_path, |
1379 true /* create symlink */); | 1371 true /* create symlink */); |
1380 } | 1372 } |
1381 | 1373 |
1382 if (*error == base::PLATFORM_FILE_OK) { | 1374 if (*error == base::PLATFORM_FILE_OK) { |
1383 // Now that file operations have completed, update cache map. | 1375 // Now that file operations have completed, update cache map. |
1384 int cache_state = ClearCacheDirty(cache_entry->cache_state); | 1376 int cache_state = ClearCacheDirty(cache_entry->cache_state); |
1385 metadata_->UpdateCache(resource_id, md5, sub_dir_type, cache_state); | 1377 UpdateCacheWithSubDirectoryType(resource_id, |
| 1378 md5, |
| 1379 sub_dir_type, |
| 1380 cache_state); |
1386 } | 1381 } |
1387 } | 1382 } |
1388 | 1383 |
1389 void GDataCache::Remove(const std::string& resource_id, | 1384 void GDataCache::Remove(const std::string& resource_id, |
1390 base::PlatformFileError* error) { | 1385 base::PlatformFileError* error) { |
1391 AssertOnSequencedWorkerPool(); | 1386 AssertOnSequencedWorkerPool(); |
1392 DCHECK(error); | 1387 DCHECK(error); |
1393 | 1388 |
1394 // MD5 is not passed into RemoveFromCache and hence | 1389 // MD5 is not passed into RemoveFromCache and hence |
1395 // RemoveFromCacheOnBlockingPool, because we would delete all cache files | 1390 // RemoveFromCacheOnBlockingPool, because we would delete all cache files |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1510 AssertOnSequencedWorkerPool(); | 1505 AssertOnSequencedWorkerPool(); |
1511 DCHECK(success); | 1506 DCHECK(success); |
1512 DCHECK(cache_entry); | 1507 DCHECK(cache_entry); |
1513 | 1508 |
1514 scoped_ptr<GDataCache::CacheEntry> value(GetCacheEntry(resource_id, md5)); | 1509 scoped_ptr<GDataCache::CacheEntry> value(GetCacheEntry(resource_id, md5)); |
1515 *success = value.get(); | 1510 *success = value.get(); |
1516 if (*success) | 1511 if (*success) |
1517 *cache_entry = *value; | 1512 *cache_entry = *value; |
1518 } | 1513 } |
1519 | 1514 |
| 1515 void GDataCache::UpdateCacheWithSubDirectoryType( |
| 1516 const std::string& resource_id, |
| 1517 const std::string& md5, |
| 1518 CacheSubDirectoryType sub_dir_type, |
| 1519 int cache_state) { |
| 1520 DCHECK(sub_dir_type == CACHE_TYPE_PERSISTENT || |
| 1521 sub_dir_type == CACHE_TYPE_TMP); |
| 1522 |
| 1523 if (sub_dir_type == CACHE_TYPE_PERSISTENT) |
| 1524 cache_state = SetCachePersistent(cache_state); |
| 1525 else |
| 1526 cache_state = ClearCachePersistent(cache_state); |
| 1527 |
| 1528 metadata_->UpdateCache(resource_id, md5, cache_state); |
| 1529 } |
| 1530 |
1520 // static | 1531 // static |
1521 FilePath GDataCache::GetCacheRootPath(Profile* profile) { | 1532 FilePath GDataCache::GetCacheRootPath(Profile* profile) { |
1522 FilePath cache_base_path; | 1533 FilePath cache_base_path; |
1523 chrome::GetUserCacheDirectory(profile->GetPath(), &cache_base_path); | 1534 chrome::GetUserCacheDirectory(profile->GetPath(), &cache_base_path); |
1524 FilePath cache_root_path = | 1535 FilePath cache_root_path = |
1525 cache_base_path.Append(chrome::kGDataCacheDirname); | 1536 cache_base_path.Append(chrome::kGDataCacheDirname); |
1526 return cache_root_path.Append(kGDataCacheVersionDir); | 1537 return cache_root_path.Append(kGDataCacheVersionDir); |
1527 } | 1538 } |
1528 | 1539 |
1529 // static | 1540 // static |
(...skipping 30 matching lines...) Expand all Loading... |
1560 } | 1571 } |
1561 return success; | 1572 return success; |
1562 } | 1573 } |
1563 | 1574 |
1564 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { | 1575 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { |
1565 delete global_free_disk_getter_for_testing; // Safe to delete NULL; | 1576 delete global_free_disk_getter_for_testing; // Safe to delete NULL; |
1566 global_free_disk_getter_for_testing = getter; | 1577 global_free_disk_getter_for_testing = getter; |
1567 } | 1578 } |
1568 | 1579 |
1569 } // namespace gdata | 1580 } // namespace gdata |
OLD | NEW |