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