Skip to content

Commit a031cae

Browse files
committed
Pull request: home: imp err handling, marshalling
Merge in DNS/adguard-home from imp-code to master Squashed commit of the following: commit 9433fb9 Author: Ainar Garipov <[email protected]> Date: Fri May 14 19:24:32 2021 +0300 home: imp err handling, marshalling
1 parent 9d788a2 commit a031cae

File tree

6 files changed

+56
-63
lines changed

6 files changed

+56
-63
lines changed

HACKING.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ on GitHub and most other Markdown renderers. -->
9595
* Constructors should validate their arguments and return meaningful errors.
9696
As a corollary, avoid lazy initialization.
9797
98+
* Define `MarshalFoo` methods on non-pointer receivers, as pointer receivers
99+
[can have surprising results][staticcheck-911].
100+
98101
* Don't mix horizontal and vertical placement of arguments in function and
99102
method calls. That is, either this:
100103
@@ -286,9 +289,10 @@ on GitHub and most other Markdown renderers. -->
286289
287290
* <https://go-proverbs.github.io/>
288291
289-
[constant errors]: https://dave.cheney.net/2016/04/07/constant-errors
290292
[Linus said]: https://www.kernel.org/doc/html/v4.17/process/coding-style.html#indentation
291293
[Text, Including Comments]: #text-including-comments
294+
[constant errors]: https://dave.cheney.net/2016/04/07/constant-errors
295+
[staticcheck-911]: https://github.com/dominikh/go-tools/issues/911
292296
293297
294298

internal/aghnet/net.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,17 @@ type NetInterface struct {
8484
Subnets []*net.IPNet `json:"-"`
8585
}
8686

87-
// MarshalJSON implements the json.Marshaler interface for *NetInterface.
88-
func (iface *NetInterface) MarshalJSON() ([]byte, error) {
87+
// MarshalJSON implements the json.Marshaler interface for NetInterface.
88+
func (iface NetInterface) MarshalJSON() ([]byte, error) {
8989
type netInterface NetInterface
9090
return json.Marshal(&struct {
9191
HardwareAddr string `json:"hardware_address"`
9292
Flags string `json:"flags"`
93-
*netInterface
93+
netInterface
9494
}{
9595
HardwareAddr: iface.HardwareAddr.String(),
9696
Flags: iface.Flags.String(),
97-
netInterface: (*netInterface)(iface),
97+
netInterface: netInterface(iface),
9898
})
9999
}
100100

internal/dhcpd/dhcpd.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func (l *Lease) IsStatic() (ok bool) {
4343
return l != nil && l.Expiry.Unix() == leaseExpireStatic
4444
}
4545

46-
// MarshalJSON implements the json.Marshaler interface for *Lease.
47-
func (l *Lease) MarshalJSON() ([]byte, error) {
46+
// MarshalJSON implements the json.Marshaler interface for Lease.
47+
func (l Lease) MarshalJSON() ([]byte, error) {
4848
var expiryStr string
4949
if !l.IsStatic() {
5050
// The front-end is waiting for RFC 3999 format of the time
@@ -59,20 +59,20 @@ func (l *Lease) MarshalJSON() ([]byte, error) {
5959
return json.Marshal(&struct {
6060
HWAddr string `json:"mac"`
6161
Expiry string `json:"expires,omitempty"`
62-
*lease
62+
lease
6363
}{
6464
HWAddr: l.HWAddr.String(),
6565
Expiry: expiryStr,
66-
lease: (*lease)(l),
66+
lease: lease(l),
6767
})
6868
}
6969

7070
// UnmarshalJSON implements the json.Unmarshaler interface for *Lease.
7171
func (l *Lease) UnmarshalJSON(data []byte) (err error) {
7272
type lease Lease
7373
aux := struct {
74-
HWAddr string `json:"mac"`
7574
*lease
75+
HWAddr string `json:"mac"`
7676
}{
7777
lease: (*lease)(l),
7878
}

internal/home/filter.go

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package home
22

33
import (
44
"bufio"
5+
"errors"
56
"fmt"
67
"hash/crc32"
78
"io"
@@ -622,29 +623,32 @@ func (f *Filtering) updateIntl(filter *filter) (updated bool, err error) {
622623
}
623624

624625
// loads filter contents from the file in dataDir
625-
func (f *Filtering) load(filter *filter) error {
626+
func (f *Filtering) load(filter *filter) (err error) {
626627
filterFilePath := filter.Path()
627-
log.Tracef("Loading filter %d contents to: %s", filter.ID, filterFilePath)
628628

629-
if _, err := os.Stat(filterFilePath); os.IsNotExist(err) {
630-
// do nothing, file doesn't exist
631-
return err
632-
}
629+
log.Tracef("filtering: loading filter %d contents to: %s", filter.ID, filterFilePath)
633630

634631
file, err := os.Open(filterFilePath)
635-
if err != nil {
636-
return err
632+
if errors.Is(err, os.ErrNotExist) {
633+
// Do nothing, file doesn't exist.
634+
return nil
635+
} else if err != nil {
636+
return fmt.Errorf("opening filter file: %w", err)
637637
}
638638
defer file.Close()
639-
st, _ := file.Stat()
640639

641-
log.Tracef("File %s, id %d, length %d",
642-
filterFilePath, filter.ID, st.Size())
640+
st, err := file.Stat()
641+
if err != nil {
642+
return fmt.Errorf("getting filter file stat: %w", err)
643+
}
644+
645+
log.Tracef("filtering: File %s, id %d, length %d", filterFilePath, filter.ID, st.Size())
646+
643647
rulesCount, checksum, _ := f.parseFilterContents(file)
644648

645649
filter.RulesCount = rulesCount
646650
filter.checksum = checksum
647-
filter.LastUpdated = filter.LastTimeUpdated()
651+
filter.LastUpdated = st.ModTime()
648652

649653
return nil
650654
}
@@ -660,24 +664,6 @@ func (filter *filter) Path() string {
660664
return filepath.Join(Context.getDataDir(), filterDir, strconv.FormatInt(filter.ID, 10)+".txt")
661665
}
662666

663-
// LastTimeUpdated returns the time when the filter was last time updated
664-
func (filter *filter) LastTimeUpdated() time.Time {
665-
filterFilePath := filter.Path()
666-
s, err := os.Stat(filterFilePath)
667-
if os.IsNotExist(err) {
668-
// if the filter file does not exist, return 0001-01-01
669-
return time.Time{}
670-
}
671-
672-
if err != nil {
673-
// if the filter file does not exist, return 0001-01-01
674-
return time.Time{}
675-
}
676-
677-
// filter file modified time
678-
return s.ModTime()
679-
}
680-
681667
func enableFilters(async bool) {
682668
var whiteFilters []dnsfilter.Filter
683669
filters := []dnsfilter.Filter{{

internal/home/service.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package home
22

33
import (
4+
"errors"
45
"fmt"
56
"io/ioutil"
67
"os"
@@ -88,24 +89,27 @@ func svcAction(s service.Service, action string) (err error) {
8889
// If pid-file doesn't exist, find our PID using 'ps' command
8990
func sendSigReload() {
9091
if runtime.GOOS == "windows" {
91-
log.Error("Not implemented on Windows")
92+
log.Error("not implemented on windows")
93+
9294
return
9395
}
9496

9597
pidfile := fmt.Sprintf("/var/run/%s.pid", serviceName)
9698
data, err := ioutil.ReadFile(pidfile)
97-
if os.IsNotExist(err) {
99+
if errors.Is(err, os.ErrNotExist) {
98100
var code int
99101
var psdata string
100102
code, psdata, err = aghos.RunCommand("ps", "-C", serviceName, "-o", "pid=")
101103
if err != nil || code != 0 {
102-
log.Error("Can't find AdGuardHome process: %s code:%d", err, code)
104+
log.Error("finding AdGuardHome process: code: %d, error: %s", code, err)
105+
103106
return
104107
}
105-
data = []byte(psdata)
106108

109+
data = []byte(psdata)
107110
} else if err != nil {
108-
log.Error("Can't read PID file %s: %s", pidfile, err)
111+
log.Error("reading pid file %s: %s", pidfile, err)
112+
109113
return
110114
}
111115

@@ -258,12 +262,12 @@ func handleServiceUninstallCommand(s service.Service) {
258262
if runtime.GOOS == "darwin" {
259263
// Remove log files on cleanup and log errors.
260264
err = os.Remove(launchdStdoutPath)
261-
if err != nil && !os.IsNotExist(err) {
265+
if err != nil && !errors.Is(err, os.ErrNotExist) {
262266
log.Printf("removing stdout file: %s", err)
263267
}
264268

265269
err = os.Remove(launchdStderrPath)
266-
if err != nil && !os.IsNotExist(err) {
270+
if err != nil && !errors.Is(err, os.ErrNotExist) {
267271
log.Printf("removing stderr file: %s", err)
268272
}
269273
}

internal/home/upgrade.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package home
22

33
import (
4+
"errors"
45
"fmt"
56
"net/url"
67
"os"
@@ -115,17 +116,16 @@ func upgradeConfigSchema(oldVersion int, diskConf yobj) (err error) {
115116

116117
// The first schema upgrade:
117118
// No more "dnsfilter.txt", filters are now kept in data/filters/
118-
func upgradeSchema0to1(diskConf yobj) error {
119+
func upgradeSchema0to1(diskConf yobj) (err error) {
119120
log.Printf("%s(): called", funcName())
120121

121122
dnsFilterPath := filepath.Join(Context.workDir, "dnsfilter.txt")
122-
if _, err := os.Stat(dnsFilterPath); !os.IsNotExist(err) {
123-
log.Printf("Deleting %s as we don't need it anymore", dnsFilterPath)
124-
err = os.Remove(dnsFilterPath)
125-
if err != nil {
126-
log.Printf("Cannot remove %s due to %s", dnsFilterPath, err)
127-
// not fatal, move on
128-
}
123+
log.Printf("deleting %s as we don't need it anymore", dnsFilterPath)
124+
err = os.Remove(dnsFilterPath)
125+
if err != nil && !errors.Is(err, os.ErrNotExist) {
126+
log.Info("warning: %s", err)
127+
128+
// Go on.
129129
}
130130

131131
diskConf["schema_version"] = 1
@@ -136,17 +136,16 @@ func upgradeSchema0to1(diskConf yobj) error {
136136
// Second schema upgrade:
137137
// coredns is now dns in config
138138
// delete 'Corefile', since we don't use that anymore
139-
func upgradeSchema1to2(diskConf yobj) error {
139+
func upgradeSchema1to2(diskConf yobj) (err error) {
140140
log.Printf("%s(): called", funcName())
141141

142142
coreFilePath := filepath.Join(Context.workDir, "Corefile")
143-
if _, err := os.Stat(coreFilePath); !os.IsNotExist(err) {
144-
log.Printf("Deleting %s as we don't need it anymore", coreFilePath)
145-
err = os.Remove(coreFilePath)
146-
if err != nil {
147-
log.Printf("Cannot remove %s due to %s", coreFilePath, err)
148-
// not fatal, move on
149-
}
143+
log.Printf("deleting %s as we don't need it anymore", coreFilePath)
144+
err = os.Remove(coreFilePath)
145+
if err != nil && !errors.Is(err, os.ErrNotExist) {
146+
log.Info("warning: %s", err)
147+
148+
// Go on.
150149
}
151150

152151
if _, ok := diskConf["dns"]; !ok {

0 commit comments

Comments
 (0)