"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
oauthutil: Reload config file off disk before updating token
Nick Craig-Wood1b2dda8c4cfe
diff --git a/fs/config.go b/fs/config.goindex 4da97e53a..9f232cf8e 100644--- a/fs/config.go+++ b/fs/config.go@@ -377,8 +377,11 @@ func LoadConfig() {// Load configuration file.var err errorConfigFile, err = loadConfigFile()- if err != nil {- log.Fatalf("Failed to config file \"%s\": %v", ConfigPath, err)+ if err == errorConfigFileNotFound {+ Log(nil, "Config file %q not found - using defaults", ConfigPath)+ ConfigFile, _ = goconfig.LoadFromReader(&bytes.Buffer{})+ } else if err != nil {+ log.Fatalf("Failed to load config file %q: %v", ConfigPath, err)}// Load filters@@ -391,13 +394,17 @@ func LoadConfig() {startTokenBucket()}+var errorConfigFileNotFound = errors.New("config file not found")+// loadConfigFile will load a config file, and// automatically decrypt it.func loadConfigFile() (*goconfig.ConfigFile, error) {b, err := ioutil.ReadFile(ConfigPath)if err != nil {- Log(nil, "Failed to load config file \"%v\" - using defaults: %v", ConfigPath, err)- return goconfig.LoadFromReader(&bytes.Buffer{})+ if os.IsNotExist(err) {+ return nil, errorConfigFileNotFound+ }+ return nil, err}// Find first non-empty line@@ -622,6 +629,34 @@ func SaveConfig() {}}+// ConfigSetValueAndSave sets the key to the value and saves just that+// value in the config file. It loads the old config file in from+// disk first and overwrites the given value only.+func ConfigSetValueAndSave(name, key, value string) (err error) {+ // Set the value in config in case we fail to reload it+ ConfigFile.SetValue(name, key, value)+ // Reload the config file+ reloadedConfigFile, err := loadConfigFile()+ if err == errorConfigFileNotFound {+ // Config file not written yet so ignore reload+ return nil+ } else if err != nil {+ return err+ }+ _, err = reloadedConfigFile.GetSection(name)+ if err != nil {+ // Section doesn't exist yet so ignore reload+ return err+ }+ // Update the config file with the reloaded version+ ConfigFile = reloadedConfigFile+ // Set the value in the reloaded version+ reloadedConfigFile.SetValue(name, key, value)+ // Save it again+ SaveConfig()+ return nil+}+// ShowRemotes shows an overview of the config filefunc ShowRemotes() {remotes := ConfigFile.GetSectionList()diff --git a/oauthutil/oauthutil.go b/oauthutil/oauthutil.goindex 51495d669..20ea845f8 100644--- a/oauthutil/oauthutil.go+++ b/oauthutil/oauthutil.go@@ -99,9 +99,12 @@ func putToken(name string, token *oauth2.Token) error {tokenString := string(tokenBytes)old := fs.ConfigFile.MustValue(name, fs.ConfigToken)if tokenString != old {- fs.ConfigFile.SetValue(name, fs.ConfigToken, tokenString)- fs.SaveConfig()- fs.Debug(name, "Saving new token in config file")+ err = fs.ConfigSetValueAndSave(name, fs.ConfigToken, tokenString)+ if err != nil {+ fs.ErrorLog(nil, "Failed to save new token in config file: %v", err)+ } else {+ fs.Debug(name, "Saved new token in config file")+ }}return nil}