* control: enable/disable filter: move code to a separate function

* don't start updating all filters after 1 filter has been enabled
* unload filter data on disable
This commit is contained in:
Simon Zolin
2019-03-18 14:12:04 +03:00
parent afa54a1339
commit ae2c7d00a9
2 changed files with 36 additions and 24 deletions

View File

@@ -44,6 +44,34 @@ func userFilter() filter {
}
}
// Enable or disable a filter
func filterEnable(url string, enable bool) bool {
r := false
config.Lock()
for i := range config.Filters {
filter := &config.Filters[i] // otherwise we will be operating on a copy
if filter.URL == url {
filter.Enabled = enable
if enable {
e := filter.load()
if e != nil {
// This isn't a fatal error,
// because it may occur when someone removes the file from disk.
// In this case the periodic update task will try to download the file.
filter.LastUpdated = time.Time{}
log.Tracef("%s filter load: %v", url, e)
}
} else {
filter.unload()
}
r = true
break
}
}
config.Unlock()
return r
}
// Load filters from the disk
// And if any filter has zero ID, assign a new one
func loadFilters() {
@@ -284,6 +312,12 @@ func (filter *filter) load() error {
return nil
}
// Clear filter rules
func (filter *filter) unload() {
filter.Rules = []string{}
filter.RulesCount = 0
}
// Path to the filter contents
func (filter *filter) Path() string {
return filepath.Join(config.ourWorkingDir, dataDir, filterDir, strconv.FormatInt(filter.ID, 10)+".txt")