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