2021-03-10 14:13:01 +00:00
|
|
|
// Package configfile implements a config file loader and saver
|
2020-11-07 21:05:58 +00:00
|
|
|
package configfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-03-10 15:40:34 +00:00
|
|
|
"context"
|
2020-11-07 21:05:58 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-03-10 14:13:01 +00:00
|
|
|
"sync"
|
2020-11-07 21:05:58 +00:00
|
|
|
|
|
|
|
"github.com/Unknwon/goconfig"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config"
|
|
|
|
)
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
// LoadConfig installs the config file handler and calls config.LoadConfig
|
|
|
|
func LoadConfig(ctx context.Context) {
|
|
|
|
config.Data = &Storage{}
|
|
|
|
config.LoadConfig(ctx)
|
2020-11-07 21:05:58 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
// Storage implements config.Storage for saving and loading config
|
|
|
|
// data in a simple INI based file.
|
|
|
|
type Storage struct {
|
2021-03-10 14:13:01 +00:00
|
|
|
gc *goconfig.ConfigFile // config file loaded - thread safe
|
|
|
|
mu sync.Mutex // to protect the following variables
|
|
|
|
fi os.FileInfo // stat of the file when last loaded
|
2021-03-10 15:40:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 14:13:01 +00:00
|
|
|
// Check to see if we need to reload the config
|
|
|
|
func (s *Storage) check() {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2021-04-08 15:49:47 +00:00
|
|
|
if configPath := config.GetConfigPath(); configPath != "" {
|
|
|
|
// Check to see if config file has changed since it was last loaded
|
|
|
|
fi, err := os.Stat(configPath)
|
|
|
|
if err == nil {
|
|
|
|
// check to see if config file has changed and if it has, reload it
|
|
|
|
if s.fi == nil || !fi.ModTime().Equal(s.fi.ModTime()) || fi.Size() != s.fi.Size() {
|
|
|
|
fs.Debugf(nil, "Config file has changed externaly - reloading")
|
|
|
|
err := s._load()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to read config file - using previous config: %v", err)
|
|
|
|
}
|
2021-03-10 14:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// _load the config from permanent storage, decrypting if necessary
|
|
|
|
//
|
|
|
|
// mu must be held when calling this
|
|
|
|
func (s *Storage) _load() (err error) {
|
2021-03-10 15:40:34 +00:00
|
|
|
// Make sure we have a sensible default even when we error
|
|
|
|
defer func() {
|
2021-03-10 14:13:01 +00:00
|
|
|
if s.gc == nil {
|
2021-03-10 15:40:34 +00:00
|
|
|
s.gc, _ = goconfig.LoadFromReader(bytes.NewReader([]byte{}))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-04-08 15:49:47 +00:00
|
|
|
configPath := config.GetConfigPath()
|
|
|
|
if configPath == "" {
|
2021-03-13 12:36:38 +00:00
|
|
|
return config.ErrorConfigFileNotFound
|
|
|
|
}
|
|
|
|
|
2021-04-08 15:49:47 +00:00
|
|
|
fd, err := os.Open(configPath)
|
2020-11-07 21:05:58 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return config.ErrorConfigFileNotFound
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2021-03-10 14:13:01 +00:00
|
|
|
defer fs.CheckClose(fd, &err)
|
|
|
|
|
|
|
|
// Update s.fi with the current file info
|
2021-04-08 15:49:47 +00:00
|
|
|
s.fi, _ = os.Stat(configPath)
|
2020-11-07 21:05:58 +00:00
|
|
|
|
2021-03-10 14:13:01 +00:00
|
|
|
cryptReader, err := config.Decrypt(fd)
|
2020-11-07 21:05:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
gc, err := goconfig.LoadFromReader(cryptReader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-11-07 21:05:58 +00:00
|
|
|
}
|
2021-03-10 15:40:34 +00:00
|
|
|
s.gc = gc
|
2020-11-07 21:05:58 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-10 14:13:01 +00:00
|
|
|
// Load the config from permanent storage, decrypting if necessary
|
|
|
|
func (s *Storage) Load() (err error) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s._load()
|
|
|
|
}
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
// Save the config to permanent storage, encrypting if necessary
|
|
|
|
func (s *Storage) Save() error {
|
2021-03-10 14:13:01 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2021-04-08 15:49:47 +00:00
|
|
|
configPath := config.GetConfigPath()
|
|
|
|
if configPath == "" {
|
|
|
|
return errors.Errorf("Failed to save config file: Path is empty")
|
2021-03-13 12:36:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 15:49:47 +00:00
|
|
|
dir, name := filepath.Split(configPath)
|
2020-11-07 21:05:58 +00:00
|
|
|
err := os.MkdirAll(dir, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create config directory")
|
|
|
|
}
|
|
|
|
f, err := ioutil.TempFile(dir, name)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Errorf("Failed to create temp file for new config: %v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = f.Close()
|
|
|
|
if err := os.Remove(f.Name()); err != nil && !os.IsNotExist(err) {
|
|
|
|
fs.Errorf(nil, "Failed to remove temp config file: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2021-03-10 15:40:34 +00:00
|
|
|
if err := goconfig.SaveConfigData(s.gc, &buf); err != nil {
|
2020-11-07 21:05:58 +00:00
|
|
|
return errors.Errorf("Failed to save config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := config.Encrypt(&buf, f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = f.Sync()
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Errorf("Failed to close config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var fileMode os.FileMode = 0600
|
2021-04-08 15:49:47 +00:00
|
|
|
info, err := os.Stat(configPath)
|
2020-11-07 21:05:58 +00:00
|
|
|
if err != nil {
|
|
|
|
fs.Debugf(nil, "Using default permissions for config file: %v", fileMode)
|
|
|
|
} else if info.Mode() != fileMode {
|
|
|
|
fs.Debugf(nil, "Keeping previous permissions for config file: %v", info.Mode())
|
|
|
|
fileMode = info.Mode()
|
|
|
|
}
|
|
|
|
|
2021-04-08 15:49:47 +00:00
|
|
|
attemptCopyGroup(configPath, f.Name())
|
2020-11-07 21:05:58 +00:00
|
|
|
|
|
|
|
err = os.Chmod(f.Name(), fileMode)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to set permissions on config file: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-04-08 15:49:47 +00:00
|
|
|
if err = os.Rename(configPath, configPath+".old"); err != nil && !os.IsNotExist(err) {
|
2020-11-07 21:05:58 +00:00
|
|
|
return errors.Errorf("Failed to move previous config to backup location: %v", err)
|
|
|
|
}
|
2021-04-08 15:49:47 +00:00
|
|
|
if err = os.Rename(f.Name(), configPath); err != nil {
|
2020-11-07 21:05:58 +00:00
|
|
|
return errors.Errorf("Failed to move newly written config from %s to final location: %v", f.Name(), err)
|
|
|
|
}
|
2021-04-08 15:49:47 +00:00
|
|
|
if err := os.Remove(configPath + ".old"); err != nil && !os.IsNotExist(err) {
|
2020-11-07 21:05:58 +00:00
|
|
|
fs.Errorf(nil, "Failed to remove backup config file: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-03-10 14:13:01 +00:00
|
|
|
// Update s.fi with the newly written file
|
2021-04-08 15:49:47 +00:00
|
|
|
s.fi, _ = os.Stat(configPath)
|
2021-03-10 14:13:01 +00:00
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize the config into a string
|
2021-03-10 15:40:34 +00:00
|
|
|
func (s *Storage) Serialize() (string, error) {
|
2021-03-10 14:13:01 +00:00
|
|
|
s.check()
|
2020-11-07 21:05:58 +00:00
|
|
|
var buf bytes.Buffer
|
2021-03-10 15:40:34 +00:00
|
|
|
if err := goconfig.SaveConfigData(s.gc, &buf); err != nil {
|
2020-11-07 21:05:58 +00:00
|
|
|
return "", errors.Errorf("Failed to save config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasSection returns true if section exists in the config file
|
2021-03-10 15:40:34 +00:00
|
|
|
func (s *Storage) HasSection(section string) bool {
|
2021-03-10 14:13:01 +00:00
|
|
|
s.check()
|
2021-03-10 15:40:34 +00:00
|
|
|
_, err := s.gc.GetSection(section)
|
2020-11-07 21:05:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSection removes the named section and all config from the
|
|
|
|
// config file
|
2021-03-10 15:40:34 +00:00
|
|
|
func (s *Storage) DeleteSection(section string) {
|
2021-03-10 14:13:01 +00:00
|
|
|
s.check()
|
2021-03-10 15:40:34 +00:00
|
|
|
s.gc.DeleteSection(section)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSectionList returns a slice of strings with names for all the
|
|
|
|
// sections
|
|
|
|
func (s *Storage) GetSectionList() []string {
|
2021-03-10 14:13:01 +00:00
|
|
|
s.check()
|
2021-03-10 15:40:34 +00:00
|
|
|
return s.gc.GetSectionList()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKeyList returns the keys in this section
|
|
|
|
func (s *Storage) GetKeyList(section string) []string {
|
2021-03-10 14:13:01 +00:00
|
|
|
s.check()
|
2021-03-10 15:40:34 +00:00
|
|
|
return s.gc.GetKeyList(section)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValue returns the key in section with a found flag
|
|
|
|
func (s *Storage) GetValue(section string, key string) (value string, found bool) {
|
2021-03-10 14:13:01 +00:00
|
|
|
s.check()
|
2021-03-10 15:40:34 +00:00
|
|
|
value, err := s.gc.GetValue(section, key)
|
|
|
|
if err != nil {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
return value, true
|
2020-11-07 21:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetValue sets the value under key in section
|
2021-03-10 15:40:34 +00:00
|
|
|
func (s *Storage) SetValue(section string, key string, value string) {
|
2021-03-10 14:13:01 +00:00
|
|
|
s.check()
|
2021-03-10 15:40:34 +00:00
|
|
|
s.gc.SetValue(section, key, value)
|
2020-11-07 21:05:58 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
// DeleteKey removes the key under section
|
|
|
|
func (s *Storage) DeleteKey(section string, key string) bool {
|
2021-03-10 14:13:01 +00:00
|
|
|
s.check()
|
2021-03-10 15:40:34 +00:00
|
|
|
return s.gc.DeleteKey(section, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interface is satisfied
|
|
|
|
var _ config.Storage = (*Storage)(nil)
|