From a1420ddc7991d8c50efabd23765d9adcd1c155ab Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Thu, 26 Nov 2020 12:56:40 +0100 Subject: conf: rework migration flows around a single Save() Signed-off-by: Jason A. Donenfeld --- conf/store.go | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) (limited to 'conf/store.go') diff --git a/conf/store.go b/conf/store.go index 35f0ba7d..9a0f8c7d 100644 --- a/conf/store.go +++ b/conf/store.go @@ -100,29 +100,20 @@ func MigrateUnencryptedConfigs(sharingBase int) (int, []error) { e++ continue } - configName := strings.TrimSuffix(name, configFileUnencryptedSuffix) - config, err := FromWgQuickWithUnknownEncoding(string(bytes), configName) + config, err := FromWgQuickWithUnknownEncoding(string(bytes), strings.TrimSuffix(name, configFileUnencryptedSuffix)) if err != nil { errs[e] = err e++ continue } - - bytes, err = dpapi.Encrypt([]byte(config.ToWgQuick()), name) - if err != nil { - errs[e] = err - e++ - continue - } - dstFile := configName + configFileSuffix - err = writeEncryptedFile(dstFile, false, bytes) + err = config.Save(false) if err != nil { errs[e] = err e++ continue } err = os.Remove(path) - if err != nil && os.Remove(dstFile) == nil { + if err != nil { errs[e] = err e++ continue @@ -183,7 +174,7 @@ func NameFromPath(path string) (string, error) { return name, nil } -func (config *Config) Save() error { +func (config *Config) Save(overwrite bool) error { if !TunnelNameIsValid(config.Name) { return errors.New("Tunnel name is not valid") } @@ -197,7 +188,7 @@ func (config *Config) Save() error { if err != nil { return err } - return writeEncryptedFile(filename, true, bytes) + return writeLockedDownFile(filename, overwrite, bytes) } func (config *Config) Path() (string, error) { -- cgit v1.2.3-59-g8ed1b