| 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 271 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 int cache_state = GDataCache::CACHE_STATE_NONE; | 292 GDataCache::CacheEntry cache_entry(md5, GDataCache::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 } |
| 303 | 303 |
| 304 CacheMap::iterator iter = cache_map->find(resource_id); | 304 CacheMap::iterator iter = cache_map->find(resource_id); |
| 305 if (iter != cache_map->end()) { // Entry exists, update pinned state. | 305 if (iter != cache_map->end()) { // Entry exists, update pinned state. |
| 306 iter->second.cache_state = | 306 iter->second.SetPinned(true); |
| 307 GDataCache::SetCachePinned(iter->second.cache_state); | |
| 308 | 307 |
| 309 processed_file_map->insert(std::make_pair(resource_id, current)); | 308 processed_file_map->insert(std::make_pair(resource_id, current)); |
| 310 continue; | 309 continue; |
| 311 } | 310 } |
| 312 // Entry doesn't exist, this is a special symlink that refers to | 311 // Entry doesn't exist, this is a special symlink that refers to |
| 313 // /dev/null; follow through to create an entry with the PINNED but not | 312 // /dev/null; follow through to create an entry with the PINNED but not |
| 314 // PRESENT state. | 313 // PRESENT state. |
| 315 cache_state = GDataCache::SetCachePinned(cache_state); | 314 cache_entry.SetPinned(true); |
| 316 } else if (sub_dir_type == GDataCache::CACHE_TYPE_OUTGOING) { | 315 } else if (sub_dir_type == GDataCache::CACHE_TYPE_OUTGOING) { |
| 317 std::string reason; | 316 std::string reason; |
| 318 if (!IsValidSymbolicLink(current, sub_dir_type, cache_paths, &reason)) { | 317 if (!IsValidSymbolicLink(current, sub_dir_type, cache_paths, &reason)) { |
| 319 LOG(WARNING) << "Removing an invalid symlink: " << current.value() | 318 LOG(WARNING) << "Removing an invalid symlink: " << current.value() |
| 320 << ": " << reason; | 319 << ": " << reason; |
| 321 file_util::Delete(current, false); | 320 file_util::Delete(current, false); |
| 322 continue; | 321 continue; |
| 323 } | 322 } |
| 324 | 323 |
| 325 // If we're scanning outgoing directory, entry must exist and be dirty. | 324 // If we're scanning outgoing directory, entry must exist and be dirty. |
| 326 // Otherwise, it's a logic error from previous execution, remove this | 325 // Otherwise, it's a logic error from previous execution, remove this |
| 327 // outgoing symlink and move on. | 326 // outgoing symlink and move on. |
| 328 CacheMap::iterator iter = cache_map->find(resource_id); | 327 CacheMap::iterator iter = cache_map->find(resource_id); |
| 329 if (iter == cache_map->end() || !iter->second.IsDirty()) { | 328 if (iter == cache_map->end() || !iter->second.IsDirty()) { |
| 330 LOG(WARNING) << "Removing an symlink to a non-dirty file: " | 329 LOG(WARNING) << "Removing an symlink to a non-dirty file: " |
| 331 << current.value(); | 330 << current.value(); |
| 332 file_util::Delete(current, false); | 331 file_util::Delete(current, false); |
| 333 continue; | 332 continue; |
| 334 } | 333 } |
| 335 | 334 |
| 336 processed_file_map->insert(std::make_pair(resource_id, current)); | 335 processed_file_map->insert(std::make_pair(resource_id, current)); |
| 337 continue; | 336 continue; |
| 338 } else if (sub_dir_type == GDataCache::CACHE_TYPE_PERSISTENT || | 337 } else if (sub_dir_type == GDataCache::CACHE_TYPE_PERSISTENT || |
| 339 sub_dir_type == GDataCache::CACHE_TYPE_TMP) { | 338 sub_dir_type == GDataCache::CACHE_TYPE_TMP) { |
| 340 if (sub_dir_type == GDataCache::CACHE_TYPE_PERSISTENT) | 339 if (sub_dir_type == GDataCache::CACHE_TYPE_PERSISTENT) |
| 341 cache_state = GDataCache::SetCachePersistent(cache_state); | 340 cache_entry.SetPersistent(true); |
| 342 | 341 |
| 343 if (file_util::IsLink(current)) { | 342 if (file_util::IsLink(current)) { |
| 344 LOG(WARNING) << "Removing a symlink in persistent/tmp directory" | 343 LOG(WARNING) << "Removing a symlink in persistent/tmp directory" |
| 345 << current.value(); | 344 << current.value(); |
| 346 file_util::Delete(current, false); | 345 file_util::Delete(current, false); |
| 347 continue; | 346 continue; |
| 348 } | 347 } |
| 349 if (extra_extension == util::kMountedArchiveFileExtension) { | 348 if (extra_extension == util::kMountedArchiveFileExtension) { |
| 350 // Mounted archives in cache should be unmounted upon logout/shutdown. | 349 // Mounted archives in cache should be unmounted upon logout/shutdown. |
| 351 // But if we encounter a mounted file at start, delete it and create an | 350 // But if we encounter a mounted file at start, delete it and create an |
| 352 // entry with not PRESENT state. | 351 // entry with not PRESENT state. |
| 353 DCHECK(sub_dir_type == GDataCache::CACHE_TYPE_PERSISTENT); | 352 DCHECK(sub_dir_type == GDataCache::CACHE_TYPE_PERSISTENT); |
| 354 file_util::Delete(current, false); | 353 file_util::Delete(current, false); |
| 355 } else { | 354 } else { |
| 356 // The cache file is present. | 355 // The cache file is present. |
| 357 cache_state = GDataCache::SetCachePresent(cache_state); | 356 cache_entry.SetPresent(true); |
| 358 | 357 |
| 359 // Adds the dirty bit if |md5| indicates that the file is dirty, and | 358 // Adds the dirty bit if |md5| indicates that the file is dirty, and |
| 360 // the file is in the persistent directory. | 359 // the file is in the persistent directory. |
| 361 if (md5 == util::kLocallyModifiedFileExtension) { | 360 if (md5 == util::kLocallyModifiedFileExtension) { |
| 362 if (sub_dir_type == GDataCache::CACHE_TYPE_PERSISTENT) { | 361 if (sub_dir_type == GDataCache::CACHE_TYPE_PERSISTENT) { |
| 363 cache_state = GDataCache::SetCacheDirty(cache_state); | 362 cache_entry.SetDirty(true); |
| 364 } else { | 363 } else { |
| 365 LOG(WARNING) << "Removing a dirty file in tmp directory: " | 364 LOG(WARNING) << "Removing a dirty file in tmp directory: " |
| 366 << current.value(); | 365 << current.value(); |
| 367 file_util::Delete(current, false); | 366 file_util::Delete(current, false); |
| 368 continue; | 367 continue; |
| 369 } | 368 } |
| 370 } | 369 } |
| 371 } | 370 } |
| 372 } else { | 371 } else { |
| 373 NOTREACHED() << "Unexpected sub directory type: " << sub_dir_type; | 372 NOTREACHED() << "Unexpected sub directory type: " << sub_dir_type; |
| 374 } | 373 } |
| 375 | 374 |
| 376 // Create and insert new entry into cache map. | 375 // Create and insert new entry into cache map. |
| 377 cache_map->insert(std::make_pair( | 376 cache_map->insert(std::make_pair(resource_id, cache_entry)); |
| 378 resource_id, GDataCache::CacheEntry(md5, cache_state))); | |
| 379 processed_file_map->insert(std::make_pair(resource_id, current)); | 377 processed_file_map->insert(std::make_pair(resource_id, current)); |
| 380 } | 378 } |
| 381 } | 379 } |
| 382 | 380 |
| 383 // static | 381 // static |
| 384 bool GDataCacheMetadataMap::CheckIfMd5Matches( | 382 bool GDataCacheMetadataMap::CheckIfMd5Matches( |
| 385 const std::string& md5, | 383 const std::string& md5, |
| 386 const GDataCache::CacheEntry& cache_entry) { | 384 const GDataCache::CacheEntry& cache_entry) { |
| 387 if (cache_entry.IsDirty()) { | 385 if (cache_entry.IsDirty()) { |
| 388 // 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" |
| 389 // during cache initialization, so we don't compare MD5. | 387 // during cache initialization, so we don't compare MD5. |
| 390 return true; | 388 return true; |
| 391 } else if (cache_entry.IsPinned() && cache_entry.md5.empty()) { | 389 } else if (cache_entry.IsPinned() && cache_entry.md5.empty()) { |
| 392 // 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 |
| 393 // 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 |
| 394 // files are collected from files in "persistent" directory, but the | 392 // files are collected from files in "persistent" directory, but the |
| 395 // persistent files do not exisit if these are not fetched yet. | 393 // persistent files do not exisit if these are not fetched yet. |
| 396 return true; | 394 return true; |
| 397 } else if (md5.empty()) { | 395 } else if (md5.empty()) { |
| 398 // If the MD5 matching is not requested, don't check MD5. | 396 // If the MD5 matching is not requested, don't check MD5. |
| 399 return true; | 397 return true; |
| 400 } else if (md5 == cache_entry.md5) { | 398 } else if (md5 == cache_entry.md5) { |
| 401 // Otherwise, compare the MD5. | 399 // Otherwise, compare the MD5. |
| 402 return true; | 400 return true; |
| 403 } | 401 } |
| 404 return false; | 402 return false; |
| 405 } | 403 } |
| 406 | 404 |
| 407 } // namespace gdata | 405 } // namespace gdata |
| OLD | NEW |