Bug 1353956 - P3. Separate file processing and prefix data processing for SafeBrowsing prefix set. r=gcp
SafeBrowsing prefix files LOAD/SAVE operations are handled in xxxPrefixSet.cpp. It would be more clear if xxxPrefixSet.cpp only processes prefix data, while LookupCacheV2/LookupCacheV4 which use prefix set process file. This patch doesn't change any behavior, testcases need to update because the LookupCache & xxxPrefixSet APIs are changed. Differential Revision: https://phabricator.services.mozilla.com/D21462
This commit is contained in:
@@ -651,11 +651,73 @@ nsresult LookupCacheV2::ClearPrefixes() {
|
||||
}
|
||||
|
||||
nsresult LookupCacheV2::StoreToFile(nsCOMPtr<nsIFile>& aFile) {
|
||||
return mPrefixSet->StoreToFile(aFile);
|
||||
nsCOMPtr<nsIOutputStream> localOutFile;
|
||||
nsresult rv =
|
||||
NS_NewLocalFileOutputStream(getter_AddRefs(localOutFile), aFile,
|
||||
PR_WRONLY | PR_TRUNCATE | PR_CREATE_FILE);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
uint32_t fileSize;
|
||||
|
||||
// Preallocate the file storage
|
||||
{
|
||||
nsCOMPtr<nsIFileOutputStream> fos(do_QueryInterface(localOutFile));
|
||||
Telemetry::AutoTimer<Telemetry::URLCLASSIFIER_PS_FALLOCATE_TIME> timer;
|
||||
|
||||
fileSize = mPrefixSet->CalculatePreallocateSize();
|
||||
|
||||
// Ignore failure, the preallocation is a hint and we write out the entire
|
||||
// file later on
|
||||
Unused << fos->Preallocate(fileSize);
|
||||
}
|
||||
|
||||
// Convert to buffered stream
|
||||
nsCOMPtr<nsIOutputStream> out;
|
||||
rv = NS_NewBufferedOutputStream(getter_AddRefs(out), localOutFile.forget(),
|
||||
std::min(fileSize, MAX_BUFFER_SIZE));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
rv = mPrefixSet->WritePrefixes(out);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
LOG(("[%s] Storing PrefixSet successful", mTableName.get()));
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult LookupCacheV2::LoadFromFile(nsCOMPtr<nsIFile>& aFile) {
|
||||
return mPrefixSet->LoadFromFile(aFile);
|
||||
Telemetry::AutoTimer<Telemetry::URLCLASSIFIER_PS_FILELOAD_TIME> timer;
|
||||
|
||||
nsCOMPtr<nsIInputStream> localInFile;
|
||||
nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(localInFile), aFile,
|
||||
PR_RDONLY | nsIFile::OS_READAHEAD);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
// Calculate how big the file is, make sure our read buffer isn't bigger
|
||||
// than the file itself which is just wasting memory.
|
||||
int64_t fileSize;
|
||||
rv = aFile->GetFileSize(&fileSize);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
if (fileSize < 0 || fileSize > UINT32_MAX) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
uint32_t bufferSize =
|
||||
std::min<uint32_t>(static_cast<uint32_t>(fileSize), MAX_BUFFER_SIZE);
|
||||
|
||||
// Convert to buffered stream
|
||||
nsCOMPtr<nsIInputStream> in;
|
||||
rv = NS_NewBufferedInputStream(getter_AddRefs(in), localInFile.forget(),
|
||||
bufferSize);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
rv = mPrefixSet->LoadPrefixes(in);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
mPrimed = true;
|
||||
LOG(("[%s] Loading PrefixSet successful", mTableName.get()));
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
size_t LookupCacheV2::SizeOfPrefixSet() const {
|
||||
|
||||
Reference in New Issue
Block a user