Merge: * dnsfilter: windows: store rules in memory

Close #1088

* commit '6ba1d857ac7961ed5a97a85a328398296c520273':
  * dnsfilter: windows: store rules in memory
  * minor
This commit is contained in:
Simon Zolin 2019-10-22 16:25:02 +03:00
commit 15e6311c63
2 changed files with 27 additions and 6 deletions

View File

@ -13,6 +13,7 @@ import (
"net"
"net/http"
"os"
"runtime"
"strings"
"sync"
"sync/atomic"
@ -772,18 +773,31 @@ func (d *Dnsfilter) initFiltering(filters map[int]string) error {
list = &urlfilter.StringRuleList{
ID: 0,
RulesText: dataOrFilePath,
IgnoreCosmetic: false,
IgnoreCosmetic: true,
}
} else if !fileExists(dataOrFilePath) {
list = &urlfilter.StringRuleList{
ID: id,
IgnoreCosmetic: false,
IgnoreCosmetic: true,
}
} else if runtime.GOOS == "windows" {
// On Windows we don't pass a file to urlfilter because
// it's difficult to update this file while it's being used.
data, err := ioutil.ReadFile(dataOrFilePath)
if err != nil {
return fmt.Errorf("ioutil.ReadFile(): %s: %s", dataOrFilePath, err)
}
list = &urlfilter.StringRuleList{
ID: id,
RulesText: string(data),
IgnoreCosmetic: true,
}
} else {
var err error
list, err = urlfilter.NewFileRuleList(id, dataOrFilePath, false)
list, err = urlfilter.NewFileRuleList(id, dataOrFilePath, true)
if err != nil {
return fmt.Errorf("urlfilter.NewFileRuleList(): %s: %s", dataOrFilePath, err)
}

View File

@ -217,8 +217,12 @@ func refreshFilters() (int, error) {
// . For each filter run the download and checksum check operation
// . For each filter:
// . If filter data hasn't changed, just set new update time on file
// . If filter data has changed: rename the old file, store the new data on disk
// . Pass new filters to dnsfilter object
// . If filter data has changed:
// . rename the old file (1.txt -> 1.txt.old)
// . store the new data on disk (1.txt)
// . Pass new filters to dnsfilter object - it analyzes new data while the old filters are still active
// . dnsfilter activates new filters
// . Remove the old filter files (1.txt.old)
func refreshFiltersIfNecessary(force bool) int {
var updateFilters []filter
var updateFlags []bool // 'true' if filter data has changed
@ -431,7 +435,10 @@ func (filter *filter) save() error {
func (filter *filter) saveAndBackupOld() error {
filterFilePath := filter.Path()
_ = os.Rename(filterFilePath, filterFilePath+".old")
err := os.Rename(filterFilePath, filterFilePath+".old")
if err != nil {
return err
}
return filter.save()
}