Pull request: all: imp code, err handling

Closes #2571.

Squashed commit of the following:

commit a5b50ee011a995f4ab3d93314acd6f0ca82d99cf
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Mon Mar 15 14:05:25 2021 +0300

    all: imp code

commit bc610f8f438549e8c6b04c8a213b5422dda2aff5
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Mar 12 20:00:14 2021 +0300

    all: imp code, err handling
This commit is contained in:
Ainar Garipov
2021-03-15 14:19:04 +03:00
parent d970b79f2b
commit 313fd7107f
15 changed files with 121 additions and 99 deletions

View File

@@ -2,10 +2,12 @@ package util
import (
"bufio"
"errors"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
@@ -76,7 +78,7 @@ func (a *AutoHosts) Init(hostsFn string) {
var err error
a.watcher, err = fsnotify.NewWatcher()
if err != nil {
log.Error("AutoHosts: %s", err)
log.Error("autohosts: %s", err)
}
}
@@ -127,7 +129,7 @@ func (a *AutoHosts) Process(host string, qtype uint16) []net.IP {
copy(ipsCopy, ips)
}
log.Debug("AutoHosts: answer: %s -> %v", host, ipsCopy)
log.Debug("autohosts: answer: %s -> %v", host, ipsCopy)
return ipsCopy
}
@@ -153,7 +155,7 @@ func (a *AutoHosts) ProcessReverse(addr string, qtype uint16) (hosts []string) {
return nil // not found
}
log.Debug("AutoHosts: reverse-lookup: %s -> %s", addr, hosts)
log.Debug("autohosts: reverse-lookup: %s -> %s", addr, hosts)
return hosts
}
@@ -191,7 +193,7 @@ func (a *AutoHosts) updateTable(table map[string][]net.IP, host string, ipAddr n
ok = true
}
if ok {
log.Debug("AutoHosts: added %s -> %s", ipAddr, host)
log.Debug("autohosts: added %s -> %s", ipAddr, host)
}
}
@@ -201,7 +203,7 @@ func (a *AutoHosts) updateTableRev(tableRev map[string][]string, newHost string,
hosts, ok := tableRev[ipStr]
if !ok {
tableRev[ipStr] = []string{newHost}
log.Debug("AutoHosts: added reverse-address %s -> %s", ipStr, newHost)
log.Debug("autohosts: added reverse-address %s -> %s", ipStr, newHost)
return
}
@@ -213,7 +215,7 @@ func (a *AutoHosts) updateTableRev(tableRev map[string][]string, newHost string,
}
tableRev[ipStr] = append(tableRev[ipStr], newHost)
log.Debug("AutoHosts: added reverse-address %s -> %s", ipStr, newHost)
log.Debug("autohosts: added reverse-address %s -> %s", ipStr, newHost)
}
// Read IP-hostname pairs from file
@@ -221,12 +223,12 @@ func (a *AutoHosts) updateTableRev(tableRev map[string][]string, newHost string,
func (a *AutoHosts) load(table map[string][]net.IP, tableRev map[string][]string, fn string) {
f, err := os.Open(fn)
if err != nil {
log.Error("AutoHosts: %s", err)
log.Error("autohosts: %s", err)
return
}
defer f.Close()
r := bufio.NewReader(f)
log.Debug("AutoHosts: loading hosts from file %s", fn)
log.Debug("autohosts: loading hosts from file %s", fn)
for done := false; !done; {
var line string
@@ -234,7 +236,7 @@ func (a *AutoHosts) load(table map[string][]net.IP, tableRev map[string][]string
if err == io.EOF {
done = true
} else if err != nil {
log.Error("AutoHosts: %s", err)
log.Error("autohosts: %s", err)
return
}
@@ -310,7 +312,7 @@ func (a *AutoHosts) watcherLoop() {
}
if event.Op&fsnotify.Write == fsnotify.Write {
log.Debug("AutoHosts: modified: %s", event.Name)
log.Debug("autohosts: modified: %s", event.Name)
a.updateHosts()
}
@@ -318,7 +320,7 @@ func (a *AutoHosts) watcherLoop() {
if !ok {
return
}
log.Error("AutoHosts: %s", err)
log.Error("autohosts: %s", err)
}
}
}
@@ -333,14 +335,15 @@ func (a *AutoHosts) updateHosts() {
for _, dir := range a.hostsDirs {
fis, err := ioutil.ReadDir(dir)
if err != nil {
if !os.IsNotExist(err) {
log.Error("AutoHosts: Opening directory: %s: %s", dir, err)
if !errors.Is(err, os.ErrNotExist) {
log.Error("autohosts: Opening directory: %q: %s", dir, err)
}
continue
}
for _, fi := range fis {
a.load(table, tableRev, dir+"/"+fi.Name())
a.load(table, tableRev, filepath.Join(dir, fi.Name()))
}
}

View File

@@ -8,29 +8,12 @@ import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
// ContainsString checks if string is in the slice of strings.
func ContainsString(strs []string, str string) bool {
for _, s := range strs {
if s == str {
return true
}
}
return false
}
// FileExists returns true if file exists.
func FileExists(fn string) bool {
_, err := os.Stat(fn)
return err == nil || !os.IsNotExist(err)
}
// RunCommand runs shell command.
func RunCommand(command string, arguments ...string) (int, string, error) {
cmd := exec.Command(command, arguments...)

View File

@@ -3,6 +3,7 @@ package util
import (
"crypto/tls"
"crypto/x509"
"errors"
"io/ioutil"
"os"
"runtime"
@@ -29,8 +30,8 @@ func LoadSystemRootCAs() *x509.CertPool {
for _, dir := range dirs {
fis, err := ioutil.ReadDir(dir)
if err != nil {
if !os.IsNotExist(err) {
log.Error("Opening directory: %s: %s", dir, err)
if !errors.Is(err, os.ErrNotExist) {
log.Error("opening directory: %q: %s", dir, err)
}
continue