Pull request: all: use renameio instead of golibs/file

Merge in DNS/adguard-home from use-renameio to master

Squashed commit of the following:

commit 807cda37b0e9e49539c1a8ce7912b74bd3c05b08
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Apr 6 19:54:21 2021 +0300

    all: use renameio instead of golibs/file
This commit is contained in:
Ainar Garipov 2021-04-06 20:48:36 +03:00
parent 65553a29e9
commit 00a61fdea0
6 changed files with 22 additions and 14 deletions

1
go.mod
View File

@ -15,6 +15,7 @@ require (
github.com/gobuffalo/packr v1.30.1
github.com/gobuffalo/packr/v2 v2.8.1 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/renameio v1.0.1-0.20210406141108-81588dbe0453
github.com/hugelgupf/socketpair v0.0.0-20190730060125-05d35a94e714
github.com/insomniacslk/dhcp v0.0.0-20210310193751-cfd4d47082c2
github.com/kardianos/service v1.2.0

2
go.sum
View File

@ -164,6 +164,8 @@ github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXi
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/renameio v1.0.1-0.20210406141108-81588dbe0453 h1:vvZyWjAX9oDB+DWpMsZMyv6Q3NZtim2C5Zcdh+H0OmQ=
github.com/google/renameio v1.0.1-0.20210406141108-81588dbe0453/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk=
github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY=
github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=

View File

@ -13,7 +13,7 @@ import (
"strings"
"github.com/AdguardTeam/AdGuardHome/internal/aghio"
"github.com/AdguardTeam/golibs/file"
"github.com/google/renameio/maybe"
)
// maxConfigFileSize is the maximum length of interfaces configuration file.
@ -145,9 +145,9 @@ func ifaceSetStaticIP(ifaceName string) (err error) {
}
body = append(body, []byte(add)...)
err = file.SafeWrite("/etc/dhcpcd.conf", body)
err = maybe.WriteFile("/etc/dhcpcd.conf", body, 0o644)
if err != nil {
return err
return fmt.Errorf("writing conf: %w", err)
}
return nil

View File

@ -10,8 +10,8 @@ import (
"os"
"time"
"github.com/AdguardTeam/golibs/file"
"github.com/AdguardTeam/golibs/log"
"github.com/google/renameio/maybe"
)
const dbFilename = "leases.db"
@ -50,7 +50,7 @@ func (s *Server) dbLoad() {
obj := []leaseJSON{}
err = json.Unmarshal(data, &obj)
if err != nil {
log.Error("DHCP: invalid DB: %v", err)
log.Error("dhcp: invalid DB: %v", err)
return
}
@ -59,7 +59,7 @@ func (s *Server) dbLoad() {
obj[i].IP = normalizeIP(obj[i].IP)
if !(len(obj[i].IP) == 4 || len(obj[i].IP) == 16) {
log.Info("DHCP: invalid IP: %s", obj[i].IP)
log.Info("dhcp: invalid IP: %s", obj[i].IP)
continue
}
@ -93,7 +93,7 @@ func (s *Server) dbLoad() {
s.srv6.ResetLeases(leases6)
}
log.Info("DHCP: loaded leases v4:%d v6:%d total-read:%d from DB",
log.Info("dhcp: loaded leases v4:%d v6:%d total-read:%d from DB",
len(leases4), len(leases6), numLeases)
}
@ -164,11 +164,13 @@ func (s *Server) dbStore() {
return
}
err = file.SafeWrite(s.conf.DBFilePath, data)
err = maybe.WriteFile(s.conf.DBFilePath, data, 0o644)
if err != nil {
log.Error("DHCP: can't store lease table on disk: %v filename: %s",
log.Error("dhcp: can't store lease table on disk: %v filename: %s",
err, s.conf.DBFilePath)
return
}
log.Info("DHCP: stored %d leases in DB", len(leases))
log.Info("dhcp: stored %d leases in DB", len(leases))
}

View File

@ -14,8 +14,8 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
"github.com/AdguardTeam/AdGuardHome/internal/stats"
"github.com/AdguardTeam/AdGuardHome/internal/version"
"github.com/AdguardTeam/golibs/file"
"github.com/AdguardTeam/golibs/log"
"github.com/google/renameio/maybe"
yaml "gopkg.in/yaml.v2"
)
@ -314,11 +314,14 @@ func (c *configuration) write() error {
config.Clients = nil
if err != nil {
log.Error("Couldn't generate YAML file: %s", err)
return err
}
err = file.SafeWrite(configFile, yamlText)
err = maybe.WriteFile(configFile, yamlText, 0o644)
if err != nil {
log.Error("Couldn't save YAML config: %s", err)
return err
}

View File

@ -7,8 +7,8 @@ import (
"path/filepath"
"runtime"
"github.com/AdguardTeam/golibs/file"
"github.com/AdguardTeam/golibs/log"
"github.com/google/renameio/maybe"
"golang.org/x/crypto/bcrypt"
yaml "gopkg.in/yaml.v2"
)
@ -99,7 +99,7 @@ func upgradeConfigSchema(oldVersion int, diskConf yobj) (err error) {
config.fileData = body
confFile := config.getConfigFilename()
err = file.SafeWrite(confFile, body)
err = maybe.WriteFile(confFile, body, 0o644)
if err != nil {
return fmt.Errorf("saving new config: %w", err)
}