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/safe_browsing/safe_browsing_store_file.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" |
6 | 6 |
| 7 #include "base/files/scoped_file.h" |
7 #include "base/md5.h" | 8 #include "base/md5.h" |
8 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
9 | 10 |
10 namespace { | 11 namespace { |
11 | 12 |
12 // NOTE(shess): kFileMagic should not be a byte-wise palindrome, so | 13 // NOTE(shess): kFileMagic should not be a byte-wise palindrome, so |
13 // that byte-order changes force corruption. | 14 // that byte-order changes force corruption. |
14 const int32 kFileMagic = 0x600D71FE; | 15 const int32 kFileMagic = 0x600D71FE; |
15 const int32 kFileVersion = 7; // SQLite storage was 6... | 16 const int32 kFileVersion = 7; // SQLite storage was 6... |
16 | 17 |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 } | 286 } |
286 | 287 |
287 bool SafeBrowsingStoreFile::WriteAddPrefix(int32 chunk_id, SBPrefix prefix) { | 288 bool SafeBrowsingStoreFile::WriteAddPrefix(int32 chunk_id, SBPrefix prefix) { |
288 add_prefixes_.push_back(SBAddPrefix(chunk_id, prefix)); | 289 add_prefixes_.push_back(SBAddPrefix(chunk_id, prefix)); |
289 return true; | 290 return true; |
290 } | 291 } |
291 | 292 |
292 bool SafeBrowsingStoreFile::GetAddPrefixes(SBAddPrefixes* add_prefixes) { | 293 bool SafeBrowsingStoreFile::GetAddPrefixes(SBAddPrefixes* add_prefixes) { |
293 add_prefixes->clear(); | 294 add_prefixes->clear(); |
294 | 295 |
295 file_util::ScopedFILE file(base::OpenFile(filename_, "rb")); | 296 base::ScopedFILE file(base::OpenFile(filename_, "rb")); |
296 if (file.get() == NULL) return false; | 297 if (file.get() == NULL) return false; |
297 | 298 |
298 FileHeader header; | 299 FileHeader header; |
299 if (!ReadAndVerifyHeader(filename_, file.get(), &header, NULL)) | 300 if (!ReadAndVerifyHeader(filename_, file.get(), &header, NULL)) |
300 return OnCorruptDatabase(); | 301 return OnCorruptDatabase(); |
301 | 302 |
302 size_t add_prefix_offset = header.add_chunk_count * sizeof(int32) + | 303 size_t add_prefix_offset = header.add_chunk_count * sizeof(int32) + |
303 header.sub_chunk_count * sizeof(int32); | 304 header.sub_chunk_count * sizeof(int32); |
304 if (!FileSkip(add_prefix_offset, file.get())) | 305 if (!FileSkip(add_prefix_offset, file.get())) |
305 return false; | 306 return false; |
306 | 307 |
307 if (!ReadToContainer(add_prefixes, header.add_prefix_count, file.get(), NULL)) | 308 if (!ReadToContainer(add_prefixes, header.add_prefix_count, file.get(), NULL)) |
308 return false; | 309 return false; |
309 | 310 |
310 return true; | 311 return true; |
311 } | 312 } |
312 | 313 |
313 bool SafeBrowsingStoreFile::GetAddFullHashes( | 314 bool SafeBrowsingStoreFile::GetAddFullHashes( |
314 std::vector<SBAddFullHash>* add_full_hashes) { | 315 std::vector<SBAddFullHash>* add_full_hashes) { |
315 add_full_hashes->clear(); | 316 add_full_hashes->clear(); |
316 | 317 |
317 file_util::ScopedFILE file(base::OpenFile(filename_, "rb")); | 318 base::ScopedFILE file(base::OpenFile(filename_, "rb")); |
318 if (file.get() == NULL) return false; | 319 if (file.get() == NULL) return false; |
319 | 320 |
320 FileHeader header; | 321 FileHeader header; |
321 if (!ReadAndVerifyHeader(filename_, file.get(), &header, NULL)) | 322 if (!ReadAndVerifyHeader(filename_, file.get(), &header, NULL)) |
322 return OnCorruptDatabase(); | 323 return OnCorruptDatabase(); |
323 | 324 |
324 size_t offset = | 325 size_t offset = |
325 header.add_chunk_count * sizeof(int32) + | 326 header.add_chunk_count * sizeof(int32) + |
326 header.sub_chunk_count * sizeof(int32) + | 327 header.sub_chunk_count * sizeof(int32) + |
327 header.add_prefix_count * sizeof(SBAddPrefix) + | 328 header.add_prefix_count * sizeof(SBAddPrefix) + |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 DCHECK_EQ(chunks_written_, 0); | 391 DCHECK_EQ(chunks_written_, 0); |
391 | 392 |
392 // Since the following code will already hit the profile looking for | 393 // Since the following code will already hit the profile looking for |
393 // database files, this is a reasonable to time delete any old | 394 // database files, this is a reasonable to time delete any old |
394 // files. | 395 // files. |
395 CheckForOriginalAndDelete(filename_); | 396 CheckForOriginalAndDelete(filename_); |
396 | 397 |
397 corruption_seen_ = false; | 398 corruption_seen_ = false; |
398 | 399 |
399 const base::FilePath new_filename = TemporaryFileForFilename(filename_); | 400 const base::FilePath new_filename = TemporaryFileForFilename(filename_); |
400 file_util::ScopedFILE new_file(base::OpenFile(new_filename, "wb+")); | 401 base::ScopedFILE new_file(base::OpenFile(new_filename, "wb+")); |
401 if (new_file.get() == NULL) | 402 if (new_file.get() == NULL) |
402 return false; | 403 return false; |
403 | 404 |
404 file_util::ScopedFILE file(base::OpenFile(filename_, "rb")); | 405 base::ScopedFILE file(base::OpenFile(filename_, "rb")); |
405 empty_ = (file.get() == NULL); | 406 empty_ = (file.get() == NULL); |
406 if (empty_) { | 407 if (empty_) { |
407 // If the file exists but cannot be opened, try to delete it (not | 408 // If the file exists but cannot be opened, try to delete it (not |
408 // deleting directly, the bloom filter needs to be deleted, too). | 409 // deleting directly, the bloom filter needs to be deleted, too). |
409 if (base::PathExists(filename_)) | 410 if (base::PathExists(filename_)) |
410 return OnCorruptDatabase(); | 411 return OnCorruptDatabase(); |
411 | 412 |
412 new_file_.swap(new_file); | 413 new_file_.swap(new_file); |
413 return true; | 414 return true; |
414 } | 415 } |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
742 // With SQLite support gone, one way to get to this code is if the | 743 // With SQLite support gone, one way to get to this code is if the |
743 // existing file is a SQLite file. Make sure the journal file is | 744 // existing file is a SQLite file. Make sure the journal file is |
744 // also removed. | 745 // also removed. |
745 const base::FilePath journal_filename( | 746 const base::FilePath journal_filename( |
746 basename.value() + FILE_PATH_LITERAL("-journal")); | 747 basename.value() + FILE_PATH_LITERAL("-journal")); |
747 if (base::PathExists(journal_filename)) | 748 if (base::PathExists(journal_filename)) |
748 base::DeleteFile(journal_filename, false); | 749 base::DeleteFile(journal_filename, false); |
749 | 750 |
750 return true; | 751 return true; |
751 } | 752 } |
OLD | NEW |