| 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/md5.h" | 7 #include "base/md5.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 chunks->erase(prev); | 128 chunks->erase(prev); |
| 129 } | 129 } |
| 130 } | 130 } |
| 131 | 131 |
| 132 // Sanity-check the header against the file's size to make sure our | 132 // Sanity-check the header against the file's size to make sure our |
| 133 // vectors aren't gigantic. This doubles as a cheap way to detect | 133 // vectors aren't gigantic. This doubles as a cheap way to detect |
| 134 // corruption without having to checksum the entire file. | 134 // corruption without having to checksum the entire file. |
| 135 bool FileHeaderSanityCheck(const base::FilePath& filename, | 135 bool FileHeaderSanityCheck(const base::FilePath& filename, |
| 136 const FileHeader& header) { | 136 const FileHeader& header) { |
| 137 int64 size = 0; | 137 int64 size = 0; |
| 138 if (!file_util::GetFileSize(filename, &size)) | 138 if (!base::GetFileSize(filename, &size)) |
| 139 return false; | 139 return false; |
| 140 | 140 |
| 141 int64 expected_size = sizeof(FileHeader); | 141 int64 expected_size = sizeof(FileHeader); |
| 142 expected_size += header.add_chunk_count * sizeof(int32); | 142 expected_size += header.add_chunk_count * sizeof(int32); |
| 143 expected_size += header.sub_chunk_count * sizeof(int32); | 143 expected_size += header.sub_chunk_count * sizeof(int32); |
| 144 expected_size += header.add_prefix_count * sizeof(SBAddPrefix); | 144 expected_size += header.add_prefix_count * sizeof(SBAddPrefix); |
| 145 expected_size += header.sub_prefix_count * sizeof(SBSubPrefix); | 145 expected_size += header.sub_prefix_count * sizeof(SBSubPrefix); |
| 146 expected_size += header.add_hash_count * sizeof(SBAddFullHash); | 146 expected_size += header.add_hash_count * sizeof(SBAddFullHash); |
| 147 expected_size += header.sub_hash_count * sizeof(SBSubFullHash); | 147 expected_size += header.sub_hash_count * sizeof(SBSubFullHash); |
| 148 expected_size += sizeof(base::MD5Digest); | 148 expected_size += sizeof(base::MD5Digest); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 174 UMA_HISTOGRAM_ENUMERATION("SB2.FormatEvent", event_type, FORMAT_EVENT_MAX); | 174 UMA_HISTOGRAM_ENUMERATION("SB2.FormatEvent", event_type, FORMAT_EVENT_MAX); |
| 175 } | 175 } |
| 176 | 176 |
| 177 // static | 177 // static |
| 178 void SafeBrowsingStoreFile::CheckForOriginalAndDelete( | 178 void SafeBrowsingStoreFile::CheckForOriginalAndDelete( |
| 179 const base::FilePath& current_filename) { | 179 const base::FilePath& current_filename) { |
| 180 const base::FilePath original_filename( | 180 const base::FilePath original_filename( |
| 181 current_filename.DirName().AppendASCII("Safe Browsing")); | 181 current_filename.DirName().AppendASCII("Safe Browsing")); |
| 182 if (base::PathExists(original_filename)) { | 182 if (base::PathExists(original_filename)) { |
| 183 int64 size = 0; | 183 int64 size = 0; |
| 184 if (file_util::GetFileSize(original_filename, &size)) { | 184 if (base::GetFileSize(original_filename, &size)) { |
| 185 UMA_HISTOGRAM_COUNTS("SB2.OldDatabaseKilobytes", | 185 UMA_HISTOGRAM_COUNTS("SB2.OldDatabaseKilobytes", |
| 186 static_cast<int>(size / 1024)); | 186 static_cast<int>(size / 1024)); |
| 187 } | 187 } |
| 188 | 188 |
| 189 if (base::DeleteFile(original_filename, false)) { | 189 if (base::DeleteFile(original_filename, false)) { |
| 190 RecordFormatEvent(FORMAT_EVENT_DELETED_ORIGINAL); | 190 RecordFormatEvent(FORMAT_EVENT_DELETED_ORIGINAL); |
| 191 } else { | 191 } else { |
| 192 RecordFormatEvent(FORMAT_EVENT_DELETED_ORIGINAL_FAILED); | 192 RecordFormatEvent(FORMAT_EVENT_DELETED_ORIGINAL_FAILED); |
| 193 } | 193 } |
| 194 | 194 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 223 // presumed not to be invalid. The never-opened case can happen if | 223 // presumed not to be invalid. The never-opened case can happen if |
| 224 // BeginUpdate() fails for any databases, and should already have | 224 // BeginUpdate() fails for any databases, and should already have |
| 225 // caused the corruption callback to fire. | 225 // caused the corruption callback to fire. |
| 226 if (!file_.get()) | 226 if (!file_.get()) |
| 227 return true; | 227 return true; |
| 228 | 228 |
| 229 if (!FileRewind(file_.get())) | 229 if (!FileRewind(file_.get())) |
| 230 return OnCorruptDatabase(); | 230 return OnCorruptDatabase(); |
| 231 | 231 |
| 232 int64 size = 0; | 232 int64 size = 0; |
| 233 if (!file_util::GetFileSize(filename_, &size)) | 233 if (!base::GetFileSize(filename_, &size)) |
| 234 return OnCorruptDatabase(); | 234 return OnCorruptDatabase(); |
| 235 | 235 |
| 236 base::MD5Context context; | 236 base::MD5Context context; |
| 237 base::MD5Init(&context); | 237 base::MD5Init(&context); |
| 238 | 238 |
| 239 // Read everything except the final digest. | 239 // Read everything except the final digest. |
| 240 size_t bytes_left = static_cast<size_t>(size); | 240 size_t bytes_left = static_cast<size_t>(size); |
| 241 CHECK(size == static_cast<int64>(bytes_left)); | 241 CHECK(size == static_cast<int64>(bytes_left)); |
| 242 if (bytes_left < sizeof(base::MD5Digest)) | 242 if (bytes_left < sizeof(base::MD5Digest)) |
| 243 return OnCorruptDatabase(); | 243 return OnCorruptDatabase(); |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 file_.reset(); | 540 file_.reset(); |
| 541 } | 541 } |
| 542 DCHECK(!file_.get()); | 542 DCHECK(!file_.get()); |
| 543 | 543 |
| 544 // Rewind the temporary storage. | 544 // Rewind the temporary storage. |
| 545 if (!FileRewind(new_file_.get())) | 545 if (!FileRewind(new_file_.get())) |
| 546 return false; | 546 return false; |
| 547 | 547 |
| 548 // Get chunk file's size for validating counts. | 548 // Get chunk file's size for validating counts. |
| 549 int64 size = 0; | 549 int64 size = 0; |
| 550 if (!file_util::GetFileSize(TemporaryFileForFilename(filename_), &size)) | 550 if (!base::GetFileSize(TemporaryFileForFilename(filename_), &size)) |
| 551 return OnCorruptDatabase(); | 551 return OnCorruptDatabase(); |
| 552 | 552 |
| 553 // Track update size to answer questions at http://crbug.com/72216 . | 553 // Track update size to answer questions at http://crbug.com/72216 . |
| 554 // Log small updates as 1k so that the 0 (underflow) bucket can be | 554 // Log small updates as 1k so that the 0 (underflow) bucket can be |
| 555 // used for "empty" in SafeBrowsingDatabase. | 555 // used for "empty" in SafeBrowsingDatabase. |
| 556 UMA_HISTOGRAM_COUNTS("SB2.DatabaseUpdateKilobytes", | 556 UMA_HISTOGRAM_COUNTS("SB2.DatabaseUpdateKilobytes", |
| 557 std::max(static_cast<int>(size / 1024), 1)); | 557 std::max(static_cast<int>(size / 1024), 1)); |
| 558 | 558 |
| 559 // Append the accumulated chunks onto the vectors read from |file_|. | 559 // Append the accumulated chunks onto the vectors read from |file_|. |
| 560 for (int i = 0; i < chunks_written_; ++i) { | 560 for (int i = 0; i < chunks_written_; ++i) { |
| (...skipping 181 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 | 742 // 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 | 743 // existing file is a SQLite file. Make sure the journal file is |
| 744 // also removed. | 744 // also removed. |
| 745 const base::FilePath journal_filename( | 745 const base::FilePath journal_filename( |
| 746 basename.value() + FILE_PATH_LITERAL("-journal")); | 746 basename.value() + FILE_PATH_LITERAL("-journal")); |
| 747 if (base::PathExists(journal_filename)) | 747 if (base::PathExists(journal_filename)) |
| 748 base::DeleteFile(journal_filename, false); | 748 base::DeleteFile(journal_filename, false); |
| 749 | 749 |
| 750 return true; | 750 return true; |
| 751 } | 751 } |
| OLD | NEW |