Pull request: all: rm var shadowing, vol. 1

Updates #2803.

Squashed commit of the following:

commit 15fa64ad4fb7561c4b0542245f99869685643bbd
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Mar 11 14:55:28 2021 +0300

    util: imp code

commit c3b0563a44ccc98a5901df19174acdca8a350d62
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Mar 11 14:38:54 2021 +0300

    all: rm var shadowing, vol. 1
This commit is contained in:
Ainar Garipov 2021-03-11 15:38:39 +03:00
parent 94e21e69af
commit 2c9992e0cc
6 changed files with 35 additions and 20 deletions

View file

@ -44,7 +44,7 @@ func prepareTestDir(t *testing.T) (dir string) {
t.Cleanup(func() { t.Cleanup(func() {
start := time.Now() start := time.Now()
for { for {
err := os.RemoveAll(dir) err = os.RemoveAll(dir)
if err == nil { if err == nil {
break break
} }

View file

@ -337,7 +337,8 @@ func tarGzFileUnpack(tarfile, outdir string) ([]string, error) {
var err2 error var err2 error
tarReader := tar.NewReader(gzReader) tarReader := tar.NewReader(gzReader)
for { for {
header, err := tarReader.Next() var header *tar.Header
header, err = tarReader.Next()
if err == io.EOF { if err == io.EOF {
err2 = nil err2 = nil
break break
@ -377,7 +378,8 @@ func tarGzFileUnpack(tarfile, outdir string) ([]string, error) {
continue continue
} }
f, err := os.OpenFile(outputName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(header.Mode&0o777)) var f io.WriteCloser
f, err = os.OpenFile(outputName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(header.Mode&0o777))
if err != nil { if err != nil {
err2 = fmt.Errorf("os.OpenFile(%s): %w", outputName, err) err2 = fmt.Errorf("os.OpenFile(%s): %w", outputName, err)
break break
@ -449,7 +451,8 @@ func zipFileUnpack(zipfile, outdir string) ([]string, error) {
continue continue
} }
f, err := os.OpenFile(outputName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fi.Mode()) var f io.WriteCloser
f, err = os.OpenFile(outputName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fi.Mode())
if err != nil { if err != nil {
err2 = fmt.Errorf("os.OpenFile(): %w", err) err2 = fmt.Errorf("os.OpenFile(): %w", err)
break break

View file

@ -10,10 +10,9 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/miekg/dns"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/miekg/dns"
) )
type onChangedT func() type onChangedT func()
@ -228,15 +227,17 @@ func (a *AutoHosts) load(table map[string][]net.IP, tableRev map[string][]string
r := bufio.NewReader(f) r := bufio.NewReader(f)
log.Debug("AutoHosts: loading hosts from file %s", fn) log.Debug("AutoHosts: loading hosts from file %s", fn)
finish := false for done := false; !done; {
for !finish { var line string
line, err := r.ReadString('\n') line, err = r.ReadString('\n')
if err == io.EOF { if err == io.EOF {
finish = true done = true
} else if err != nil { } else if err != nil {
log.Error("AutoHosts: %s", err) log.Error("AutoHosts: %s", err)
return return
} }
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
if len(line) == 0 || line[0] == '#' { if len(line) == 0 || line[0] == '#' {
continue continue
@ -247,26 +248,30 @@ func (a *AutoHosts) load(table map[string][]net.IP, tableRev map[string][]string
continue continue
} }
ipAddr := net.ParseIP(fields[0]) ip := net.ParseIP(fields[0])
if ipAddr == nil { if ip == nil {
continue continue
} }
for i := 1; i != len(fields); i++ { for i := 1; i != len(fields); i++ {
host := fields[i] host := fields[i]
if len(host) == 0 { if len(host) == 0 {
break break
} }
sharp := strings.IndexByte(host, '#') sharp := strings.IndexByte(host, '#')
if sharp == 0 { if sharp == 0 {
break // skip the rest of the line after # // Skip the comments.
break
} else if sharp > 0 { } else if sharp > 0 {
host = host[:sharp] host = host[:sharp]
} }
a.updateTable(table, host, ipAddr) a.updateTable(table, host, ip)
a.updateTableRev(tableRev, host, ipAddr) a.updateTableRev(tableRev, host, ip)
if sharp >= 0 { if sharp >= 0 {
break // skip the rest of the line after # // Skip the comments again.
break
} }
} }
} }

View file

@ -94,7 +94,8 @@ func IsOpenWRT() bool {
continue continue
} }
body, err := ioutil.ReadFile(filepath.Join(etcDir, fileInfo.Name())) var body []byte
body, err = ioutil.ReadFile(filepath.Join(etcDir, fileInfo.Name()))
if err != nil { if err != nil {
continue continue
} }

View file

@ -69,7 +69,8 @@ func GetValidNetInterfacesForWeb() ([]*NetInterface, error) {
var netInterfaces []*NetInterface var netInterfaces []*NetInterface
for _, iface := range ifaces { for _, iface := range ifaces {
addrs, err := iface.Addrs() var addrs []net.Addr
addrs, err = iface.Addrs()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get addresses for interface %s: %w", iface.Name, err) return nil, fmt.Errorf("failed to get addresses for interface %s: %w", iface.Name, err)
} }

View file

@ -32,19 +32,24 @@ func LoadSystemRootCAs() *x509.CertPool {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
log.Error("Opening directory: %s: %s", dir, err) log.Error("Opening directory: %s: %s", dir, err)
} }
continue continue
} }
rootsAdded := false rootsAdded := false
for _, fi := range fis { for _, fi := range fis {
data, err := ioutil.ReadFile(dir + "/" + fi.Name()) var certData []byte
if err == nil && roots.AppendCertsFromPEM(data) { certData, err = ioutil.ReadFile(dir + "/" + fi.Name())
if err == nil && roots.AppendCertsFromPEM(certData) {
rootsAdded = true rootsAdded = true
} }
} }
if rootsAdded { if rootsAdded {
return roots return roots
} }
} }
return nil return nil
} }