remove_stale_cache_files.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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
3// found in the LICENSE file.
4
5#include "chrome/browser/chromeos/drive/remove_stale_cache_files.h"
6
7#include <string>
8#include <vector>
9
10#include "base/bind.h"
11#include "base/logging.h"
12#include "chrome/browser/chromeos/drive/drive.pb.h"
13#include "chrome/browser/chromeos/drive/file_cache.h"
14#include "chrome/browser/chromeos/drive/resource_metadata.h"
15
16namespace drive {
17namespace internal {
18
19void RemoveStaleCacheFiles(FileCache* cache,
20                           ResourceMetadata* resource_metadata) {
21  std::vector<std::string> resource_ids_to_be_removed;
22
23  scoped_ptr<FileCache::Iterator> it = cache->GetIterator();
24  for (; !it->IsAtEnd(); it->Advance()) {
25    ResourceEntry entry;
26    FileError error = resource_metadata->GetResourceEntryById(it->GetID(),
27                                                              &entry);
28    // Stale = the entry is not found, or not dirty but the MD5 does not match.
29    if (error != FILE_ERROR_OK ||
30        (!it->GetValue().is_dirty() &&
31         it->GetValue().md5() != entry.file_specific_info().md5())) {
32      FileError error = cache->Remove(it->GetID());
33      LOG_IF(WARNING, error != FILE_ERROR_OK)
34          << "Failed to remove a stale cache file. resource_id: "
35          << it->GetID();
36    }
37  }
38}
39
40}  // namespace internal
41}  // namespace drive
42