folderconfiguration.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 http://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "os"
  9. "path/filepath"
  10. "runtime"
  11. "strings"
  12. "github.com/syncthing/syncthing/lib/osutil"
  13. "github.com/syncthing/syncthing/lib/protocol"
  14. )
  15. type FolderConfiguration struct {
  16. ID string `xml:"id,attr" json:"id"`
  17. Label string `xml:"label,attr" json:"label"`
  18. RawPath string `xml:"path,attr" json:"path"`
  19. Type FolderType `xml:"type,attr" json:"type"`
  20. Devices []FolderDeviceConfiguration `xml:"device" json:"devices"`
  21. RescanIntervalS int `xml:"rescanIntervalS,attr" json:"rescanIntervalS"`
  22. IgnorePerms bool `xml:"ignorePerms,attr" json:"ignorePerms"`
  23. AutoNormalize bool `xml:"autoNormalize,attr" json:"autoNormalize"`
  24. MinDiskFreePct float64 `xml:"minDiskFreePct" json:"minDiskFreePct"`
  25. Versioning VersioningConfiguration `xml:"versioning" json:"versioning"`
  26. Copiers int `xml:"copiers" json:"copiers"` // This defines how many files are handled concurrently.
  27. Pullers int `xml:"pullers" json:"pullers"` // Defines how many blocks are fetched at the same time, possibly between separate copier routines.
  28. Hashers int `xml:"hashers" json:"hashers"` // Less than one sets the value to the number of cores. These are CPU bound due to hashing.
  29. Order PullOrder `xml:"order" json:"order"`
  30. IgnoreDelete bool `xml:"ignoreDelete" json:"ignoreDelete"`
  31. ScanProgressIntervalS int `xml:"scanProgressIntervalS" json:"scanProgressIntervalS"` // Set to a negative value to disable. Value of 0 will get replaced with value of 2 (default value)
  32. PullerSleepS int `xml:"pullerSleepS" json:"pullerSleepS"`
  33. PullerPauseS int `xml:"pullerPauseS" json:"pullerPauseS"`
  34. MaxConflicts int `xml:"maxConflicts" json:"maxConflicts"`
  35. DisableSparseFiles bool `xml:"disableSparseFiles" json:"disableSparseFiles"`
  36. DisableTempIndexes bool `xml:"disableTempIndexes" json:"disableTempIndexes"`
  37. cachedPath string
  38. DeprecatedReadOnly bool `xml:"ro,attr,omitempty" json:"-"`
  39. }
  40. type FolderDeviceConfiguration struct {
  41. DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"`
  42. }
  43. func NewFolderConfiguration(id, path string) FolderConfiguration {
  44. f := FolderConfiguration{
  45. ID: id,
  46. RawPath: path,
  47. }
  48. f.prepare()
  49. return f
  50. }
  51. func (f FolderConfiguration) Copy() FolderConfiguration {
  52. c := f
  53. c.Devices = make([]FolderDeviceConfiguration, len(f.Devices))
  54. copy(c.Devices, f.Devices)
  55. c.Versioning = f.Versioning.Copy()
  56. return c
  57. }
  58. func (f FolderConfiguration) Path() string {
  59. // This is intentionally not a pointer method, because things like
  60. // cfg.Folders["default"].Path() should be valid.
  61. if f.cachedPath == "" && f.RawPath != "" {
  62. l.Infoln("bug: uncached path call (should only happen in tests)")
  63. return f.cleanedPath()
  64. }
  65. return f.cachedPath
  66. }
  67. func (f *FolderConfiguration) CreateMarker() error {
  68. if !f.HasMarker() {
  69. marker := filepath.Join(f.Path(), ".stfolder")
  70. fd, err := os.Create(marker)
  71. if err != nil {
  72. return err
  73. }
  74. fd.Close()
  75. osutil.HideFile(marker)
  76. }
  77. return nil
  78. }
  79. func (f *FolderConfiguration) HasMarker() bool {
  80. _, err := os.Stat(filepath.Join(f.Path(), ".stfolder"))
  81. if err != nil {
  82. return false
  83. }
  84. return true
  85. }
  86. func (f *FolderConfiguration) DeviceIDs() []protocol.DeviceID {
  87. deviceIDs := make([]protocol.DeviceID, len(f.Devices))
  88. for i, n := range f.Devices {
  89. deviceIDs[i] = n.DeviceID
  90. }
  91. return deviceIDs
  92. }
  93. func (f *FolderConfiguration) prepare() {
  94. if f.RawPath != "" {
  95. // The reason it's done like this:
  96. // C: -> C:\ -> C:\ (issue that this is trying to fix)
  97. // C:\somedir -> C:\somedir\ -> C:\somedir
  98. // C:\somedir\ -> C:\somedir\\ -> C:\somedir
  99. // This way in the tests, we get away without OS specific separators
  100. // in the test configs.
  101. f.RawPath = filepath.Dir(f.RawPath + string(filepath.Separator))
  102. // If we're not on Windows, we want the path to end with a slash to
  103. // penetrate symlinks. On Windows, paths must not end with a slash.
  104. if runtime.GOOS != "windows" && f.RawPath[len(f.RawPath)-1] != filepath.Separator {
  105. f.RawPath = f.RawPath + string(filepath.Separator)
  106. }
  107. }
  108. f.cachedPath = f.cleanedPath()
  109. if f.RescanIntervalS > MaxRescanIntervalS {
  110. f.RescanIntervalS = MaxRescanIntervalS
  111. } else if f.RescanIntervalS < 0 {
  112. f.RescanIntervalS = 0
  113. }
  114. if f.Versioning.Params == nil {
  115. f.Versioning.Params = make(map[string]string)
  116. }
  117. }
  118. func (f *FolderConfiguration) cleanedPath() string {
  119. if f.RawPath == "" {
  120. return ""
  121. }
  122. cleaned := f.RawPath
  123. // Attempt tilde expansion; leave unchanged in case of error
  124. if path, err := osutil.ExpandTilde(cleaned); err == nil {
  125. cleaned = path
  126. }
  127. // Attempt absolutification; leave unchanged in case of error
  128. if !filepath.IsAbs(cleaned) {
  129. // Abs() looks like a fairly expensive syscall on Windows, while
  130. // IsAbs() is a whole bunch of string mangling. I think IsAbs() may be
  131. // somewhat faster in the general case, hence the outer if...
  132. if path, err := filepath.Abs(cleaned); err == nil {
  133. cleaned = path
  134. }
  135. }
  136. // Attempt to enable long filename support on Windows. We may still not
  137. // have an absolute path here if the previous steps failed.
  138. if runtime.GOOS == "windows" && filepath.IsAbs(cleaned) && !strings.HasPrefix(f.RawPath, `\\`) {
  139. return `\\?\` + cleaned
  140. }
  141. // If we're not on Windows, we want the path to end with a slash to
  142. // penetrate symlinks. On Windows, paths must not end with a slash.
  143. if runtime.GOOS != "windows" && cleaned[len(cleaned)-1] != filepath.Separator {
  144. cleaned = cleaned + string(filepath.Separator)
  145. }
  146. return cleaned
  147. }
  148. type FolderDeviceConfigurationList []FolderDeviceConfiguration
  149. func (l FolderDeviceConfigurationList) Less(a, b int) bool {
  150. return l[a].DeviceID.Compare(l[b].DeviceID) == -1
  151. }
  152. func (l FolderDeviceConfigurationList) Swap(a, b int) {
  153. l[a], l[b] = l[b], l[a]
  154. }
  155. func (l FolderDeviceConfigurationList) Len() int {
  156. return len(l)
  157. }