From a2e9d6945200bce5f270fbe95dedd65a0d08b5e4 Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Wed, 6 Mar 2019 12:20:34 +0300 Subject: [PATCH] * use golibs v0.1.1: file.SafeWrite() --- config.go | 3 ++- filter.go | 3 ++- go.mod | 2 +- go.sum | 3 ++- helpers.go | 21 --------------------- upgrade.go | 3 ++- 6 files changed, 9 insertions(+), 26 deletions(-) diff --git a/config.go b/config.go index b0da6532..1c12f1c6 100644 --- a/config.go +++ b/config.go @@ -10,6 +10,7 @@ import ( "github.com/AdguardTeam/AdGuardHome/dhcpd" "github.com/AdguardTeam/AdGuardHome/dnsfilter" "github.com/AdguardTeam/AdGuardHome/dnsforward" + "github.com/AdguardTeam/golibs/file" "github.com/AdguardTeam/golibs/log" yaml "gopkg.in/yaml.v2" ) @@ -217,7 +218,7 @@ func (c *configuration) write() error { log.Error("Couldn't generate YAML file: %s", err) return err } - err = safeWriteFile(configFile, yamlText) + err = file.SafeWrite(configFile, yamlText) if err != nil { log.Error("Couldn't save YAML config: %s", err) return err diff --git a/filter.go b/filter.go index 280c403f..6b61d504 100644 --- a/filter.go +++ b/filter.go @@ -12,6 +12,7 @@ import ( "time" "github.com/AdguardTeam/AdGuardHome/dnsfilter" + "github.com/AdguardTeam/golibs/file" "github.com/AdguardTeam/golibs/log" ) @@ -220,7 +221,7 @@ func (filter *filter) save() error { log.Printf("Saving filter %d contents to: %s", filter.ID, filterFilePath) body := []byte(strings.Join(filter.Rules, "\n")) - err := safeWriteFile(filterFilePath, body) + err := file.SafeWrite(filterFilePath, body) // update LastUpdated field after saving the file filter.LastUpdated = filter.LastTimeUpdated() diff --git a/go.mod b/go.mod index 589d0982..2f0dd5bd 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.12 require ( github.com/AdguardTeam/dnsproxy v0.11.2 - github.com/AdguardTeam/golibs v0.1.0 + github.com/AdguardTeam/golibs v0.1.1 github.com/StackExchange/wmi v0.0.0-20180725035823-b12b22c5341f // indirect github.com/bluele/gcache v0.0.0-20171010155617-472614239ac7 github.com/go-ole/go-ole v1.2.1 // indirect diff --git a/go.sum b/go.sum index 6e4a79f1..51c15f76 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,8 @@ github.com/AdguardTeam/dnsproxy v0.11.2 h1:S/Ag2q9qoZsmW1fvMohPZP7/5amEtz8NmFCp8kxUalQ= github.com/AdguardTeam/dnsproxy v0.11.2/go.mod h1:EPp92b5cYR7HZpO+OQu6xC7AyhUoBaXW3sfa3exq/0I= -github.com/AdguardTeam/golibs v0.1.0 h1:Mo1QNKC8eSbqczhxfdBXYCrUMwvgCyCwZFyWv+2Gdng= github.com/AdguardTeam/golibs v0.1.0/go.mod h1:zhi6xGwK4cMpjDocybhhLgvcGkstiSIjlpKbvyxC5Yc= +github.com/AdguardTeam/golibs v0.1.1 h1:aepIN7yulf8I4Ub2c0cAaIizfSHPVXB2wrh8j4BJxl4= +github.com/AdguardTeam/golibs v0.1.1/go.mod h1:b0XkhgIcn2TxwX6C5AQMtpIFAgjPehNgxJErWkwA3ko= github.com/StackExchange/wmi v0.0.0-20180725035823-b12b22c5341f h1:5ZfJxyXo8KyX8DgGXC5B7ILL8y51fci/qYz2B4j8iLY= github.com/StackExchange/wmi v0.0.0-20180725035823-b12b22c5341f/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da h1:KjTM2ks9d14ZYCvmHS9iAKVt9AyzRSqNU1qabPih5BY= diff --git a/helpers.go b/helpers.go index 8e884d36..25e35205 100644 --- a/helpers.go +++ b/helpers.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net" "net/http" "net/url" @@ -19,26 +18,6 @@ import ( "github.com/joomcode/errorx" ) -// ---------------------------------- -// helper functions for working with files -// ---------------------------------- - -// Writes data first to a temporary file and then renames it to what's specified in path -func safeWriteFile(path string, data []byte) error { - dir := filepath.Dir(path) - err := os.MkdirAll(dir, 0755) - if err != nil { - return err - } - - tmpPath := path + ".tmp" - err = ioutil.WriteFile(tmpPath, data, 0644) - if err != nil { - return err - } - return os.Rename(tmpPath, path) -} - // ---------------------------------- // helper functions for HTTP handlers // ---------------------------------- diff --git a/upgrade.go b/upgrade.go index 0c9aeca7..e730d34b 100644 --- a/upgrade.go +++ b/upgrade.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" + "github.com/AdguardTeam/golibs/file" "github.com/AdguardTeam/golibs/log" yaml "gopkg.in/yaml.v2" ) @@ -86,7 +87,7 @@ func upgradeConfigSchema(oldVersion int, diskConfig *map[string]interface{}) err return err } - err = safeWriteFile(configFile, body) + err = file.SafeWrite(configFile, body) if err != nil { log.Printf("Couldn't save YAML config: %s", err) return err