Do not store last_updated in the config file anymore

This commit is contained in:
Andrey Meshkov
2019-02-10 21:44:16 +03:00
parent 9a03190a62
commit 9ff420bb52
4 changed files with 40 additions and 10 deletions

View File

@@ -26,7 +26,7 @@ type filter struct {
URL string `json:"url"`
Name string `json:"name" yaml:"name"`
RulesCount int `json:"rulesCount" yaml:"-"`
LastUpdated time.Time `json:"lastUpdated,omitempty" yaml:"last_updated,omitempty"`
LastUpdated time.Time `json:"lastUpdated,omitempty" yaml:"-"`
dnsfilter.Filter `yaml:",inline"`
}
@@ -95,6 +95,15 @@ func refreshFiltersIfNecessary(force bool) int {
filter.ID = assignUniqueFilterID()
}
// Re-load it from the disk before updating
if len(filter.Rules) == 0 {
err := filter.load()
if err != nil {
log.Printf("Failed to reload filter %s: %s", filter.URL, err)
continue
}
}
updated, err := filter.update(force)
if err != nil {
log.Printf("Failed to update filter %s: %s\n", filter.URL, err)
@@ -162,9 +171,6 @@ func (filter *filter) update(force bool) (bool, error) {
log.Printf("Downloading update for filter %d from %s", filter.ID, filter.URL)
// use the same update period for failed filter downloads to avoid flooding with requests
filter.LastUpdated = time.Now()
resp, err := client.Get(filter.URL)
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
@@ -217,7 +223,11 @@ func (filter *filter) save() error {
log.Printf("Saving filter %d contents to: %s", filter.ID, filterFilePath)
body := []byte(strings.Join(filter.Rules, "\n"))
return safeWriteFile(filterFilePath, body)
err := safeWriteFile(filterFilePath, body)
// update LastUpdated field after saving the file
filter.LastUpdated = filter.LastTimeUpdated()
return err
}
// loads filter contents from the file in dataDir
@@ -245,6 +255,7 @@ func (filter *filter) load() error {
filter.RulesCount = rulesCount
filter.Rules = rules
filter.LastUpdated = filter.LastTimeUpdated()
return nil
}
@@ -253,3 +264,21 @@ func (filter *filter) load() error {
func (filter *filter) Path() string {
return filepath.Join(config.ourWorkingDir, dataDir, filterDir, strconv.FormatInt(filter.ID, 10)+".txt")
}
// LastUpdated returns the time when the filter was last time updated
func (filter *filter) LastTimeUpdated() time.Time {
filterFilePath := filter.Path()
if _, err := os.Stat(filterFilePath); os.IsNotExist(err) {
// if the filter file does not exist, return 0001-01-01
return time.Time{}
}
s, err := os.Stat(filterFilePath)
if err != nil {
// if the filter file does not exist, return 0001-01-01
return time.Time{}
}
// filter file modified time
return s.ModTime()
}