From df719f03dab886a95daae98eaf6fe02c7608d388 Mon Sep 17 00:00:00 2001 From: Bipin B Narayan Date: Wed, 29 May 2024 23:05:00 +0530 Subject: [PATCH 1/2] Update conf if existing one is the deprecated one --- main.go | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 7ad5b559..71ee4d85 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,8 @@ package main import ( + "bytes" + "crypto/md5" "flag" "io" "io/ioutil" @@ -48,9 +50,27 @@ func main() { } if autoCreate { - if _, err := os.Stat(confFile); err != nil && os.IsNotExist(err) { - logger.Warn("No conf file, creating one", "confFile", confFile) + shouldCreateOrUpdate := false + _, err := os.Stat(confFile) + if err != nil { + if os.IsNotExist(err) { + logger.Warn("No conf file, creating one", "confFile", confFile) + shouldCreateOrUpdate = true + } + } else { + file, errOpen := os.ReadFile(confFile) + if errOpen != nil { + logger.Error("Cannot open config file", "err", errOpen) + return + } + if bytes.Equal(md5.New().Sum(file), md5.New().Sum(deprecatedConfFileContent())) { + logger.Warn("Deprecated conf file found, writing new conf", "confFile", string(file)) + shouldCreateOrUpdate = true + } + } + + if shouldCreateOrUpdate { if err := ioutil.WriteFile(confFile, confFileContent(), 0600); err != nil { //nolint: gomnd logger.Warn("Couldn't create conf file", "confFile", confFile) } @@ -165,3 +185,25 @@ func confFileContent() []byte { return []byte(str) } + +func deprecatedConfFileContent() []byte { + str := `{ + "version": 1, + "accesses": [ + { + "user": "test", + "pass": "test", + "fs": "os", + "params": { + "basePath": "/tmp" + } + } + ], + "passive_transfer_port_range": { + "start": 2122, + "end": 2130 + } +}` + + return []byte(str) +} From 79b1afc0f55d34afc0815bf1209b48d1a5c3a29c Mon Sep 17 00:00:00 2001 From: Bipin B Narayan Date: Fri, 31 May 2024 15:03:13 +0530 Subject: [PATCH 2/2] Refactor a bit --- main.go | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/main.go b/main.go index 71ee4d85..dc270ba3 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,24 @@ var ( driver *server.Server ) +var deprecatedConfFileChecksum = md5.New().Sum([]byte(`{ + "version": 1, + "accesses": [ + { + "user": "test", + "pass": "test", + "fs": "os", + "params": { + "basePath": "/tmp" + } + } + ], + "passive_transfer_port_range": { + "start": 2122, + "end": 2130 + } +}`)) + func main() { // Arguments vars var confFile string @@ -64,9 +82,11 @@ func main() { return } - if bytes.Equal(md5.New().Sum(file), md5.New().Sum(deprecatedConfFileContent())) { - logger.Warn("Deprecated conf file found, writing new conf", "confFile", string(file)) + if bytes.Equal(md5.New().Sum(bytes.TrimSpace(file)), []byte(deprecatedConfFileChecksum)) { + logger.Warn("Deprecated conf file found, writing new conf") shouldCreateOrUpdate = true + } else { + logger.Warn("Existing conf file is not same a deprecated one. No modifying.") } } @@ -185,25 +205,3 @@ func confFileContent() []byte { return []byte(str) } - -func deprecatedConfFileContent() []byte { - str := `{ - "version": 1, - "accesses": [ - { - "user": "test", - "pass": "test", - "fs": "os", - "params": { - "basePath": "/tmp" - } - } - ], - "passive_transfer_port_range": { - "start": 2122, - "end": 2130 - } -}` - - return []byte(str) -}