versioningconfiguration.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "encoding/json"
  9. "encoding/xml"
  10. "sort"
  11. "github.com/syncthing/syncthing/lib/fs"
  12. "github.com/syncthing/syncthing/lib/structutil"
  13. )
  14. // internalVersioningConfiguration is used in XML serialization
  15. type internalVersioningConfiguration struct {
  16. Type string `xml:"type,attr,omitempty"`
  17. Params []internalParam `xml:"param"`
  18. CleanupIntervalS int `xml:"cleanupIntervalS" default:"3600"`
  19. FSPath string `xml:"fsPath"`
  20. FSType fs.FilesystemType `xml:"fsType"`
  21. }
  22. type internalParam struct {
  23. Key string `xml:"key,attr"`
  24. Val string `xml:"val,attr"`
  25. }
  26. func (c VersioningConfiguration) Copy() VersioningConfiguration {
  27. cp := c
  28. cp.Params = make(map[string]string, len(c.Params))
  29. for k, v := range c.Params {
  30. cp.Params[k] = v
  31. }
  32. return cp
  33. }
  34. func (c *VersioningConfiguration) UnmarshalJSON(data []byte) error {
  35. structutil.SetDefaults(c)
  36. type noCustomUnmarshal VersioningConfiguration
  37. ptr := (*noCustomUnmarshal)(c)
  38. return json.Unmarshal(data, ptr)
  39. }
  40. func (c *VersioningConfiguration) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  41. var intCfg internalVersioningConfiguration
  42. structutil.SetDefaults(&intCfg)
  43. if err := d.DecodeElement(&intCfg, &start); err != nil {
  44. return err
  45. }
  46. c.fromInternal(intCfg)
  47. return nil
  48. }
  49. func (c VersioningConfiguration) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  50. // Using EncodeElement instead of plain Encode ensures that we use the
  51. // outer tag name from the VersioningConfiguration (i.e.,
  52. // `<versioning>`) rather than whatever the internal representation
  53. // would otherwise be.
  54. return e.EncodeElement(c.toInternal(), start)
  55. }
  56. func (c *VersioningConfiguration) toInternal() internalVersioningConfiguration {
  57. var tmp internalVersioningConfiguration
  58. tmp.Type = c.Type
  59. tmp.CleanupIntervalS = c.CleanupIntervalS
  60. tmp.FSPath = c.FSPath
  61. tmp.FSType = c.FSType
  62. for k, v := range c.Params {
  63. tmp.Params = append(tmp.Params, internalParam{k, v})
  64. }
  65. sort.Slice(tmp.Params, func(a, b int) bool {
  66. return tmp.Params[a].Key < tmp.Params[b].Key
  67. })
  68. return tmp
  69. }
  70. func (c *VersioningConfiguration) fromInternal(intCfg internalVersioningConfiguration) {
  71. c.Type = intCfg.Type
  72. c.CleanupIntervalS = intCfg.CleanupIntervalS
  73. c.FSPath = intCfg.FSPath
  74. c.FSType = intCfg.FSType
  75. c.Params = make(map[string]string, len(intCfg.Params))
  76. for _, p := range intCfg.Params {
  77. c.Params[p.Key] = p.Val
  78. }
  79. }