2015-06-25 22:56:48 -04:00
|
|
|
/*
|
|
|
|
* Quick - Quick key value store for config files and persistent state files
|
|
|
|
*
|
|
|
|
* Minio Client (C) 2015 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package quick
|
|
|
|
|
|
|
|
import (
|
2015-09-29 18:11:46 -04:00
|
|
|
"bytes"
|
2015-06-25 22:56:48 -04:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2015-08-22 21:34:00 -04:00
|
|
|
"github.com/fatih/structs"
|
2016-04-06 19:05:30 -04:00
|
|
|
"github.com/minio/minio/pkg/safe"
|
2015-06-25 22:56:48 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config - generic config interface functions
|
|
|
|
type Config interface {
|
|
|
|
String() string
|
|
|
|
Version() string
|
2016-04-29 17:24:10 -04:00
|
|
|
Save(string) error
|
|
|
|
Load(string) error
|
2015-06-25 22:56:48 -04:00
|
|
|
Data() interface{}
|
2016-04-29 17:24:10 -04:00
|
|
|
Diff(Config) ([]structs.Field, error)
|
|
|
|
DeepDiff(Config) ([]structs.Field, error)
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// config - implements quick.Config interface
|
|
|
|
type config struct {
|
2015-08-13 19:24:48 -04:00
|
|
|
data interface{}
|
2015-06-25 22:56:48 -04:00
|
|
|
lock *sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2016-02-10 19:40:09 -05:00
|
|
|
// CheckData - checks the validity of config data. Data should be of
|
|
|
|
// type struct and contain a string type field called "Version".
|
2016-04-29 17:24:10 -04:00
|
|
|
func CheckData(data interface{}) error {
|
2015-06-25 22:56:48 -04:00
|
|
|
if !structs.IsStruct(data) {
|
2016-11-15 21:14:23 -05:00
|
|
|
return fmt.Errorf("Invalid argument type. Expecing \"struct\" type")
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
st := structs.New(data)
|
|
|
|
f, ok := st.FieldOk("Version")
|
|
|
|
if !ok {
|
2016-11-15 21:14:23 -05:00
|
|
|
return fmt.Errorf("Invalid type of struct argument. No [%s.Version] field found", st.Name())
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if f.Kind() != reflect.String {
|
2016-11-15 21:14:23 -05:00
|
|
|
return fmt.Errorf("Invalid type of struct argument. Expecting \"string\" type [%s.Version] field", st.Name())
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// New - instantiate a new config
|
2016-04-29 17:24:10 -04:00
|
|
|
func New(data interface{}) (Config, error) {
|
2016-01-14 21:17:24 -05:00
|
|
|
if err := CheckData(data); err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return nil, err
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
d := new(config)
|
2015-08-13 19:24:48 -04:00
|
|
|
d.data = data
|
2015-06-25 22:56:48 -04:00
|
|
|
d.lock = new(sync.RWMutex)
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
2015-10-07 20:39:18 -04:00
|
|
|
// CheckVersion - loads json and compares the version number provided returns back true or false - any failure
|
|
|
|
// is returned as error.
|
2016-04-29 17:24:10 -04:00
|
|
|
func CheckVersion(filename string, version string) (bool, error) {
|
|
|
|
_, err := os.Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2015-10-07 20:39:18 -04:00
|
|
|
}
|
|
|
|
|
2016-04-29 17:24:10 -04:00
|
|
|
fileData, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2015-10-07 20:39:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
fileData = []byte(strings.Replace(string(fileData), "\r\n", "\n", -1))
|
|
|
|
}
|
|
|
|
data := struct {
|
|
|
|
Version string
|
|
|
|
}{
|
|
|
|
Version: "",
|
|
|
|
}
|
2016-04-29 17:24:10 -04:00
|
|
|
err = json.Unmarshal(fileData, &data)
|
|
|
|
if err != nil {
|
|
|
|
switch err := err.(type) {
|
2015-10-07 20:39:18 -04:00
|
|
|
case *json.SyntaxError:
|
2016-04-29 17:24:10 -04:00
|
|
|
return false, FormatJSONSyntaxError(bytes.NewReader(fileData), err)
|
2015-10-07 20:39:18 -04:00
|
|
|
default:
|
2016-04-29 17:24:10 -04:00
|
|
|
return false, err
|
2015-10-07 20:39:18 -04:00
|
|
|
}
|
|
|
|
}
|
2016-01-14 21:17:24 -05:00
|
|
|
config, err := New(data)
|
|
|
|
if err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return false, err
|
2015-10-07 20:39:18 -04:00
|
|
|
}
|
|
|
|
if config.Version() != version {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load - loads json config from filename for the a given struct data
|
2016-04-29 17:24:10 -04:00
|
|
|
func Load(filename string, data interface{}) (Config, error) {
|
|
|
|
_, err := os.Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-10-07 20:39:18 -04:00
|
|
|
}
|
|
|
|
|
2016-04-29 17:24:10 -04:00
|
|
|
fileData, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-10-07 20:39:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
fileData = []byte(strings.Replace(string(fileData), "\r\n", "\n", -1))
|
|
|
|
}
|
|
|
|
|
2016-04-29 17:24:10 -04:00
|
|
|
err = json.Unmarshal(fileData, &data)
|
|
|
|
if err != nil {
|
|
|
|
switch err := err.(type) {
|
2015-10-07 20:39:18 -04:00
|
|
|
case *json.SyntaxError:
|
2016-04-29 17:24:10 -04:00
|
|
|
return nil, FormatJSONSyntaxError(bytes.NewReader(fileData), err)
|
2015-10-07 20:39:18 -04:00
|
|
|
default:
|
2016-04-29 17:24:10 -04:00
|
|
|
return nil, err
|
2015-10-07 20:39:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 21:17:24 -05:00
|
|
|
config, err := New(data)
|
|
|
|
if err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return nil, err
|
2015-10-07 20:39:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2015-06-25 22:56:48 -04:00
|
|
|
// Version returns the current config file format version
|
|
|
|
func (d config) Version() string {
|
2015-08-13 19:24:48 -04:00
|
|
|
st := structs.New(d.data)
|
2015-06-25 22:56:48 -04:00
|
|
|
|
|
|
|
f, ok := st.FieldOk("Version")
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
val := f.Value()
|
|
|
|
ver, ok := val.(string)
|
|
|
|
if ok {
|
|
|
|
return ver
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-01-14 21:17:24 -05:00
|
|
|
// writeFile writes data to a file named by filename.
|
|
|
|
// If the file does not exist, writeFile creates it;
|
|
|
|
// otherwise writeFile truncates it before writing.
|
2016-04-29 17:24:10 -04:00
|
|
|
func writeFile(filename string, data []byte) error {
|
|
|
|
safeFile, err := safe.CreateFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-01-14 21:17:24 -05:00
|
|
|
}
|
2016-04-29 17:24:10 -04:00
|
|
|
_, err = safeFile.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-01-14 21:17:24 -05:00
|
|
|
}
|
2016-04-29 17:24:10 -04:00
|
|
|
return safeFile.Close()
|
2016-01-14 21:17:24 -05:00
|
|
|
}
|
|
|
|
|
2015-06-25 22:56:48 -04:00
|
|
|
// String converts JSON config to printable string
|
|
|
|
func (d config) String() string {
|
2015-08-13 19:24:48 -04:00
|
|
|
configBytes, _ := json.MarshalIndent(d.data, "", "\t")
|
2015-06-25 22:56:48 -04:00
|
|
|
return string(configBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save writes config data in JSON format to a file.
|
2016-04-29 17:24:10 -04:00
|
|
|
func (d config) Save(filename string) error {
|
2015-06-25 22:56:48 -04:00
|
|
|
d.lock.Lock()
|
|
|
|
defer d.lock.Unlock()
|
|
|
|
|
2016-01-14 21:17:24 -05:00
|
|
|
// Check for existing file, if yes create a backup.
|
2016-04-29 17:24:10 -04:00
|
|
|
st, err := os.Stat(filename)
|
2016-01-14 21:17:24 -05:00
|
|
|
// If file exists and stat failed return here.
|
2016-04-29 17:24:10 -04:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
2016-01-14 21:17:24 -05:00
|
|
|
}
|
|
|
|
// File exists and proceed to take backup.
|
2016-04-29 17:24:10 -04:00
|
|
|
if err == nil {
|
2016-01-14 21:17:24 -05:00
|
|
|
// File exists and is not a regular file return error.
|
|
|
|
if !st.Mode().IsRegular() {
|
2016-04-29 17:24:10 -04:00
|
|
|
return fmt.Errorf("%s is not a regular file", filename)
|
2016-01-14 21:17:24 -05:00
|
|
|
}
|
|
|
|
// Read old data.
|
|
|
|
var oldData []byte
|
2016-04-29 17:24:10 -04:00
|
|
|
oldData, err = ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-01-14 21:17:24 -05:00
|
|
|
}
|
|
|
|
// Save read data to the backup file.
|
2016-04-29 17:24:10 -04:00
|
|
|
if err = writeFile(filename+".old", oldData); err != nil {
|
|
|
|
return err
|
2016-01-14 21:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Proceed to create or overwrite file.
|
2016-04-29 17:24:10 -04:00
|
|
|
jsonData, err := json.MarshalIndent(d.data, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
jsonData = []byte(strings.Replace(string(jsonData), "\n", "\r\n", -1))
|
|
|
|
}
|
2015-08-03 19:17:21 -04:00
|
|
|
|
2016-01-14 21:17:24 -05:00
|
|
|
// Save data.
|
2016-04-29 17:24:10 -04:00
|
|
|
return writeFile(filename, jsonData)
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load - loads JSON config from file and merge with currently set values
|
2016-04-29 17:24:10 -04:00
|
|
|
func (d *config) Load(filename string) error {
|
2015-08-13 19:24:48 -04:00
|
|
|
d.lock.Lock()
|
|
|
|
defer d.lock.Unlock()
|
2015-06-25 22:56:48 -04:00
|
|
|
|
2016-04-29 17:24:10 -04:00
|
|
|
_, err := os.Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
2016-04-29 17:24:10 -04:00
|
|
|
fileData, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
fileData = []byte(strings.Replace(string(fileData), "\r\n", "\n", -1))
|
|
|
|
}
|
|
|
|
|
2015-08-20 23:33:49 -04:00
|
|
|
st := structs.New(d.data)
|
|
|
|
f, ok := st.FieldOk("Version")
|
|
|
|
if !ok {
|
2016-11-15 21:14:23 -05:00
|
|
|
return fmt.Errorf("Argument struct [%s] does not contain field \"Version\"", st.Name())
|
2015-08-20 23:33:49 -04:00
|
|
|
}
|
|
|
|
|
2016-04-29 17:24:10 -04:00
|
|
|
err = json.Unmarshal(fileData, d.data)
|
|
|
|
if err != nil {
|
|
|
|
switch err := err.(type) {
|
2015-09-29 18:11:46 -04:00
|
|
|
case *json.SyntaxError:
|
2016-04-29 17:24:10 -04:00
|
|
|
return FormatJSONSyntaxError(bytes.NewReader(fileData), err)
|
2015-09-29 18:11:46 -04:00
|
|
|
default:
|
2016-04-29 17:24:10 -04:00
|
|
|
return err
|
2015-09-29 18:11:46 -04:00
|
|
|
}
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
2015-08-13 19:24:48 -04:00
|
|
|
if err := CheckData(d.data); err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return err
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*d).Version() != f.Value() {
|
2016-04-29 17:24:10 -04:00
|
|
|
return fmt.Errorf("Version mismatch")
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data - grab internal data map for reading
|
|
|
|
func (d config) Data() interface{} {
|
2015-08-13 19:24:48 -04:00
|
|
|
return d.data
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//Diff - list fields that are in A but not in B
|
2016-04-29 17:24:10 -04:00
|
|
|
func (d config) Diff(c Config) ([]structs.Field, error) {
|
2015-08-03 19:17:21 -04:00
|
|
|
var fields []structs.Field
|
|
|
|
err := CheckData(c.Data())
|
2015-06-25 22:56:48 -04:00
|
|
|
if err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return []structs.Field{}, err
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
currFields := structs.Fields(d.Data())
|
|
|
|
newFields := structs.Fields(c.Data())
|
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, currField := range currFields {
|
|
|
|
found = false
|
|
|
|
for _, newField := range newFields {
|
|
|
|
if reflect.DeepEqual(currField.Name(), newField.Name()) {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
fields = append(fields, *currField)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fields, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//DeepDiff - list fields in A that are missing or not equal to fields in B
|
2016-04-29 17:24:10 -04:00
|
|
|
func (d config) DeepDiff(c Config) ([]structs.Field, error) {
|
2015-08-03 19:17:21 -04:00
|
|
|
var fields []structs.Field
|
|
|
|
err := CheckData(c.Data())
|
2015-06-25 22:56:48 -04:00
|
|
|
if err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return []structs.Field{}, err
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
currFields := structs.Fields(d.Data())
|
|
|
|
newFields := structs.Fields(c.Data())
|
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, currField := range currFields {
|
|
|
|
found = false
|
|
|
|
for _, newField := range newFields {
|
|
|
|
if reflect.DeepEqual(currField.Value(), newField.Value()) {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
fields = append(fields, *currField)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fields, nil
|
|
|
|
}
|