From 00a61fdea0b974ced597857060798d4ec87f2e29 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Tue, 6 Apr 2021 20:48:36 +0300 Subject: [PATCH] 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 Date: Tue Apr 6 19:54:21 2021 +0300 all: use renameio instead of golibs/file --- go.mod | 1 + go.sum | 2 ++ internal/aghnet/net_linux.go | 6 +++--- internal/dhcpd/db.go | 16 +++++++++------- internal/home/config.go | 7 +++++-- internal/home/upgrade.go | 4 ++-- 6 files changed, 22 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index c339c991..2618a128 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e7af222a..02c0a539 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/aghnet/net_linux.go b/internal/aghnet/net_linux.go index 47614067..2154f0ab 100644 --- a/internal/aghnet/net_linux.go +++ b/internal/aghnet/net_linux.go @@ -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 diff --git a/internal/dhcpd/db.go b/internal/dhcpd/db.go index 8891835f..34a38050 100644 --- a/internal/dhcpd/db.go +++ b/internal/dhcpd/db.go @@ -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)) } diff --git a/internal/home/config.go b/internal/home/config.go index da181153..554d1872 100644 --- a/internal/home/config.go +++ b/internal/home/config.go @@ -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 } diff --git a/internal/home/upgrade.go b/internal/home/upgrade.go index 26583a49..8f4aa6c6 100644 --- a/internal/home/upgrade.go +++ b/internal/home/upgrade.go @@ -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) }