* filter: use CRC32 to check whether filter data should be updated

This commit is contained in:
Simon Zolin 2019-03-15 16:49:10 +03:00
parent 783ac967a1
commit b4732c83c5
1 changed files with 11 additions and 9 deletions

View File

@ -2,10 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"hash/crc32"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -28,6 +28,7 @@ type filter struct {
Name string `json:"name" yaml:"name"` Name string `json:"name" yaml:"name"`
RulesCount int `json:"rulesCount" yaml:"-"` RulesCount int `json:"rulesCount" yaml:"-"`
LastUpdated time.Time `json:"lastUpdated,omitempty" yaml:"-"` LastUpdated time.Time `json:"lastUpdated,omitempty" yaml:"-"`
checksum uint32 // checksum of the file data
dnsfilter.Filter `yaml:",inline"` dnsfilter.Filter `yaml:",inline"`
} }
@ -201,22 +202,22 @@ func (filter *filter) update(force bool) (bool, error) {
return false, err return false, err
} }
// Extract filter name and count number of rules
rulesCount, filterName, rules := parseFilterContents(body)
if filterName != "" {
filter.Name = filterName
}
// Check if the filter has been really changed // Check if the filter has been really changed
if reflect.DeepEqual(filter.Rules, rules) { checksum := crc32.ChecksumIEEE(body)
if filter.checksum == checksum {
log.Tracef("Filter #%d at URL %s hasn't changed, not updating it", filter.ID, filter.URL) log.Tracef("Filter #%d at URL %s hasn't changed, not updating it", filter.ID, filter.URL)
return false, nil return false, nil
} }
// Extract filter name and count number of rules
rulesCount, filterName, rules := parseFilterContents(body)
log.Printf("Filter %d has been updated: %d bytes, %d rules", filter.ID, len(body), rulesCount) log.Printf("Filter %d has been updated: %d bytes, %d rules", filter.ID, len(body), rulesCount)
if filterName != "" {
filter.Name = filterName
}
filter.RulesCount = rulesCount filter.RulesCount = rulesCount
filter.Rules = rules filter.Rules = rules
filter.checksum = checksum
return true, nil return true, nil
} }
@ -259,6 +260,7 @@ func (filter *filter) load() error {
filter.RulesCount = rulesCount filter.RulesCount = rulesCount
filter.Rules = rules filter.Rules = rules
filter.checksum = crc32.ChecksumIEEE(filterFileContents)
filter.LastUpdated = filter.LastTimeUpdated() filter.LastUpdated = filter.LastTimeUpdated()
return nil return nil