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_metadata.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_cache_metadata.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 8 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
9 | 9 |
10 namespace gdata { | 10 namespace gdata { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 GDataCacheMetadataMap::CacheMap* cache_map) { | 60 GDataCacheMetadataMap::CacheMap* cache_map) { |
61 for (GDataCacheMetadataMap::ResourceIdToFilePathMap::const_iterator iter = | 61 for (GDataCacheMetadataMap::ResourceIdToFilePathMap::const_iterator iter = |
62 persistent_file_map.begin(); | 62 persistent_file_map.begin(); |
63 iter != persistent_file_map.end(); ++iter) { | 63 iter != persistent_file_map.end(); ++iter) { |
64 const std::string& resource_id = iter->first; | 64 const std::string& resource_id = iter->first; |
65 const FilePath& file_path = iter->second; | 65 const FilePath& file_path = iter->second; |
66 | 66 |
67 GDataCacheMetadataMap::CacheMap::iterator cache_map_iter = | 67 GDataCacheMetadataMap::CacheMap::iterator cache_map_iter = |
68 cache_map->find(resource_id); | 68 cache_map->find(resource_id); |
69 if (cache_map_iter != cache_map->end()) { | 69 if (cache_map_iter != cache_map->end()) { |
70 const GDataCache::CacheEntry& cache_entry = cache_map_iter->second; | 70 const GDataCacheEntry& cache_entry = cache_map_iter->second; |
71 // If the file is dirty but not committed, remove it. | 71 // If the file is dirty but not committed, remove it. |
72 if (cache_entry.IsDirty() && outgoing_file_map.count(resource_id) == 0) { | 72 if (cache_entry.IsDirty() && outgoing_file_map.count(resource_id) == 0) { |
73 LOG(WARNING) << "Removing dirty-but-not-committed file: " | 73 LOG(WARNING) << "Removing dirty-but-not-committed file: " |
74 << file_path.value(); | 74 << file_path.value(); |
75 file_util::Delete(file_path, false); | 75 file_util::Delete(file_path, false); |
76 cache_map->erase(cache_map_iter); | 76 cache_map->erase(cache_map_iter); |
77 } else if (!cache_entry.IsDirty() && !cache_entry.IsPinned()) { | 77 } else if (!cache_entry.IsDirty() && !cache_entry.IsPinned()) { |
78 // If the file is neither dirty nor pinned, remove it. | 78 // If the file is neither dirty nor pinned, remove it. |
79 LOG(WARNING) << "Removing persistent-but-dangling file: " | 79 LOG(WARNING) << "Removing persistent-but-dangling file: " |
80 << file_path.value(); | 80 << file_path.value(); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 &outgoing_file_map); | 169 &outgoing_file_map); |
170 | 170 |
171 RemoveInvalidFilesFromPersistentDirectory(persistent_file_map, | 171 RemoveInvalidFilesFromPersistentDirectory(persistent_file_map, |
172 outgoing_file_map, | 172 outgoing_file_map, |
173 &cache_map_); | 173 &cache_map_); |
174 DVLOG(1) << "Directory scan finished"; | 174 DVLOG(1) << "Directory scan finished"; |
175 } | 175 } |
176 | 176 |
177 void GDataCacheMetadataMap::UpdateCache( | 177 void GDataCacheMetadataMap::UpdateCache( |
178 const std::string& resource_id, | 178 const std::string& resource_id, |
179 const GDataCache::CacheEntry& cache_entry) { | 179 const GDataCacheEntry& cache_entry) { |
180 AssertOnSequencedWorkerPool(); | 180 AssertOnSequencedWorkerPool(); |
181 | 181 |
182 CacheMap::iterator iter = cache_map_.find(resource_id); | 182 CacheMap::iterator iter = cache_map_.find(resource_id); |
183 if (iter == cache_map_.end()) { // New resource, create new entry. | 183 if (iter == cache_map_.end()) { // New resource, create new entry. |
184 // Makes no sense to create new entry if cache state is NONE. | 184 // Makes no sense to create new entry if cache state is NONE. |
185 DCHECK(cache_entry.cache_state() != GDataCache::CACHE_STATE_NONE); | 185 DCHECK(cache_entry.cache_state() != CACHE_STATE_NONE); |
186 if (cache_entry.cache_state() != GDataCache::CACHE_STATE_NONE) { | 186 if (cache_entry.cache_state() != CACHE_STATE_NONE) { |
187 cache_map_.insert(std::make_pair(resource_id, cache_entry)); | 187 cache_map_.insert(std::make_pair(resource_id, cache_entry)); |
188 DVLOG(1) << "Added res_id=" << resource_id | 188 DVLOG(1) << "Added res_id=" << resource_id |
189 << ", " << cache_entry.ToString(); | 189 << ", " << cache_entry.ToString(); |
190 } | 190 } |
191 } else { // Resource exists. | 191 } else { // Resource exists. |
192 // If cache state is NONE, delete entry from cache map. | 192 // If cache state is NONE, delete entry from cache map. |
193 if (cache_entry.cache_state() == GDataCache::CACHE_STATE_NONE) { | 193 if (cache_entry.cache_state() == CACHE_STATE_NONE) { |
194 DVLOG(1) << "Deleting res_id=" << resource_id | 194 DVLOG(1) << "Deleting res_id=" << resource_id |
195 << ", " << iter->second.ToString(); | 195 << ", " << iter->second.ToString(); |
196 cache_map_.erase(iter); | 196 cache_map_.erase(iter); |
197 } else { // Otherwise, update entry in cache map. | 197 } else { // Otherwise, update entry in cache map. |
198 iter->second.set_md5(cache_entry.md5()); | 198 iter->second.set_md5(cache_entry.md5()); |
199 iter->second.set_cache_state(cache_entry.cache_state()); | 199 iter->second.set_cache_state(cache_entry.cache_state()); |
200 DVLOG(1) << "Updated res_id=" << resource_id | 200 DVLOG(1) << "Updated res_id=" << resource_id |
201 << ", " << iter->second.ToString(); | 201 << ", " << iter->second.ToString(); |
202 } | 202 } |
203 } | 203 } |
204 } | 204 } |
205 | 205 |
206 void GDataCacheMetadataMap::RemoveFromCache(const std::string& resource_id) { | 206 void GDataCacheMetadataMap::RemoveFromCache(const std::string& resource_id) { |
207 AssertOnSequencedWorkerPool(); | 207 AssertOnSequencedWorkerPool(); |
208 | 208 |
209 CacheMap::iterator iter = cache_map_.find(resource_id); | 209 CacheMap::iterator iter = cache_map_.find(resource_id); |
210 if (iter != cache_map_.end()) { | 210 if (iter != cache_map_.end()) { |
211 // Delete the CacheEntry and remove it from the map. | 211 // Delete the CacheEntry and remove it from the map. |
212 cache_map_.erase(iter); | 212 cache_map_.erase(iter); |
213 } | 213 } |
214 } | 214 } |
215 | 215 |
216 scoped_ptr<GDataCache::CacheEntry> GDataCacheMetadataMap::GetCacheEntry( | 216 scoped_ptr<GDataCacheEntry> GDataCacheMetadataMap::GetCacheEntry( |
217 const std::string& resource_id, | 217 const std::string& resource_id, |
218 const std::string& md5) { | 218 const std::string& md5) { |
219 AssertOnSequencedWorkerPool(); | 219 AssertOnSequencedWorkerPool(); |
220 | 220 |
221 CacheMap::iterator iter = cache_map_.find(resource_id); | 221 CacheMap::iterator iter = cache_map_.find(resource_id); |
222 if (iter == cache_map_.end()) { | 222 if (iter == cache_map_.end()) { |
223 DVLOG(1) << "Can't find " << resource_id << " in cache map"; | 223 DVLOG(1) << "Can't find " << resource_id << " in cache map"; |
224 return scoped_ptr<GDataCache::CacheEntry>(); | 224 return scoped_ptr<GDataCacheEntry>(); |
225 } | 225 } |
226 | 226 |
227 scoped_ptr<GDataCache::CacheEntry> cache_entry( | 227 scoped_ptr<GDataCacheEntry> cache_entry( |
228 new GDataCache::CacheEntry(iter->second)); | 228 new GDataCacheEntry(iter->second)); |
229 | 229 |
230 if (!CheckIfMd5Matches(md5, *cache_entry)) { | 230 if (!CheckIfMd5Matches(md5, *cache_entry)) { |
231 DVLOG(1) << "Non-matching md5: want=" << md5 | 231 DVLOG(1) << "Non-matching md5: want=" << md5 |
232 << ", found=[res_id=" << resource_id | 232 << ", found=[res_id=" << resource_id |
233 << ", " << cache_entry->ToString() | 233 << ", " << cache_entry->ToString() |
234 << "]"; | 234 << "]"; |
235 return scoped_ptr<GDataCache::CacheEntry>(); | 235 return scoped_ptr<GDataCacheEntry>(); |
236 } | 236 } |
237 | 237 |
238 DVLOG(1) << "Found entry for res_id=" << resource_id | 238 DVLOG(1) << "Found entry for res_id=" << resource_id |
239 << ", " << cache_entry->ToString(); | 239 << ", " << cache_entry->ToString(); |
240 | 240 |
241 return cache_entry.Pass(); | 241 return cache_entry.Pass(); |
242 } | 242 } |
243 | 243 |
244 void GDataCacheMetadataMap::RemoveTemporaryFiles() { | 244 void GDataCacheMetadataMap::RemoveTemporaryFiles() { |
245 AssertOnSequencedWorkerPool(); | 245 AssertOnSequencedWorkerPool(); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 util::kWildCard); | 282 util::kWildCard); |
283 for (FilePath current = enumerator.Next(); !current.empty(); | 283 for (FilePath current = enumerator.Next(); !current.empty(); |
284 current = enumerator.Next()) { | 284 current = enumerator.Next()) { |
285 // Extract resource_id and md5 from filename. | 285 // Extract resource_id and md5 from filename. |
286 std::string resource_id; | 286 std::string resource_id; |
287 std::string md5; | 287 std::string md5; |
288 std::string extra_extension; | 288 std::string extra_extension; |
289 util::ParseCacheFilePath(current, &resource_id, &md5, &extra_extension); | 289 util::ParseCacheFilePath(current, &resource_id, &md5, &extra_extension); |
290 | 290 |
291 // Determine cache state. | 291 // Determine cache state. |
292 GDataCache::CacheEntry cache_entry(md5, GDataCache::CACHE_STATE_NONE); | 292 GDataCacheEntry cache_entry(md5, CACHE_STATE_NONE); |
293 // If we're scanning pinned directory and if entry already exists, just | 293 // If we're scanning pinned directory and if entry already exists, just |
294 // update its pinned state. | 294 // update its pinned state. |
295 if (sub_dir_type == GDataCache::CACHE_TYPE_PINNED) { | 295 if (sub_dir_type == GDataCache::CACHE_TYPE_PINNED) { |
296 std::string reason; | 296 std::string reason; |
297 if (!IsValidSymbolicLink(current, sub_dir_type, cache_paths, &reason)) { | 297 if (!IsValidSymbolicLink(current, sub_dir_type, cache_paths, &reason)) { |
298 LOG(WARNING) << "Removing an invalid symlink: " << current.value() | 298 LOG(WARNING) << "Removing an invalid symlink: " << current.value() |
299 << ": " << reason; | 299 << ": " << reason; |
300 file_util::Delete(current, false); | 300 file_util::Delete(current, false); |
301 continue; | 301 continue; |
302 } | 302 } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 | 374 |
375 // Create and insert new entry into cache map. | 375 // Create and insert new entry into cache map. |
376 cache_map->insert(std::make_pair(resource_id, cache_entry)); | 376 cache_map->insert(std::make_pair(resource_id, cache_entry)); |
377 processed_file_map->insert(std::make_pair(resource_id, current)); | 377 processed_file_map->insert(std::make_pair(resource_id, current)); |
378 } | 378 } |
379 } | 379 } |
380 | 380 |
381 // static | 381 // static |
382 bool GDataCacheMetadataMap::CheckIfMd5Matches( | 382 bool GDataCacheMetadataMap::CheckIfMd5Matches( |
383 const std::string& md5, | 383 const std::string& md5, |
384 const GDataCache::CacheEntry& cache_entry) { | 384 const GDataCacheEntry& cache_entry) { |
385 if (cache_entry.IsDirty()) { | 385 if (cache_entry.IsDirty()) { |
386 // If the entry is dirty, its MD5 may have been replaced by "local" | 386 // If the entry is dirty, its MD5 may have been replaced by "local" |
387 // during cache initialization, so we don't compare MD5. | 387 // during cache initialization, so we don't compare MD5. |
388 return true; | 388 return true; |
389 } else if (cache_entry.IsPinned() && cache_entry.md5().empty()) { | 389 } else if (cache_entry.IsPinned() && cache_entry.md5().empty()) { |
390 // If the entry is pinned, it's ok for the entry to have an empty | 390 // If the entry is pinned, it's ok for the entry to have an empty |
391 // MD5. This can happen if the pinned file is not fetched. MD5 for pinned | 391 // MD5. This can happen if the pinned file is not fetched. MD5 for pinned |
392 // files are collected from files in "persistent" directory, but the | 392 // files are collected from files in "persistent" directory, but the |
393 // persistent files do not exisit if these are not fetched yet. | 393 // persistent files do not exisit if these are not fetched yet. |
394 return true; | 394 return true; |
395 } else if (md5.empty()) { | 395 } else if (md5.empty()) { |
396 // If the MD5 matching is not requested, don't check MD5. | 396 // If the MD5 matching is not requested, don't check MD5. |
397 return true; | 397 return true; |
398 } else if (md5 == cache_entry.md5()) { | 398 } else if (md5 == cache_entry.md5()) { |
399 // Otherwise, compare the MD5. | 399 // Otherwise, compare the MD5. |
400 return true; | 400 return true; |
401 } | 401 } |
402 return false; | 402 return false; |
403 } | 403 } |
404 | 404 |
405 } // namespace gdata | 405 } // namespace gdata |
OLD | NEW |