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

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)
}