2015-10-27 10:37:03 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-10-27 10:37:03 +00:00
|
|
|
|
|
|
|
package config
|
|
|
|
|
2020-04-26 22:13:35 +00:00
|
|
|
import (
|
2020-07-14 08:48:50 +00:00
|
|
|
"encoding/json"
|
2020-04-26 22:13:35 +00:00
|
|
|
"encoding/xml"
|
|
|
|
"sort"
|
2020-07-14 08:48:50 +00:00
|
|
|
|
2021-02-26 11:04:05 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2020-07-14 08:48:50 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
2020-04-26 22:13:35 +00:00
|
|
|
)
|
2015-10-27 10:37:03 +00:00
|
|
|
|
2020-07-14 08:48:50 +00:00
|
|
|
// internalVersioningConfiguration is used in XML serialization
|
|
|
|
type internalVersioningConfiguration struct {
|
2021-02-26 11:04:05 +00:00
|
|
|
Type string `xml:"type,attr,omitempty"`
|
|
|
|
Params []internalParam `xml:"param"`
|
|
|
|
CleanupIntervalS int `xml:"cleanupIntervalS" default:"3600"`
|
|
|
|
FSPath string `xml:"fsPath"`
|
|
|
|
FSType fs.FilesystemType `xml:"fsType"`
|
2015-10-27 10:37:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 08:48:50 +00:00
|
|
|
type internalParam struct {
|
2015-10-27 10:37:03 +00:00
|
|
|
Key string `xml:"key,attr"`
|
|
|
|
Val string `xml:"val,attr"`
|
|
|
|
}
|
|
|
|
|
2015-10-27 10:53:42 +00:00
|
|
|
func (c VersioningConfiguration) Copy() VersioningConfiguration {
|
|
|
|
cp := c
|
|
|
|
cp.Params = make(map[string]string, len(c.Params))
|
|
|
|
for k, v := range c.Params {
|
|
|
|
cp.Params[k] = v
|
|
|
|
}
|
|
|
|
return cp
|
|
|
|
}
|
|
|
|
|
2020-07-14 08:48:50 +00:00
|
|
|
func (c *VersioningConfiguration) UnmarshalJSON(data []byte) error {
|
|
|
|
util.SetDefaults(c)
|
|
|
|
type noCustomUnmarshal VersioningConfiguration
|
|
|
|
ptr := (*noCustomUnmarshal)(c)
|
|
|
|
return json.Unmarshal(data, ptr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VersioningConfiguration) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|
|
|
var intCfg internalVersioningConfiguration
|
|
|
|
util.SetDefaults(&intCfg)
|
|
|
|
if err := d.DecodeElement(&intCfg, &start); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.fromInternal(intCfg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-04 20:10:41 +00:00
|
|
|
func (c VersioningConfiguration) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
2020-07-28 10:07:54 +00:00
|
|
|
// Using EncodeElement instead of plain Encode ensures that we use the
|
|
|
|
// outer tag name from the VersioningConfiguration (i.e.,
|
|
|
|
// `<versioning>`) rather than whatever the internal representation
|
|
|
|
// would otherwise be.
|
|
|
|
return e.EncodeElement(c.toInternal(), start)
|
2020-07-14 08:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VersioningConfiguration) toInternal() internalVersioningConfiguration {
|
|
|
|
var tmp internalVersioningConfiguration
|
2015-10-27 10:37:03 +00:00
|
|
|
tmp.Type = c.Type
|
2020-07-14 08:48:50 +00:00
|
|
|
tmp.CleanupIntervalS = c.CleanupIntervalS
|
2021-02-26 11:04:05 +00:00
|
|
|
tmp.FSPath = c.FSPath
|
|
|
|
tmp.FSType = c.FSType
|
2015-10-27 10:37:03 +00:00
|
|
|
for k, v := range c.Params {
|
2020-07-14 08:48:50 +00:00
|
|
|
tmp.Params = append(tmp.Params, internalParam{k, v})
|
2015-10-27 10:37:03 +00:00
|
|
|
}
|
2020-04-26 22:13:35 +00:00
|
|
|
sort.Slice(tmp.Params, func(a, b int) bool {
|
|
|
|
return tmp.Params[a].Key < tmp.Params[b].Key
|
|
|
|
})
|
2020-07-14 08:48:50 +00:00
|
|
|
return tmp
|
2015-10-27 10:37:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 08:48:50 +00:00
|
|
|
func (c *VersioningConfiguration) fromInternal(intCfg internalVersioningConfiguration) {
|
|
|
|
c.Type = intCfg.Type
|
|
|
|
c.CleanupIntervalS = intCfg.CleanupIntervalS
|
2021-02-26 11:04:05 +00:00
|
|
|
c.FSPath = intCfg.FSPath
|
|
|
|
c.FSType = intCfg.FSType
|
2020-07-14 08:48:50 +00:00
|
|
|
c.Params = make(map[string]string, len(intCfg.Params))
|
|
|
|
for _, p := range intCfg.Params {
|
2015-10-27 10:37:03 +00:00
|
|
|
c.Params[p.Key] = p.Val
|
|
|
|
}
|
|
|
|
}
|