- /filtering/remove_url: windows: remove filter file only after DNS server has been stopped

Otherwise, os.Remove() will return with an error "file is being used".
This commit is contained in:
Simon Zolin 2019-07-15 14:31:17 +03:00
parent c81c79aad7
commit 375e410aa3
2 changed files with 9 additions and 2 deletions

View File

@ -722,6 +722,11 @@ func handleFilteringRemoveURL(w http.ResponseWriter, r *http.Request) {
return return
} }
// Stop DNS server:
// we close urlfilter object which in turn closes file descriptors to filter files.
// Otherwise, Windows won't allow us to remove the file which is being currently used.
_ = dnsServer.Stop()
// go through each element and delete if url matches // go through each element and delete if url matches
config.Lock() config.Lock()
newFilters := config.Filters[:0] newFilters := config.Filters[:0]
@ -732,9 +737,11 @@ func handleFilteringRemoveURL(w http.ResponseWriter, r *http.Request) {
// Remove the filter file // Remove the filter file
err := os.Remove(filter.Path()) err := os.Remove(filter.Path())
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
config.Unlock()
httpError(w, http.StatusInternalServerError, "Couldn't remove the filter file: %s", err) httpError(w, http.StatusInternalServerError, "Couldn't remove the filter file: %s", err)
return return
} }
log.Debug("os.Remove(%s)", filter.Path())
} }
} }
// Update the configuration after removing filter files // Update the configuration after removing filter files

View File

@ -257,7 +257,7 @@ func startDNSServer() error {
func reconfigureDNSServer() error { func reconfigureDNSServer() error {
if !isRunning() { if !isRunning() {
return fmt.Errorf("Refusing to reconfigure forwarding DNS server: not running") return nil
} }
config, err := generateServerConfig() config, err := generateServerConfig()
@ -274,7 +274,7 @@ func reconfigureDNSServer() error {
func stopDNSServer() error { func stopDNSServer() error {
if !isRunning() { if !isRunning() {
return fmt.Errorf("Refusing to stop forwarding DNS server: not running") return nil
} }
err := dnsServer.Stop() err := dnsServer.Stop()