OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/drive/resource_metadata_storage.h" | 5 #include "chrome/browser/chromeos/drive/resource_metadata_storage.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 std::string serialized_header; | 278 std::string serialized_header; |
279 ResourceMetadataHeader header; | 279 ResourceMetadataHeader header; |
280 if (!resource_map->Get(leveldb::ReadOptions(), | 280 if (!resource_map->Get(leveldb::ReadOptions(), |
281 leveldb::Slice(GetHeaderDBKey()), | 281 leveldb::Slice(GetHeaderDBKey()), |
282 &serialized_header).ok() || | 282 &serialized_header).ok() || |
283 !header.ParseFromString(serialized_header)) | 283 !header.ParseFromString(serialized_header)) |
284 return false; | 284 return false; |
285 UMA_HISTOGRAM_SPARSE_SLOWLY("Drive.MetadataDBVersionBeforeUpgradeCheck", | 285 UMA_HISTOGRAM_SPARSE_SLOWLY("Drive.MetadataDBVersionBeforeUpgradeCheck", |
286 header.version()); | 286 header.version()); |
287 | 287 |
288 if (header.version() == kDBVersion) { // Nothing to do. | 288 if (header.version() == kDBVersion) { |
289 return true; | 289 // Before r272134, UpgradeOldDB() was not deleting unused ID entries. |
| 290 // Delete unused ID entries to fix crbug.com/374648. |
| 291 std::set<std::string> used_ids; |
| 292 |
| 293 scoped_ptr<leveldb::Iterator> it( |
| 294 resource_map->NewIterator(leveldb::ReadOptions())); |
| 295 it->Seek(leveldb::Slice(GetHeaderDBKey())); |
| 296 it->Next(); |
| 297 for (; it->Valid(); it->Next()) { |
| 298 if (IsCacheEntryKey(it->key())) { |
| 299 used_ids.insert(GetIdFromCacheEntryKey(it->key())); |
| 300 } else if (!IsChildEntryKey(it->key()) && !IsIdEntryKey(it->key())) { |
| 301 used_ids.insert(it->key().ToString()); |
| 302 } |
| 303 } |
| 304 if (!it->status().ok()) |
| 305 return false; |
| 306 |
| 307 leveldb::WriteBatch batch; |
| 308 for (it->SeekToFirst(); it->Valid(); it->Next()) { |
| 309 if (IsIdEntryKey(it->key()) && !used_ids.count(it->value().ToString())) |
| 310 batch.Delete(it->key()); |
| 311 } |
| 312 if (!it->status().ok()) |
| 313 return false; |
| 314 |
| 315 return resource_map->Write(leveldb::WriteOptions(), &batch).ok(); |
290 } else if (header.version() < 6) { // Too old, nothing can be done. | 316 } else if (header.version() < 6) { // Too old, nothing can be done. |
291 return false; | 317 return false; |
292 } else if (header.version() < 11) { // Cache entries can be reused. | 318 } else if (header.version() < 11) { // Cache entries can be reused. |
293 leveldb::ReadOptions options; | 319 leveldb::ReadOptions options; |
294 options.verify_checksums = true; | 320 options.verify_checksums = true; |
295 scoped_ptr<leveldb::Iterator> it(resource_map->NewIterator(options)); | 321 scoped_ptr<leveldb::Iterator> it(resource_map->NewIterator(options)); |
296 | 322 |
297 leveldb::WriteBatch batch; | 323 leveldb::WriteBatch batch; |
298 for (it->SeekToFirst(); it->Valid(); it->Next()) { | 324 for (it->SeekToFirst(); it->Valid(); it->Next()) { |
299 if (IsCacheEntryKey(it->key())) { | 325 if (IsCacheEntryKey(it->key())) { |
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
926 if (!it->status().ok() || num_child_entries != num_entries_with_parent) { | 952 if (!it->status().ok() || num_child_entries != num_entries_with_parent) { |
927 DLOG(ERROR) << "Error during checking resource map. status = " | 953 DLOG(ERROR) << "Error during checking resource map. status = " |
928 << it->status().ToString(); | 954 << it->status().ToString(); |
929 return false; | 955 return false; |
930 } | 956 } |
931 return true; | 957 return true; |
932 } | 958 } |
933 | 959 |
934 } // namespace internal | 960 } // namespace internal |
935 } // namespace drive | 961 } // namespace drive |
OLD | NEW |